basic client
This commit is contained in:
parent
d0cdf6c38e
commit
5fc9b2cccf
1 changed files with 113 additions and 0 deletions
113
client.py
Normal file
113
client.py
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
import json
|
||||
|
||||
import requests
|
||||
|
||||
OAUTH_CLIENT_ID = '5yjU0g18LOIl1fzNgRQ9xGU0Ou4UTsimoMvvRZsG'
|
||||
OAUTH_CLIENT_SECRET = 'vyNixGW5OhOnvn9s1Z21RWdsEtsau1a45e15FAhFV3WfWy4lPdcTu6dmhR5MRKqpcKeScN7WvMGomZhzh7vJgcGWEbs7hptYriHI5ZIw3672hm4D3zUfcWYMjMUMuUOO'
|
||||
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 login(self, password):
|
||||
phone_number = self.phone_number
|
||||
|
||||
data = {
|
||||
"grant_type": "password",
|
||||
"username": phone_number,
|
||||
"password": password,
|
||||
"scope": 'read write groups',
|
||||
"auth_gateway": 'phone_number:otp'
|
||||
}
|
||||
auth = (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET)
|
||||
|
||||
response = requests.post(f'{API_URI}/oauth2/token/',
|
||||
data=data,
|
||||
auth=auth)
|
||||
|
||||
auth_data = response.json()
|
||||
if 'access_token' in auth_data:
|
||||
self.auth_data = auth_data
|
||||
return auth_data
|
||||
|
||||
def _request(self, path, 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, f'{API_URI}/{path}', headers=header, data=data, files=files)
|
||||
try:
|
||||
return response.json()
|
||||
except:
|
||||
return response.content.decode()
|
||||
|
||||
def request_otp(self):
|
||||
phone_number = self.phone_number
|
||||
|
||||
path = 'request_otp/'
|
||||
method = 'post'
|
||||
data = {
|
||||
'phone_number': phone_number
|
||||
}
|
||||
result = self._request(path=path, data=data, method=method, with_auth=False)
|
||||
|
||||
return result
|
||||
|
||||
def get_account(self):
|
||||
path = 'account'
|
||||
return self._request(path=path)
|
||||
|
||||
def update_account(self, **kwargs):
|
||||
path = 'account/'
|
||||
data = {}
|
||||
fields = (
|
||||
'username',
|
||||
"first_name",
|
||||
"last_name",
|
||||
'name',
|
||||
'iban',
|
||||
'iban_card_number',
|
||||
'iban_account_number',
|
||||
)
|
||||
for key, value in kwargs.items():
|
||||
if key in fields:
|
||||
data.setdefault(key, value)
|
||||
|
||||
avatar = kwargs.get('avatar')
|
||||
if avatar:
|
||||
files = {'avatar': avatar}
|
||||
encode = False
|
||||
else:
|
||||
files = None
|
||||
encode = True
|
||||
|
||||
return self._request(path=path, data=data, method='put', files=files, encode=encode)
|
||||
|
||||
def change_password(self, old_password, new_password, old_password_gateway='otp'):
|
||||
path = 'change_password/'
|
||||
data = {'old_password': old_password,
|
||||
'new_password': new_password,
|
||||
'old_password_gateway': old_password_gateway
|
||||
}
|
||||
|
||||
return self._request(path=path, data=data, method='put')
|
||||
|
||||
def get_user_profile(self, code):
|
||||
path = f'users/{code}/'
|
||||
return self._request(path=path)
|
||||
Loading…
Add table
Reference in a new issue