def places(self, location: RequestLocation = None, action: RequestAction = None, filter_: [RequestFilter] = None, sort: RequestSort = None, params: Dict[ParameterType, str] = None, query: Dict[RequestQuery, str] = None): """ Performs an API request to get places data for a specified location. Params: - location: Optional - RequestLocation - the location for which the request is processed - action: Optional - RequestAction - the API request action option - filter_: Optional - [RequestFilter] - a list of API request filters - sort: Optional - RequestSort - the API request sort option - params: Optional - Dict[ParameterType, str] - a list of API request parameters - query: Optional - Dict[RequestQuery, str] - a list of API request quesries Returns: - a list of PlacesResponse objects if successful - an empty list if there is no data """ endpoint = Endpoint(endpoint_type=EndpointType.PLACES, location=location, action=action, filter_=filter_, sort=sort, params=params, query=query) return self.request(endpoint=endpoint)
def test_api_response(self): """ Test the ObservationSummary code against a live response from the API """ try: awx = AerisWeather(app_id=app_id, client_id=client_id, client_secret=client_secret) endpoint = Endpoint( endpoint_type=EndpointType.OBSERVATIONS_SUMMARY, location=None, action=RequestAction.OBSERVATIONS_SUMMARY.CLOSEST, filter_=[RequestFilter.OBSERVATIONS_SUMMARY.ALL_STATIONS], sort=None, params={ParameterType.OBSERVATIONS_SUMMARY.P: "54601"}, query=None) obs_sum_list = awx.request(endpoint=endpoint) for obs_sum in obs_sum_list: assert obs_sum.id is not None loc = obs_sum.loc assert loc is not None assert type(loc) is AerisLocation assert obs_sum.loc.lat > 43 place = obs_sum.place assert place is not None # assert place.name == "la crosse" assert place.state == "wi" periods = obs_sum.periods assert periods is not None temp = periods[0].temp assert type(temp) is ObservationsSummaryTemp assert temp.avgF > -10 profile = obs_sum.profile assert profile is not None assert profile.elevFT > 600 except URLError as url_err: print("URL Error: " + url_err.reason) raise url_err except AerisError as aeris_err: print("AerisError: " + str(aeris_err)) raise aeris_err except Exception as ex: print(ex.args) raise ex
def test_api_response(self): """ Test the Observation code against a live response from the API """ try: awx = AerisWeather(app_id=app_id, client_id=client_id, client_secret=client_secret) endpoint = Endpoint(endpoint_type=EndpointType.OBSERVATIONS, location=None, action=RequestAction.OBSERVATIONS.CLOSEST, filter_=[RequestFilter.OBSERVATIONS.MESONET], sort=None, params={ParameterType.OBSERVATIONS.P: "54601"}, query={RequestQuery.OBSERVATIONS.ID: "KLSE"}) obs_list = awx.request(endpoint=endpoint) assert len(obs_list) > 0 for obs in obs_list: assert obs.id is not None loc = obs.loc assert loc is not None assert type(loc) is AerisLocation assert obs.loc.lat > 43 place = obs.place assert place is not None # assert place.name == "la crosse" assert place.state == "wi" profile = obs.profile assert profile is not None assert profile.elevFT > 600 relative_to = obs.relativeTo assert relative_to.long < -91 assert obs.obTimestamp.__class__ is int except URLError as url_err: print("URL Error: " + url_err.reason) raise url_err except AerisError as aeris_err: print("AerisError: " + str(aeris_err)) raise aeris_err except Exception as ex: print(ex.args) raise ex
def test_api_response(self): """ Test the Places code against a live response from the API """ try: awx = AerisWeather(app_id=app_id, client_id=client_id, client_secret=client_secret) endpoint = Endpoint(endpoint_type=EndpointType.PLACES, location=None, action=RequestAction.PLACES.CLOSEST, filter_=[RequestFilter.PLACES.AIRPORT], sort=None, params={ParameterType.PLACES.P: "54601"}) places_list = awx.request(endpoint=endpoint) for places in places_list: assert type(places) is PlacesResponse loc = places.loc assert type(loc) is AerisLocation assert type(loc.long) is not None place = places.place assert type(place) is AerisPlacePlaces assert place.name is not None assert place.state == "WI" assert place.stateFull == "Wisconsin" assert place.country == "US" assert place.countryFull == "United States" assert place.region == "usnc" assert place.regionFull == "North Central" assert place.continent == "na" assert place.continentFull == "North America" profile = places.profile assert type(profile) is AerisProfilePlaces assert profile.elevM is not None except URLError as url_err: print("URL Error: " + url_err.reason) raise url_err except AerisError as aeris_err: print("AerisError: " + str(aeris_err)) raise aeris_err except Exception as ex: print(ex.args) raise ex
def custom_endpoint(self, location: RequestLocation = None, action: RequestAction = None, filter_: [RequestFilter] = None, sort: RequestSort = None, params: Dict[ParameterType, str] = None, query: Dict[RequestQuery, str] = None): """ Performs an API request to get custom endpoint data for a specified location. When calling custom_endpoint, in addition to setting the EndpointType of the Endpoint object to CUSTOM, the EndpointType.custom value must be set to the string value of the endpoint you are requesting. See the examples section to see hwo this is done. Params: - location: Optional - RequestLocation - the location for which the request is processed - action: Optional - RequestAction - the API request action option - filter_: Optional - [RequestFilter] - a list of API request filters - sort: Optional - RequestSort - the API request sort option - params: Optional - Dict[ParameterType, str] - a list of API request parameters - query: Optional - Dict[RequestQuery, str] - a list of API request quesries Returns: - a list of CustomResponse objects if successful - an empty list if there is no data Examples: # You can also use the custom endpoint type to request data from a known valid endpoint, for cases # where new API data fields have not yet been added to an endpoint's response class. EndpointType.custom = "forecasts" f_list = awx.request(endpoint=Endpoint(endpoint_type=EndpointType.CUSTOM, location=RequestLocation(postal_code="54660"))) forecast = f_list[0] period = forecast.periods[0] # type: ForecastPeriod # Valid endpoint, not in our Endpoint Enum - run this to test a beta or pre-release endpoint EndpointType.custom = "stormreports" endpt = Endpoint(EndpointType.CUSTOM, location=RequestLocation(postal_code="54660")) resp_list = awx.request(endpt) response = resp_list[0] """ endpoint = Endpoint(endpoint_type=EndpointType.CUSTOM, location=location, action=action, filter_=filter_, sort=sort, params=params, query=query) return self.request(endpoint=endpoint)
def test_api_response(self): """ Test the Alerts code against a live response from the API """ try: awx = AerisWeather(app_id=app_id, client_id=client_id, client_secret=client_secret) endpoint = Endpoint(endpoint_type=EndpointType.ALERTS, location=RequestLocation(postal_code="55124"), action=None, filter_=[RequestFilter.ALERTS.ALL], sort=None, params=None, query=None) alerts_list = awx.request(endpoint=endpoint) for alert in alerts_list: # type: AlertsResponse assert alert.place is not None timestamps = alert.timestamps assert type(timestamps) == AlertTimestamps assert timestamps.issued is not None includes = alert.includes assert type(includes) is AlertIncludes assert includes.wxzones is not None assert alert.active is True except URLError as url_err: print("URL Error: " + url_err.reason) raise url_err except AerisError as aeris_err: print("AerisError: " + str(aeris_err)) raise aeris_err except Exception as ex: print(ex.args) raise ex
def test_forecasts_method(self): """ Test the AerisWeather.forecasts method """ try: awx = AerisWeather(app_id=app_id, client_id=client_id, client_secret=client_secret) endpoint = Endpoint(endpoint_type=EndpointType.FORECASTS, location=RequestLocation(postal_code="54601"), action=None, filter_=[RequestFilter.FORECASTS.DAY_NIGHT], sort=None, params={ParameterType.FORECASTS.LIMIT: "1"}, query=None) forecast_list = awx.request(endpoint=endpoint) for forecast in forecast_list: # type: ForecastsResponse assert forecast is not None assert forecast.loc.long < 0 assert forecast.interval is not None period = forecast.periods[0] assert type(period) is ForecastPeriod assert period.weather is not None assert period.validTime is not None except URLError as url_err: print("URL Error: " + url_err.reason) raise url_err except AerisError as aeris_err: print("AerisError: " + str(aeris_err)) raise aeris_err except Exception as ex: print(ex.args) raise ex
def test_api_response(self): """ Test against a live response from the API """ try: awx = AerisWeather(app_id=app_id, client_id=client_id, client_secret=client_secret) # Valid endpoint, not in our Endpoint Enum - run this to test a beta or pre-release endpoint EndpointType.custom = "stormreports" endpt = Endpoint(EndpointType.CUSTOM, location=RequestLocation(postal_code="54660")) try: resp_list = awx.request(endpt) response = resp_list[0] assert type(response) is CustomResponse except AerisError as ae_ex: logging.basicConfig(level=logging.ERROR) logger = logging.getLogger(' stormreports endpoint test ') logger.error(str(ae_ex)) except Exception as ex: logging.basicConfig(level=logging.ERROR) logger = logging.getLogger(' stormreports endpoint test ') logger.error(str(ex)) # You can also use the custom endpoint type to request data from a known valid endpoint, for cases # where new API data fields have not yet been added to an endpoint's response class. EndpointType.custom = "forecasts" f_list = awx.request( endpoint=Endpoint(endpoint_type=EndpointType.CUSTOM, location=RequestLocation( postal_code="54660"))) forecast = f_list[0] assert type(forecast) is CustomResponse assert len(forecast.periods) > 0 period = forecast.periods[0] # type: ForecastPeriod assert period.weather is not None # Another example, with lots of nested lists, etc. # rivers/gauges EndpointType.custom = "rivers/gauges" rg_list = awx.request( endpoint=Endpoint(endpoint_type=EndpointType.CUSTOM, location=RequestLocation( postal_code="57101"))) rg = rg_list[0] assert type(rg) is CustomResponse profile = rg.profile # type: AerisProfileRiversGauges crests = profile.crests # type: RiversCrests recent = crests.recent # type: [RiversCrestsRecent] assert len(recent) > 0 assert recent[0].heightFT is not None # Unknown and invalid endpoint EndpointType.custom = "bogus/endpoint" invalid_list = awx.request( endpoint=Endpoint(endpoint_type=EndpointType.CUSTOM, location=RequestLocation( postal_code="57101"))) # the results of this call will be a thrown AerisError exception. except URLError as url_err: print("URL Error: " + url_err.reason) raise url_err except AerisError as aeris_err: assert aeris_err.code == "invalid_request" print("AerisError: " + "Level: " + aeris_err.level.value + " - " + str(aeris_err)) # raise aeris_err except Exception as ex: print(ex.args) raise ex
def test_api_batch_response(self): """ Test against a batch response from the API """ try: awx = AerisWeather(app_id=app_id, client_id=client_id, client_secret=client_secret) endpoint = Endpoint(endpoint_type=EndpointType.OBSERVATIONS, location=None, action=None, filter_=None, sort=None, params={ParameterType.OBSERVATIONS.LIMIT: "3"}) endpoint2 = Endpoint(endpoint_type=EndpointType.FORECASTS, params={ParameterType.FORECASTS.LIMIT: "3"}) endpoint3 = Endpoint(endpoint_type=EndpointType.OBSERVATIONS_SUMMARY) endpoints = [endpoint, endpoint2, endpoint3] response_list = awx.batch_request(endpoints=endpoints, global_location=RequestLocation(postal_code="54660")) for resp in response_list: if type(resp) is ObservationsResponse: # Observations obs = resp assert type(obs) is ObservationsResponse assert obs.id is not None loc = obs.loc assert loc is not None assert type(loc) is AerisLocation assert obs.loc.lat > 43 place = obs.place assert place is not None # assert place.name == "la crosse" assert place.state == "wi" profile = obs.profile assert profile is not None assert profile.elevFT > 600 relative_to = obs.relativeTo assert relative_to.long < -80 assert obs.obTimestamp.__class__ is int elif type(resp) is ForecastsResponse: # Forecasts forecast = resp assert type(forecast) is ForecastsResponse loc = forecast.loc assert loc is not None assert type(loc) is AerisLocation assert forecast.loc.lat > 43 assert forecast.interval is not None period = forecast.periods[0] assert type(period) is ForecastPeriod assert period.weather is not None assert period.validTime is not None elif type(resp) is ObservationsSummaryResponse: # ObservationsSummary obs_sum = resp assert type(obs_sum) is ObservationsSummaryResponse assert obs_sum.id is not None loc = obs_sum.loc assert loc is not None assert type(loc) is AerisLocation assert obs_sum.loc.lat > 43 place = obs_sum.place assert place is not None # assert place.name == "la crosse" assert place.state == "wi" periods = obs_sum.periods assert periods is not None temp = periods[0].temp assert type(temp) is ObservationsSummaryTemp assert temp.avgF > -10 profile = obs_sum.profile assert profile is not None assert profile.elevFT > 600 except URLError as url_err: print("URL Error: " + url_err.reason) raise url_err except AerisError as aeris_err: print("AerisError: " + str(aeris_err)) raise aeris_err except Exception as ex: print(ex.args) raise ex