jsonAndRequest.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import json
  2. import requests
  3. import config
  4. import ast
  5. from classes.Answer import Answer
  6. from classes.Order import Order
  7. from classes.OrderTicket import OrderTicket
  8. from classes.Ticket_user import Ticket_user
  9. from classes.Ticket import Ticket
  10. #Функция преобразования json в объект класса Order
  11. def jsonToOrder(jsonstring) -> Order:
  12. jsonObj = json.loads(jsonstring)
  13. print(jsonObj)
  14. u = Order(**jsonObj)
  15. return u
  16. #Функция преобразования json в объект класса OrderTicket
  17. def jsonToOrderTicket(jsonstring) -> OrderTicket:
  18. print (jsonstring)
  19. jsonObj = json.loads(jsonstring)
  20. print(jsonObj)
  21. u = OrderTicket(**jsonObj)
  22. return u
  23. #Функция обращения к апи и получение токена
  24. def requestGetToken(login, password, url) -> str:
  25. login = "79999999999"
  26. password = "12345"
  27. # login = "11111111111"
  28. # password = "Sharix!"
  29. #url = "https://user.sharix-app.org/auth/token/login/"
  30. data = {
  31. "password":password,
  32. "phone_number":login
  33. }
  34. response = requests.post(url,json=data)
  35. result = response.json()
  36. return result['auth_token']
  37. #Функция обращения к апи и получение токена
  38. def msg_to_text(jsonstring):
  39. jsonObj = json.loads(jsonstring)
  40. u = jsonObj
  41. return u
  42. def createUser(username, phone, password, url) -> str:
  43. data = {
  44. "username": username,
  45. "phone_number": phone,
  46. "password": password
  47. }
  48. response = requests.post(url, json=data)
  49. result = response.json()
  50. return result
  51. #Функция обращения к апи и получение списка пользователей
  52. def requestGetListUser():
  53. auth_token=requestGetToken()
  54. print(auth_token)
  55. headers = {'Authorization': f'Token {auth_token}'}
  56. url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  57. response = requests.get(url, headers=headers)
  58. print(response)
  59. print(response.json())
  60. # def requestGetList(auth_token, url):
  61. # headers = {'Authorization': f'Token {auth_token}'}
  62. # response = requests.get(url, headers=headers)
  63. # #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  64. # ordertext=response.json()
  65. # startlist = []
  66. # if not ordertext:
  67. # print("Empty array")
  68. # else:
  69. # for i in ordertext:
  70. # print (i, "PROCESSING")
  71. # try:
  72. # orderticket = OrderTicket(**i)
  73. # print(i, "ORDER ", orderticket)
  74. # startlist.append(orderticket)
  75. # except Exception as ex:
  76. # print(ex)
  77. # print (startlist)
  78. # return startlist
  79. def requestGetList(auth_token, url):
  80. headers = {'Authorization': f'Token {auth_token}'}
  81. response = requests.get(url, headers=headers)
  82. #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  83. ordertext=response.json()
  84. startlist = []
  85. if not ordertext:
  86. print("Empty array")
  87. else:
  88. for i in ordertext:
  89. print (i, "PROCESSING")
  90. try:
  91. order = Order(**i)
  92. print(i, "ORDER ", order)
  93. startlist.append(order)
  94. except Exception as ex:
  95. print(ex)
  96. print (startlist)
  97. return startlist
  98. def requestGetTicket_user(auth_token, url):
  99. headers = {'Authorization': f'Token {auth_token}'}
  100. response = requests.get(url, headers=headers)
  101. #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  102. tickettext=response.json()
  103. startlist = []
  104. if not tickettext:
  105. print("Empty array")
  106. else:
  107. for i in tickettext:
  108. try:
  109. ticket = Ticket_user(**i)
  110. startlist.append(ticket)
  111. except Exception as ex:
  112. print(ex)
  113. print (startlist)
  114. return startlist
  115. def requestPatchTicketUser(token,url, data):
  116. headers = {'Authorization': f'Token {token}'}
  117. response = requests.patch(url, json=data, headers=headers)
  118. return response
  119. def change_groups(auth_token, url,group):
  120. headers = {
  121. 'Content-Type': 'application/json',
  122. 'Authorization': f'Token {auth_token}'
  123. }
  124. response = requests.get(url, headers=headers)
  125. user_data = response.json()
  126. current_groups = user_data.get('groups', [])
  127. updated_groups = list(set([group]+current_groups))
  128. data = {'groups': updated_groups}
  129. print(data)
  130. response = requests.put(url, json=data, headers=headers)
  131. print(url)
  132. print(user_data)
  133. return response
  134. def change_status_company(auth_token, url,id_metaservice):
  135. headers = {
  136. 'Content-Type': 'application/json',
  137. 'Authorization': f'Token {auth_token}'
  138. }
  139. response = requests.get(url, headers=headers)
  140. user_data = response.json()
  141. print(user_data)
  142. current_metaservice = user_data.get('id_metaservice', [])
  143. repsonse = requests.put(config.API_URL+f"dbsynce/api/company/{id_metaservice}", json=data, headers=headers)
  144. return response
  145. def to_answer(jsonstring):
  146. json_answer = json.loads(jsonstring)
  147. print(json_answer)
  148. u = Answer(**json_answer)
  149. return u
  150. def change_to_task(localtask,token):
  151. headers = {
  152. 'Authorization': f'Token {token}',
  153. 'Content-Type': 'application/json' # Установка заголовка Content-Type
  154. }
  155. data = json.dumps(localtask.__dict__)
  156. response = requests.patch(config.API_URL+f"/dbsynce/api/orders/{localtask.id}/", headers=headers, data=data)
  157. return response