Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions freshdesk_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def delete(self, id):
return self.client.req(
requests.delete, self.api_endpoint(id), self.wrapper_name)

def get(self, id):
def get(self, id=None, args=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ?

return self.client.req(
requests.get, self.api_endpoint(id), self.wrapper_name)

Expand Down Expand Up @@ -160,6 +160,7 @@ def __init__(self, url, key):
# Resources types
self.customers = FreshDeskCustomers(self)
self.contacts = FreshDeskContacts(self)
self.tickets = FreshDeskTickets(self)

def req(self, func, path, resource_type, params={}, **kwargs):
abs_url = urlparse.urljoin(self.url, path)
Expand All @@ -186,7 +187,11 @@ def req(self, func, path, resource_type, params={}, **kwargs):
# Either we get a list or a single object
json_obj = resp.json()
if isinstance(json_obj, (list, tuple)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would better see this implemented as :

  • if resource_type is none, do not unpack
  • in Ticket.get() call self.api.get(..., resource_type=None)

return [i[resource_type] for i in json_obj]
try:
return [i[resource_type] for i in json_obj]
# Tickets are not wrapped in a dic like others objects
except KeyError:
return json_obj
else:
return json_obj[resource_type]
else:
Expand All @@ -212,3 +217,19 @@ def api_endpoint(self, id):

def create(self, id, **kwargs):
return self.client.req(requests.post, self.api_endpoint(id), self.wrapper_name, **kwargs)

class FreshDeskTickets(FreshDeskObjects):
api_name = 'ticket'
wrapper_name = 'helpdesk_ticket'

def api_endpoint(self, id=None, args=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default arg would lead to some url with helpdesk/tickets/None.json.

if id:
return '/helpdesk/{}s/{}.json'.format(self.api_name, id)
else:
if args:
# Add search params in the end of the URL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Search params are already handled here (although the docstring don't explain what params do, I admit). Does it fit your need ?

text_args = self.convert_args_from_dic(args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not exist

return '/helpdesk/{}s.json{}'.format(self.api_name, text_args)
else:
return '/helpdesk/{}s.json'.format(self.api_name)