baserow-ifto/api/baserow_api.py

34 lines
1.1 KiB
Python
Raw Normal View History

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
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()
print("Response content: {response_content}")
2023-09-03 22:16:34 +02:00
return None
try:
return response.json()
2023-09-12 16:38:44 +02:00
except:
print({response.content})
2023-09-03 22:16:34 +02:00
print("Error: Failed to decode the response as JSON.")
2023-09-05 00:57:29 +02:00
return None