Skip to content

Commit 29d84d1

Browse files
committed
0.0.9 - user auth moved to new file, code breaking, must refactor if using user auth
1 parent 176c30d commit 29d84d1

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

src/quickbase_json/auth.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import datetime
2+
from xml.etree import ElementTree
3+
4+
import requests
5+
6+
7+
class AuthResponse:
8+
def __init__(self, error_code, error=None):
9+
if error_code == 0:
10+
self.ok = True
11+
else:
12+
self.ok = False
13+
self.error_code = error_code
14+
self.error = error
15+
16+
def __str__(self):
17+
return f'{self.error_code}: {self.error}'
18+
19+
20+
class UserToken:
21+
def __init__(self, string, hours):
22+
self.string = string
23+
self.expiration = datetime.datetime.now() + datetime.timedelta(hours=hours)
24+
25+
26+
class User:
27+
def __init__(self, realm, username):
28+
self.realm = realm
29+
self.username = username
30+
self.authenticated = False
31+
self.token = None
32+
33+
def is_authenticated(self):
34+
"""
35+
Method to check if the user is authenticated.
36+
:return:
37+
"""
38+
return self.authenticated
39+
40+
def authenticate(self, password, hours):
41+
url = f'https://{self.realm}.quickbase.com/db/main?a=API_Authenticate&username={self.username}&password={password}&hours={hours}'
42+
r = requests.post(url=url)
43+
44+
tree = ElementTree.fromstring(r.content)
45+
46+
xml_dict = {}
47+
for child in tree:
48+
xml_dict.update({child.tag: child.text})
49+
50+
error_code = int(xml_dict.get('errcode'))
51+
52+
if error_code == 0:
53+
token = xml_dict.get('ticket')
54+
self.token = token
55+
self.authenticated = True
56+
return AuthResponse(error_code=error_code)
57+
else:
58+
return AuthResponse(error_code=int(xml_dict.get("errcode")), error=xml_dict.get("errtext"))

src/quickbase_json/client.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,6 @@ def get_fields(self, table_id: str, **kwargs):
127127
params = {'tableId': f'{table_id}'}
128128
return requests.get('https://api.quickbase.com/v1/fields', params=params, headers=headers).json()
129129

130-
"""
131-
User Authentication
132-
"""
133-
def auth_user(self, username, pw, hours):
134-
url = f'https://{self.realm}.quickbase.com/db/main?a=API_Authenticate&username={username}&password={pw}&hours={hours}'
135-
r = requests.post(url=url)
136-
137-
tree = ElementTree.fromstring(r.content)
138-
139-
xml_dict = {}
140-
for child in tree:
141-
xml_dict.update({child.tag: child.text})
142-
143-
error_code = int(xml_dict.get('errcode'))
144-
if error_code == 0:
145-
return xml_dict.get('ticket')
146-
else:
147-
raise ValueError(f'{xml_dict.get("errcode")}: {xml_dict.get("errtext")}')
148-
149130
"""
150131
Misc.
151132
"""

0 commit comments

Comments
 (0)