From 824373cad1cbb2374af6f4faaf8773cbf316925f Mon Sep 17 00:00:00 2001 From: soggyomelette <85685255+soggyomelette@users.noreply.github.com> Date: Fri, 5 Aug 2022 15:53:44 +0100 Subject: [PATCH] Adds santiment to keys menu in response to #2224 (#2238) * 'Adds santiment key in keys_controller.py and adds tests for santiment and messari keys' * fix linting * fix linting * Fixed \n issue Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com> Co-authored-by: colin99d --- openbb_terminal/keys_controller.py | 65 +++++++++++++++++++ ...est_check_messari_key[VALIDKEY-False].yaml | 47 ++++++++++++++ ...t_check_santiment_key[VALIDKEY-False].yaml | 63 ++++++++++++++++++ tests/openbb_terminal/test_keys_controller.py | 26 ++++++++ 4 files changed, 201 insertions(+) create mode 100644 tests/openbb_terminal/cassettes/test_keys_controller/test_check_messari_key[VALIDKEY-False].yaml create mode 100644 tests/openbb_terminal/cassettes/test_keys_controller/test_check_santiment_key[VALIDKEY-False].yaml diff --git a/openbb_terminal/keys_controller.py b/openbb_terminal/keys_controller.py index 232e996d195..82e1d8e1868 100644 --- a/openbb_terminal/keys_controller.py +++ b/openbb_terminal/keys_controller.py @@ -68,6 +68,7 @@ class KeysController(BaseController): # pylint: disable=too-many-public-methods "smartstake", "github", "messari", + "santiment", ] PATH = "/keys/" key_dict: Dict = {} @@ -738,6 +739,38 @@ class KeysController(BaseController): # pylint: disable=too-many-public-methods if show_output: console.print(self.key_dict["MESSARI"] + "\n") + def check_santiment_key(self, show_output: bool = False) -> None: + """Check Santiment key""" + self.cfg_dict["SANTIMENT"] = "santiment" + if cfg.API_SANTIMENT_KEY == "REPLACE_ME": + logger.info("santiment key not defined") + self.key_dict["SANTIMENT"] = "not defined" + else: + headers = { + "Content-Type": "application/graphql", + "Authorization": f"Apikey {cfg.API_SANTIMENT_KEY}", + } + + # pylint: disable=line-too-long + data = '\n{{ getMetric(metric: "dev_activity"){{ timeseriesData( slug: "ethereum" from: ""2020-02-10T07:00:00Z"" to: "2020-03-10T07:00:00Z" interval: "1w"){{ datetime value }} }} }}' # noqa: E501 + + response = requests.post( + "https://api.santiment.net/graphql", headers=headers, data=data + ) + try: + if response.status_code == 200: + logger.info("santiment key defined, test passed") + self.key_dict["SANTIMENT"] = "defined, test passed" + else: + logger.warning("santiment key defined, test failed") + self.key_dict["SANTIMENT"] = "defined, test failed" + except Exception as _: # noqa: F841 + logger.exception("santiment key defined, test failed") + self.key_dict["SANTIMENT"] = "defined, test failed" + + if show_output: + console.print(self.key_dict["SANTIMENT"] + "\n") + def check_keys_status(self) -> None: """Check keys status""" self.check_av_key() @@ -767,6 +800,7 @@ class KeysController(BaseController): # pylint: disable=too-many-public-methods self.check_smartstake_key() self.check_github_key() self.check_messari_key() + self.check_santiment_key() def print_help(self): """Print help""" @@ -1711,6 +1745,7 @@ class KeysController(BaseController): # pylint: disable=too-many-public-methods self.check_ethplorer_key(show_output=True) + @log_start_end(log=logger) def call_smartstake(self, other_args: List[str]): """Process smartstake command""" parser = argparse.ArgumentParser( @@ -1779,3 +1814,33 @@ class KeysController(BaseController): # pylint: disable=too-many-public-methods dotenv.set_key(self.env_file, "OPENBB_API_MESSARI_KEY", ns_parser.key) cfg.API_MESSARI_KEY = ns_parser.key self.check_messari_key(show_output=True) + + @log_start_end(log=logger) + def call_santiment(self, other_args: List[str]): + """Process santiment command""" + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="santiment", + description="Set Santiment API key.", + ) + parser.add_argument( + "-k", + "--key", + type=str, + dest="key", + help="key", + ) + if not other_args: + console.print( + "For your API Key, visit: https://academy.santiment.net/products-and-plans/create-an-api-key\n" + ) + return + if other_args and "-" not in other_args[0][0]: + other_args.insert(0, "-k") + ns_parser = parse_simple_args(parser, other_args) + if ns_parser: + os.environ["OPENBB_API_SANTIMENT_KEY"] = ns_parser.key + dotenv.set_key(self.env_file, "OPENBB_API_SANTIMENT_KEY", ns_parser.key) + cfg.API_SANTIMENT_KEY = ns_parser.key + self.check_santiment_key(show_output=True) diff --git a/tests/openbb_terminal/cassettes/test_keys_controller/test_check_messari_key[VALIDKEY-False].yaml b/tests/openbb_terminal/cassettes/test_keys_controller/test_check_messari_key[VALIDKEY-False].yaml new file mode 100644 index 00000000000..3dfebe2ddac --- /dev/null +++ b/tests/openbb_terminal/cassettes/test_keys_controller/test_check_messari_key[VALIDKEY-False].yaml @@ -0,0 +1,47 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - python-requests/2.28.1 + x-messari-api-key: + - VALIDKEY + method: GET + uri: https://data.messari.io/api/v2/assets/bitcoin/profile?fields=profile%2Fgeneral%2Foverview%2Fofficial_links + response: + body: + string: !!binary | + H4sIAAAAAAAA/zTIsQrCMBAG4N2nKP/cyjWtNN4buDu5yGFPCTYm5KIgpe/u5PqtsCr1beAVukg2 + ncHUooaoViVmMBw515HvyJ17z4eeh2F/9MM4Tn5yF7TQUlK53tKs4JHoD1HN5KFgnF4fWcLcSA7N + U7/NPZUoFdu2+wUAAP//5ua/OYAAAAA= + headers: + CF-Cache-Status: + - DYNAMIC + CF-RAY: + - 73490170cf55b40d-HKG + Connection: + - keep-alive + Content-Length: + - '137' + Content-Type: + - application/json; charset=UTF-8 + Date: + - Tue, 02 Aug 2022 18:51:34 GMT + Expect-CT: + - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + Server: + - cloudflare + content-encoding: + - gzip + vary: + - Accept-Encoding,Origin + status: + code: 400 + message: Bad Request +version: 1 diff --git a/tests/openbb_terminal/cassettes/test_keys_controller/test_check_santiment_key[VALIDKEY-False].yaml b/tests/openbb_terminal/cassettes/test_keys_controller/test_check_santiment_key[VALIDKEY-False].yaml new file mode 100644 index 00000000000..7e50e133840 --- /dev/null +++ b/tests/openbb_terminal/cassettes/test_keys_controller/test_check_santiment_key[VALIDKEY-False].yaml @@ -0,0 +1,63 @@ +interactions: +- request: + body: ' + + { getMetric(metric: "dev_activity"){ timeseriesData( slug: "ethereum" from: + ""2020-02-10T07:00:00Z"" to: "2020-03-10T07:00:00Z" interval: "1w"){ datetime + value } } }' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Authorization: + - Apikey VALIDKEY + Connection: + - keep-alive + Content-Length: + - '166' + Content-Type: + - application/graphql + User-Agent: + - python-requests/2.28.1 + method: POST + uri: https://api.santiment.net/graphql + response: + body: + string: '{"errors":{"details":"Apikey ''VALIDKEY'' is malformed - it must have + two string parts separated by underscore"}}' + headers: + CF-Cache-Status: + - DYNAMIC + CF-RAY: + - 7349016d4ecf32bf-CDG + Cache-Control: + - max-age=0, private, must-revalidate + Connection: + - keep-alive + Content-Length: + - '111' + Date: + - Tue, 02 Aug 2022 18:51:33 GMT + Expect-CT: + - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + NEL: + - '{"success_fraction":0,"report_to":"cf-nel","max_age":604800}' + Report-To: + - '{"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=5Hdkrh%2FvVOltAojpKA7r6Afsa2JjGSXyvWFs2ZmO8sYg5JUe1KQnsrQMienw2Q2t0xuh%2Bz75OaywGgZ6jw9babwGyRjJrEYZ9AiqdDuuQRHq038JYIvzsKSkw3NERNV48378THuo6mlqNg%2BzPKhf"}],"group":"cf-nel","max_age":604800}' + Server: + - cloudflare + access-control-allow-credentials: + - 'true' + access-control-allow-headers: + - DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,X-SharedAccess-Authorization + access-control-allow-methods: + - GET, POST, OPTIONS + set-cookie: + - INGRESSCOOKIE=1659466294.415.6206.552324; Path=/.+; Secure; HttpOnly + x-request-id: + - 324628ad84b63697bb8472f70ad3cd73 + status: + code: 400 + message: Bad Request +version: 1 diff --git a/tests/openbb_terminal/test_keys_controller.py b/tests/openbb_terminal/test_keys_controller.py index fa8b375a843..c129bec04ce 100644 --- a/tests/openbb_terminal/test_keys_controller.py +++ b/tests/openbb_terminal/test_keys_controller.py @@ -54,6 +54,8 @@ class MockCFG: self.API_ETHPLORER_KEY = kwargs.get("ETHPLOR", None) self.API_SMARTSTAKE_TOKEN = kwargs.get("SMARTSTAKE", None) self.API_SMARTSTAKE_KEY = kwargs.get("SMARTSTAKE", None) + self.API_SANTIMENT_KEY = kwargs.get("SANTIMENT", None) + self.API_MESSARI_KEY = kwargs.get("MESSARI", None) @pytest.mark.vcr @@ -242,6 +244,20 @@ def test_check_smartstake_key(key, output, mocker): controller.check_smartstake_key(output) +@pytest.mark.vcr +@pytest.mark.parametrize("key, output", [("REPLACE_ME", True), ("VALIDKEY", False)]) +def test_check_santiment_key(key, output, mocker): + mocker.patch("openbb_terminal.keys_controller.cfg", MockCFG(SANTIMENT=key)) + controller.check_santiment_key(output) + + +@pytest.mark.vcr +@pytest.mark.parametrize("key, output", [("REPLACE_ME", True), ("VALIDKEY", False)]) +def test_check_messari_key(key, output, mocker): + mocker.patch("openbb_terminal.keys_controller.cfg", MockCFG(MESSARI=key)) + controller.check_messari_key(output) + + def test_print_help(mocker): mocker.patch("openbb_terminal.keys_controller.KeysController.check_coinbase_key") controller.print_help() @@ -378,3 +394,13 @@ def test_call_ethplorer(other): @pytest.mark.parametrize("other", [[], ["-k", "1234", "-t", "456"]]) def test_call_smartstake(other): controller.call_smartstake(other) + + +@pytest.mark.parametrize("other", [[], ["-k", "1234"], ["1234"]]) +def test_call_santiment(other): + controller.call_santiment(other) + + +@pytest.mark.parametrize("other", [[], ["-k", "1234", "-t", "456"]]) +def test_call_messari(other): + controller.call_messari(other)