1

Edit: I realize this isn't a nested function, instead I'm calling a function within a function!

I am pulling data from 5 different endpoints (endpoint_a through endpoint_e) and each endpoint has its own function get_endpoint_ such as below:

def get_endpoint_a() -> pd.dataFrame:
    """
    Return data from API endpoint.

    Includes all data. It is initially returned as a JSON and
    then transformed into a DataFrame.
    """
    url = configs.ENDPOINT_A
    querystring = configs.a_querystring
    try:
        response = requests.request("GET", url, headers=header, params=querystring)
    except requests.exceptions.RequestException as e:
        raise sys.exit(e)
    if response.status_code != 200:
        raise Exception(f"Response from API: {response.status_code}")

    response = response.json()
    return _get_all_data(response)

And I have a sub function _get_all_data to append the results along with a whilr loop for pagination as defined below:

def _get_all_data(response):
    """
    Return the results of the get request.

    Loops over api response and appends results to list in addition to appending the
    results of a while loop for pagination.
    """
    results = []
    for x in response["results"]:
        results.append(x)
    while response["paging"]["next"]["link"] is not None:
        url = response["paging"]["next"]["link"]
        response = requests.request("GET", url, headers=header, params=querystring)

        response = response.json()

        for x in response["results"]:
            results.append(x)
    return json_normalize(results)

When I run the functions in Jupyter it works, however when I run the code through a linter I'm getting an undefined variable "querystring" error message referring to the params=querystring in the sub function. Given that each endpoint requires a different querystring, I'm unable to define it globally and using nonlocal returns unable to parse or nonlocal variable must be bound in outer function scope.

Could anyone help me or point me in the right direction? Thank you in advance

2
  • I don't see any nested functions. Commented Oct 26, 2022 at 16:01
  • Oops, you're right! I meant I'm calling a function within a function! Commented Oct 26, 2022 at 16:25

1 Answer 1

1

As pointed out in a comment, this isnt a nested function. However, to fix your problem you just need to add querystring as an argument to your function and pass it in each time:

def _get_all_data(response, querystring):
"""
Return the results of the get request.

Loops over api response and appends results to list in addition to appending the
results of a while loop for pagination.
"""
results = []
for x in response["results"]:
    results.append(x)
while response["paging"]["next"]["link"] is not None:
    url = response["paging"]["next"]["link"]
    response = requests.request("GET", url, headers=header, params=querystring)

    response = response.json()

    for x in response["results"]:
        results.append(x)
return json_normalize(results)


def get_endpoint_a() -> pd.dataFrame:
    """
    Return data from API endpoint.

    Includes all data. It is initially returned as a JSON and
    then transformed into a DataFrame.
    """
    url = configs.ENDPOINT_A
    querystring = configs.a_querystring
    try:
        response = requests.request("GET", url, headers=header, params=querystring)
    except requests.exceptions.RequestException as e:
        raise sys.exit(e)
    if response.status_code != 200:
        raise Exception(f"Response from API: {response.status_code}")

    response = response.json()
    return _get_all_data(response, querystring)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so very much! This helped! You're right, it's not nested! I meant to say I'm calling a function within a function! I appreciate your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.