diff --git a/stacklet/client/platform/commands/cube.py b/stacklet/client/platform/commands/cube.py index fbad67b..4e844cf 100644 --- a/stacklet/client/platform/commands/cube.py +++ b/stacklet/client/platform/commands/cube.py @@ -7,6 +7,7 @@ import textwrap from datetime import datetime from pprint import pformat +from typing import Any import click import requests @@ -39,7 +40,7 @@ def run(ctx, query): @cubejs.command() @click.pass_context def meta(ctx): - data = _get(ctx, "v1/meta") + data = _request(ctx, "GET", "v1/meta") cubes = {cube["name"]: cube for cube in data["cubes"]} for name in sorted(cubes): @@ -60,29 +61,20 @@ def resource_counts(ctx): def _run_query(ctx, query): - context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"]) - token = get_token() - - url = f"https://{context.config.cubejs}/cubejs-api/v1/load" - - data = {"query": query} - response = requests.post( - url=url, - headers={"Authorization": token, "Content-Type": "application/json"}, - data=json.dumps(data), - ) - return response.json() + return _request(ctx, "POST", "v1/load", payload={"query": query}) -def _get(ctx, path: str): +def _request(ctx, method: str, path: str, payload: Any = None): context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"]) token = get_token() - - url = f"https://{context.config.cubejs}/cubejs-api/{path}" - - response = requests.get( - url=url, - headers={"Authorization": token, "Content-Type": "application/json"}, + response = requests.request( + method, + url=f"{context.config.cubejs}/cubejs-api/{path}", + headers={ + "Authorization": f"Bearer {token}", + "Content-Type": "application/json", + }, + json=payload, ) return response.json() diff --git a/stacklet/client/platform/executor.py b/stacklet/client/platform/executor.py index 2b1eb89..96bc38f 100644 --- a/stacklet/client/platform/executor.py +++ b/stacklet/client/platform/executor.py @@ -28,7 +28,7 @@ def __init__(self, context, token): self.session = requests.Session() self.session.headers.update( { - "Authorization": self.token, + "Authorization": f"Bearer {self.token}", } ) diff --git a/tests/test_graphql.py b/tests/test_graphql.py index 4674786..ef347f4 100644 --- a/tests/test_graphql.py +++ b/tests/test_graphql.py @@ -14,7 +14,7 @@ def test_executor_run(self): executor, adapter = get_executor_adapter() self.assertEqual(executor.token, "foo") self.assertEqual(executor.api, "mock://stacklet.acme.org/api") - self.assertEqual(executor.session.headers["authorization"], "foo") + self.assertEqual(executor.session.headers["authorization"], "Bearer foo") adapter.register_uri( "POST",