126 lines
4 KiB
Python
126 lines
4 KiB
Python
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_2,
|
|
wallet_api_application_deposit_verify_retrieve_2,
|
|
wallet_api_application_withdraw_create,
|
|
wallet_api_application_withdraw_verify_retrieve)
|
|
from gooyal_wallet_client.types import Response
|
|
|
|
from django.conf import Settings
|
|
|
|
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:
|
|
print('already in cache')
|
|
return access_token
|
|
|
|
print('not 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_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('ads_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(category_uuid, data):
|
|
class Tmp():
|
|
def to_dict(self):
|
|
return data
|
|
|
|
deposit_data = Tmp()
|
|
client = get_client()
|
|
response: ApplicationDeposit = wallet_api_application_deposit_create_2.sync(category_uuid= category_uuid,
|
|
client=client,
|
|
body=deposit_data)
|
|
print(response)
|
|
|
|
return response
|
|
|
|
|
|
def deposit_to_user_wallet_verify(category_uuid, uuid):
|
|
client = get_client()
|
|
response: ApplicationDeposit = wallet_api_application_deposit_verify_retrieve_2.sync(category_uuid=category_uuid, uuid=uuid, client=client)
|
|
print(response)
|
|
|
|
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)
|
|
print(response)
|
|
|
|
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)
|
|
print(response)
|
|
|
|
return response
|
|
|
|
|
|
sample_data = {
|
|
"uuid": "3fa85f64-5717-4562-b3fc-2c963f66afb1",
|
|
"payee_id": "5d66d02e-6087-4b0a-bb0c-291d5c11755a",
|
|
"payee_type": 1,
|
|
"amount": 2000,
|
|
"details": "string",
|
|
}
|