Example #1
0
    def list(self, request):
        self.validate_query_params(request.query_params)

        try:
            lat, lon = latitude_longitude_from_location(
                request.query_params.get("location"))
        except AttributeError:
            raise ValidationError("Invalid location.")

        weather_client = OpenWeatherMapClient(lat, lon)

        period = request.query_params.get("period", None)

        if period:
            periods = period.split(",")
            if len(periods) > 1:
                period_start, period_end = periods
            else:
                period_start, period_end = periods[0], periods[0]

            period_start = int(period_start)
            period_end = int(period_end)

            weather_client.filter(period_start, period_end)

        return Response(
            dict(
                temp=dict(
                    avg=weather_client.average_temp(),
                    max=weather_client.max_temp(),
                    min=weather_client.min_temp(),
                    median=weather_client.median_temp(),
                ),
                humidity=dict(
                    avg=weather_client.average_humidity(),
                    max=weather_client.max_humidity(),
                    min=weather_client.min_humidity(),
                    median=weather_client.median_humidity(),
                ),
            ),
            status=200,
        )
Example #2
0
 def test_min_humidity(self, mock_response) -> None:
     mock_response.return_value = MOCK_RESPONSE.copy()
     wither = OpenWeatherMapClient(self.lat, self.lon)
     wither.filter(1598205600, 1598292000)
     self.assertEqual(wither.min_humidity(), 37)