Skip to content

Commit

Permalink
[2.0.0] Dictionary gets of a resource id returns an element instead o…
Browse files Browse the repository at this point in the history
…f a len 1 list
  • Loading branch information
santiher committed Oct 14, 2019
1 parent eaa0b0c commit d1cfba1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented here.

## [2.0.0] - 2019-10-14
### Changed
- When requesting a single resource using the dictionary way, only a single
object will be returned instead of a list with a single object.

## [1.0.1] - 2019-10-14
### Fixed
- Correctly return entities when a single one is requested using a resource id
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,19 @@ api depending on the endpoint.
> from helpscout.client import HelpScout
> hs = HelpScout(app_id='laknsdo', app_secret='12haosd9')
> print(hs.mailboxes[1930].get())
[{'id': 1930,
'name': 'Fake Support',
'slug': '0912301u',
'email': '[email protected]',
'createdAt': '2018-12-20T20:00:00Z',
'updatedAt': '2019-05-01T16:00:00Z',
'_links': {
Mailbox(
id=1930,
name='Fake Support',
slug='0912301u',
email='[email protected]',
createdAt='2018-12-20T20:00:00Z',
updatedAt='2019-05-01T16:00:00Z',
_links={
'fields': {'href': 'https://api.helpscout.net/v2/mailboxes/1930/fields/'},
'folders': {'href': 'https://api.helpscout.net/v2/mailboxes/1930/folders/'},
'self': {'href': 'https://api.helpscout.net/v2/mailboxes/1930'}
}
}]
)
```

### Listing conversations using a dictionary parameters
Expand Down
2 changes: 1 addition & 1 deletion helpscout/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from helpscout.client import HelpScout # noqa


__version__ = '1.0.1'
__version__ = '2.0.0'
33 changes: 26 additions & 7 deletions helpscout/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ def __getattr__(self, endpoint):
attributes forwards the requests to the appropriate get_objects /
hit client calls.
"""
return HelpScoutEndpointRequester(self, endpoint)
return HelpScoutEndpointRequester(self, endpoint, False)

def get_objects(self, endpoint, resource_id=None, params=None):
def get_objects(self, endpoint, resource_id=None, params=None,
specific_resource=False):
"""Returns the objects from the endpoint filtering by the parameters.
Parameters
Expand All @@ -90,6 +91,10 @@ def get_objects(self, endpoint, resource_id=None, params=None):
params: dict or str or None
Dictionary with the parameters to send to the url.
Or the parameters already un url format.
specific_resource: bool
Specifies if the endpoint is for an specific resource_id even if
the id is contained in the endpoint uri and resource_id None is
provided.
Returns
-------
Expand All @@ -99,7 +104,7 @@ def get_objects(self, endpoint, resource_id=None, params=None):
cls = HelpScoutObject.cls(endpoint, endpoint)
results = cls.from_results(
self.hit_(endpoint, 'get', resource_id, params=params))
if resource_id is not None:
if resource_id is not None or specific_resource:
return results[0]
return results

Expand Down Expand Up @@ -302,7 +307,7 @@ def __repr__(self):

class HelpScoutEndpointRequester:

def __init__(self, client, endpoint):
def __init__(self, client, endpoint, specific_resource):
"""Client wrapper to perform requester.get/post/put/patch/delete.
Parameters
Expand All @@ -311,9 +316,13 @@ def __init__(self, client, endpoint):
A help scout client instance to query the API.
endpoint: str
One of the endpoints in the API. E.g.: conversations, mailboxes.
specific_resource: bool
Specifies if the current endpoint requester is for a single
specific resource id or not.
"""
self.client = client
self.endpoint = endpoint
self.specific_resource = specific_resource

def __getattr__(self, method):
"""Catches http methods like get, post, patch, put and delete.
Expand All @@ -335,13 +344,20 @@ def __getattr__(self, method):
subendpoints of specific resources, like tags from a conversation.
"""
if method == 'get':
return partial(self.client.get_objects, self.endpoint)
return partial(
self.client.get_objects,
self.endpoint,
specific_resource=self.specific_resource,
)
elif method in ('head', 'post', 'put', 'delete', 'patch', 'connect',
'options', 'trace'):
return partial(self._yielded_function, method)
else:
return HelpScoutEndpointRequester(
self.client, urljoin(self.endpoint + '/', str(method)))
self.client,
urljoin(self.endpoint + '/', str(method)),
False,
)

def __getitem__(self, resource_id):
"""Returns a second endpoint requester extending the endpoint to a
Expand All @@ -363,7 +379,10 @@ def __getitem__(self, resource_id):
main requester's endpoint.
"""
return HelpScoutEndpointRequester(
self.client, urljoin(self.endpoint + '/', str(resource_id)))
self.client,
urljoin(self.endpoint + '/', str(resource_id)),
True,
)

def _yielded_function(self, method, *args, **kwargs):
"""Calls a generator function and calls next.
Expand Down

0 comments on commit d1cfba1

Please sign in to comment.