-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_api.py
69 lines (55 loc) · 2.42 KB
/
clean_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import json
import requests
from datetime import datetime
from termcolor import colored
import logging
server = "http://192.168.10.90:8000/"
headers = { "Content-Type":"application/json", "Connection":"keep-alive" }
cookies = {}
logger = logging.getLogger()
def get_datetime(datetime_str: str):
try:
curDt = datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S')
except:
try:
curDt = datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S')
except:
return None
return curDt
def post_connect_request(json_data):
response = requests.post(f'{server}connectDWH/', data=json_data, headers=headers)
if response.ok:
global cookies
cookies = response.cookies
return True
else:
logger.error(f'Could not connect; status = {response.status_code}, info = {response.text}')
return False
def post_disconnect_request():
response = requests.post(f'{server}disconnectDWH/', data='', headers=headers)
if response.ok:
return True
else:
logger.error(f'disconnectDWH failed with status {colored(response.status_code, "light_red")} and info {colored(response.text, "light_red")}')
return False
def get_request(request_name: str, json_data = {}):
logger.debug(colored(f"GET /{request_name}/ with body {json.dumps(json_data)}", 'dark_grey'))
response = requests.get(f'{server}{request_name}/', data=json.dumps(json_data), headers=headers, cookies=cookies)
if response.ok:
# logger.debug(response.json())
return response.json()
else:
logger.error(f'{request_name} failed with status {colored(response.status_code, "light_red")} and info {colored(response.text, "light_red")}')
return None
def post_request(request_name: str, json_data):
logger.debug(colored(f"POST /{request_name}/ with body {json.dumps(json_data)}", 'dark_grey'))
response = requests.post(f'{server}{request_name}/', data=json.dumps(json_data), headers=headers, cookies=cookies)
if response.ok:
# logger.debug(response.json())
return response.json()
else:
logger.error(f'{request_name} failed with status {colored(response.status_code, "light_red")} and info {colored(response.text, "light_red")}')
return None
def connect(username: str, password: str) -> bool:
json_data = f'{{"Username": "{username}", "Password": "{password}"}}'
return post_connect_request(json_data)