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