Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from apiclient.errors import HttpError
from apiclient.http import MediaFileUpload
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
# Explicitly tell the underlying HTTP transport library not to retry, since
# we are handling retry logic ourselves.
httplib2.RETRIES = 1
# Maximum number of times to retry before giving up.
MAX_RETRIES = 20
# Always retry when these exceptions are raised.
RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, httplib.NotConnected,
httplib.IncompleteRead, httplib.ImproperConnectionState,
httplib.CannotSendRequest, httplib.CannotSendHeader,
httplib.ResponseNotReady, httplib.BadStatusLine)
# Always retry when an apiclient.errors.HttpError with one of these status
# codes is raised.
RETRIABLE_STATUS_CODES = [500, 502, 503, 504]
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the Google Developers Console at
# https://console.developers.google.com/.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
# https://developers.google.com/youtube/v3/guides/authentication
"""Makes a Google API request, and possibly raises an appropriate exception.
Args:
stub_method: A string, the name of the method to call.
request: A protobuf; 'instance' and 'connection_id' will be set
when available.
Returns:
A protobuf.
Raises:
OperationalError: httplib2 transport failure, or non 2xx http response.
"""
try:
response = getattr(self._client, stub_method)(request)
except (errors.Error, client.Error, httplib2.HttpLib2Error), e:
raise OperationalError('could not connect: ' + str(e))
return response
[
{"name": "cluster-1", ...}
{"name": "cluster-2", ...},
{...}
]
Raises:
ApiExecutionError: ApiExecutionError is raised if the call to the
GCP API fails
"""
try:
repository = self.repository.projects_zones_clusters
results = repository.list(project_id, zone=zone)
return api_helpers.flatten_list_results(results, 'clusters')
except (errors.HttpError, HttpLib2Error) as e:
LOGGER.warning(api_errors.ApiExecutionError(project_id, e))
raise api_errors.ApiExecutionError(project_id, e)
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
%s
with information from the APIs Console
.
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
CLIENT_SECRETS_FILE))
# Retry transport and file IO errors.
RETRYABLE_ERRORS = (httplib2.HttpLib2Error, IOError)
# Number of times to retry failed downloads.
NUM_RETRIES = 5
# Number of bytes to send/receive in each request.
CHUNKSIZE = 2 * 1024 * 1024
# Mimetype to use if one can't be guessed from the file extension.
DEFAULT_MIMETYPE = 'application/octet-stream'
def get_authenticated_service(scope):
print 'Authenticating...'
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=scope,
message=MISSING_CLIENT_SECRETS_MESSAGE)
Returns:
dict: ACL json for bucket
Raises:
ApiExecutionError: ApiExecutionError is raised if the call to the
GCP API fails
"""
try:
results = self.repository.bucket_acls.list(resource=bucket)
flattened_results = api_helpers.flatten_list_results(results,
'items')
LOGGER.debug('Getting acls for a GCS Bucket, bucket = %s,'
' flattened_results = %s',
bucket, flattened_results)
return flattened_results
except (errors.HttpError, HttpLib2Error) as e:
api_exception = api_errors.ApiExecutionError(
'bucketAccessControls', e, 'bucket', bucket)
LOGGER.error(api_exception)
raise api_exception
from helpers import import_by_string
from settings import WEBHOOK_BACKEND_PIPELINES
from settings import ENABLE_VIDEO_CONVERTING
# Explicitly tell the underlying HTTP transport library not to retry,
# since we are handling retry logic ourselves.
httplib2.RETRIES = 1
# Maximum number of times to retry before giving up.
MAX_RETRIES = 10
# Always retry when these exceptions are raised.
RETRIABLE_EXCEPTIONS = (
httplib2.HttpLib2Error, IOError, httplib.NotConnected,
httplib.IncompleteRead, httplib.ImproperConnectionState,
httplib.CannotSendRequest, httplib.CannotSendHeader,
httplib.ResponseNotReady, httplib.BadStatusLine)
# Always retry when an apiclient.errors.HttpError
# with one of these status codes is raised.
RETRIABLE_STATUS_CODES = (500, 502, 503, 504)
class YoutubeClient(object):
def __init__(self, client_id, client_sercet, refresh_token):
self.client_id = client_id
self.client_secret = client_sercet
self.refresh_token = refresh_token
def get_auth_code(self):
def __init__(self, url, basic_auth={}, form_auth={},
http_opts={}, request_opts={}):
self.HttpLib2Error = httplib2.HttpLib2Error
self.url = url
self.request_opts = request_opts
self.auth_type = 'basic_auth' if basic_auth else (
'form_auth' if form_auth else None)
self.auth = basic_auth if basic_auth else (
form_auth if form_auth else None)
# Proxify
try:
from urlparse import urlparse
import socks
proxy_uri = urlparse(environ.get('HTTPS_PROXY', environ.get('HTTP_PROXY')))
if proxy_uri:
proxy = {'proxy_info': httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP,
proxy_uri.hostname, proxy_uri.port)}
def parse_page(self):
try:
h = httplib2.Http('.cache', timeout=5) # timeout in seconds
resp, page = h.request(self.url, 'GET')
except AttributeError:
raise httplib2.HttpLib2Error('could not open a socket for %s.' %self.url)
# parses only the parts of the page that we want
strainer = BeautifulSoup.SoupStrainer({'a': True, 'title': True, 'body': True, 'script': True})
try:
soup = BeautifulSoup.BeautifulSoup(page, parseOnlyThese=strainer) # what if it fails to parse?
except UnicodeEncodeError, e:
raise ParseError('%s is not HTML.' % self.url)
except httplib.IncompleteRead, e:
raise ParseError(self.url +' '+ str(e))
try:
self.get_content(soup)
links = soup('a')
self.get_links(links)
except Exception, e:
raise ParseError( '%s could not be parsed.' % self.url )
api_stub, 'No list_next() method.')
next_stub = api_stub.list_next
results = []
while request is not None:
try:
response = self._execute(request, rate_limiter)
results.append(response)
request = next_stub(request, response)
except api_errors.ApiNotEnabledError:
# If the API isn't enabled on the resource, there must
# not be any resources. So, just swallow the error:
# we're done!
break
except (errors.HttpError, httplib2.HttpLib2Error) as e:
raise api_errors.ApiExecutionError(api_stub, e)
return results
from apiclient.errors import HttpError
from apiclient.http import MediaFileUpload
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
# Explicitly tell the underlying HTTP transport library not to retry, since
# we are handling retry logic ourselves.
httplib2.RETRIES = 1
# Maximum number of times to retry before giving up.
MAX_RETRIES = 10
# Always retry when these exceptions are raised.
RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, httplib.NotConnected,
httplib.IncompleteRead, httplib.ImproperConnectionState,
httplib.CannotSendRequest, httplib.CannotSendHeader,
httplib.ResponseNotReady, httplib.BadStatusLine)
# Always retry when an apiclient.errors.HttpError with one of these status
# codes is raised.
RETRIABLE_STATUS_CODES = [500, 502, 503, 504]
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the {{ Google Cloud Console }} at
# {{ https://cloud.google.com/console }}.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
# https://developers.google.com/youtube/v3/guides/authentication