-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbasic_api.py
150 lines (125 loc) · 5.26 KB
/
basic_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
# =====================================
# Python Module for Basic APIs
# Version 2023.03.19 - Brian Seltzer
# =====================================
#
# 2022.03.19 - initial release
#
# =====================================
from datetime import datetime
import time
import requests
import getpass
import urllib3
import base64
import os
from os.path import expanduser
# ignore unsigned certificates ============================================================================
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
__all__ = ['bapiauth',
'bapi',
'usecsToDate',
'usecsToDateTime',
'dateToUsecs']
# initialization ==========================================================================================
BASIC_API = {
'base_url': '',
'headers': {'accept': 'application/json', 'content-type': 'application/json'}
}
CONFIGDIR = expanduser("~") + '/.basic-api'
SCRIPTDIR = os.path.dirname(os.path.realpath(__file__))
# authentication function =================================================================================
def bapiauth(endpoint, username, password=None):
password = __getpassword(endpoint=endpoint, username=username, password=password)
authString = '%s:%s' % (username, password)
encodedPassword = base64.b64encode(authString.encode('utf-8')).decode('utf-8')
BASIC_API['headers']['Authorization'] = "Basic %s" % encodedPassword
BASIC_API['base_url'] = 'https://%s' % endpoint
# api call function =======================================================================================
def bapi(method, uri, data=None):
url = BASIC_API['base_url'] + uri
responsejson = None
try:
if method == 'get':
response = requests.get(url, headers=BASIC_API['headers'], verify=False)
if method == 'post':
response = requests.post(url, headers=BASIC_API['headers'], json=data, verify=False)
if method == 'put':
response = requests.put(url, headers=BASIC_API['headers'], json=data, verify=False)
if method == 'delete':
response = requests.delete(url, headers=BASIC_API['headers'], json=data, verify=False)
try:
responsejson = response.json()
except ValueError:
pass
if response.status_code != 200:
print('Error (%s) %s' % (response.status_code, response.reason))
return None
if responsejson is not None:
return responsejson
except requests.exceptions.RequestException as e:
print(e)
return None
# date functions ==========================================================================================
def usecsToDate(uedate, fmt='%Y-%m-%d %H:%M:%S'):
"""Convert Unix Epoc Microseconds to Date String"""
uedate = int(uedate) / 1000000
return datetime.fromtimestamp(uedate).strftime(fmt)
def usecsToDateTime(uedate):
"""Convert Unix Epoc Microseconds to Date String"""
uedate = int(uedate) / 1000000
return datetime.fromtimestamp(uedate)
def dateToUsecs(dt=datetime.now()):
"""Convert Date String to Unix Epoc Microseconds"""
if isinstance(dt, str):
dt = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
return int(time.mktime(dt.timetuple())) * 1000000
def timeAgo(timedelta, timeunit):
"""Convert Date Difference to Unix Epoc Microseconds"""
nowsecs = int(time.mktime(datetime.now().timetuple())) * 1000000
secs = {'seconds': 1, 'sec': 1, 'secs': 1,
'minutes': 60, 'min': 60, 'mins': 60,
'hours': 3600, 'hour': 3600,
'days': 86400, 'day': 86400,
'weeks': 604800, 'week': 604800,
'months': 2628000, 'month': 2628000,
'years': 31536000, 'year': 31536000}
age = int(timedelta) * int(secs[timeunit.lower()]) * 1000000
return nowsecs - age
# password storage function ===============================================================================
def __getpassword(endpoint, username, password):
"""get/set stored password"""
if ':' in endpoint:
endpoint = endpoint.replace(':', '--')
pwpath = os.path.join(CONFIGDIR, endpoint + '-' + username)
if password is not None:
pwd = password
try:
pwdfile = open(pwpath, 'w')
opwd = base64.b64encode(pwd.encode('utf-8')).decode('utf-8')
pwdfile.write(opwd)
pwdfile.close()
return pwd
except Exception:
print('error storing password')
return pwd
try:
pwdfile = open(pwpath, 'r')
opwd = pwdfile.read()
pwd = base64.b64decode(opwd.encode('utf-8')).decode('utf-8')
pwdfile.close()
return pwd
except Exception:
pwd = getpass.getpass("Enter your password: ")
try:
pwdfile = open(pwpath, 'w')
opwd = base64.b64encode(pwd.encode('utf-8')).decode('utf-8')
pwdfile.write(opwd)
pwdfile.close()
return pwd
except Exception:
print('error storing password')
return pwd