def _WebRequest(self, method, url, headers=None): """Internal method to make requests against web URLs. Args: method: request method, e.g. GET url: request URL headers: dictionary of request headers Returns: Response body as a string Raises: Error: If the response has a status code >= 400. """ if base.UseRequests(): r = requests.GetSession().request(method, url, headers=headers) content = r.content status = r.status_code else: r, content = http.Http().request(url, method, headers=headers) status = r.status if status >= 400: raise exceptions.Error('status: {}, reason: {}'.format( status, r.reason)) return content
def GetApitoolsTransport(timeout='unset', response_encoding=None, ca_certs=None): """Get an unauthenticated transport client for use with apitools. Args: timeout: double, The request timeout in seconds. This is the socket level timeout. If timeout is None, timeout is infinite. If default argument 'unset' is given, a sensible default is selected. response_encoding: str, the encoding to use to decode the response. ca_certs: str, absolute filename of a ca_certs file that overrides the default Returns: 1. A httplib2.Http-like object backed by httplib2 or requests. """ if base.UseRequests(): session = requests.GetSession(timeout=timeout, response_encoding=response_encoding, ca_certs=ca_certs) return requests.GetApitoolsRequests(session) return http.Http(timeout=timeout, response_encoding=response_encoding, ca_certs=ca_certs)
def GetApitoolsTransport(timeout='unset', enable_resource_quota=True, force_resource_quota=False, response_encoding=None, ca_certs=None, allow_account_impersonation=True, use_google_auth=None): """Get an transport client for use with apitools. Args: timeout: double, The timeout in seconds to pass to httplib2. This is the socket level timeout. If timeout is None, timeout is infinite. If default argument 'unset' is given, a sensible default is selected. enable_resource_quota: bool, By default, we are going to tell APIs to use the quota of the project being operated on. For some APIs we want to use gcloud's quota, so you can explicitly disable that behavior by passing False here. force_resource_quota: bool, If true resource project quota will be used by this client regardless of the settings in gcloud. This should be used for newer APIs that cannot work with legacy project quota. response_encoding: str, the encoding to use to decode the response. ca_certs: str, absolute filename of a ca_certs file that overrides the default allow_account_impersonation: bool, True to allow use of impersonated service account credentials for calls made with this client. If False, the active user credentials will always be used. use_google_auth: bool, True if the calling command indicates to use google-auth library for authentication. If False, authentication will fallback to using the oauth2client library. Returns: 1. A httplib2.Http-like object backed by httplib2 or requests. """ if base.UseRequests(): session = requests.GetSession( timeout=timeout, enable_resource_quota=enable_resource_quota, force_resource_quota=force_resource_quota, response_encoding=response_encoding, ca_certs=ca_certs, allow_account_impersonation=allow_account_impersonation) return requests.GetApitoolsRequests(session) return http.Http(timeout=timeout, enable_resource_quota=enable_resource_quota, force_resource_quota=force_resource_quota, response_encoding=response_encoding, ca_certs=ca_certs, allow_account_impersonation=allow_account_impersonation, use_google_auth=use_google_auth)
def _Communicate(url, method, body, headers): """Returns HTTP status, reason, and response body for a given HTTP request.""" if base.UseRequests(): response = requests.GetSession().request( method, url, data=body, headers=headers, stream=True) status = response.status_code reason = response.reason data = response.content return status, reason, data response, data = http.Http().request( url, method, body=body, headers=headers) status = response.status reason = response.reason return status, reason, data
def _GetPrediction(response_encoding, url, body, headers): """Make http request to get prediction results.""" if base.UseRequests(): response = requests.GetSession().request('POST', url, data=body, headers=headers) return getattr(response, 'status_code'), getattr(response, 'text') response, response_body = http.Http( response_encoding=response_encoding).request(uri=url, method='POST', body=body, headers=headers) return response.get('status'), response_body
def GetApitoolsTransport(timeout='unset', response_encoding=None, ca_certs=None, client_certificate=None, client_key=None, client_cert_domain=None): """Get an unauthenticated transport client for use with apitools. Args: timeout: double, The request timeout in seconds. This is the socket level timeout. If timeout is None, timeout is infinite. If default argument 'unset' is given, a sensible default is selected. response_encoding: str, the encoding to use to decode the response. ca_certs: str, absolute filename of a ca_certs file that overrides the default client_certificate: str, absolute filename of a client_certificate file client_key: str, absolute filename of a client_key file client_cert_domain: str, domain we are connecting to (used only by httplib2) Returns: 1. A httplib2.Http-like object backed by httplib2 or requests. """ if base.UseRequests(): # pylint: disable=g-import-not-at-top from googlecloudsdk.core import requests session = requests.GetSession( timeout=timeout, ca_certs=ca_certs, client_certificate=client_certificate, client_key=client_key) return requests.GetApitoolsRequests( session, response_encoding=response_encoding) else: from googlecloudsdk.core import http # pylint: disable=g-import-not-at-top http_client = http.Http( timeout=timeout, response_encoding=response_encoding, ca_certs=ca_certs) # httplib2 always applies the first client certificate # in the chain for authentication http_client.certificates.credentials.insert( 0, (client_cert_domain, client_key, client_certificate, '')) return http_client
def GetApitoolsTransport(timeout='unset', enable_resource_quota=True, response_encoding=None, ca_certs=None, allow_account_impersonation=True, use_google_auth=None, response_handler=None, redact_request_body_reason=None): """Get an transport client for use with apitools. Args: timeout: double, The timeout in seconds to pass to httplib2. This is the socket level timeout. If timeout is None, timeout is infinite. If default argument 'unset' is given, a sensible default is selected. enable_resource_quota: bool, By default, we are going to tell APIs to use the quota of the project being operated on. For some APIs we want to use gcloud's quota, so you can explicitly disable that behavior by passing False here. response_encoding: str, the encoding to use to decode the response. ca_certs: str, absolute filename of a ca_certs file that overrides the default allow_account_impersonation: bool, True to allow use of impersonated service account credentials for calls made with this client. If False, the active user credentials will always be used. use_google_auth: bool, True if the calling command indicates to use google-auth library for authentication. If False, authentication will fallback to using the oauth2client library. response_handler: requests.ResponseHandler, handler that gets executed before any other response handling. redact_request_body_reason: str, the reason why the request body must be redacted if --log-http is used. If None, the body is not redacted. Returns: 1. A httplib2.Http-like object backed by httplib2 or requests. """ if base.UseRequests(): if response_handler: if not isinstance(response_handler, core_requests.ResponseHandler): raise ValueError('response_handler should be of type ResponseHandler.') if (properties.VALUES.core.log_http.GetBool() and properties.VALUES.core.log_http_streaming_body.GetBool()): # We want to print the actual body instead of printing the placeholder. # To achieve this, we need to set streaming_response_body as False. # Not that the body will be empty if the response_handler has already # consumed the stream. streaming_response_body = False else: streaming_response_body = response_handler.use_stream else: streaming_response_body = False session = requests.GetSession( timeout=timeout, enable_resource_quota=enable_resource_quota, ca_certs=ca_certs, allow_account_impersonation=allow_account_impersonation, streaming_response_body=streaming_response_body, redact_request_body_reason=redact_request_body_reason) return core_requests.GetApitoolsRequests(session, response_handler, response_encoding) return http.Http(timeout=timeout, enable_resource_quota=enable_resource_quota, response_encoding=response_encoding, ca_certs=ca_certs, allow_account_impersonation=allow_account_impersonation, use_google_auth=use_google_auth)
def GetLogTailerTransport(): if base.UseRequests(): return RequestsLogTailer() return Httplib2LogTailer()
def GetGCSLogTailerTransport(): """Return a GCS LogTailer transport.""" if base.UseRequests(): return RequestsLogTailer() return Httplib2LogTailer()