Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7107c24
commit aa7842b
Showing
2 changed files
with
134 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,90 @@ | ||
import json | ||
import requests as r | ||
import requests | ||
from requests.auth import AuthBase | ||
import os | ||
from datetime import datetime, timedelta | ||
|
||
users = {} | ||
|
||
DB_API = os.environ["DB_API_HOST"] | ||
|
||
def gethandler(url, temptoken): | ||
headers = {'Accept': 'text/plain', 'Authorization': 'Bearer ' + temptoken} | ||
request = json.loads((r.get(url, headers=headers)).text) | ||
print('-----REQUEST COMPLETED-----') | ||
i = 0 | ||
DB = [] | ||
DB2 = [] | ||
while i != len(request): | ||
DB.append(request[i]["value"]) | ||
DB2.append(request[i]["id"]) | ||
i += 1 | ||
|
||
return DB, DB2 | ||
|
||
def gentempkey(): | ||
url = f'{DB_API}/AuthManagement/Login' | ||
data = {'email': 'legalov.2017@stud.nstu.ru', 'password': '~biSXA7SSU]pwI0x&1f'} | ||
headers = {'Content-type': 'application/json', 'Accept': '*/*'} | ||
temptoken = json.loads(r.post(url, data=json.dumps(data), headers=headers).text)["token"] | ||
return(temptoken) | ||
class TokenAuth(AuthBase): | ||
"""Implements a custom authentication scheme.""" | ||
|
||
def __init__(self, auth_url, email, password): | ||
self.auth_url = auth_url | ||
self.auth_data = { "email": email, "password": password } | ||
self.auth_token = { "token": "", "expire_date": datetime.utcnow() } | ||
|
||
def __call__(self, r): | ||
"""Attach an API token to a custom auth header.""" | ||
expire_date = self.auth_token["expire_date"] | ||
if expire_date <= datetime.utcnow(): | ||
response = requests.post(self.auth_url, json=self.auth_data).json() | ||
token = response["token"] | ||
expires = response["expires"] | ||
expire_date = datetime.utcnow() + timedelta(seconds=expires - 30) | ||
self.auth_token = { "token": token, "expire_date": expire_date } | ||
r.headers["Authorization"] = f"Bearer {self.auth_token['token']}" | ||
return r | ||
|
||
|
||
|
||
|
||
class Database: | ||
|
||
def __init__(self): | ||
self.db_host = os.environ["DB_API_HOST"] | ||
auth_url = self.db_host + "/AuthManagement/Login" | ||
email = os.environ["DB_API_USER"] | ||
password = os.environ["DB_API_PASSWORD"] | ||
self.auth = TokenAuth(auth_url, email, password) | ||
|
||
def request(self, method, endpoint, *args, **kwargs): | ||
print("=========================") | ||
print(method) | ||
print(endpoint) | ||
print(args) | ||
print(kwargs) | ||
print("=========================") | ||
response = requests.request( | ||
method=method, | ||
url=self.db_host + endpoint, | ||
auth=self.auth, | ||
*args, | ||
**kwargs, | ||
) | ||
print(response.url) | ||
print(response.status_code) | ||
print(response.text) | ||
print(response.headers) | ||
print(response.content) | ||
print("=========================") | ||
return response.json() | ||
|
||
def get(self, endpoint, *args, **kwargs): | ||
return self.request(method="GET", endpoint=endpoint, *args, **kwargs) | ||
|
||
def post(self, endpoint, *args, **kwargs): | ||
return self.request(method="POST", endpoint=endpoint, *args, **kwargs) | ||
|
||
def get_topics(self, offset=0, size=100): | ||
return self.get(endpoint="/Topics/get/all", params={ "offset": offset, "size": size }) | ||
|
||
def topic_get_subotpics(self, topic_id): | ||
return self.get(f"/Topics/get/{topic_id}/subtopics") | ||
|
||
def subtopic_get_topic(self, subtopic_id): | ||
return self.get(f"/Subtopics/get/{subtopic_id}/topic") | ||
|
||
def subtopic_get_questions(self, subtopic_id): | ||
return self.get(f"/Subtopics/get/{subtopic_id}/ui_questions") | ||
|
||
def question_get_question(self, question_id): | ||
return self.get(f"/Questions/get/{question_id}") | ||
|
||
def question_get_answer(self, question_id): | ||
return self.get(f"/Questions/get/{question_id}/answer") | ||
|
||
def question_get_subtopic(self, question_id): | ||
return self.get(f"/Questions/get/{question_id}/subtopic") | ||
|
||
def parse(self, msg): | ||
return self.post(endpoint=f"/Nlu/parse", json=msg)["response_selector"]["default"]["responses"][0]["text"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters