Skip to content

Commit

Permalink
Merge pull request #91 from gadventures/add-GET-headers
Browse files Browse the repository at this point in the history
Add option to include headers in 'GET' requests
  • Loading branch information
marz619 committed Feb 14, 2018
2 parents 46e5cb9 + 77ae9e0 commit 914b1c0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ only supported for queries whose resources are listable.
``options()``
Get the options for a single resource

``get(resource_id)``
Get a single resource.
``get(resource_id, [headers={}])``
Get a single resource; optionally passing in a dictionary of header
values.

``create(data)``
Create an instance of the query resource using the given data.
Expand Down
9 changes: 5 additions & 4 deletions gapipy/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _to_dict(self):
def options(self):
return self.resource.options(client=self._client)

def get(self, resource_id, variation_id=None, cached=True):
def get(self, resource_id, variation_id=None, cached=True, headers=None):
"""
Returns an instance of the query resource with the given `resource_id`
(and optional `variation_id`) or `None` if the resource with the given
Expand All @@ -63,7 +63,8 @@ def get(self, resource_id, variation_id=None, cached=True):
data = self.get_resource_data(
resource_id,
variation_id=variation_id,
cached=cached
cached=cached,
headers=headers
)
except HTTPError as e:
if e.response.status_code in HTTPERRORS_MAPPED_TO_NONE:
Expand All @@ -72,7 +73,7 @@ def get(self, resource_id, variation_id=None, cached=True):
resource_object = self.resource(data, client=self._client)
return resource_object

def get_resource_data(self, resource_id, variation_id=None, cached=True):
def get_resource_data(self, resource_id, variation_id=None, cached=True, headers=None):
'''
Returns a dictionary of resource data, which is used to initialize
a Resource object in the `get` method.
Expand All @@ -86,7 +87,7 @@ def get_resource_data(self, resource_id, variation_id=None, cached=True):

# Cache miss; get fresh data from the backend, set in cache
requestor = APIRequestor(self._client, self.resource)
out = requestor.get(resource_id, variation_id=variation_id)
out = requestor.get(resource_id, variation_id=variation_id, headers=headers)
if out is not None:
self._client._cache.set(key, out)
self._filters = {}
Expand Down
4 changes: 2 additions & 2 deletions gapipy/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def options(self):
"""
return self._request('/{0}'.format(self._get_uri()), 'OPTIONS')

def get(self, resource_id=None, uri=None, variation_id=None):
def get(self, resource_id=None, uri=None, variation_id=None, headers=None):
"""
Get a single resource with the given resource_id or uri
Expand All @@ -128,7 +128,7 @@ def get(self, resource_id=None, uri=None, variation_id=None):
if variation_id:
components.append(str(variation_id))
uri = '/'.join(components)
return self._request(uri, 'GET')
return self._request(uri, 'GET', additional_headers=headers)

def update(self, resource_id, data, partial=True, uri=None):
"""
Expand Down

0 comments on commit 914b1c0

Please sign in to comment.