From 8e800d545b59b6ba4e5b0e9265775130d87fe878 Mon Sep 17 00:00:00 2001 From: Viraat Chandra Date: Fri, 29 Dec 2017 18:20:35 +0530 Subject: [PATCH] zulip_bots: Check API key on initialization for Baremetrics bot. --- .../zulip_bots/bots/baremetrics/baremetrics.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py index 3627f23..a41097a 100644 --- a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py +++ b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py @@ -2,6 +2,7 @@ from typing import Any import requests +import logging class BaremetricsHandler(object): def initialize(self, bot_handler: Any) -> None: @@ -20,6 +21,19 @@ class BaremetricsHandler(object): 'List the sources', 'List the plans for the source', 'List the customers in the source', 'List the subscriptions in the source'] + self.check_api_key() + + def check_api_key(self) -> None: + url = "https://api.baremetrics.com/v1/account" + test_query_response = requests.get(url, headers=self.auth_header) + test_query_data = test_query_response.json() + + try: + if test_query_data['error'] == "Unauthorized. Token not found": + logging.error('API Key not valid. Please see doc.md to find out how to get it.') + except KeyError: + pass + def usage(self) -> str: return ''' This bot gives updates about customer behavior, financial performance, and analytics