Coverage for beyonic.api_client : 66%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
''' # This return the api client. It should first check if requests is installed, if its installed then returns RequestsClient, # if requests is not installed then check for urlfetch lib, if its installed then return its client UrlfetchClient # if both of them not installed then throws the exception '''
# let's try to import requests if its available except ImportError: requests = None
# let's try to import urlfetch
raise BeyonicError('requests is not installed. Please install/setup it first using pip. e.g. pip install requests>=1.0')
kwargs['verify'] = True else:
url, headers=headers, params=params, timeout=80, **kwargs) else: url, headers=headers, data=params, timeout=80, **kwargs) except TypeError, e: raise TypeError( 'Please upgrade your request library. The ' 'underlying error was: %s' % (e,))
except Exception, e: # Would catch just requests.exceptions.RequestException, but can # also raise ValueError, RuntimeError, etc. self._handle_request_error(e)
msg = ("Unexpected error communicating with Beyonic API.") err = "A %s was raised" % (type(e).__name__,) if str(e): err += " with error message %s" % (str(e),) else: err += " with no error message" msg = textwrap.fill(msg) + "\n\n(error: %s)" % (err,) raise BeyonicError(msg)
if not urlfetch: raise BeyonicError('urlfetch is not installed. Please install/setup it first.') try: result = urlfetch.fetch( url=url, method=method, headers=headers,
validate_certificate=self._verify_ssl_certs, deadline=55, payload=params ) except urlfetch.Error, e: self._handle_request_error(e, url)
return result.content, result.status_code
if isinstance(e, urlfetch.InvalidURLError): msg = ("Unexpected error communicating with Beyonic API.") elif isinstance(e, urlfetch.DownloadError): msg = "There was a problem retrieving data from Beyonic." elif isinstance(e, urlfetch.ResponseTooLargeError): msg = ("Unexpected error communicating with Beyonic API.") else: msg = ("Unexpected error communicating with Beyonic API.")
msg = textwrap.fill(msg) + "\n\n(error: " + str(e) + ")" raise BeyonicError(msg)
impl = UrlFetchClient else: # none of them is available so let's throw an error raise BeyonicError( 'Either of requests or urlfetch is not installed. Please install either of them using requirements.txt')
''' 'API Client class interacts with api using available RequestClient or UrlFetchClient '''
""" A client for the api """ # if not passed then let's try to get it from env variable try: api_key = os.environ['BEYONIC_ACCESS_KEY'] except KeyError: raise BeyonicError('BEYONIC_ACCESS_KEY not set.')
raise BeyonicError('Base url can\'t be empty. You should set base url using beyonic.api_endpoint_base')
''' Makes an HTTP GET request to the API. Any keyword arguments will be converted to query string parameters. '''
''' Makes an HTTP POST request to the API. '''
''' Makes an HTTP PUT request to the API. ''' return self._request("put", **kwargs)
''' Makes an HTTP patch request to the API. '''
''' Makes an HTTP DELETE request to the API. '''
method=method, url=self._url, headers=self._build_headers(), params=kwargs, )
# TODO: need exact status code for different type error else:
#status_code 204 is for delete so for it retruning True else:
# TODO need to handle the all the status
|