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

Closing discovery item fails with AttributeError: 'Http' object has no attribute 'http' #1046

Closed
ghaithbilbeisi opened this issue Sep 25, 2020 · 10 comments · Fixed by #1231
Closed
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. 🚨 This issue needs some love. type: question Request for information or clarification. Not an issue.

Comments

@ghaithbilbeisi
Copy link

ghaithbilbeisi commented Sep 25, 2020

The following line fails, looking at httplib2 shouldn't this be self._http.close() instead?

self._http.http.close()

@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Sep 26, 2020
@busunkim96 busunkim96 added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Sep 28, 2020
@yoshi-automation yoshi-automation removed the triage me I really want to be triaged. label Sep 28, 2020
@busunkim96
Copy link
Contributor

Hey @ghaithbilbeisi, could you show more of the stack trace?

From discussion in #1047 it looks like the issue is the low version of httplib2. I also see that this wouldn't work correctly if you were passing in your own httplib2 object (in that case it should just be self._http.close()).

@busunkim96 busunkim96 self-assigned this Sep 28, 2020
@ghaithbilbeisi
Copy link
Author

Hi @busunkim96 , I get this when running my unittest with the following:

def test_build_drive(self):
    creds = utils.authenticate('client_id.json')
    drive = utils.build_drive(creds)
    self.assertIsNotNone(drive)
    if drive:
        drive.close()

Using the following authenticate and build_drive methods:

def authenticate(secret):
    """ Returns an oauth credentials object using the given secret, or stored credentials if available. """
    SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
    store = file.Storage('storage.json')
    creds = store.get()       
            
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets(secret, SCOPES)
        creds = tools.run_flow(flow, store)
    
    return creds

def build_drive(creds):
    try:
        drive = discovery.build('drive', 'v3', credentials=creds)#http=creds.authorize(Http()))
    except:
        drive = None
    return drive

Running this with google-api-python-client 1.12.3, httplib2 0.18.1, and python 3.8.3 gives the following error:

Traceback (most recent call last):
  File "token_test.py", line 16, in test_build_drive
    drive.close()
  File "/Users/ghaithbilbeisi/miniconda3/lib/python3.8/site-packages/googleapiclient/discovery.py", line 1335, in close
    self._http.http.close()
AttributeError: 'Http' object has no attribute 'http'

@busunkim96
Copy link
Contributor

I think the proper fix for this is to add close to AuthorizedHttp. Opened googleapis/google-auth-library-python-httplib2#14

@yoshi-automation yoshi-automation added 🚨 This issue needs some love. and removed 🚨 This issue needs some love. labels Dec 30, 2020
gcf-merge-on-green bot pushed a commit to googleapis/google-auth-library-python-httplib2 that referenced this issue Mar 1, 2021
@parthea
Copy link
Contributor

parthea commented Mar 3, 2021

I'm going to close off this issue as google-auth-httplib2 0.1.0 was released on Monday with the fix from googleapis/google-auth-library-python-httplib2#14 . If you're still having trouble after updating google-auth-httplib2, please re-open this issue with more information.

@parthea parthea closed this as completed Mar 3, 2021
@solzard
Copy link

solzard commented Mar 5, 2021

This simple code still raises the same error with latest packages (google-auth-httplib2 0.1.0, httplib2 0.19.0, google-api-python-client 2.0.2).

from googleapiclient.discovery import build

yt = build("youtube", "v3", developerKey="MY_KEY")
yt.close()
Traceback (most recent call last):
  File "test_code.py", line 4, in <module>
    yt.close()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googleapiclient/discovery.py", line 1364, in close
    self._http.http.close()
AttributeError: 'Http' object has no attribute 'http'

Process finished with exit code 1

@busunkim96
Copy link
Contributor

I think #1231 will (finally) fix this. I'll circle back to it next week. Thanks for the report @solzard!

gcf-merge-on-green bot pushed a commit that referenced this issue Mar 15, 2021
Call `self._http.close()`, since `google_auth_httplib2.AuthorizedHttp` (googleapis/google-auth-library-python-httplib2@feda187) has been fixed to also have a `close` method like `httplib2.Http()`


Fixes #1046 🦕
@nikokaoja
Copy link

@busunkim96 actually #1231 did not fixed the issue since I am still having it:

Traceback (most recent call last):
  File "./src/sheet2xls.py", line 20, in <module>
    sheet = sheet_obj.get(sheet_id)
  File "/home/runner/.local/lib/python3.8/site-packages/gsheets/api.py", line 141, in get
    return self[id***
  File "/home/runner/.local/lib/python3.8/site-packages/gsheets/api.py", line 121, in __getitem__
    response = backend.spreadsheet(self._sheets, id)
  File "/home/runner/.local/lib/python3.8/site-packages/gsheets/tools.py", line 39, in __get__
    result = instance.__dict__[self.__name__*** = self.fget(instance)
  File "/home/runner/.local/lib/python3.8/site-packages/gsheets/api.py", line 69, in _sheets
    return backend.build_service('sheets', credentials=self._creds,
  File "/home/runner/.local/lib/python3.8/site-packages/gsheets/backend.py", line 34, in build_service
    return apiclient.discovery.build(**kwargs)
  File "/home/runner/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/runner/.local/lib/python3.8/site-packages/googleapiclient/discovery.py", line 324, in build
    discovery_http.close()
AttributeError: 'Http' object has no attribute 'close'
##[error***Process completed with exit code 1.

from pip list:

google-api-core 1.26.3
google-api-python-client 2.1.0
google-auth 1.28.0
google-auth-httplib2 0.1.0
googleapis-common-protos 1.53.0
httplib2 0.19.1

@parthea parthea reopened this Apr 7, 2021
@busunkim96
Copy link
Contributor

@niva83 I'm not able to reproduce the error you're seeing. Could you share the bit of your code that calls discovery.build?

from googleapiclient import discovery


service = discovery.build("cloudasset", "v1")

service.close()

The stack trace seems to suggest it's failing on closing discovery_http, which is just a plain httplib2 object rather than one from google_auth_httplib2.

Could you try this code to see if you can close a plain httplib2.Http instance? If this doesn't work, the version of httplib2 you have installed may be too old.

import httplib2


http = httplib2.Http()

http.close()

@nikokaoja
Copy link

@busunkim96 thanks, I will test this week and let you know how it goes.

@busunkim96 busunkim96 added type: question Request for information or clarification. Not an issue. and removed type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Apr 7, 2021
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Apr 8, 2021
@parthea
Copy link
Contributor

parthea commented Apr 14, 2021

Hi @niva83 , I'm going to close this off as guidance has been provided in #1046 (comment). If you're still having trouble, please re-open it with more information.

@parthea parthea closed this as completed Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. 🚨 This issue needs some love. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants