22
33import requests
44
5+ from quickbase_json .helpers import FileUpload
6+ from quickbase_json .qb_insert_update_response import QBInsertResponse
57from quickbase_json .qb_response import QBResponse
68
79
810class 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 } )'
0 commit comments