def check_status_code(cls, status_code, payload): if status_code < 400: return if status_code == 400 or status_code not in [401, 404, 502]: raise exceptions.APIRequestError(payload) elif status_code == 401: raise exceptions.UnauthorizedError('Invalid API Key provided') elif status_code == 404: raise exceptions.NotFoundError('Unable to find the resource') else: raise exceptions.BadGatewayError( 'Unable to contact the upstream server')
def find_closest_weather(weathers_list, unixtime): """ Extracts from the provided list of Weather objects the item which is closest in time to the provided UNIXtime. :param weathers_list: a list of *Weather* objects :type weathers_list: list :param unixtime: a UNIX time :type unixtime: int :returns: the *Weather* object which is closest in time or ``None`` if the list is empty """ if not weathers_list: return None if not is_in_coverage(unixtime, weathers_list): raise exceptions.NotFoundError('Error: the specified time is ' + \ 'not included in the weather coverage range') closest_weather = weathers_list[0] time_distance = abs(closest_weather.reference_time() - unixtime) for weather in weathers_list: if abs(weather.reference_time() - unixtime) < time_distance: time_distance = abs(weather.reference_time() - unixtime) closest_weather = weather return closest_weather