weather: Pass API query parameters as params dict in requests.get().

This commit is contained in:
novokrest 2018-05-11 08:01:59 +03:00 committed by Tim Abbott
parent 927fcaa521
commit 8654ce53db
5 changed files with 26 additions and 8 deletions

View file

@ -1,6 +1,10 @@
{ {
"request":{ "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":{ "response":{
"cod":"404", "cod":"404",

View file

@ -1,6 +1,10 @@
{ {
"request":{ "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":{ "response":{
"coord":{ "coord":{

View file

@ -1,6 +1,10 @@
{ {
"request":{ "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":{ "response":{
"coord":{ "coord":{

View file

@ -1,6 +1,10 @@
{ {
"request":{ "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":{ "response":{
"coord":{ "coord":{

View file

@ -5,6 +5,8 @@ import logging
from typing import Any, Dict from typing import Any, Dict
api_url = 'http://api.openweathermap.org/data/2.5/weather'
class WeatherHandler(object): class WeatherHandler(object):
def initialize(self, bot_handler: Any) -> None: def initialize(self, bot_handler: Any) -> None:
self.api_key = bot_handler.get_config_info('weather')['key'] self.api_key = bot_handler.get_config_info('weather')['key']
@ -12,8 +14,8 @@ class WeatherHandler(object):
self.check_api_key(bot_handler) self.check_api_key(bot_handler)
def check_api_key(self, bot_handler: Any) -> None: def check_api_key(self, bot_handler: Any) -> None:
url = 'http://api.openweathermap.org/data/2.5/weather?q=nyc&APPID=' + self.api_key api_params = dict(q='nyc', APPID=self.api_key)
test_response = requests.get(url) test_response = requests.get(api_url, params=api_params)
try: try:
test_response_data = test_response.json() test_response_data = test_response.json()
if test_response_data['cod'] == 401: if test_response_data['cod'] == 401:
@ -40,8 +42,8 @@ class WeatherHandler(object):
if (message['content'] == 'help') or (message['content'] == ''): if (message['content'] == 'help') or (message['content'] == ''):
response = help_content response = help_content
else: else:
url = 'http://api.openweathermap.org/data/2.5/weather?q=' + message['content'] + '&APPID=' api_params = dict(q=message['content'], APPID=self.api_key)
r = requests.get(url + self.api_key) r = requests.get(api_url, params=api_params)
if r.json()['cod'] == "404": if r.json()['cod'] == "404":
response = "Sorry, city not found" response = "Sorry, city not found"
else: else: