diff --git a/README.md b/README.md index 4cd9a271..155493c5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,26 @@ ### Getting Started +Create a file `~/.canvas/credentials.ini` and add the client_id and client_secret credentials for each of your Canvas instances. You can define your default host with `is_default=true`. If no default is explicitly defined, the Canvas CLI will use the first instance in the file as the default for each of the CLI commands. + +**Example:** + +``` +[my-canvas-instance] +client_id=myclientid +client_secret=myclientsecret + +[my-dev-canvas-instance] +client_id=devclientid +client_secret=devclientsecret +is_default=true + +[localhost] +client_id=localclientid +client_secret=localclientsecret +``` + +Next, you're ready to install canvas. + `pip install canvas` **Usage**: diff --git a/canvas_cli/apps/auth/utils.py b/canvas_cli/apps/auth/utils.py index e8ad8fd4..27ffa976 100644 --- a/canvas_cli/apps/auth/utils.py +++ b/canvas_cli/apps/auth/utils.py @@ -6,6 +6,8 @@ import keyring import requests +from canvas_sdk.utils import Http + # Keyring namespace we'll use KEYRING_SERVICE = __name__ @@ -32,7 +34,23 @@ def get_config() -> configparser.ConfigParser: """Reads the config file and returns a ConfigParser object.""" config = configparser.ConfigParser() if not config.read(CONFIG_PATH): - raise Exception(f"Please add your configuration file at '{CONFIG_PATH}'") + raise Exception( + f"""Please add your configuration file at '{CONFIG_PATH}' with the following format: + + [my-canvas-instance] + client_id=myclientid + client_secret=myclientsecret + + [my-dev-canvas-instance] + client_id=devclientid + client_secret=devclientsecret + is_default=true + + [localhost] + client_id=localclientid + client_secret=localclientsecret + """ + ) return config @@ -65,14 +83,16 @@ def get_default_host(host: str | None = None) -> str: (host for host in hosts if config.getboolean(host, "is_default", fallback=False) is True), hosts[0], ) - if first_default_host == 'localhost': + if first_default_host == "localhost": return "http://localhost:8000" + return f"https://{first_default_host}.canvasmedical.com" def request_api_token(host: str, api_client_credentials: str) -> dict: """Request an api token using the provided client_id and client_secret.""" - token_response = requests.post( + http = Http() + token_response = http.post( f"{host}/auth/token/", headers={"Content-Type": "application/x-www-form-urlencoded"}, data=f"grant_type=client_credentials&{api_client_credentials}", diff --git a/canvas_sdk/utils/http.py b/canvas_sdk/utils/http.py index 07e3d214..03d9254b 100644 --- a/canvas_sdk/utils/http.py +++ b/canvas_sdk/utils/http.py @@ -22,7 +22,6 @@ def measure_time(fn: F | None = None) -> Callable[[F], F] | F: def _decorator(fn: F) -> F: @wraps(fn) def wrapper(self: "Http", *args: Any, **kwargs: Any) -> Any: - print(fn.__name__) start_time = time.time() result = fn(self, *args, **kwargs) end_time = time.time() diff --git a/canvas_sdk/utils/tests.py b/canvas_sdk/utils/tests.py index be9e1678..ecb0a99f 100644 --- a/canvas_sdk/utils/tests.py +++ b/canvas_sdk/utils/tests.py @@ -7,7 +7,9 @@ def test_http_get(mock_get: MagicMock) -> None: http = Http() http.get("https://www.canvasmedical.com/", headers={"Authorization": "Bearer as;ldkfjdkj"}) - mock_get.assert_called_once() + mock_get.assert_called_once_with( + "https://www.canvasmedical.com/", headers={"Authorization": "Bearer as;ldkfjdkj"} + ) @patch("requests.Session.post") @@ -19,7 +21,12 @@ def test_http_post(mock_post: MagicMock) -> None: data="grant-type=client_credentials", headers={"Content-type": "application/json"}, ) - mock_post.assert_called_once() + mock_post.assert_called_once_with( + "https://www.canvasmedical.com/", + json={"hey": "hi"}, + data="grant-type=client_credentials", + headers={"Content-type": "application/json"}, + ) @patch("requests.Session.put") @@ -31,7 +38,12 @@ def test_http_put(mock_put: MagicMock) -> None: data="grant-type=client_credentials", headers={"Content-type": "application/json"}, ) - mock_put.assert_called_once() + mock_put.assert_called_once_with( + "https://www.canvasmedical.com/", + json={"hey": "hi"}, + data="grant-type=client_credentials", + headers={"Content-type": "application/json"}, + ) @patch("requests.Session.patch") @@ -43,4 +55,9 @@ def test_http_patch(mock_patch: MagicMock) -> None: data="grant-type=client_credentials", headers={"Content-type": "application/json"}, ) - mock_patch.assert_called_once() + mock_patch.assert_called_once_with( + "https://www.canvasmedical.com/", + json={"hey": "hi"}, + data="grant-type=client_credentials", + headers={"Content-type": "application/json"}, + )