Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # coding=utf-8
- import requests
- import json
- class DeskError(Exception):
- def __init__(self, status):
- Exception.__init__(self, status) # Exception is an old-school class
- self.status = status
- def __str__(self):
- return self.status
- def __unicode__(self):
- return unicode(self.__str__())
- class DeskClient(object):
- BASE_URL = 'desk.com/api/v2'
- sitename = "värde"
- username = "värde"
- password = "värde"
- def __init__(self, sitename, username, password):
- self.sitename = sitename
- self.username = username
- self.password = password
- def build_url(self, service):
- return 'https://%s.%s/%s' % (self.sitename,
- DeskClient.BASE_URL, service)
- def call_get(self, service, params=None):
- url = self.build_url(service)
- r = requests.get(url, params=params,
- auth=(self.username, self.password))
- print r.content
- if r.status_code != requests.codes.ok:
- raise DeskError(str(r.status_code))
- return json.loads(r.content)
- def call_post(self, service, data=None):
- url = self.build_url(service)
- r = requests.post(url, data=json.dumps(data),
- auth=(self.username, self.password))
- if r.status_code >= 400:
- raise DeskError(str(r.status_code))
- return json.loads(r.content)
- def find_customer(self, params):
- customer_link = None
- try:
- customer_json = self.call_get('customers/search', params)
- if customer_json['total_entries'] > 0:
- customer_link = customer_json['_embedded'][
- 'entries'][0]['_links']['self']
- except DeskError:
- pass
- return customer_link
- if __name__ == "__main__":
- search_name = raw_input("Search for customer: ")
- find_customer(search_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement