Skip to content

api_utils

Recreation.gov Web Searching Utilities

filter_json(json, filters) #

Extension Method to Dictionaries, allows easy filtering

Parameters:

Name Type Description Default
json dict
required
filters Union[str, List[str]]
required

Returns:

Name Type Description
filtered_object object

Resulting JSON Filtered Object

Source code in camply/utils/api_utils.py
def filter_json(json: dict, filters: Union[str, List[str]]) -> object:
    """
    Extension Method to Dictionaries, allows easy filtering

    Parameters
    ----------
    json
    filters

    Returns
    -------
    filtered_object: object
        Resulting JSON Filtered Object
    """
    if not isinstance(filters, list):
        filters = [filters]
    object_layers = {0: json}
    try:
        for index, filter_layer in enumerate(filters):
            layer_index = index + 1
            object_layers[layer_index] = object_layers[index][filter_layer]
            del object_layers[index]
    except KeyError as key_error:
        error_message = (
            f"Unable to find matching JSON Filtering | {key_error} | {filters}"
        )
        logger.error(error_message)
        raise KeyError from key_error
    return object_layers[len(filters)]

generate_url(scheme, netloc, path='', params='', query='', fragment='') #

Build a URL

Parameters:

Name Type Description Default
scheme str

URL scheme specifier

required
netloc str

Network location part

required
path str

Hierarchical path

''
params str

Parameters for last path element

''
query str

Query component

''
fragment str

Fragment identifier

''

Returns:

Name Type Description
url str

Compiled URL

Source code in camply/utils/api_utils.py
def generate_url(
    scheme: str,
    netloc: str,
    path: str = "",
    params: str = "",
    query: str = "",
    fragment: str = "",
):
    """
    Build a URL

    Parameters
    ----------
    scheme: str
        URL scheme specifier
    netloc: str
        Network location part
    path: str
        Hierarchical path
    params: str
        Parameters for last path element
    query: str
        Query component
    fragment: str
        Fragment identifier

    Returns
    -------
    url: str
        Compiled URL
    """
    url_components = {
        "scheme": scheme,
        "netloc": netloc,
        "path": path,
        "params": params,
        "query": query,
        "fragment": fragment,
    }
    return parse.urlunparse(components=tuple(url_components.values()))