import json import requests import config import ast #Функция обращения к апи и получение токена def requestGetToken(url) -> str: login = "11111111111" password = "Sharix!" #url = "https://user.sharix-app.org/auth/token/login/" data = { "password":password, "phone_number":login } response = requests.post(url,json=data) result = response.json() return result['auth_token'] #Функция обращения к апи и получение токена def msg_to_text(jsonstring): jsonObj = json.loads(jsonstring) u = jsonObj return u def createUser(username, phone, password, url) -> str: data = { "username": username, "phone_number": phone, "password": password } response = requests.post(url, json=data) result = response.json() return result #Функция обращения к апи и получение списка пользователей def requestGetListUser(token): headers = {'Authorization': f'Token {token}'} url = 'http://testopen.sharix-app.org/api/v1/platform/sharix-users/' response = requests.get(url, headers=headers) return response class Ticket(object): def __init__(self, id, name, group): self.id = id self.name = name self.group = group def __str__(self): return "{0}, {1}, {2},\n".format(self.id, self.name, self.group) def requestGetList(auth_token, url): headers = {'Authorization': f'Token {auth_token}'} response = requests.get(url, headers=headers) #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers) tickettext=response.json() startlist = [] if not tickettext: print("Empty array") else: for i in tickettext: print (i, "PROCESSING") try: ticket = Ticket(**i) print(i, "Ticket ", ticket) startlist.append(ticket) except Exception as ex: print(ex) print (startlist) return startlist class Ticket_user(object): def __init__(self, id, status, title, ticket_type, created_at, updated_at, due_date, priority, created_by, assigned_to): self.id = id self.status = status self.title = title self.ticket_type = ticket_type self.created_at = created_at self.updated_at = updated_at self.due_date = due_date self.priority = priority self.created_by = created_by self.assigned_to = assigned_to def __str__(self): return "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}\n".format( self.id, self.status, self.title, self.ticket_type, self.created_at, self.updated_at, self.due_date, self.priority, self.created_by, self.assigned_to ) class User(object): def __init__(self, group_name, last_login, is_superuser, username, is_staff, is_active, date_joined, middle_name, phone_number, email, avatar, groups, user_permissions, full_name="", last_name="", first_name="", ): self.full_name = full_name self.group_name = group_name self.last_login = last_login self.is_superuser = is_superuser self.username = username self.is_staff = is_staff self.is_active = is_active self.date_joined = date_joined self.last_name = last_name self.first_name = first_name self.middle_name = middle_name self.phone_number = phone_number self.email = email self.avatar = avatar self.groups = groups self.user_permissions = user_permissions def __str__(self): return ', '.join([ str(self.full_name), str(self.group_name), str(self.last_login), str(self.is_superuser), str(self.username), str(self.is_staff), str(self.is_active), str(self.date_joined), str(self.last_name), str(self.first_name), str(self.middle_name), str(self.phone_number), str(self.email), str(self.avatar), str(self.groups), str(self.user_permissions) ]) + '\n' def requestGetUser(auth_token, url): headers = {'Authorization': f'Token {auth_token}'} response = requests.get(url, headers=headers) #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers) usertext=response.json() print(usertext) user = None if not usertext: print("Empty user") None else: try: user = User(**usertext) except Exception as ex: print(ex) print (user) return user def requestGetTicket_user(auth_token, url): headers = {'Authorization': f'Token {auth_token}'} response = requests.get(url, headers=headers) #response = requests.get(config.API_URL+"/dbsynce/api/orders/", headers=headers) tickettext=response.json() startlist = [] if not tickettext: print("Empty array") else: for i in tickettext: try: ticket = Ticket_user(**i) startlist.append(ticket) except Exception as ex: print(ex) print (startlist) return startlist def requestPatchTicketUser(token,url, data): headers = {'Authorization': f'Token {token}'} response = requests.patch(url, json=data, headers=headers) return response def change_groups(auth_token, url,group): headers = { 'Content-Type': 'application/json', 'Authorization': f'Token {auth_token}' } response = requests.get(url, headers=headers) user_data = response.json() current_groups = user_data.get('groups', []) updated_groups = list(set([group]+current_groups)) data = {'groups': updated_groups} print(data) response = requests.put(url, json=data, headers=headers) print(url) print(user_data) return response def change_status_company(auth_token, url,id_metaservice): headers = { 'Content-Type': 'application/json', 'Authorization': f'Token {auth_token}' } response = requests.get(url, headers=headers) user_data = response.json() print(user_data) current_metaservice = user_data.get('id_metaservice', []) repsonse = requests.put(config.API_URL+f"dbsynce/api/company/{id_metaservice}", json=data, headers=headers) return response