From a04df0f8e3c1faebbd3965ec2e887f8ab36b437b Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Wed, 20 Apr 2016 15:48:54 -0700 Subject: [PATCH] Extract part of get_json into get_response For some experiments I wanted the ability to grab the raw requests response, previously it was hidden by just directly returning response.json() --- htsworkflow/submission/encoded.py | 54 ++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/htsworkflow/submission/encoded.py b/htsworkflow/submission/encoded.py index f235828..6c30a59 100644 --- a/htsworkflow/submission/encoded.py +++ b/htsworkflow/submission/encoded.py @@ -198,22 +198,10 @@ class ENCODED: if len(kwargs) == 0: kwargs['limit'] = 'all' - url = self.prepare_url(obj_id) - LOGGER.info('requesting url: {}'.format(url)) - - # do the request - - LOGGER.debug('username: %s, password: %s', self.username, self.password) - arguments = {} - if self.username and self.password: - arguments['auth'] = self.auth - response = requests.get(url, headers=self.json_headers, - params=kwargs, - **arguments) - if not response.status_code == requests.codes.ok: - LOGGER.error("Error http status: {}".format(response.status_code)) - response.raise_for_status() - return response.json() + response = self.get_response(obj_id, **kwargs) + data = response.json() + response.close() + return data def get_jsonld(self, obj_id, **kwargs): '''Get ENCODE object as JSONLD annotated with classses contexts @@ -237,6 +225,40 @@ class ENCODED: raise ValueError('@type is not a sequence') return obj_type[0] + def get_response(self, fragment, **kwargs): + '''GET an ENCODED url and return the requests request + + Uses prepare_url to allow url short-cuts + if no keyword arguments are specified it will default to adding limit=all + Alternative keyword arguments can be passed in and will be sent to the host. + + Known keywords are: + limit - (integer or 'all') how many records to return, all for all of them + embed - (bool) if true expands linking ids into their associated object. + format - text/html or application/json + ''' + url = self.prepare_url(fragment) + LOGGER.info('requesting url: {}'.format(url)) + + # do the request + LOGGER.debug('username: %s, password: %s', self.username, self.password) + arguments = {} + if self.username and self.password: + arguments['auth'] = self.auth + + if 'stream' in kwargs: + arguments['stream'] = kwargs['stream'] + del kwargs['stream'] + + response = requests.get(url, headers=self.json_headers, + params=kwargs, + **arguments) + if not response.status_code == requests.codes.ok: + LOGGER.error("Error http status: {}".format(response.status_code)) + response.raise_for_status() + + return response + def get_schema_url(self, object_type): """Create the ENCODED jsonschema url. -- 2.30.2