def _make_request(self, endpoint, params=None): """Send request to COVE API and return results as json object. Keyword arguments: `endpoint` -- endpoint of COVE API request `**params` -- filters, fields, sorts (see api documentation) Returns: `dict` (json) """ if not params: params = {} query = endpoint if params: params = params.items() params.sort() # Note: We're using urllib.urlencode() below which escapes spaces as # a plus ("+") since that is what the COVE API expects. But a space # should really be encoded as "%20" (ie. urllib.quote()). I believe # this is a bug in the COVE API authentication scheme... but we have # to live with this in the client. We'll update this to use "%20" # once the COVE API supports it properly. query = '%s?%s' % (query, urllib.urlencode(params)) request = urllib2.Request(query) auth = PBSAuthorization(self.api_app_id, self.api_app_secret) signed_request = auth.sign_request(request) response = urllib2.urlopen(signed_request) print query return json.loads(response.read())
def _make_request(self, endpoint, params=None): """Send request to COVE API and return results as json object. Keyword arguments: `endpoint` -- endpoint of COVE API request `**params` -- filters, fields, sorts (see api documentation) Returns: `dict` (json) """ if not params: params = {} query = endpoint if params: sorted_dict = sorted(params.items(), key=lambda x: x[0]) sorted_params = OrderedDict(sorted_dict) # Note: We're using urllib.urlencode() below which escapes spaces as # a plus ("+") since that is what the COVE API expects. But a space # should really be encoded as "%20" (ie. urllib.quote()). I believe # this is a bug in the COVE API authentication scheme... but we have # to live with this in the client. We'll update this to use "%20" # once the COVE API supports it properly. query = '{0}?{1}'.format(query, urllib.parse.urlencode(sorted_params)) request = urllib.request.Request(query) auth = PBSAuthorization(self.api_app_id, self.api_app_secret) signed_request = auth.sign_request(request) response = urllib.request.urlopen(signed_request) str_response = response.readall().decode('utf-8') obj = json.loads(str_response) return obj