Moo
Moo

Reputation: 137

How can I pass a list of different query params to a request body respectively?

I have a list of query params that I want to pass to a function as a parameter. This function calls an api, so each item in this list of params get passed respectively for each call and fetches the results associated with this param till it hits the end of this list.

Here is the params list(each item in this list has a different range of dates that has a different set of results):

REQUEST_PARAMS = [
    {
        "period": "range",
        "date": "2020-10-08,2020-10-31",
        "format": "JSON",
        "module": "API",
        "method": "**",
        "flat": "1",
        "filter_limit": "-1",
    },
    {
        "period": "range",
        "date": "2020-11-01,2020-11-30",
        "format": "JSON",
        "module": "API",
        "method": "**",
        "flat": "1",
        "filter_limit": "-1",
    },
    {
        "period": "range",
        "date": "2020-12-01,2020-12-31",
        "format": "JSON",
        "module": "API",
        "method": "**",
        "flat": "1",
        "filter_limit": "-1",
    }
]

Here is the function that makes the call to the targeted api:

 def get_the_results(max_retries=4):
        """
        Fetch the response object from the api
        :param max_retries: The max number of retries in case of error.
        :return: list of dictionaries for the results
        """
        return fetch_and_validate_response(URL, REQUEST_PARAMS, max_retries)


def fetch_and_validate_response(url, params, max_retries=0):
"""
Fetch a response object from provided URL.
:param url: The url for the request
:param params: The parameters for the request
:param max_retries: The max number of retries in case of error.
:return: json object if successful, None if retries due to network error continues to fail
"""
try:
    response = get(url=url, params=params, verify=False)
    response.raise_for_status()
    response_decoded = response.json()
    # Raise a RequestException if the API returns a result with an error.
    try:
        if response_decoded.get('result') == 'error':
            log.info(f"Error in response: {response_decoded.get('result')} - {response_decoded.get('message')}")
            raise RequestException
    except AttributeError:
        log.info("No error detected in response object.")

    return response_decoded
except HTTPError as e:
    log.exception(f'Network Error: HTTP error {response.status_code} occurred while getting query: {url}: {e}')
except ConnectionError as e:
    log.exception(f'Network Error: A connection error occurred while getting query: {url} with {params}: {e}')
except Timeout as e:
    log.exception(f'Network Error: The request timed out for query: {url} with {params}: {e}')
except RequestException as e:
    log.exception(f'Network Error: An error occurred while getting {url}. Please double-check the url: {e}')

if max_retries <= 0:
    return None

time.sleep(5)

return fetch_and_validate_response(url, params, max_retries - 1)

I want to print the results that fetched from this call in this function:

def construct_results_dict():
    data = get_the_results()
    for item in data:
        print(item)

your help is so much appreciated - Thanks :)

Upvotes: 0

Views: 593

Answers (1)

Ram
Ram

Reputation: 4779

You could do something like this.

Iterate over the REQUEST_PARAMS list and call fetch_and_validate_response() with each parameter and then store the return values of fetch_and_validate_response() in a list and return that list.

def get_the_results(max_retries=4):
        """
        Fetch the response object from the api
        :param max_retries: The max number of retries in case of error.
        :return: list of dictionaries for the results
        """
        # results list to store the results returned by the below function
        results = []
        for param in REQUEST_PARAMS:
            results.append(fetch_and_validate_response(URL, param, max_retries)) 
        
        # Finally return the results list
        return results

And Now, the results list will be returned to data in the below function.

def construct_results_dict():
    data = get_the_results()
    for item in data:
        print(item)

Upvotes: 1

Related Questions