One of the most common weather API requirements is to obtain multiple pieces of information for a location. We can do this with multiple API requests for each data type, but a batch request is a little nicer because we can query multiple endpoints with a single request. For more details on the API side of a batch request, check out the weather API Batch Request page.
Batch requests in the AerisWeather Python library are somewhat similar to the custom endpoint request, which we learned about in the last section. To make a batch request, we define an Endpoint object for each request we want to make, then send a list containing all of the endpoint objects to the aerisweather.batch_request() method.
Just like all other requests, the batch_request method returns a list of responses. In this case however, the list will contain the responses from each endpoint, in the order that they were requested.
Note: Because some endpoints can return more than one response per request, we need to check each response type in the list to determine how to handle it. Check the example below to see how we handled it.
Along with the list of endpoints, we can also pass global properties to the batch_request() method. These properties will then be applied to each of the included endpoints, unless they override that particular property. For example, if we set up two endpoints like this:
endpoint1 = Endpoint(endpoint_type=EndpointType.OBSERVATIONS, location=RequestLocation(postal_code="54660")) endpoint2 = Endpoint(endpoint_type=EndpointType.FORECASTS)
add these to a list:
endpoint_list = [endpoint1, endpoint2]
and then pass these to the batch_request method:
response_list = awx.batch_request(endpoints=endpoints, global_location=RequestLocation(postal_code="55124"))
the result will be that endpoint1 will request observations data for 54660, while endpoint2 will request forecast for 55124.
Last modified: July 14, 2020