jsonAndRequest.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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, name, address):
  44. self.name = name
  45. self.address = Address(**address)
  46. def __str__(self):
  47. return "{0} ,{1}".format(self.name, self.address)
  48. #Функция преобразования json в объект класса
  49. def jsonToClass(jsonstring) -> Order:
  50. jsonObj = json.loads(jsonstring)
  51. print(jsonObj)
  52. u = Order(**jsonObj)
  53. return u
  54. #Функция обращения к апи и получение токена
  55. def requestGetToken(login, password, url) -> str:
  56. #login = "79876543210"
  57. #password = "admin12345@"
  58. #url = "https://user.sharix-app.org/auth/token/login/"
  59. data = {
  60. "password": password,
  61. "phone_number": login
  62. }
  63. response = requests.post(url, json=data)
  64. result = response.json()
  65. return result['auth_token']
  66. #Функция обращения к апи и получение токена
  67. def createUser(username, phone, password, url) -> str:
  68. data = {
  69. "username": username,
  70. "phone_number": phone,
  71. "password": password
  72. }
  73. response = requests.post(url, json=data)
  74. result = response.json()
  75. return result
  76. #Функция обращения к апи и получение списка пользователей
  77. def requestGetListUser():
  78. auth_token=requestGetToken()
  79. print(auth_token)
  80. headers = {'Authorization': f'Token {auth_token}'}
  81. url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  82. response = requests.get(url, headers=headers)
  83. print(response)
  84. print(response.json())
  85. def requestGetList(token, url):
  86. #auth_token=requestGetToken()
  87. #print(auth_token)
  88. headers = {'Authorization': f'Token {auth_token}'}
  89. #url = 'https://user.sharix-app.org/platform/api/sharix-users/'
  90. response = requests.get(url, headers=headers)
  91. print(response)
  92. print(response.json())