jsonAndRequest.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import json
  2. import requests
  3. #Тестовый класс User и Address (ВАЖНО!!!) поля классов должны называться также как и поля json объекта
  4. #{
  5. #
  6. # "ticket": null,
  7. # "state": "",
  8. # "id_metaservice": null,
  9. # "is_global": false,
  10. # "is_visible": false,
  11. # "service": null,
  12. # "service_type": null,
  13. #
  14. # "time_placed": null,
  15. # "time_start": null,
  16. # "time_start_predicted": null
  17. # "time_finish_predicted": null,
  18. # "time_start_real":
  19. # "time_finish_real": null,
  20. #
  21. # "order_place_start": "р-н Беговой",
  22. # "order_place_start_gps": "р-н Беговой",
  23. # "order_place_start_real": "р-н Беговой",
  24. # "order_place_start_real_gps": "р-н Беговой",
  25. # "order_place_finish_predicted": "р-н Мещанский",
  26. # "order_place_finish_predicted_gps": "р-н Мещанский",
  27. # "order_place_finish_real": "р-н Мещанский",
  28. # "order_place_finish_real_gps": "р-н Мещанский",
  29. #
  30. # "predicted_price": null,
  31. # "real_price": null,
  32. # "payment_transaction_id"
  33. # "rating_id"
  34. #
  35. # "provider": null,
  36. # "provider_screen_name": null
  37. # "receiver": null,
  38. # "receiver_screen_name": nulll
  39. # "client_id": null
  40. # "resource_id"
  41. #}
  42. class Order(object):
  43. def __init__(self, ticket, state, id_metaservice, is_global, is_visible, service, service_type, time_placed, time_start, time_start_predicted, time_finish_predicted, time_start_real, time_finish_real, order_place_start, order_place_start_gps, order_place_start_real, order_place_start_real_gps, order_place_finish_predicted, order_place_finish_predicted_gps, order_place_finish_real, order_place_finish_real_gps, predicted_price, real_price, payment_transaction_id, rating_id, provider, provider_screen_name, receiver, receiver_screen_name, client_id, resource_id):
  44. self.ticket = ticket
  45. self.state = state
  46. self.id_metaservice = id_metaservice
  47. self.is_global = is_global
  48. self.is_visible = is_visible
  49. self.service = service
  50. self.service_type = service_type
  51. self.time_placed = time_placed
  52. self.time_start = time_start
  53. self.time_start_predicted = time_start_predicted
  54. self.time_finish_predicted = time_finish_predicted
  55. self.time_start_real = time_start_real
  56. self.time_finish_real = time_finish_real
  57. self.order_place_start = order_place_start
  58. self.order_place_start_gps = order_place_start_gps
  59. self.order_place_start_real = order_place_start_real
  60. self.order_place_start_real_gps = order_place_start_real_gps
  61. self.order_place_finish_predicted = order_place_finish_predicted
  62. self.order_place_finish_predicted_gps = order_place_finish_predicted_gps
  63. self.order_place_finish_real = order_place_finish_real
  64. self.order_place_finish_real_gps = order_place_finish_real_gps
  65. self.predicted_price = predicted_price
  66. self.real_price = real_price
  67. self.payment_transaction_id = payment_transaction_id
  68. self.rating_id = rating_id
  69. self.provider = provider
  70. self.provider_screen_name = provider_screen_name
  71. self.receiver = receiver
  72. self.receiver_screen_name = receiver_screen_name
  73. self.client_id = client_id
  74. self.resource_id = resource_id
  75. #доделать до конца по аналогии с закоммиченным json
  76. def __str__(self):
  77. return "{0}, {1}, {2}, {3}, {4}, {5}, {6},\n{7}, {8}, {9}, {10}, {11}, {12},\n{13}, {14}, {15}, {16}, {17}, {18}, {19}, {20},\n{21}, {22}, {23}, {24},\n{25}, {26}, {27}, {28}, {29}, {30}".format(self.ticket, self.state, self.id_metaservice, self.is_global, self.is_visible, self.service, self.service_type, 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_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)
  78. #\n добавляете в местах с пустой строкой в шаблоне
  79. #Функция преобразования json в объект класса
  80. def jsonToClass(jsonstring) -> Order:
  81. jsonObj = json.loads(jsonstring)
  82. print(jsonObj)
  83. u = Order(**jsonObj)
  84. return u
  85. #Функция обращения к апи и получение токена
  86. def requestGetToken(login, password, url) -> str:
  87. #login = "79876543210"
  88. #password = "admin12345@"
  89. #url = "https://user.sharix-app.org/auth/token/login/"
  90. data = {
  91. "password": password,
  92. "phone_number": login
  93. }
  94. response = requests.post(url, json=data)
  95. result = response.json()
  96. return result['auth_token']
  97. #Функция обращения к апи и получение токена
  98. def createUser(username, phone, password, url) -> str:
  99. data = {
  100. "username": username,
  101. "phone_number": phone,
  102. "password": password
  103. }
  104. response = requests.post(url, json=data)
  105. result = response.json()
  106. return result
  107. #Функция обращения к апи и получение списка пользователей
  108. def requestGetListUser():
  109. auth_token=requestGetToken()
  110. print(auth_token)
  111. headers = {'Authorization': f'Token {auth_token}'}
  112. url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  113. response = requests.get(url, headers=headers)
  114. print(response)
  115. print(response.json())
  116. def requestGetList(token, url):
  117. #auth_token=requestGetToken()
  118. #print(auth_token)
  119. headers = {'Authorization': f'Token {auth_token}'}
  120. #url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  121. response = requests.get(url, headers=headers)
  122. print(response)
  123. print(response.json())