import time import requests from django.conf import settings from django.core.cache import cache from django.db import models from httpx import Request from gooyal_wallet_client import AuthenticatedClient from gooyal_wallet_client.models import MyWallet, UserTransaction, ApplicationDeposit, ApplicationWithdraw from gooyal_wallet_client.api.wallet import (wallet_api_application_deposit_create, wallet_api_application_deposit_verify_retrieve, wallet_api_application_withdraw_create, wallet_api_application_withdraw_verify_retrieve) import logging # logging.basicConfig( # format="%(levelname)s [%(asctime)s] %(name)s - %(message)s", # datefmt="%Y-%m-%d %H:%M:%S", # level=logging.DEBUG # ) logger = logging.getLogger('ads.utils.wallet_client') class ContentTypeChoices(models.IntegerChoices): IMAGE = 1, 'image' VOICE = 2, 'voice' VIDEO = 3, 'video' def login_as_client_credentials(): access_token = cache.get('ads_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 if access_token: logger.debug('token available in cache') return access_token logger.debug('no token available in cache') data = { "grant_type": 'client_credentials', "scope": settings.SCOPES, } auth = (settings.CLIENT_ID, settings.CLIENT_SECRET) response = requests.post(f'{settings.BASE_OAUTH2_PROVIDER_PRIVATE_URL}/token/', data=data, auth=auth) logger.debug(response.content) auth_data = response.json() if 'access_token' in auth_data: auth_data['created_at'] = time.time() cache.set('ads_access_token', auth_data, timeout=auth_data.get('expires_in') - 5) return auth_data def log_request(request: Request): logger.debug(f'authorization: {request.headers["authorization"]}') logger.debug( f"Request event hook: {request.method} {request.url} {request.method} {request.headers} - Waiting for response") def log_response(response): request = response.request logger.debug(f"Response event hook: {request.method} {request.url} - Status {response.status_code}") # logger.debug(f"Response event hook: {response.read()}") def get_client(): access_token = login_as_client_credentials()['access_token'] client = AuthenticatedClient(base_url="https://wallet.gooyal.com", token=access_token, httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}}, ) return client def deposit_to_user_wallet_submit(data): class Tmp(): def to_dict(self): return data deposit_data = Tmp() client = get_client() response: ApplicationDeposit = wallet_api_application_deposit_create.sync(client=client, body=deposit_data) return response def deposit_to_user_wallet_verify(uuid): client = get_client() response: ApplicationDeposit = wallet_api_application_deposit_verify_retrieve.sync(uuid=uuid, client=client) return response def withdraw_from_user_wallet_submit(data): class Tmp(): def to_dict(self): return data withdraw_data = Tmp() client = get_client() response: ApplicationWithdraw = wallet_api_application_withdraw_create.sync(client=client, body=withdraw_data) return response def withdraw_from_user_wallet_verify(uuid): client = get_client() response: ApplicationWithdraw = wallet_api_application_withdraw_verify_retrieve.sync(uuid=uuid, client=client) return response # def deposit_to_user_wallet_submit(data): # import uuid as sys_uuid # class Tmp(): # uuid = sys_uuid.uuid4() # # # data = Tmp() # # return data # # # def deposit_to_user_wallet_verify(uuid): # import uuid as sys_uuid # class Tmp(): # uuid = sys_uuid.uuid4() # state = 5 # # data = Tmp() # # return data # # # # def withdraw_from_user_wallet_submit(data): # import uuid as sys_uuid # class Tmp(): # uuid = sys_uuid.uuid4() # # data = Tmp() # # return data # # # # def withdraw_from_user_wallet_verify(uuid): # import uuid as sys_uuid # class Tmp(): # uuid = sys_uuid.uuid4() # state = 5 # # data = Tmp() # # return data # sample_data = { "uuid": "3fa85f64-5717-4562-b3fc-2c963f66afb1", "payee_id": "5d66d02e-6087-4b0a-bb0c-291d5c11755a", "payee_type": 1, "amount": 2000, "details": "string", }