|
| 1 | +import requests |
| 2 | +import json |
| 3 | + |
| 4 | + |
| 5 | +class QuickbaseJSONClient: |
| 6 | + def __init__(self, realm, auth, **kwargs): |
| 7 | + self.realm = realm |
| 8 | + self.auth = auth |
| 9 | + self.headers = { |
| 10 | + 'QB-Realm-Hostname': f'{self.realm}.quickbase.com', |
| 11 | + 'User-Agent': '{User-Agent}', |
| 12 | + 'Authorization': f'QB-USER-TOKEN {auth}' |
| 13 | + } |
| 14 | + self.debug = True if kwargs.get('debug') else False |
| 15 | + |
| 16 | + """ |
| 17 | + Records API |
| 18 | + """ |
| 19 | + |
| 20 | + def query_records(self, table: str, select: list, where: str, **kwargs): |
| 21 | + """ |
| 22 | + Queries for record data. |
| 23 | + https://developer.quickbase.com/operation/runQuery |
| 24 | + :param table: quickbase table |
| 25 | + :param select: list, list of fids to query |
| 26 | + :param where: Quickbase query language string. i.e. {3.EX.100} |
| 27 | + :param kwargs: optional request parameters. |
| 28 | + :return: json data of records {data: ..., fields: ...} |
| 29 | + """ |
| 30 | + |
| 31 | + body = {'from': table, 'select': select, 'where': where} |
| 32 | + return requests.post('https://api.quickbase.com/v1/records/query', headers=self.headers, json=body).json() |
| 33 | + |
| 34 | + def insert_update_records(self, table: str, data: list): |
| 35 | + """ |
| 36 | + Inerts or updates records in a given table. |
| 37 | + https://developer.quickbase.com/operation/upsert |
| 38 | + :param table: table to add records to |
| 39 | + :param data: list of dict of data, [{"6": {"value": 'example'}}] (do not include 3, record id to insert) |
| 40 | + :return: record id of created/updated records |
| 41 | + """ |
| 42 | + |
| 43 | + body = {'to': table, 'data': data} |
| 44 | + return requests.post('https://api.quickbase.com/v1/records', headers=self.headers, json=body).json() |
| 45 | + |
| 46 | + def delete_records(self, table: str, where: str): |
| 47 | + """ |
| 48 | + Deletes records in a table based on a query. |
| 49 | + https://developer.quickbase.com/operation/deleteRecords |
| 50 | + :param table: The unique identifier of the table. |
| 51 | + :param where: The filter to delete records. To delete all records specify a filter that will include all records. |
| 52 | + :return: dict, numberDeleted is number of records deleted. |
| 53 | + """ |
| 54 | + |
| 55 | + headers = self.headers |
| 56 | + body = {'to': table, 'where': where} |
| 57 | + return requests.delete('https://api.quickbase.com/v1/records', headers=headers, json=body).json() |
| 58 | + |
| 59 | + """ |
| 60 | + Table API |
| 61 | + """ |
| 62 | + |
| 63 | + def create_table(self, app_id: str, name: str, **kwargs): |
| 64 | + """ |
| 65 | + Creates a table in an application. |
| 66 | + https://developer.quickbase.com/operation/createTable |
| 67 | + :param app_id: The unique identifier of an app |
| 68 | + :param name: name of table |
| 69 | + :param kwargs: optional args |
| 70 | + :return: |
| 71 | + """ |
| 72 | + |
| 73 | + headers = self.headers |
| 74 | + params = {'appId': f'{app_id}'} |
| 75 | + body = {'name': name}.update(kwargs) |
| 76 | + return requests.post('https://api.quickbase.com/v1/tables', params=params, headers=headers, json=body).json() |
| 77 | + |
| 78 | + def get_tables(self, app_id: str): |
| 79 | + """ |
| 80 | + Gets all tables in an application |
| 81 | + https://developer.quickbase.com/operation/getAppTables |
| 82 | + :param app_id: The unique identifier of an app. |
| 83 | + :return: dict of all tables in application. |
| 84 | + """ |
| 85 | + |
| 86 | + headers = self.headers |
| 87 | + params = {'appId': f'{app_id}'} |
| 88 | + return requests.post('https://api.quickbase.com/v1/tables', params=params, headers=headers, json=body).json() |
0 commit comments