Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. def read_file_contents(path):
  2. try:
  3. with open('token') as infile:
  4. return infile.read().strip()
  5. except FileNotFoundError:
  6. print("Couldn't find the token file!")
  7. return None
  8.  
  9. class ByiloApiClient(object):
  10. token = None
  11. session = requests.Session()
  12.  
  13. def __init__(self, api_url):
  14.  
  15. self.api_url = api_url
  16. self.login()
  17. self.token = self.load_token()
  18. self.headers = {'content-type': 'application/json', 'token': self.token}
  19. if self.token is None:
  20. print('Authentication is failed')
  21. else:
  22. print('Authentication is successful Token is present.')
  23. # Authorization
  24. def login(self):
  25. username = 'kravchuk@gmail.com'
  26. password = 'hjb567HgMjylh'
  27. headers = {'content-type': 'application/json'}
  28. payload = {
  29. 'email': username,
  30. 'password': password
  31. }
  32. r = requests.post(self.api_url + '/auth/admin',
  33. data=json.dumps(payload),
  34. headers=headers)
  35. r.raise_for_status()
  36. self.token = r.json()['token']
  37. self.save_token(self.token)
  38. self.session.headers.update({'Authorization': self.token})
  39. # GET Constraction standarts
  40. def get_cs(self):
  41. r = requests.get(self.api_url + '/cs', headers=self.headers)
  42. r.raise_for_status()
  43. return r.content
  44. # GET Clients
  45. def get_client(self):
  46. headers = {'content-type': 'application/json', 'token': self.token}
  47. payload = {'id': 'client id', 'name': 'client name', 'csCount': 'amount of assigned construction standards',
  48. 'activeUserCount': 'amount of active users'}
  49. r = requests.get(self.api_url + '/client', headers=self.headers, params=payload)
  50. r.raise_for_status()
  51. return r.text
  52. # POST Client
  53. def post_client(self):
  54. json = {'code': 'EN-1993', 'limit': '3','c': '3', 'end': '2016-09-24T16:49:45.415Z'}'
  55. data = {'name': datetime.datetime.now(), 'csCount': '1', 'code': 'SC', 'email': 'sc@gmail.com', 'phone': '+304422222222', 'country':'Ukraine',
  56. 'city': 'Kiev', 'postcode': '22222', 'address1': 'mazepa str., 2', 'address2': 'petlura str., 2', "csInfo": json }
  57.  
  58.  
  59. r = requests.post(self.api_url + 'client', headers=self.headers, params=data)
  60. return r.text
  61.  
  62.  
  63. def load_token(self):
  64. return read_file_contents('token')
  65.  
  66. def save_token(self, str):
  67. with open('token', 'w') as outfile:
  68. outfile.write(str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement