Skip to content

Commit d1cfba1

Browse files
committed
[2.0.0] Dictionary gets of a resource id returns an element instead of a len 1 list
1 parent eaa0b0c commit d1cfba1

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to this project will be documented here.
44

5+
## [2.0.0] - 2019-10-14
6+
### Changed
7+
- When requesting a single resource using the dictionary way, only a single
8+
object will be returned instead of a list with a single object.
9+
510
## [1.0.1] - 2019-10-14
611
### Fixed
712
- Correctly return entities when a single one is requested using a resource id

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,19 @@ api depending on the endpoint.
150150
> from helpscout.client import HelpScout
151151
> hs = HelpScout(app_id='laknsdo', app_secret='12haosd9')
152152
> print(hs.mailboxes[1930].get())
153-
[{'id': 1930,
154-
'name': 'Fake Support',
155-
'slug': '0912301u',
156-
'email': '[email protected]',
157-
'createdAt': '2018-12-20T20:00:00Z',
158-
'updatedAt': '2019-05-01T16:00:00Z',
159-
'_links': {
153+
Mailbox(
154+
id=1930,
155+
name='Fake Support',
156+
slug='0912301u',
157+
158+
createdAt='2018-12-20T20:00:00Z',
159+
updatedAt='2019-05-01T16:00:00Z',
160+
_links={
160161
'fields': {'href': 'https://api.helpscout.net/v2/mailboxes/1930/fields/'},
161162
'folders': {'href': 'https://api.helpscout.net/v2/mailboxes/1930/folders/'},
162163
'self': {'href': 'https://api.helpscout.net/v2/mailboxes/1930'}
163164
}
164-
}]
165+
)
165166
```
166167

167168
### Listing conversations using a dictionary parameters

helpscout/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from helpscout.client import HelpScout # noqa
22

33

4-
__version__ = '1.0.1'
4+
__version__ = '2.0.0'

helpscout/client.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ def __getattr__(self, endpoint):
7474
attributes forwards the requests to the appropriate get_objects /
7575
hit client calls.
7676
"""
77-
return HelpScoutEndpointRequester(self, endpoint)
77+
return HelpScoutEndpointRequester(self, endpoint, False)
7878

79-
def get_objects(self, endpoint, resource_id=None, params=None):
79+
def get_objects(self, endpoint, resource_id=None, params=None,
80+
specific_resource=False):
8081
"""Returns the objects from the endpoint filtering by the parameters.
8182
8283
Parameters
@@ -90,6 +91,10 @@ def get_objects(self, endpoint, resource_id=None, params=None):
9091
params: dict or str or None
9192
Dictionary with the parameters to send to the url.
9293
Or the parameters already un url format.
94+
specific_resource: bool
95+
Specifies if the endpoint is for an specific resource_id even if
96+
the id is contained in the endpoint uri and resource_id None is
97+
provided.
9398
9499
Returns
95100
-------
@@ -99,7 +104,7 @@ def get_objects(self, endpoint, resource_id=None, params=None):
99104
cls = HelpScoutObject.cls(endpoint, endpoint)
100105
results = cls.from_results(
101106
self.hit_(endpoint, 'get', resource_id, params=params))
102-
if resource_id is not None:
107+
if resource_id is not None or specific_resource:
103108
return results[0]
104109
return results
105110

@@ -302,7 +307,7 @@ def __repr__(self):
302307

303308
class HelpScoutEndpointRequester:
304309

305-
def __init__(self, client, endpoint):
310+
def __init__(self, client, endpoint, specific_resource):
306311
"""Client wrapper to perform requester.get/post/put/patch/delete.
307312
308313
Parameters
@@ -311,9 +316,13 @@ def __init__(self, client, endpoint):
311316
A help scout client instance to query the API.
312317
endpoint: str
313318
One of the endpoints in the API. E.g.: conversations, mailboxes.
319+
specific_resource: bool
320+
Specifies if the current endpoint requester is for a single
321+
specific resource id or not.
314322
"""
315323
self.client = client
316324
self.endpoint = endpoint
325+
self.specific_resource = specific_resource
317326

318327
def __getattr__(self, method):
319328
"""Catches http methods like get, post, patch, put and delete.
@@ -335,13 +344,20 @@ def __getattr__(self, method):
335344
subendpoints of specific resources, like tags from a conversation.
336345
"""
337346
if method == 'get':
338-
return partial(self.client.get_objects, self.endpoint)
347+
return partial(
348+
self.client.get_objects,
349+
self.endpoint,
350+
specific_resource=self.specific_resource,
351+
)
339352
elif method in ('head', 'post', 'put', 'delete', 'patch', 'connect',
340353
'options', 'trace'):
341354
return partial(self._yielded_function, method)
342355
else:
343356
return HelpScoutEndpointRequester(
344-
self.client, urljoin(self.endpoint + '/', str(method)))
357+
self.client,
358+
urljoin(self.endpoint + '/', str(method)),
359+
False,
360+
)
345361

346362
def __getitem__(self, resource_id):
347363
"""Returns a second endpoint requester extending the endpoint to a
@@ -363,7 +379,10 @@ def __getitem__(self, resource_id):
363379
main requester's endpoint.
364380
"""
365381
return HelpScoutEndpointRequester(
366-
self.client, urljoin(self.endpoint + '/', str(resource_id)))
382+
self.client,
383+
urljoin(self.endpoint + '/', str(resource_id)),
384+
True,
385+
)
367386

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

0 commit comments

Comments
 (0)