1
0

jsonAndRequest.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. def requestGetTicket_user(auth_token, url):
  82. headers = {'Authorization': f'Token {auth_token}'}
  83. response = requests.get(url, headers=headers)
  84. #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers)
  85. tickettext=response.json()
  86. startlist = []
  87. if not tickettext:
  88. print("Empty array")
  89. else:
  90. for i in tickettext:
  91. try:
  92. ticket = Ticket_user(**i)
  93. startlist.append(ticket)
  94. except Exception as ex:
  95. print(ex)
  96. print (startlist)
  97. return startlist
  98. def requestPatchTicketUser(token,url, data):
  99. headers = {'Authorization': f'Token {token}'}
  100. response = requests.patch(url, json=data, headers=headers)
  101. return response
  102. def change_groups(auth_token, url,group):
  103. headers = {
  104. 'Content-Type': 'application/json',
  105. 'Authorization': f'Token {auth_token}'
  106. }
  107. response = requests.get(url, headers=headers)
  108. user_data = response.json()
  109. current_groups = user_data.get('groups', [])
  110. updated_groups = list(set([group]+current_groups))
  111. data = {'groups': updated_groups}
  112. print(data)
  113. response = requests.put(url, json=data, headers=headers)
  114. print(url)
  115. print(user_data)
  116. return response
  117. def change_status_company(auth_token, url,id_metaservice):
  118. headers = {
  119. 'Content-Type': 'application/json',
  120. 'Authorization': f'Token {auth_token}'
  121. }
  122. response = requests.get(url, headers=headers)
  123. user_data = response.json()
  124. print(user_data)
  125. current_metaservice = user_data.get('id_metaservice', [])
  126. repsonse = requests.put(config.API_URL+f"dbsynce/api/company/{id_metaservice}", json=data, headers=headers)
  127. return response