Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e73ddd

Browse files
ViRu-ThE-ViRuSroberthoenig
authored andcommitted
zulip_bots: Check API key on initialization for Weather bot.
1 parent 524804f commit 9e73ddd

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

zulip_bots/zulip_bots/bots/weather/weather.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
# See readme.md for instructions on running this code.
22
import requests
33
import json
4+
import logging
45

56
from typing import Any, Dict
67

78
class WeatherHandler(object):
89
def initialize(self, bot_handler: Any) -> None:
910
self.api_key = bot_handler.get_config_info('weather')['key']
1011
self.response_pattern = 'Weather in {}, {}:\n{:.2f} F / {:.2f} C\n{}'
12+
self.check_api_key()
13+
14+
def check_api_key(self) -> None:
15+
url = 'http://api.openweathermap.org/data/2.5/weather?q=nyc&APPID=' + self.api_key
16+
test_response = requests.get(url)
17+
try:
18+
test_response_data = test_response.json()
19+
if test_response_data['cod'] == 401:
20+
logging.error('API Key not valid. Please see doc.md to find out how to get it.')
21+
except KeyError:
22+
pass
1123

1224
def usage(self) -> str:
1325
return '''

0 commit comments

Comments
 (0)