Last active
February 1, 2024 22:57
-
-
Save shawnbot/6f059e5eff55b9e3d5258e019cbb422a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from airtable import Airtable | |
class AirtableMeta: | |
""" | |
The AirtableMeta class provides access to the Airtable base schema API: | |
https://airtable.com/developers/web/api/get-base-schema | |
It's totally a hack: The base schema API ("/v0/meta/bases/{baseId}/tables") | |
doesn't live under the Airtable class's base_url ("/v0/{baseId}"), so we | |
create an Airtable instance, modify its base_url, then hijack the "mangled" | |
private __request() method (_Airtable__request) to make requests. | |
""" | |
def __init__(self, base_id: str, api_key: str): | |
self.airtable = Airtable(base_id, api_key, dict_class=dict) | |
self.base_id = base_id | |
self.airtable.base_url = ( | |
f"https://api.airtable.com/v0/meta/bases/{base_id}" | |
) | |
@property | |
def tables(self): | |
return self.request("tables").get("tables", []) | |
@property | |
def tables_by_name(self): | |
return dict((table["name"], table) for table in self.tables) | |
def table(self, name): | |
return self.tables_by_name.get(name, None) | |
def request(self, uri, method="GET", **kwargs): | |
return self.airtable._Airtable__request(method, uri, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment