main_api_class module
- class API
Bases:
object- api = ''
- authentication_details = {}
- base_url = ''
- connect_to_api(auth_type=AuthType.BASIC, **auth_params)
Connects to a REST API using the specified authentication mechanism.
This method creates and configures a requests.Session object based on the provided authentication type and credentials. Supported authentication methods are Basic and Bearer. If OAuth2 authentication is requested, a NotImplementedError is raised.
- Parameters:
auth_type (str) – The authentication mechanism to use. Expected values include: - AuthType.BASIC.value for Basic authentication - AuthType.BEARER.value for Bearer token authentication - AuthType.OAUTH2.value for OAuth2 authentication (not implemented)
**auth_params – Additional keyword arguments needed for authentication. For example: - For Basic authentication: ‘username’ (str) and ‘password’ (str) - For Bearer authentication: ‘token’ (str)
- Returns:
- A session object configured with the authentication credentials.
If an unrecognized auth_type is provided, the session is returned without extra authentication.
- Return type:
requests.Session
- Raises:
NotImplementedError – If OAuth2 authentication (AuthType.OAUTH2.value) is specified.
- connection_name = ''
- connection_type = 'api'
- construct_endpoint(endpoint)
Constructs the endpoint URL for the given endpoint.
- Parameters:
endpoint (str) – The endpoint to construct the URL for.
- Returns:
The constructed endpoint URL.
- Return type:
str
- create_df(resp)
Creates a pandas DataFrame from a given dictionary or list of dictionaries.
- Parameters:
resp (dict or list) – A dictionary or list of dictionaries containing the data to be converted into a DataFrame.
- Returns:
A pandas DataFrame created from the input dictionary or list of dictionaries.
- Return type:
pd.DataFrame
This function takes in a dictionary or list of dictionaries and converts it into a pandas DataFrame. It iterates over the keys of the dictionary and checks if the corresponding value is a dictionary or a list. If it is a dictionary, it uses pd.json_normalize to flatten the nested dictionary into a DataFrame. If it is a list, it uses pd.json_normalize to flatten each element of the list into a DataFrame. The resulting DataFrames are then concatenated using pd.concat. Finally, the function converts all object columns to strings using df.astype(str) and returns the resulting DataFrame.
- database = 'public'
- fetch_data(api_session, table, main_response_key=None)
Fetches data from the API using the provided session object.
Args: - api_session (requests.Session): The session object with authentication configured. - table (str): The table endpoint to fetch data from. - main_response_key (str, optional): The key to use for nested structure. Defaults to None.
Returns: - dict: The JSON response containing the fetched data.
- Return type:
dict
- get_metadata(*args, **kwargs)
Returns the metadata for the API.
- Returns:
A dictionary containing the metadata for the API.
- Return type:
dict
- get_table_schema(api_session, table_name)
Retrieve the schema details of a table.
- Parameters:
table_name (str) – The name of the table to get the schema for.
- Returns:
A dictionary containing the schema details of the table.
- Return type:
dict
- install_missing_libraries()
Checks if there are any missing libraries required for the function to run. If there are missing libraries, it calls the install_libraries function to install them.
- Return type:
bool- Returns:
True if the libraries are installed successfully, False otherwise.
- limit = {'limit': 100}
- logo = ''
- pagination = {}
- return_final_df(responses)
Generates a pandas DataFrame by concatenating the normalized JSON responses.
- Parameters:
responses (list) – A list of JSON responses to be normalized and concatenated.
- Returns:
The concatenated DataFrame containing the normalized JSON responses.
- Return type:
pd.DataFrame
- schema = 'public'
- supported_auths = [AuthType.BEARER, AuthType.OAUTH2, AuthType.BASIC]
- tables = {}
- test_connection(api_session)
Tests the connection to the API by attempting to retrieve the schema for each table.
- Parameters:
api_session (requests.Session) – The session object with authentication configured.
- Returns:
True if the connection is successful and the schema can be retrieved for at least one table, False otherwise.
- Return type:
bool
- class OAuth2Client(client_id, client_secret, auth_url, token_url, redirect_uri, scope)
Bases:
object- get_access_token(authorization_code)
Exchange the authorization code for an access token.
- get_authorization_url()
Generate the authorization URL to redirect the user to the provider’s OAuth2 login.