2023-09-03 20:12:18 +02:00
|
|
|
import requests
|
2023-09-03 21:29:04 +02:00
|
|
|
import json
|
2023-09-03 20:12:18 +02:00
|
|
|
|
2023-09-03 22:16:34 +02:00
|
|
|
class BaserowAPI:
|
|
|
|
def __init__(self, base_url, api_key, verbose=False):
|
|
|
|
self.base_url = base_url
|
|
|
|
self.api_key = api_key
|
|
|
|
self.verbose = verbose
|
2023-09-04 19:08:23 +02:00
|
|
|
self.headers = self.create_headers()
|
2023-09-03 22:16:34 +02:00
|
|
|
|
|
|
|
def create_headers(self):
|
|
|
|
"""Create headers for API requests."""
|
|
|
|
return {
|
|
|
|
"Authorization": f"Token {self.api_key}",
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
2023-09-05 00:57:29 +02:00
|
|
|
|
|
|
|
def request_response(self, command):
|
|
|
|
return requests.get(f"{self.base_url}{command}", headers=self.headers)
|
2023-09-03 22:16:34 +02:00
|
|
|
|
|
|
|
def handle_api_response(self, response):
|
|
|
|
"""Handle API response, check for errors and decode JSON."""
|
|
|
|
if response.status_code != 200:
|
|
|
|
print(f"Error: Received status code {response.status_code} from Baserow API.")
|
2023-09-05 00:57:29 +02:00
|
|
|
response_content = response.content.decode()
|
2023-09-04 19:08:23 +02:00
|
|
|
print("Response content: {response_content}")
|
2023-09-03 22:16:34 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
return response.json()
|
|
|
|
except requests.RequestsJSONDecodeError:
|
|
|
|
print("Error: Failed to decode the response as JSON.")
|
2023-09-05 00:57:29 +02:00
|
|
|
return None
|