示例#1
0
文件: views.py 项目: crintus/wither
    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)
        return Response(
            weather_client.get(),
            status=200,
        )
示例#2
0
 def test_get(self) -> None:
     wither = OpenWeatherMapClient(self.lat, self.lon)
     with mock.patch.object(requests, "get") as get:
         wither.get()
     get.assert_called_with(
         f"{wither.base_url}",
         params=dict(
             appid=OpenWeatherMapClient.api_key,
             lat=self.lat,
             lon=self.lon,
             dt=None,
             exclude="current,minutely,hourly",
             units="metric",
         ),
     )
示例#3
0
文件: views.py 项目: crintus/wither
    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,
        )
示例#4
0
 def test_median_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.median_humidity(), 39.0)
示例#5
0
 def test_min_temp(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_temp(), 20.28)
示例#6
0
 def test_filter_from_start(self, mock_response) -> None:
     mock_response.return_value = MOCK_RESPONSE.copy()
     wither = OpenWeatherMapClient(self.lat, self.lon)
     self.assertDictEqual(wither.data, MOCK_RESPONSE)
     wither.filter(1598292000)
     self.assertEqual(len(wither.data["daily"]), 4)