-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
Ideally this would be supported (relying on the default credential loading chain): build("admin", "directory_v1", http_timeout=100).
Currently you have to pass in your own Http, BUT you can't just do this:
http_transport = httplib2.Http(timeout=20)
build("admin", "directory_v1", http=http_transport)
You need to authorize it yourself because of all of these conditions that trigger when its not None (the purpose of which are not clear to me)
Why isn't http just passed along at this stage?
This is all build_http is doing so its mostly just setting the timeout, but its also explicitly forcing you to alter global state with socket.setdefaulttimeout(timeout_in_sec) instead of just accepting an arg.
def build_http():
"""Builds httplib2.Http object
Returns:
A httplib2.Http object, which is used to make http requests, and which has timeout set by default.
To override default timeout call
socket.setdefaulttimeout(timeout_in_sec)
before interacting with this method.
"""
if socket.getdefaulttimeout() is not None:
http_timeout = socket.getdefaulttimeout()
else:
http_timeout = DEFAULT_HTTP_TIMEOUT_SEC
http = httplib2.Http(timeout=http_timeout)
try:
http.redirect_codes = http.redirect_codes - {308}
except AttributeError:
pass
return http
If I pass in my own Http I have to contend with all these conditions
def build_from_document(
....
if http is not None:
# if http is passed, the user cannot provide credentials
banned_options = [
(credentials, "credentials"),
(
client_options.credentials_file,
"client_options.credentials_file",
),
]
....
if http is None:
<make my life 10000x easier>
So if I'm relying on the default cred loading chain and just doing this build("admin", "directory_v1") and I just want to set the timeout I have to construct and authorize Http myself and I can't rely on the default behavior.
considering build_http()'s almost sole purpose is setting the timeout could it just accept a timeout arg passed along from build()?
Maybe another option is you consider build_http() public for users and have it return an instance of a class that inherits from or adds the built Http as a property. Then you can control how Http() is built, check if the http param passed in to build() is the wrapper class, and skip all the if http is not None logic because you can trust it was built correctly and with whatever guard rails you want.