From 8654ce53db3f87d3beb506daa173e74a0103733b Mon Sep 17 00:00:00 2001 From: novokrest Date: Fri, 11 May 2018 08:01:59 +0300 Subject: [PATCH] weather: Pass API query parameters as `params` dict in requests.get(). --- .../bots/weather/fixtures/test_city_not_found.json | 6 +++++- .../bots/weather/fixtures/test_city_with_country.json | 6 +++++- .../bots/weather/fixtures/test_only_city.json | 6 +++++- .../bots/weather/fixtures/test_only_country.json | 6 +++++- zulip_bots/zulip_bots/bots/weather/weather.py | 10 ++++++---- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_not_found.json b/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_not_found.json index 9965b20..c6a203c 100644 --- a/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_not_found.json +++ b/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_not_found.json @@ -1,6 +1,10 @@ { "request":{ - "api_url":"http://api.openweathermap.org/data/2.5/weather?q=fghjklasdfgh&APPID=123456" + "api_url":"http://api.openweathermap.org/data/2.5/weather", + "params": { + "q": "fghjklasdfgh", + "APPID": "123456" + } }, "response":{ "cod":"404", diff --git a/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_with_country.json b/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_with_country.json index 1bacfea..f745959 100644 --- a/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_with_country.json +++ b/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_with_country.json @@ -1,6 +1,10 @@ { "request":{ - "api_url":"http://api.openweathermap.org/data/2.5/weather?q=New Delhi, India&APPID=123456" + "api_url":"http://api.openweathermap.org/data/2.5/weather", + "params": { + "q": "New Delhi, India", + "APPID": "123456" + } }, "response":{ "coord":{ diff --git a/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_city.json b/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_city.json index 9e5c1dc..8c048e5 100644 --- a/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_city.json +++ b/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_city.json @@ -1,6 +1,10 @@ { "request":{ - "api_url":"http://api.openweathermap.org/data/2.5/weather?q=New York&APPID=123456" + "api_url":"http://api.openweathermap.org/data/2.5/weather", + "params": { + "q": "New York", + "APPID": "123456" + } }, "response":{ "coord":{ diff --git a/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_country.json b/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_country.json index 5d27801..28a0e51 100644 --- a/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_country.json +++ b/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_country.json @@ -1,6 +1,10 @@ { "request":{ - "api_url":"http://api.openweathermap.org/data/2.5/weather?q=United Kingdom&APPID=123456" + "api_url":"http://api.openweathermap.org/data/2.5/weather", + "params": { + "q": "United Kingdom", + "APPID": "123456" + } }, "response":{ "coord":{ diff --git a/zulip_bots/zulip_bots/bots/weather/weather.py b/zulip_bots/zulip_bots/bots/weather/weather.py index 9df10c1..8ad12c9 100644 --- a/zulip_bots/zulip_bots/bots/weather/weather.py +++ b/zulip_bots/zulip_bots/bots/weather/weather.py @@ -5,6 +5,8 @@ import logging from typing import Any, Dict +api_url = 'http://api.openweathermap.org/data/2.5/weather' + class WeatherHandler(object): def initialize(self, bot_handler: Any) -> None: self.api_key = bot_handler.get_config_info('weather')['key'] @@ -12,8 +14,8 @@ class WeatherHandler(object): self.check_api_key(bot_handler) def check_api_key(self, bot_handler: Any) -> None: - url = 'http://api.openweathermap.org/data/2.5/weather?q=nyc&APPID=' + self.api_key - test_response = requests.get(url) + api_params = dict(q='nyc', APPID=self.api_key) + test_response = requests.get(api_url, params=api_params) try: test_response_data = test_response.json() if test_response_data['cod'] == 401: @@ -40,8 +42,8 @@ class WeatherHandler(object): if (message['content'] == 'help') or (message['content'] == ''): response = help_content else: - url = 'http://api.openweathermap.org/data/2.5/weather?q=' + message['content'] + '&APPID=' - r = requests.get(url + self.api_key) + api_params = dict(q=message['content'], APPID=self.api_key) + r = requests.get(api_url, params=api_params) if r.json()['cod'] == "404": response = "Sorry, city not found" else: