import json import time import requests OAUTH_CLIENT_ID = 'xrFXKf53jrxVOygbLEoqrtOlUwBP00jIQ4zVpzc6' OAUTH_CLIENT_SECRET = 'qelUdRCdEEalVdko5aHRojxsC3CaL29yjwxmc6FeWs39SeVSb6aM9mNrYQixMJNTYQK7s2wffwPW94JPPbP6jTdd9dSf1mlqYcr6hW2COuayFUk4jP33OWu4taUYP2F5' API_URI = 'https://accounts.gooyal.com' # API_URI = 'http://127.0.0.1:8000' class ApiClient(): def __init__(self, phone_number, auth_data=None): self.auth_data = {} if auth_data: self.auth_data = auth_data self.phone_number = phone_number def otp_login(self, password): phone_number = self.phone_number data = { "grant_type": "password", "username": phone_number, "password": password, "scope": 'accounts.account:request_ott accounts.account:change_password accounts.profile:inquiry accounts.account:update accounts.account:retrieve accounts.profile:retrieve accounts.profile:list introspection wallet.wallet:get_balance', "auth_fields": 'phone_number:otp' } auth = (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET) response = requests.post(f'{API_URI}/oauth2/token/', data=data, auth=auth, verify=False ) auth_data = response.json() if 'access_token' in auth_data: self.auth_data = auth_data print(auth_data) return auth_data def ott_login(self, token): phone_number = self.phone_number data = { "grant_type": "password", "username": phone_number, "password": token, "scope": 'introspection education.course:retrieve education.course:submit superapp.applications:list accounts.account:request_ott accounts.profile:list accounts.profile:retrieve accounts.account:retrieve accounts.account:update accounts.profile:inquiry accounts.account:change_password wallet.transaction:list wallet.invoice:create wallet.transaction:retrieve wallet.invoice:pay wallet.invoice:receipt wallet.deposit:submit wallet.deposit:verify wallet.withdraw:submit wallet.withdraw:verify ', "auth_fields": 'phone_number:ott' } auth = (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET) response = requests.post(f'{API_URI}/oauth2/token/', data=data, auth=auth, verify=False ) auth_data = response.json() if 'access_token' in auth_data: self.auth_data = auth_data return auth_data def login_as_client_credentials(self): access_token = self.auth_data.get('access_token') expires_in = self.auth_data.get('expires_in') created_at = self.auth_data.get('created_at') if access_token and expires_in > time.time() - created_at: return self.auth_data data = { "grant_type": 'client_credentials', "scope": 'wallet.deposit:submit wallet.deposit:verify', } auth = (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET) response = requests.post(f'{API_URI}/oauth2/token/', data=data, auth=auth) auth_data = response.json() print(auth_data) if 'access_token' in auth_data: auth_data['created_at'] = time.time() self.auth_data = auth_data return auth_data def _request(self, url, data=None, files=None, method='get', with_auth=True, encode=True): if files: header = {} else: header = { "Content-Type": "application/json", "charset": "utf-8" } if with_auth: access_token = self.auth_data.get('access_token', '') header.setdefault("Authorization", f"Bearer {access_token}", ) if data and encode: data = json.dumps(data) response = requests.request(method, url, headers=header, data=data, files=files, verify=False) try: return response.json() except: return response.content.decode() def request_otp(self): phone_number = self.phone_number path = 'users/api/request_otp/' url = f'{API_URI}/{path}' method = 'post' data = { 'phone_number': phone_number } result = self._request(url=url, data=data, method=method, with_auth=False) return result def get_application_list(self, page=None): path = 'api/applications/' if page: page = int(page) path = f'{path}?page={page}' url = f'{API_URI}/{path}' return self._request(url=url) def get_course_list(self): url = 'https://edu-srv.mindplus-dev.ir/api/courses/' return self._request(url=url) def get_application_token(self): path = 'users/api/request_ott/' data = { 'application': 'https://edu.mindplus-dev.ir' } url = f'{API_URI}/{path}' response = self._request(url, method='POST', data=data) return response and 'code' in response, response def update_account(self, first_name): path = '/users/api/account/' data = { 'first_name': first_name } url = f'{API_URI}/{path}' response = self._request(url, method='PUT', data=data) return response and 'code' in response, response def introspect_account(self, token): path = '/oauth2/introspect/' data = { 'token': token } headers = { 'Content-Type': 'application/x-www-form-urlencoded' } access_token = self.auth_data.get('access_token', '') headers.setdefault("Authorization", f"Bearer {access_token}", ) response = requests.post( url=f'{API_URI}/{path}', headers=headers, verify=False, data=data) return response.json() def get_wallet_balance(self): url = 'http://127.0.0.1:8000/wallet/api/wallet/default/balance' return self._request(url=url) def get_wallet_balance(self): url = 'http://127.0.0.1:8000/wallet/api/wallet/default/balance' return self._request(url=url) client = ApiClient('+989106853582') # print(client.login_as_client_credentials()) print(client.request_otp()) print(client.otp_login('12345')) print(client.introspect_account('V3LUQ9OryHOy6q5iWwLBRAtRBm80ex')) print(client.get_wallet_balance()) # ott = client.get_application_token()[1]['ott'] # print(client.ott_login(ott)) # print(ott)