Batch Requests Example

Code:


    from aerisweather.aerisweather import AerisWeather
    from aerisweather.requests.ParameterType import ParameterType
    from aerisweather.requests.RequestLocation import RequestLocation
    from aerisweather.requests.RequestAction import RequestAction
    from aerisweather.requests.RequestFilter import RequestFilter
    from keys import client_id, client_secret, app_id

    # add some imports
    from aerisweather.responses.ForecastsResponse import ForecastsResponse
    from aerisweather.responses.ObservationsResponse import ObservationsResponse
    from aerisweather.responses.ObservationsSummaryResponse import ObservationsSummaryResponse
    
    # get our aerisweather instance 
    awx = AerisWeather(app_id=app_id,
                       client_id=client_id,
                       client_secret=client_secret)

    # define the first endpoint for our batch request
    endpoint = Endpoint(endpoint_type=EndpointType.OBSERVATIONS,
                        action=RequestAction.OBSERVATIONS.CLOSEST,
                        params={ParameterType.OBSERVATIONS.P: "54601"})

    # define the second endpoint of our batch request
    endpoint2 = Endpoint(endpoint_type=EndpointType.FORECASTS,
                         params={ParameterType.FORECASTS.LIMIT: "1"})endpoint our location too

    # and then the third
    endpoint3 = Endpoint(endpoint_type=EndpointType.OBSERVATIONS_SUMMARY)

    # add all three endpoints to a list
    endpoints = [endpoint, endpoint2, endpoint3]

    # pass the list of endpoints to the batch_request method, along with some global properties that will apply to all of 
    # the endpoints (unless they override it)

    response_list = awx.batch_request(endpoints=endpoints,
                                      global_location=RequestLocation(postal_code="54660"))

    for resp in response_list:  
        if type(resp) is ObservationsResponse:
            # Observations
            print ("Observations Data")

            obs = resp
            loc = obs.loc
            print("loc.latitude = " + str(loc.lat))

            place = obs.place
            print("place.state = " + place.state)
            print("")

        elif type(resp) is ForecastsResponse:
            # Forecasts
            print("Forecasts Data")

            forecast = resp
            print("forecast.loc.lat = " + str(forecast.loc.lat))
            print("forecast.interval = " + forecast.interval)

            period = forecast.periods[0]
            print("period.weather = " + period.weather)
            print("")

        elif type(resp) is ObservationsSummaryResponse:
            # ObservationsSummary
            print("Observations Summary Data")

            obs_sum = resp
            place = obs_sum.place
            print("place.state = " + place.state)

            periods = obs_sum.periods
            temp = periods[0].temp
            print("temp.avgF = " + str(temp.avgF))
            print("")

Expected output:

Observations Data
loc.latitude = 43.883333333333
place.state = wi

Forecasts Data
forecast.loc.lat = 43.979
forecast.interval = day
period.weather = Mostly Cloudy

Observations Summary Data
place.state = wi
temp.avgF = 59.2

 

*The actual values may vary

Last modified: July 30, 2020