1
0

jsonAndRequest.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import json
  2. import requests
  3. import config
  4. import ast
  5. #Тестовый класс User и Address (ВАЖНО!!!) поля классов должны называться также как и поля json объекта
  6. '''
  7. #Full json order example
  8. https://wiki.sharix-app.org/doku.php/open/tech/dev/sharix_open_-_primery_json
  9. {
  10. "ticket": 123,
  11. "status": 111,
  12. "priority": 1,
  13. "title": "123",
  14. "note": "123",
  15. "due_date": 2023-05-15T17:16:22.955601Z,
  16. "id_metaservice": 1,
  17. "is_global": false,
  18. "is_visible": false,
  19. "service": 1,
  20. "service_type": 1,
  21. "status_changed_date": "2023-05-15T17:16:22.955601Z",
  22. "time_placed": "2023-05-15T16:40:32.179777Z",
  23. "time_start": "2023-05-15T16:40:32.179777Z",
  24. "time_start_predicted": "2023-05-15T16:40:32.179777Z",
  25. "time_finish_predicted": "2023-05-15T16:40:32.179777Z",
  26. "time_start_real": "2023-05-15T16:40:32.179777Z",
  27. "time_finish_real": "2023-05-15T16:40:32.179777Z",
  28. "order_place_type": "address",
  29. "order_place_start": "р-н Беговой",
  30. "order_place_start_gps": "55.751941, 37.607513",
  31. "order_place_start_sector": 4,
  32. "order_place_start_real": "р-н Беговой",
  33. "order_place_start_real_gps": "55.751941, 37.607513",
  34. "order_place_finish_predicted": "р-н Мещанский",
  35. "order_place_finish_predicted_gps": "55.751941, 37.607513",
  36. "order_place_finish_sector": 4,
  37. "order_place_finish_real": "р-н Мещанский",
  38. "order_place_finish_real_gps": "55.751941, 37.607513",
  39. "predicted_price": 1000,
  40. "real_price": 1001,
  41. "payment_transaction_id": 1,
  42. "rating_id": 1,
  43. "provider": 1,
  44. "provider_screen_name": "Vasya Ispolnitelev",
  45. "receiver": 1,
  46. "receiver_screen_name": "Marfa Clientovna",
  47. "client_id": 1,
  48. "client_screen_name": "Fedor Client",
  49. "resource_id": 1,
  50. "attempts": null
  51. }
  52. '''
  53. #надо адаптировать класс к новому формату JSON
  54. class Order(object):
  55. def __init__(self, order_synced, ticket, status, title, note, due_date, id_metaservice, is_global, is_visible, service, service_type, status_changed_date, time_placed, time_start=None, time_start_predicted=None, time_finish_predicted=None, time_start_real=None, time_finish_real=None, order_place_type=None, order_place_start=None, order_place_start_gps=None, order_place_start_real=None, order_place_start_real_gps=None, order_place_finish_predicted=None, order_place_finish_predicted_gps=None, order_place_finish_real=None, order_place_finish_real_gps=None, predicted_price=None, real_price=None, payment_transaction_id=None, rating_id=None, provider=None, provider_screen_name=None, receiver=None, receiver_screen_name=None, client_id=None, resource_id=None, attempts=None, asap=None):
  56. self.order_synced = order_synced
  57. self.ticket = ticket
  58. self.status = status
  59. self.title = title
  60. self.note = note
  61. self.due_date = due_date
  62. self.id_metaservice = id_metaservice
  63. self.is_global = is_global
  64. self.is_visible = is_visible
  65. self.service = service
  66. self.service_type = service_type
  67. self.status_changed_date = status_changed_date
  68. self.time_placed = time_placed
  69. self.time_start = time_start
  70. self.time_start_predicted = time_start_predicted
  71. self.time_finish_predicted = time_finish_predicted
  72. self.time_start_real = time_start_real
  73. self.time_finish_real = time_finish_real
  74. self.order_place_type =order_place_type
  75. self.order_place_start = order_place_start
  76. self.order_place_start_gps = order_place_start_gps
  77. self.order_place_start_real = order_place_start_real
  78. self.order_place_start_real_gps = order_place_start_real_gps
  79. self.order_place_finish_predicted = order_place_finish_predicted
  80. self.order_place_finish_predicted_gps = order_place_finish_predicted_gps
  81. self.order_place_finish_real = order_place_finish_real
  82. self.order_place_finish_real_gps = order_place_finish_real_gps
  83. self.predicted_price = predicted_price
  84. self.real_price = real_price
  85. self.payment_transaction_id = payment_transaction_id
  86. self.rating_id = rating_id
  87. self.provider = provider
  88. self.provider_screen_name = provider_screen_name
  89. self.receiver = receiver
  90. self.receiver_screen_name = receiver_screen_name
  91. self.client_id = client_id
  92. self.resource_id = resource_id
  93. self.attempts = attempts
  94. self.asap = asap
  95. #доделать до конца по аналогии с закоммиченным json
  96. def __str__(self):
  97. return "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {27}, {28}, {29}, {30}, {31}, {32}".format(self.order_synced, self.ticket, self.status, self.title, self.note, self.due_date, self.id_metaservice, self.is_global, self.is_visible, self.service, self.service_type, self.status_changed_date, self.time_placed, self.time_start, self.time_start_predicted, self.time_finish_predicted, self.time_start_real, self.time_finish_real, self.order_place_type, self.order_place_start, self.order_place_start_gps, self.order_place_start_real, self.order_place_start_real_gps, self.order_place_finish_predicted, self.order_place_finish_predicted_gps, self.order_place_finish_real, self.order_place_finish_real_gps, self.predicted_price, self.real_price, self.payment_transaction_id, self.rating_id, self.provider, self.provider_screen_name, self.receiver, self.receiver_screen_name, self.client_id, self.resource_id, self.attempts, self.asap)
  98. #\n добавляете в местах с пустой строкой в шаблоне
  99. #ticket json example
  100. '''
  101. {
  102. "id": 2,
  103. "status": 111,
  104. "created_date": "2023-05-15",
  105. "status_changed_date": "2023-05-15T17:16:22.955601Z",
  106. "priority": 0,
  107. "title": "Test",
  108. "note": "{\n \n \"ticket\": 1,\n \"state\": 1,\n \"id_metaservice\": 1,\n \"is_global\": false,\n \"is_visible\": false,\n \"service\": 1,\n \"service_type\": 1,\n\n \"time_placed\": 1}"
  109. "due_date": "2023-05-15",
  110. "task_list": 10,
  111. "created_by": 1,
  112. "type": 1,
  113. "assigned_to": 1
  114. }
  115. '''
  116. #ticket minimal example
  117. '''
  118. {
  119. "id": 2,
  120. "status": 111,
  121. "created_date": "2023-05-15",
  122. "status_changed_date": "2023-05-15T17:16:22.955601Z",
  123. "priority": 0,
  124. "title": null,
  125. "note": null,
  126. "due_date": null,
  127. "task_list": 10,
  128. "created_by": 1,
  129. "type": 1,
  130. "assigned_to": null
  131. }
  132. {
  133. "serviceType": 2,
  134. "driverSearchEngine": 0,
  135. "comment": "Yyyy",
  136. "arrivalTime": "2023-12-07T14:39:54Z",
  137. "additionalServices": [],
  138. "tasks": [
  139. {
  140. "text": "Eeee",
  141. "address": "Jjjj"
  142. }
  143. ],
  144. "spendingLimit": 6668,
  145. "isWithPaymentDocument": true,
  146. "id": 294830,
  147. "isProceedBeforeArrival": true
  148. }
  149. '''
  150. class OrderTicket(object):
  151. def __init__(self, serviceType, driverSearchEngine, comment, arrivalTime, additionalServices, tasks, spendingLimit, isWithPaymentDocument, id, isProceedBeforeArrival):
  152. self.serviceType = serviceType
  153. self.driverSearchEngine = driverSearchEngine
  154. self.comment = comment
  155. self.arrivalTime = arrivalTime
  156. self.additionalServices = additionalServices
  157. self.tasks = tasks
  158. self.spendingLimit = spendingLimit
  159. self.isWithPaymentDocument = isWithPaymentDocument
  160. self.id = id
  161. self.isProceedBeforeArrival = isProceedBeforeArrival
  162. # class OrderTicketOLD(object):
  163. # def __init__(self, id, status, created_date, status_changed_date, priority, title=None, note=None, due_date=None, task_list=None, created_by=None, type=None, assigned_to=None):
  164. # self.id = id
  165. # self.status = status
  166. # self.created_date = created_date
  167. # self.status_changed_date = status_changed_date
  168. # self.priority = priority
  169. # self.title = title
  170. # self.note = note
  171. # self.due_date = due_date
  172. # self.task_list = task_list
  173. # self.created_by = created_by
  174. # self.type = type
  175. # self.assigned_to = assigned_to
  176. # def __str__(self):
  177. # return "{0}, {1}, {2}, {3}, {4}, {5}, {6},\n{7}, {8}, {9}, {10}, {11}\n".format(self.id, self.status, self.created_date, self.status_changed_date, self.priority, self.title, self.note, self.due_date, self.task_list, self.created_by, self.type, self.assigned_to)
  178. #Функция преобразования json в объект класса Order
  179. def jsonToOrder(jsonstring) -> Order:
  180. jsonObj = json.loads(jsonstring)
  181. print(jsonObj)
  182. u = Order(**jsonObj)
  183. return u
  184. #Функция преобразования json в объект класса OrderTicket
  185. def jsonToOrderTicket(jsonstring) -> OrderTicket:
  186. print (jsonstring)
  187. jsonObj = json.loads(jsonstring)
  188. print(jsonObj)
  189. u = OrderTicket(**jsonObj)
  190. return u
  191. #Функция обращения к апи и получение токена
  192. def requestGetToken(login, password, url) -> str:
  193. login = "11111111111"
  194. password = "Sharix!"
  195. #url = "https://user.sharix-app.org/auth/token/login/"
  196. data = {
  197. "password":password,
  198. "phone_number":login
  199. }
  200. response = requests.post(url,json=data)
  201. result = response.json()
  202. return result['auth_token']
  203. #Функция обращения к апи и получение токена
  204. def createUser(username, phone, password, url) -> str:
  205. data = {
  206. "username": username,
  207. "phone_number": phone,
  208. "password": password
  209. }
  210. response = requests.post(url, json=data)
  211. result = response.json()
  212. return result
  213. #Функция обращения к апи и получение списка пользователей
  214. def requestGetListUser():
  215. auth_token=requestGetToken()
  216. print(auth_token)
  217. headers = {'Authorization': f'Token {auth_token}'}
  218. url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  219. response = requests.get(url, headers=headers)
  220. print(response)
  221. print(response.json())
  222. def requestGetList(auth_token, url):
  223. headers = {'Authorization': f'Token {auth_token}'}
  224. response = requests.get(url, headers=headers)
  225. #response = requests.get(config.API_URL+"/metaservicesynced/api/orders/", headers=headers)
  226. ordertext=response.json()
  227. startlist = []
  228. if not ordertext:
  229. print("Empty array")
  230. else:
  231. for i in ordertext:
  232. print (i, "PROCESSING")
  233. try:
  234. orderticket = OrderTicket(**i)
  235. print(i, "ORDER ", orderticket)
  236. startlist.append(orderticket)
  237. except Exception as ex:
  238. print(ex)
  239. print (startlist)
  240. return startlist