implemented apihandler class and functions
This commit is contained in:
parent
75d856d53a
commit
8d5d777de6
@ -5,3 +5,50 @@ __author__ = "Florian Kellermann, Linus Eickhoff"
|
||||
__date__ = "16.03.2022"
|
||||
__version__ = "0.0.1"
|
||||
__license__ = "None"
|
||||
|
||||
#side-dependencies: none
|
||||
#Work in Progress
|
||||
|
||||
import sys
|
||||
import os
|
||||
import requests as r
|
||||
|
||||
class API_Handler:
|
||||
#class for communicating with webservice to get data from database
|
||||
def __init__(self, db_adress):
|
||||
self.db_adress = db_adress
|
||||
return
|
||||
|
||||
def get_auth_token(self, email, password):
|
||||
payload = {'email': email, 'password': password}
|
||||
with r.Session() as s:
|
||||
p = s.post(self.db_adress + "/user/login", json=payload)
|
||||
if p.status_code == 200:
|
||||
self.token = p.json()["data"]['token']
|
||||
return p.json()["data"]['token']
|
||||
else:
|
||||
#print server response
|
||||
print(p.text)
|
||||
return None
|
||||
|
||||
def get_user_keywords(self, token, user_id):
|
||||
with r.Session() as s:
|
||||
headers = {'Authorization': 'Bearer ' + token + ":" + user_id}
|
||||
p = s.get(self.db_adress + "/api/keywords", headers=headers)
|
||||
keywords_json = p.json()["data"]
|
||||
keywords = []
|
||||
for keyword in keywords_json:
|
||||
keywords.append(keyword["keyword"])
|
||||
return keywords
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print("This is a module for the telegram bot. It is not intended to be run directly.")
|
||||
handler = API_Handler("https://aktienbot.flokaiser.com/api")
|
||||
try:
|
||||
handler.get_auth_token("bot@example.com", "bot")
|
||||
except None:
|
||||
print("Could not connect to server.")
|
||||
|
||||
sys.exit(1)
|
Loading…
Reference in New Issue
Block a user