Skip to content

Commit dc953d7

Browse files
committed
add new features to response
1 parent a594af0 commit dc953d7

File tree

3 files changed

+123
-7
lines changed

3 files changed

+123
-7
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = quickbase-json-api-client
3-
version = 0.0.9
3+
version = 0.0.10
44
author = Robert Carroll
55
author_email = rob@shenandoah.capital
66
description = Python wrapper for quickbase JSON API.

src/quickbase_json/client.py

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import requests
44

5+
from quickbase_json.helpers import FileUpload
6+
from quickbase_json.qb_insert_update_response import QBInsertResponse
57
from quickbase_json.qb_response import QBResponse
68

79

810
class QuickbaseJSONClient:
9-
def __init__(self, realm, auth, **kwargs):
11+
def __init__(self, realm, auth, debug=False, **kwargs):
1012
"""
1113
Creates a client object.
1214
:param realm: quickbase realm
@@ -20,7 +22,7 @@ def __init__(self, realm, auth, **kwargs):
2022
'User-Agent': '{User-Agent}',
2123
'Authorization': f'QB-USER-TOKEN {auth}'
2224
}
23-
self.debug = True if kwargs.get('debug') else False
25+
self.debug = debug
2426

2527
"""
2628
Records API
@@ -44,13 +46,21 @@ def query_records(self, table: str, select: list, where: str, **kwargs):
4446

4547
# add optional args
4648
body.update(kwargs)
47-
r = requests.post('https://api.quickbase.com/v1/records/query', headers=self.headers, json=body).json()
49+
r = requests.post('https://api.quickbase.com/v1/records/query', headers=self.headers, json=body)
50+
51+
if self.debug:
52+
print(f'QJAC : query_records : response ---> {r}')
53+
print(f'QJAC : query_records : response.json() ---> \n{r.json()}')
4854

4955
# create response object
5056
res = QBResponse('records')
5157

5258
# update response object with JSON data from request
53-
res.update(r)
59+
res.update(r.json())
60+
61+
if self.debug:
62+
print(f'QJAC : query_records : QBResponse ---> \n{res}')
63+
5464
return res
5565

5666
def insert_update_records(self, table: str, data: list):
@@ -63,7 +73,17 @@ def insert_update_records(self, table: str, data: list):
6373
"""
6474

6575
body = {'to': table, 'data': data}
66-
return requests.post('https://api.quickbase.com/v1/records', headers=self.headers, json=body).json()
76+
77+
if self.debug:
78+
print(f'QJAC : insert_update : body ---> \n{body}')
79+
80+
r = requests.post('https://api.quickbase.com/v1/records', headers=self.headers, json=body)
81+
82+
res = QBInsertResponse()
83+
84+
85+
86+
return
6787

6888
def delete_records(self, table: str, where: str):
6989
"""
@@ -76,8 +96,39 @@ def delete_records(self, table: str, where: str):
7696

7797
headers = self.headers
7898
body = {'to': table, 'where': where}
99+
100+
if self.debug:
101+
print(f'QJAC : delete_records : body ---> \n{body}')
102+
79103
return requests.delete('https://api.quickbase.com/v1/records', headers=headers, json=body).json()
80104

105+
"""
106+
Easy Upload
107+
"""
108+
109+
def easy_upload(self, table: str, rid: int, fid: str, file_path: str):
110+
"""
111+
Allows for easy upload of files to quickbase
112+
:param table: id of table to upload to
113+
:param rid: record id to update
114+
:param fid: fid (where to put the file)
115+
:param file_path: path to local file to upload
116+
:return: request response
117+
"""
118+
file = FileUpload(file_path)
119+
data = [
120+
{
121+
3: rid,
122+
fid: file
123+
}
124+
]
125+
126+
if self.debug:
127+
print(f'QJAC : easy_upload : file ---> \n{file}')
128+
print(f'QJAC : easy_upload : data ---> \n{data}')
129+
130+
return self.insert_update_records(table, data=data)
131+
81132
"""
82133
Table API
83134
"""
@@ -95,6 +146,10 @@ def create_table(self, app_id: str, name: str, **kwargs):
95146
headers = self.headers
96147
params = {'appId': f'{app_id}'}
97148
body = {'name': name}.update(kwargs)
149+
150+
if self.debug:
151+
print(f'QJAC : create_table : body ---> \n{body}')
152+
98153
return requests.post('https://api.quickbase.com/v1/tables', params=params, headers=headers, json=body).json()
99154

100155
def get_tables(self, app_id: str):
@@ -108,6 +163,10 @@ def get_tables(self, app_id: str):
108163
headers = self.headers
109164
params = {'appId': f'{app_id}'}
110165
body = None
166+
167+
if self.debug:
168+
print(f'QJAC : get_tables : params ---> \n{params}')
169+
111170
return requests.post('https://api.quickbase.com/v1/tables', params=params, headers=headers, json=body).json()
112171

113172
"""
@@ -130,5 +189,11 @@ def get_fields(self, table_id: str, **kwargs):
130189
"""
131190
Misc.
132191
"""
192+
133193
def __str__(self):
134-
return f'Quickbase Client: {self.realm}'
194+
"""
195+
Shows a string representation of a QuickbaseJSONClient.
196+
:return: client's realm and last 5 of auth
197+
"""
198+
auth_str = ''.join(['*' for _ in range(len(list(self.auth)))] + list(self.auth)[-5:])
199+
return f'Quickbase Client\t--->\t{self.realm} : {auth_str} (DEBUG: {self.debug})'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import datetime
2+
from functools import wraps
3+
4+
5+
class Bcolors:
6+
HEADER = '\033[95m'
7+
OKBLUE = '\033[94m'
8+
OKCYAN = '\033[96m'
9+
OKGREEN = '\033[92m'
10+
WARNING = '\033[93m'
11+
FAIL = '\033[91m'
12+
ENDC = '\033[0m'
13+
BOLD = '\033[1m'
14+
UNDERLINE = '\033[4m'
15+
16+
17+
def operation(method):
18+
@wraps(method)
19+
def wrapper(self, *args, **kwargs):
20+
self.operations.append(method.__name__)
21+
return method(self, *args, **kwargs)
22+
23+
return wrapper
24+
25+
26+
class QBInsertResponse(dict):
27+
28+
def __init__(self, **kwargs):
29+
self.ok = False
30+
self.status = None
31+
self.processed = 0
32+
self.created_rids = []
33+
self.updated_rids = []
34+
super().__init__()
35+
36+
def info(self):
37+
"""
38+
Prints information about the response.
39+
:prt: Set to False to only grab the return as a string.
40+
"""
41+
print(f'Response:\nOK:\t--->\t{self.ok}\nChanged:\t--->\t{len(self.updated_rids)}\nInserted:\t--->\t{len(self.created_rids)}')
42+
43+
def from_response(self, response):
44+
self.ok = response.ok
45+
self.status = response.status
46+
47+
if self.ok:
48+
self.update(response.json())
49+
self.created_rids = self.get('metadata').get('createdRecordIds')
50+
self.updated_rids = self.get('metadata').get('updatedRecordIds')
51+
self.processed = self.get('metadata').get('totalNumberOfRecordsProcessed')

0 commit comments

Comments
 (0)