profusion / sgqlc

Simple GraphQL Client
https://sgqlc.readthedocs.io/
ISC License
513 stars 85 forks source link

RequestsEndpoint raising AttributeError '__enter__' #100

Closed modasserbillah closed 3 years ago

modasserbillah commented 4 years ago

I am trying to use the RequestsEndpoint to query AppSync. Here's what I tried:

from sgqlc.endpoint.http import RequestsEndpoint

endpoint = RequestsEndpoint(url=MY_URL, base_headers=MY_HEADER)
query = SOME_QUERY 
response = endpoint(query)

Here's what I get:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-22-4fa47e0ad070> in <module>
----> 1 response = endpoint(query)

/usr/local/lib/python3.6/site-packages/sgqlc/endpoint/requests.py in __call__(self, query, variables, operation_name, extra_headers, timeout)
    153                 prepped = session.prepare_request(req)
    154                 with session.send(prepped,
--> 155                                   timeout=timeout or self.timeout) as f:
    156                     try:
    157                         f.raise_for_status()

AttributeError: __enter__

Any idea why this is happening? The HTTPEndpoint works fine with the same input.

barbieri commented 4 years ago

I never used the requests version myself, but it seems to work, at least that's what the contributors that uploaded said and so far no complaints.

It looks like session.send() doesn't return an object that supports https://docs.python.org/2.5/whatsnew/pep-343.html

A quick look at https://github.com/psf/requests/blob/master/requests/sessions.py and https://github.com/psf/requests/blob/master/requests/models.py#L644 seems that the model Response is returned by Session.send() and it does implement such protocol.

Maybe check your requests version? Or provide some information on what's the return of session.send()?

@strazeadin do you know?

strazeadin commented 4 years ago

I noticed from the path in the error message that @modasserbillah is using Python 3.6 and this package Pipfile requires Python 3.7 so I'm not sure if this is a potential issue or not.

Having said that I did run a fresh install of Python 3.6.10, installed requests and sgqlc and was unable to replicate this error (my request worked successfully).

@modasserbillah are you able to share more information regarding your configuration or steps to replicate from a fresh install? Might just be an old requests version as @barbieri said.

modasserbillah commented 4 years ago

Thanks for looking into it! My requests version is 2.13.0 and Python version is 3.6 . I'll check the session.send() and get back to you.

strazeadin commented 4 years ago

@modasserbillah that's a 3 year old version of requests, I highly suggest you upgrade to a newer version.

@barbieri not sure if we should update the Pipfile with some minimum version numbers for the dependencies, let me know what you think (I'm not a very active Python developer, so not sure what's the common practice around it is)

barbieri commented 4 years ago

@strazeadin I'd suggest using a minimum version in setup.py and reflect that in the pipfile.

Pipfile is not mandatory, most people will ignore it (unlike in NodeJS where everyone uses the same package.json). Then setup.py is where the official deps go, but good to be reflected in the Pipfile as well.

barbieri commented 3 years ago

could we close this?

modasserbillah commented 3 years ago

Yeah, updating requests solved it. I think this can be closed.