1
0

jsonAndRequest.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import json
  2. import requests
  3. import config
  4. import ast
  5. #Функция обращения к апи и получение токена
  6. def requestGetToken(url) -> str:
  7. login = "11111111111"
  8. password = "Sharix!"
  9. #url = "https://user.sharix-app.org/auth/token/login/"
  10. data = {
  11. "password":password,
  12. "phone_number":login
  13. }
  14. response = requests.post(url,json=data)
  15. result = response.json()
  16. return result['auth_token']
  17. #Функция обращения к апи и получение токена
  18. def msg_to_text(jsonstring):
  19. jsonObj = json.loads(jsonstring)
  20. u = jsonObj
  21. return u
  22. def createUser(username, phone, password, url) -> str:
  23. data = {
  24. "username": username,
  25. "phone_number": phone,
  26. "password": password
  27. }
  28. response = requests.post(url, json=data)
  29. result = response.json()
  30. return result
  31. #Функция обращения к апи и получение списка пользователей
  32. def requestGetListUser(token):
  33. headers = {'Authorization': f'Token {token}'}
  34. url = 'http://testopen.sharix-app.org/api/v1/platform/sharix-users/'
  35. response = requests.get(url, headers=headers)
  36. return response
  37. class Ticket(object):
  38. def __init__(self, id, name, group):
  39. self.id = id
  40. self.name = name
  41. self.group = group
  42. def __str__(self):
  43. return "{0}, {1}, {2},\n".format(self.id, self.name, self.group)
  44. def requestGetList(auth_token, url):
  45. headers = {'Authorization': f'Token {auth_token}'}
  46. response = requests.get(url, headers=headers)
  47. #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  48. tickettext=response.json()
  49. startlist = []
  50. if not tickettext:
  51. print("Empty array")
  52. else:
  53. for i in tickettext:
  54. print (i, "PROCESSING")
  55. try:
  56. ticket = Ticket(**i)
  57. print(i, "Ticket ", ticket)
  58. startlist.append(ticket)
  59. except Exception as ex:
  60. print(ex)
  61. print (startlist)
  62. return startlist
  63. class Ticket_user(object):
  64. def __init__(self, id, status, title, ticket_type, created_at, updated_at, due_date, priority, created_by, assigned_to):
  65. self.id = id
  66. self.status = status
  67. self.title = title
  68. self.ticket_type = ticket_type
  69. self.created_at = created_at
  70. self.updated_at = updated_at
  71. self.due_date = due_date
  72. self.priority = priority
  73. self.created_by = created_by
  74. self.assigned_to = assigned_to
  75. def __str__(self):
  76. return "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}\n".format(
  77. self.id, self.status, self.title, self.ticket_type, self.created_at,
  78. self.updated_at, self.due_date, self.priority,
  79. self.created_by, self.assigned_to
  80. )
  81. class User(object):
  82. def __init__(self,
  83. group_name,
  84. last_login,
  85. is_superuser,
  86. username,
  87. is_staff,
  88. is_active,
  89. date_joined,
  90. middle_name,
  91. phone_number,
  92. email,
  93. avatar,
  94. groups,
  95. user_permissions,
  96. full_name="",
  97. last_name="",
  98. first_name="",
  99. ):
  100. self.full_name = full_name
  101. self.group_name = group_name
  102. self.last_login = last_login
  103. self.is_superuser = is_superuser
  104. self.username = username
  105. self.is_staff = is_staff
  106. self.is_active = is_active
  107. self.date_joined = date_joined
  108. self.last_name = last_name
  109. self.first_name = first_name
  110. self.middle_name = middle_name
  111. self.phone_number = phone_number
  112. self.email = email
  113. self.avatar = avatar
  114. self.groups = groups
  115. self.user_permissions = user_permissions
  116. def __str__(self):
  117. return ', '.join([
  118. str(self.full_name),
  119. str(self.group_name),
  120. str(self.last_login),
  121. str(self.is_superuser),
  122. str(self.username),
  123. str(self.is_staff),
  124. str(self.is_active),
  125. str(self.date_joined),
  126. str(self.last_name),
  127. str(self.first_name),
  128. str(self.middle_name),
  129. str(self.phone_number),
  130. str(self.email),
  131. str(self.avatar),
  132. str(self.groups),
  133. str(self.user_permissions)
  134. ]) + '\n'
  135. def requestGetUser(auth_token, url):
  136. headers = {'Authorization': f'Token {auth_token}'}
  137. response = requests.get(url, headers=headers)
  138. #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  139. usertext=response.json()
  140. print(usertext)
  141. user = None
  142. if not usertext:
  143. print("Empty user")
  144. None
  145. else:
  146. try:
  147. user = User(**usertext)
  148. except Exception as ex:
  149. print(ex)
  150. print (user)
  151. return user
  152. def requestGetTicket_user(auth_token, url):
  153. headers = {'Authorization': f'Token {auth_token}'}
  154. response = requests.get(url, headers=headers)
  155. #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  156. tickettext=response.json()
  157. startlist = []
  158. if not tickettext:
  159. print("Empty array")
  160. else:
  161. for i in tickettext:
  162. try:
  163. ticket = Ticket_user(**i)
  164. startlist.append(ticket)
  165. except Exception as ex:
  166. print(ex)
  167. print (startlist)
  168. return startlist
  169. def requestPatchTicketUser(token,url, data):
  170. headers = {'Authorization': f'Token {token}'}
  171. response = requests.patch(url, json=data, headers=headers)
  172. return response
  173. def change_groups(auth_token, url,group):
  174. headers = {
  175. 'Content-Type': 'application/json',
  176. 'Authorization': f'Token {auth_token}'
  177. }
  178. response = requests.get(url, headers=headers)
  179. user_data = response.json()
  180. current_groups = user_data.get('groups', [])
  181. updated_groups = list(set([group]+current_groups))
  182. data = {'groups': updated_groups}
  183. print(data)
  184. response = requests.put(url, json=data, headers=headers)
  185. print(url)
  186. print(user_data)
  187. return response
  188. def change_status_company(auth_token, url,id_metaservice):
  189. headers = {
  190. 'Content-Type': 'application/json',
  191. 'Authorization': f'Token {auth_token}'
  192. }
  193. response = requests.get(url, headers=headers)
  194. user_data = response.json()
  195. print(user_data)
  196. current_metaservice = user_data.get('id_metaservice', [])
  197. repsonse = requests.put(config.API_URL+f"dbsynce/api/company/{id_metaservice}", json=data, headers=headers)
  198. return response