API amendments

This commit is contained in:
havlong
2023-01-13 12:12:49 +03:00
parent 65a78ebc1b
commit 3f8974f7e4
3 changed files with 41 additions and 27 deletions

View File

@@ -4,7 +4,11 @@ from typing import Optional, Any, Dict
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
vue_apps_url: str = 'https://apps-vue.ru'
from .models import PongModel, PostModel
oauth_url: str = 'https://sso.arbina.com/oauth'
vue_client_url: str = 'https://vue-client-api.arbina.com'
vue_interact_url: str = 'https://vue-app-interaction-api.arbina.com'
class ClientAPI:
@@ -12,12 +16,16 @@ class ClientAPI:
client_secret: str
token: Optional[Any]
session: OAuth2Session
endpoints: Dict[str, str] = {'access': '/auth', 'refresh': '/refresh', 'ping': '/ping'}
endpoints: Dict[str, str] = {'create': '/api/pages/{page_id}/posts', 'remove': '/api/posts/{post_id}',
'ping': f'{vue_interact_url}/api/app/interactions/ping', 'token': f'{oauth_url}/token'}
def build_url(self, endpoint: str) -> str:
return f'{self.vue_apps_url}{self.endpoints[endpoint]}'
if self.endpoints[endpoint].startswith('/'):
return f'{self.vue_apps_url}{self.endpoints[endpoint]}'
else:
return self.endpoints[endpoint]
def __init__(self, client_id: str, client_secret: str, vue_url: str = vue_apps_url):
def __init__(self, client_id: str, client_secret: str, vue_url: str = vue_client_url):
"""
Constructor of Vue Apps Client API
@@ -32,7 +40,7 @@ class ClientAPI:
self.client_secret = client_secret
oauth = OAuth2Session(client=BackendApplicationClient(client_id=self.client_id))
client_token = oauth.fetch_token(token_url=self.build_url('access'), client_id=self.client_id,
client_token = oauth.fetch_token(token_url=self.build_url('token'), client_id=self.client_id,
client_secret=self.client_secret)
self.token = client_token
@@ -49,7 +57,7 @@ class ClientAPI:
auto_refresh_info = {'client_id': self.client_id, 'client_secret': self.client_secret}
self.session = OAuth2Session(token=self.token, client_id=self.client_id,
auto_refresh_url=self.build_url('refresh'),
auto_refresh_url=self.build_url('token'),
auto_refresh_kwargs=auto_refresh_info, token_updater=save_token)
def acquire_token(self) -> Optional[Any]:
@@ -59,6 +67,16 @@ class ClientAPI:
"""
return self.session.access_token
def ping_server(self) -> Optional[PongModel]:
query_url = self.build_url('ping')
response = self.session.get(query_url)
if response.status_code != 200:
return None
model = response.json()
return PongModel(ping_ttl_seconds=model['ping_ttl_seconds'])
def refresh_token(self) -> bool:
"""
Forces token to be refreshed and sends requests to /ping
@@ -66,5 +84,9 @@ class ClientAPI:
"""
self.token['expires_at'] = time() - 10
self.session.token = self.token
response = self.session.get(self.build_url('ping'))
return response.status_code == 200
pong = self.ping_server()
return pong is not None
def create_post(self, post: PostModel) -> bool:
url = self.build_url('/create').format(page_id=post.page_uin)
return True