diff --git a/nbs/00_cli.ipynb b/nbs/00_cli.ipynb index a82bf0d..f355e93 100644 --- a/nbs/00_cli.ipynb +++ b/nbs/00_cli.ipynb @@ -109,7 +109,8 @@ "def _mk_auth_req(url:str, method:str='get', timeout=300., **kwargs):\n", " r = getattr(_get_client(), method)(url, timeout=timeout, **kwargs)\n", " if r.status_code == 200: return r\n", - " else: print(f'Failure: {r.headers[\"X-Plash-Error\"]}')" + " msg = r.headers.get('X-Plash-Error') or r.text.strip() or f'HTTP {r.status_code}'\n", + " raise PlashError(msg)" ] }, { @@ -185,6 +186,39 @@ "class PlashError(Exception): pass" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "b06e8f77", + "metadata": {}, + "outputs": [], + "source": [ + "#| hide\n", + "class _FakeClient:\n", + " def __init__(self, response): self.response = response\n", + " def get(self, url, timeout=300., **kwargs): return self.response\n", + " def post(self, url, timeout=300., **kwargs): return self.response\n", + "\n", + "orig_get_client = _get_client\n", + "try:\n", + " ok = httpx.Response(200, text='ok')\n", + " _get_client = lambda: _FakeClient(ok)\n", + " test_is(_mk_auth_req('https://example.com'), ok)\n", + "\n", + " err = httpx.Response(401, headers={'X-Plash-Error': 'from header'}, text='from body')\n", + " _get_client = lambda: _FakeClient(err)\n", + " test_fail(_mk_auth_req, args=('https://example.com',), contains='from header', exc=PlashError)\n", + "\n", + " err = httpx.Response(422, text='from body')\n", + " _get_client = lambda: _FakeClient(err)\n", + " test_fail(_mk_auth_req, args=('https://example.com',), contains='from body', exc=PlashError)\n", + "\n", + " err = httpx.Response(503)\n", + " _get_client = lambda: _FakeClient(err)\n", + " test_fail(_mk_auth_req, args=('https://example.com',), contains='HTTP 503', exc=PlashError)\n", + "finally: _get_client = orig_get_client" + ] + }, { "cell_type": "code", "execution_count": null, @@ -424,12 +458,30 @@ " plash_app.write_text(f'export PLASH_APP_NAME={name}')\n", " \n", " tarz, _ = create_tar_archive(path, _force_data)\n", - " r = _mk_auth_req(_endpoint(rt=\"/upload\"), \"post\", files={'file': tarz},\n", - " data={'name': name, 'force_data': _force_data})\n", - " if not r: raise PlashError('Unknown failure')\n", + " _mk_auth_req(_endpoint(rt=\"/upload\"), \"post\", files={'file': tarz}, data={'name': name, 'force_data': _force_data})\n", " return name if \".\" in name else _endpoint(sub=name)" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "9e22428d", + "metadata": {}, + "outputs": [], + "source": [ + "#| hide\n", + "with TemporaryDirectory() as td:\n", + " td = Path(td)\n", + " (td/'main.py').write_text('print(\"hi\")\\n')\n", + "\n", + " orig_get_client = _get_client\n", + " try:\n", + " msg = \"Invalid name 'pla.sh': platform-owned domains under 'pla.sh' cannot be used as custom domains.\"\n", + " _get_client = lambda: _FakeClient(httpx.Response(422, text=msg))\n", + " test_fail(deploy, args=(td,), kwargs={'name': 'pla.sh'}, contains='platform-owned domains', exc=PlashError)\n", + " finally: _get_client = orig_get_client" + ] + }, { "cell_type": "code", "execution_count": null, @@ -586,8 +638,7 @@ " name:str=None): # Overrides the .plash file in project root if provided\n", " 'Delete your deployed app'\n", " name = _prep(path, name)\n", - " r = _mk_auth_req(_endpoint(rt=f\"/delete?name={name}\"), \"delete\")\n", - " if not r: raise PlashError('Failed to delete app')\n", + " _mk_auth_req(_endpoint(rt=f\"/delete?name={name}\"), \"delete\")\n", " return f\"App '{name}' deleted successfully\"\n", "\n", "@call_parse\n", @@ -672,8 +723,7 @@ " name:str=None): # Overrides the .plash file in project root if provided\n", " 'Start your deployed app'\n", " name = _prep(path, name)\n", - " r = _mk_auth_req(_endpoint(rt=f\"/start?name={name}\"))\n", - " if not r: raise PlashError('Failed to start app')\n", + " _mk_auth_req(_endpoint(rt=f\"/start?name={name}\"))\n", " return f\"App '{name}' started\"\n", "\n", "@call_parse\n", @@ -743,8 +793,7 @@ " name:str=None): # Overrides the .plash file in project root if provided\n", " 'Stop your deployed app'\n", " name = _prep(path, name)\n", - " r = _mk_auth_req(_endpoint(rt=f\"/stop?name={name}\"))\n", - " if not r: raise PlashError('Failed to stop app')\n", + " _mk_auth_req(_endpoint(rt=f\"/stop?name={name}\"))\n", " return f\"App '{name}' stopped\"\n", "\n", "@call_parse\n", @@ -827,7 +876,6 @@ " 'Get logs for your deployed app'\n", " name = _prep(path, name)\n", " r = _mk_auth_req(_endpoint(rt=f\"/logs?name={name}&mode={mode}\"))\n", - " if not r: raise PlashError('Failed to retrieve logs')\n", " return r.text\n", "\n", "@call_parse\n", @@ -972,7 +1020,6 @@ " save_path.mkdir(exist_ok=True)\n", " if not save_path._is_dir_empty(): raise PlashError(f'Save path ({save_path}) is not empty.')\n", " r = _mk_auth_req(_endpoint(rt=f'/download?name={name}'))\n", - " if not r: raise PlashError('Download request failed')\n", " with tarfile.open(fileobj=io.BytesIO(r.content), mode=\"r:gz\") as tar: \n", " tar.extractall(path=save_path, filter='data')\n", " return save_path\n", @@ -1049,7 +1096,6 @@ "def app_list():\n", " \"List your deployed apps\"\n", " r = _mk_auth_req(_endpoint(rt=\"/user_apps\"))\n", - " if not r: raise PlashError('Failed to retrieve')\n", " return r.json()\n", "\n", "@call_parse\n", diff --git a/plash_cli/cli.py b/plash_cli/cli.py index c42a1d9..19d046b 100644 --- a/plash_cli/cli.py +++ b/plash_cli/cli.py @@ -35,7 +35,8 @@ def _get_client(cfg=PLASH_CONFIG_HOME): def _mk_auth_req(url:str, method:str='get', timeout=300., **kwargs): r = getattr(_get_client(), method)(url, timeout=timeout, **kwargs) if r.status_code == 200: return r - else: print(f'Failure: {r.headers["X-Plash-Error"]}') + msg = r.headers.get('X-Plash-Error') or r.text.strip() or f'HTTP {r.status_code}' + raise PlashError(msg) # %% ../nbs/00_cli.ipynb #7edae5c8 def _get_app_name(path:Path): @@ -169,9 +170,7 @@ def deploy( plash_app.write_text(f'export PLASH_APP_NAME={name}') tarz, _ = create_tar_archive(path, _force_data) - r = _mk_auth_req(_endpoint(rt="/upload"), "post", files={'file': tarz}, - data={'name': name, 'force_data': _force_data}) - if not r: raise PlashError('Unknown failure') + _mk_auth_req(_endpoint(rt="/upload"), "post", files={'file': tarz}, data={'name': name, 'force_data': _force_data}) return name if "." in name else _endpoint(sub=name) # %% ../nbs/00_cli.ipynb #59d06b03 @@ -205,8 +204,7 @@ def delete_app( name:str=None): # Overrides the .plash file in project root if provided 'Delete your deployed app' name = _prep(path, name) - r = _mk_auth_req(_endpoint(rt=f"/delete?name={name}"), "delete") - if not r: raise PlashError('Failed to delete app') + _mk_auth_req(_endpoint(rt=f"/delete?name={name}"), "delete") return f"App '{name}' deleted successfully" @call_parse @@ -227,8 +225,7 @@ def start_app( name:str=None): # Overrides the .plash file in project root if provided 'Start your deployed app' name = _prep(path, name) - r = _mk_auth_req(_endpoint(rt=f"/start?name={name}")) - if not r: raise PlashError('Failed to start app') + _mk_auth_req(_endpoint(rt=f"/start?name={name}")) return f"App '{name}' started" @call_parse @@ -244,8 +241,7 @@ def stop_app( name:str=None): # Overrides the .plash file in project root if provided 'Stop your deployed app' name = _prep(path, name) - r = _mk_auth_req(_endpoint(rt=f"/stop?name={name}")) - if not r: raise PlashError('Failed to stop app') + _mk_auth_req(_endpoint(rt=f"/stop?name={name}")) return f"App '{name}' stopped" @call_parse @@ -266,7 +262,6 @@ def logs( 'Get logs for your deployed app' name = _prep(path, name) r = _mk_auth_req(_endpoint(rt=f"/logs?name={name}&mode={mode}")) - if not r: raise PlashError('Failed to retrieve logs') return r.text @call_parse @@ -302,7 +297,6 @@ def download_app( save_path.mkdir(exist_ok=True) if not save_path._is_dir_empty(): raise PlashError(f'Save path ({save_path}) is not empty.') r = _mk_auth_req(_endpoint(rt=f'/download?name={name}')) - if not r: raise PlashError('Download request failed') with tarfile.open(fileobj=io.BytesIO(r.content), mode="r:gz") as tar: tar.extractall(path=save_path, filter='data') return save_path @@ -318,7 +312,6 @@ def _download(**kwargs): def app_list(): "List your deployed apps" r = _mk_auth_req(_endpoint(rt="/user_apps")) - if not r: raise PlashError('Failed to retrieve') return r.json() @call_parse diff --git a/scripts/deploy_docs.sh b/scripts/deploy_docs.sh index 3e961f5..babaeb0 100755 --- a/scripts/deploy_docs.sh +++ b/scripts/deploy_docs.sh @@ -3,6 +3,7 @@ set -e rm -rf _proc _docs rm -rf docs_site/_docs -nbdev_docs +nbdev-docs cp -r _docs docs_site/ -plash_deploy --path docs_site ${1:+--name "$1"} \ No newline at end of file +plash_deploy --path docs_site ${1:+--name "$1"} +