64 lines
2 KiB
Python
64 lines
2 KiB
Python
import time
|
|
|
|
import requests
|
|
from django.conf import settings
|
|
from django.core.cache import cache
|
|
|
|
from gooyal_accounts_client import AuthenticatedClient
|
|
from gooyal_accounts_client.models import PublicUser, Application
|
|
from gooyal_accounts_client.api.users import users_api_users_retrieve
|
|
from gooyal_accounts_client.api.oauth2 import oauth2_apis_applications_retrieve
|
|
from gooyal_accounts_client.types import Response
|
|
|
|
|
|
def login_as_client_credentials():
|
|
access_token = cache.get('wallet_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)
|
|
|
|
auth_data = response.json()
|
|
if 'access_token' in auth_data:
|
|
auth_data['created_at'] = time.time()
|
|
cache.set('wallet_access_token', auth_data, timeout=auth_data.get('expires_in')-5)
|
|
|
|
return auth_data
|
|
|
|
def get_client():
|
|
access_token = login_as_client_credentials()['access_token']
|
|
client = AuthenticatedClient(base_url=settings.BASE_OAUTH2_PROVIDER_PUBLIC_URL, token=access_token)
|
|
return client
|
|
|
|
|
|
def get_user_details(uuid):
|
|
client = get_client()
|
|
data: PublicUser = users_api_users_retrieve.sync(client=client, uuid=uuid)
|
|
print(data)
|
|
# or if you need more info (e.g. status_code)
|
|
# response: Response[MyDataModel] = get_my_data_model.sync_detailed(client=client)
|
|
|
|
return data
|
|
|
|
def get_application_details(uuid):
|
|
client = get_client()
|
|
data: Application = oauth2_apis_applications_retrieve.sync(client=client, id=uuid)
|
|
print(data)
|
|
|
|
return data
|