Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change requests to httpx #208

Closed
camilamaia opened this issue Jul 13, 2020 · 14 comments · Fixed by #557
Closed

Change requests to httpx #208

camilamaia opened this issue Jul 13, 2020 · 14 comments · Fixed by #557
Assignees
Labels
Breaking Change It will break the previous versions of ScanAPI Feature New feature or request

Comments

@camilamaia
Copy link
Member

camilamaia commented Jul 13, 2020

Change requests to httpx https://github.com/encode/httpx

Features

HTTPX builds on the well-established usability of requests, and gives you:

Plus all the standard features of requests...

  • International Domains and URLs
  • Keep-Alive & Connection Pooling
  • Sessions with Cookie Persistence
  • Browser-style SSL Verification
  • Basic/Digest Authentication
  • Elegant Key/Value Cookies
  • Automatic Decompression
  • Automatic Content Decoding
  • Unicode Response Bodies
  • Multipart File Uploads
  • HTTP(S) Proxy Support
  • Connection Timeouts
  • Streaming Downloads
  • .netrc Support
  • Chunked Requests
@camilamaia camilamaia added the Feature New feature or request label Jul 13, 2020
@tpiekarski
Copy link

Migrated recently HTTP requests from requests to aiohttp and looking forward to migrate some more requests to httpx this time.
Would like to take a look and migrate these in the next week to prepare myself for coming sprint at EuroPython :)
Did you start to change to httpx?

@camilamaia
Copy link
Member Author

Migrated recently HTTP requests from requests to aiohttp and looking forward to migrate some more requests to httpx this time.
Would like to take a look and migrate these in the next week to prepare myself for coming sprint at EuroPython :)
Did you start to change to httpx?

This would be awesome! I didn't have the chance to start tackling this issue, so you can go ahead 🚀

If you have any troubles running it locally, please let me know. We are moving to poetry and soon it will be way easier to configure your development env.

@tpiekarski
Copy link

Migrated recently HTTP requests from requests to aiohttp and looking forward to migrate some more requests to httpx this time.
Would like to take a look and migrate these in the next week to prepare myself for coming sprint at EuroPython :)
Did you start to change to httpx?

This would be awesome! I didn't have the chance to start tackling this issue, so you can go ahead

That's great :)

If you have any troubles running it locally, please let me know. We are moving to poetry and soon it will be way easier to configure your development env.

Thanks, I keep that in mind.

@camilamaia camilamaia added the Breaking Change It will break the previous versions of ScanAPI label Jul 25, 2020
@Pradhvan
Copy link
Member

Pradhvan commented Jul 29, 2020

@tpiekarski are you working on this ? If not, I would be happy to take this up. 😄

@tpiekarski
Copy link

Please go ahead, I am out of order for some time. Thanks 😊

@Pradhvan
Copy link
Member

Pradhvan commented Aug 5, 2020

@camilamaia on a quick overview of the issue I found requests to be used only in two files.

  • scanapi/scanapi/tree/request_node.py : here I can try to find an quivalent of requests.requests for httpx
        response = requests.request(
            method,
            url,
            headers=self.headers,
            params=self.params,
            json=self.body,
            allow_redirects=False,
        )
  • scanapi/scanapi/tree/testing_node.py:
import requests

requests is being imported but it isn't being used. But the linter is not showing it as an unused import? Am I missing something here?

Besides those, a couple of test files and of what I saw for tests requests is only used for GET. So for tests, it would be an easy replacement I am assuming.

  • scanapi/tests/unit/test_hide_utils.py
  • scanapi/tests/unit/test_scan.py
  • scanapi/tests/unit/test_utils.py
  • scanapi/tests/unit/evaluators/test_code_evaluator.py

@marcuxyz
Copy link
Member

marcuxyz commented Aug 6, 2020

@camilamaia on a quick overview of the issue I found requests to be used only in two files.

  • scanapi/scanapi/tree/request_node.py : here I can try to find an quivalent of requests.requests for httpx
        response = requests.request(
            method,
            url,
            headers=self.headers,
            params=self.params,
            json=self.body,
            allow_redirects=False,
        )
  • scanapi/scanapi/tree/testing_node.py:
import requests

requests is being imported but it isn't being used. But the linter is not showing it as an unused import? Am I missing something here?

Besides those, a couple of test files and of what I saw for tests requests is only used for GET. So for tests, it would be an easy replacement I am assuming.

  • scanapi/tests/unit/test_hide_utils.py
  • scanapi/tests/unit/test_scan.py
  • scanapi/tests/unit/test_utils.py
  • scanapi/tests/unit/evaluators/test_code_evaluator.py

to remove imports and unused variables. I recommend:

https://pypi.org/project/autoflake/#:~:text=To%20remove%20all%20unused%20imports,remove-unused-variables%20option.

@camilamaia
Copy link
Member Author

camilamaia commented Aug 13, 2020

I tested changing requests to httpx, but we have one problem. Curlify does not work with httpx:

Loading file ../examples/demo-api/.scanapi.yaml
Loading file ../examples/demo-api/scanapi.yaml
Writing documentation
Making request GET http://demo.scanapi.dev/api/health/
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/camilamaia/Library/Caches/pypoetry/virtualenvs/scanapi-KvDFA3Cq-py3.7/lib/python3.7/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/Users/camilamaia/Library/Caches/pypoetry/virtualenvs/scanapi-KvDFA3Cq-py3.7/lib/python3.7/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/Users/camilamaia/Library/Caches/pypoetry/virtualenvs/scanapi-KvDFA3Cq-py3.7/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/camilamaia/Library/Caches/pypoetry/virtualenvs/scanapi-KvDFA3Cq-py3.7/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/camilamaia/Library/Caches/pypoetry/virtualenvs/scanapi-KvDFA3Cq-py3.7/lib/python3.7/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/camilamaia/workspace/scanapi-org/scanapi/scanapi/__main__.py", line 62, in run
    scan()
  File "/Users/camilamaia/workspace/scanapi-org/scanapi/scanapi/scan.py", line 50, in scan
    write_report(results)
  File "/Users/camilamaia/workspace/scanapi-org/scanapi/scanapi/scan.py", line 63, in write_report
    reporter.write(results)
  File "/Users/camilamaia/workspace/scanapi-org/scanapi/scanapi/reporter.py", line 27, in write
    content = render(template_path, context, has_external_template)
  File "/Users/camilamaia/workspace/scanapi-org/scanapi/scanapi/template_render.py", line 13, in render
    return chosen_template.render(**context)
  File "/Users/camilamaia/Library/Caches/pypoetry/virtualenvs/scanapi-KvDFA3Cq-py3.7/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/Users/camilamaia/Library/Caches/pypoetry/virtualenvs/scanapi-KvDFA3Cq-py3.7/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/Users/camilamaia/Library/Caches/pypoetry/virtualenvs/scanapi-KvDFA3Cq-py3.7/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "/Users/camilamaia/workspace/scanapi-org/scanapi/scanapi/templates/html.jinja", line 408, in top-level template code
    <p>{{request|curlify}}</p>
  File "/Users/camilamaia/Library/Caches/pypoetry/virtualenvs/scanapi-KvDFA3Cq-py3.7/lib/python3.7/site-packages/curlify.py", line 25, in to_curl
    if request.body:
AttributeError: 'Request' object has no attribute 'body'

https://github.com/ofw/curlify/blob/master/curlify.py#L27

I only changed this to this:

import httpx
....
response = httpx.request(
            method,
            url,
            headers=self.headers,
            params=self.params,
            json=self.body,
            allow_redirects=False,
        )

If I remove this line, everything works properly.

About the differences between requests and httpx: https://www.python-httpx.org/compatibility/

@marcuxyz marcuxyz self-assigned this Oct 1, 2020
@marcorichetta
Copy link
Contributor

File "/Users/camilamaia/Library/Caches/pypoetry/virtualenvs/scanapi-KvDFA3Cq-py3.7/lib/python3.7/site-packages/curlify.py", line 25, in to_curl
if request.body:
AttributeError: 'Request' object has no attribute 'body'


https://github.com/ofw/curlify/blob/master/curlify.py#L27

I only changed [this](https://github.com/scanapi/scanapi/blob/97c4451c6b4a52d13f32c617053bbfe8cecdc75f/scanapi/tree/request_node.py#L99) to this:

```python
import httpx
....
response = httpx.request(
            method,
            url,
            headers=self.headers,
            params=self.params,
            json=self.body,
            allow_redirects=False,
        )

If I remove this line, everything works properly.

It seems curlify doesn't support httpx at the moment, specifically:

httpx does not have a .body but contents can be accessed via .read() instead.

I think the best option is to use this enhanced (and updated) version of curlify https://github.com/marcuxyz/curlify2, otherwise it'll be needed to maybe fork (?) curlify or modify it's use inside this project.

env.filters["curlify"] = curlify.to_curl

@camilamaia
Copy link
Member Author

@marcorichetta great catch. And in fact we are already using curlify2:

https://github.com/scanapi/scanapi/blob/master/pyproject.toml#L23

Actually, @marcuxyz created this curlify2 exactly for this reason!

@marcorichetta
Copy link
Contributor

@marcorichetta great catch. And in fact we are already using curlify2:

https://github.com/scanapi/scanapi/blob/master/pyproject.toml#L23

Actually, @marcuxyz created this curlify2 exactly for this reason!

Oh, I read #208 (comment) and thought it was unresolved.

So this issue is already solved by PR #330, right?

@camilamaia
Copy link
Member Author

@marcorichetta yes, exactly, the curl issue was solved. But we are not using httpx yet. We are still using requests.

@taconi
Copy link
Member

taconi commented Nov 27, 2022

@Pradhvan are you working on this ? If not, I would be happy to take this up

@Pradhvan
Copy link
Member

@Pradhvan are you working on this ? If not, I would be happy to take this up

no, feel free to take it. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change It will break the previous versions of ScanAPI Feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants