promotions/utils/clients/wallet_client.py
2026-07-07 16:42:34 +03:30

129 lines
4.8 KiB
Python

import time
import requests
from django.conf import settings
from django.core.cache import cache
from httpx import Request
from utils.clients.gooyal_wallet_client import AuthenticatedClient
from utils.clients.gooyal_wallet_client.models import Transaction, Account, ApplicationDeposit, ApplicationWithdraw, \
PaginatedAccountList
from utils.clients.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,
wallet_api_application_user_wallets_list)
def login_as_client_credentials():
access_token = cache.get('promotions_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:
print('already in cache')
return access_token
print('not in cache')
data = {
"grant_type": 'client_credentials',
"scope": settings.OAUTH2_SCOPES,
}
auth = (settings.OAUTH2_CLIENT_ID, settings.OAUTH2_CLIENT_SECRET)
response = requests.post(f'{settings.OAUTH2_PROVIDER_PUBLIC_URL}/token/',
data=data,
auth=auth)
print(response.content)
auth_data = response.json()
if 'access_token' in auth_data:
auth_data['created_at'] = time.time()
cache.set('promotions_access_token', auth_data, timeout=auth_data.get('expires_in') - 5)
return auth_data
def log_request(request: Request):
print(request.headers['authorization'])
print(
f"Request event hook: {request.method} {request.url} {request.method} {request.headers} - Waiting for response")
def log_response(response):
request = response.request
print(f"Response event hook: {request.method} {request.url} - Status {response.status_code}")
def get_client():
access_token = login_as_client_credentials()['access_token']
client = AuthenticatedClient(base_url=settings.WALLET_BASE_PUBLIC_URL, token=access_token,
httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}},
)
return client
def deposit_to_user_wallet_submit(payer_wallet, data):
class Tmp():
def to_dict(self):
return data
deposit_data = Tmp()
client = get_client()
response: ApplicationDeposit = wallet_api_application_deposit_create.sync(payer_wallet=payer_wallet,
client=client,
body=deposit_data)
print(response)
return response
def deposit_to_user_wallet_verify(payer_wallet, uuid):
client = get_client()
response: ApplicationDeposit = wallet_api_application_deposit_verify_retrieve.sync(payer_wallet=payer_wallet,
uuid=uuid, client=client)
print(response)
return response
def withdraw_from_user_wallet_submit(payee_wallet, data):
class Tmp():
def to_dict(self):
return data
withdraw_data = Tmp()
client = get_client()
response: ApplicationWithdraw = wallet_api_application_withdraw_create.sync(payee_wallet=payee_wallet,
client=client, body=withdraw_data)
print(response)
return response
def withdraw_from_user_wallet_verify(payee_wallet, uuid):
client = get_client()
response: ApplicationWithdraw = wallet_api_application_withdraw_verify_retrieve.sync(payee_wallet=payee_wallet,
uuid=uuid, client=client)
print(response)
return response
def get_user_wallets(owner, wallets):
client = get_client()
# by pass several wallet query params: [','.join([str(i) for i in wallets])]
response: PaginatedAccountList = wallet_api_application_user_wallets_list.sync(owner=owner, wallet=[
','.join([str(i) for i in wallets])], client=client)
print(response)
balances = dict(map(lambda result: (str(result.wallet), result.balance), response.results))
return balances
sample_data = {
"uuid": "3fa85f64-5717-4562-b3fc-2c963f66afb1",
"payee_id": "5d66d02e-6087-4b0a-bb0c-291d5c11755a",
"payee_type": 1,
"amount": 2000,
"details": "string",
}