def check_connection(self, logger, config) -> Tuple[bool, any]: try: authenticator = TokenAuthenticator(config["api_token"]) url = f"{ZenloopStream.url_base}surveys" session = requests.get(url, headers=authenticator.get_auth_header()) session.raise_for_status() return True, None except Exception as error: return False, f"Unable to connect to Zenloop API with the provided credentials - {error}"
def test_token_authenticator(): """ Should match passed in token, no matter how many times token is retrieved. """ token_auth = TokenAuthenticator(token="test-token") header1 = token_auth.get_auth_header() header2 = token_auth.get_auth_header() prepared_request = requests.PreparedRequest() prepared_request.headers = {} token_auth(prepared_request) assert {"Authorization": "Bearer test-token"} == prepared_request.headers assert {"Authorization": "Bearer test-token"} == header1 assert {"Authorization": "Bearer test-token"} == header2
def get_first_row(auth: TokenAuthenticator, base_id: str, table: str) -> Dict[str, Any]: url = f"https://api.airtable.com/v0/{base_id}/{table}?pageSize=1" try: response = requests.get(url, headers=auth.get_auth_header()) response.raise_for_status() except requests.exceptions.HTTPError as e: if e.response.status_code == 401: raise Exception("Invalid API key") elif e.response.status_code == 404: raise Exception(f"Table '{table}' not found") else: raise Exception(f"Error getting first row from table {table}: {e}") json_response = response.json() record = json_response.get("records", [])[0] return record