Skip to content

Commit

Permalink
Splitted CLI and basic SDK functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatestParrot committed Nov 6, 2023
1 parent 7ef75b7 commit 495efc4
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 68 deletions.
31 changes: 17 additions & 14 deletions StorageCLI/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
import requests


def check_response_for_error(response: requests.Response):
match response.status_code:
case 200 | 201:
pass
case 403 | 404 | 422 | 410 | 503 | 500:
try:
detail = (response.json().get("detail")
.replace("Tenant", "Organization").replace("tenant", "organization"))
except:
detail = response.content
raise click.ClickException(f"{response.status_code}\n"
f"{detail}")
case _:
pass
def check_response_for_error(response: requests.Response, cli_mode=False):
if response.status_code in [200, 201]:
pass
elif response.status_code in [403, 404, 422, 410, 503, 500]:
try:
detail = (response.json().get("detail")
.replace("Tenant", "Organization").replace("tenant", "organization"))
except:
detail = response.content
if cli_mode:
raise click.ClickException(f"{response.status_code}\n"
f"{detail}")
else:
raise Exception(f"{response.status_code}\n"
f"{detail}")
else:
pass
76 changes: 76 additions & 0 deletions StorageCLI/sdk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import requests


from StorageCLI.exceptions import check_response_for_error
from StorageCLI.utils import download_with_progressbar


def list_content(organization, token, page, size, cli_mode=False):
url = f"https://{organization}.storage.api2.merklebot.com/contents/"
rqst = requests.get(url, headers={
"Authorization": token,
}, params={
"page": page,
"size": size
})
check_response_for_error(rqst, cli_mode=cli_mode)
return rqst.json()


def get_content(organization, token, content_id, cli_mode=False):
url = f"https://{organization}.storage.api2.merklebot.com/contents/{content_id}"
rqst = requests.get(url, headers={
"Authorization": token,
})

check_response_for_error(rqst, cli_mode=cli_mode)
return rqst.json()


def delete_content(organization, token, content_id, cli_mode=False):
url = f"https://{organization}.storage.api2.merklebot.com/contents/{content_id}"
rqst = requests.delete(url, headers={
"Authorization": token,
})

check_response_for_error(rqst, cli_mode=cli_mode)
return rqst.json()


def get_content_link(organization, token, content_id, cli_mode=False):
url = f"https://{organization}.storage.api2.merklebot.com/contents/{content_id}/download"
rqst = requests.get(url, headers={
"Authorization": token,
})

check_response_for_error(rqst, cli_mode=cli_mode)
return rqst.json()


def download_content(organization, token, content_id, dest_file, cli_mode=False):
url = f"https://{organization}.storage.api2.merklebot.com/contents/{content_id}/download"
rqst = requests.get(url, headers={
"Authorization": token,
})

check_response_for_error(rqst, cli_mode=cli_mode)
download_url = rqst.json().get("url")
download_with_progressbar(download_url, dest_file)
return True


def restore_content(organization, token, content_id, restore_days, web_hook, cli_mode=False):
json_data = {
'restoreDays': restore_days,
}

if web_hook:
json_data["webhookUrl"] = web_hook

url = f"https://{organization}.storage.api2.merklebot.com/contents/{content_id}/restore"
rqst = requests.post(url, headers={
"Authorization": token,
}, json=json_data)

check_response_for_error(rqst, cli_mode=cli_mode)
return rqst.json()
67 changes: 15 additions & 52 deletions StorageCLI/storage_io.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import click
import requests
from platformdirs import user_config_path
import json

from StorageCLI.validator import *
from StorageCLI.CustomComponents.OptionalPrompt import OptionPromptNull
from StorageCLI.config import *
from StorageCLI.exceptions import check_response_for_error
from StorageCLI.utils import download_with_progressbar

from StorageCLI.sdk import list_content, get_content, delete_content, get_content_link, download_content, restore_content

@click.group()
def cli():
Expand Down Expand Up @@ -90,15 +89,9 @@ def ls(ctx, page, size):
organization = ctx.obj["ORGANIZATION"]
token = ctx.obj["TOKEN"]

url = f"https://{organization}.storage.api2.merklebot.com/contents/"
rqst = requests.get(url, headers={
"Authorization": token,
}, params={
"page": page,
"size": size
})
check_response_for_error(rqst)
click.echo(json.dumps(rqst.json(), indent=2))
rez = list_content(organization, token, page, size, cli_mode=True)

click.echo(json.dumps(rez, indent=2))


@content.command()
Expand All @@ -110,14 +103,10 @@ def get(ctx, content_id):
organization = ctx.obj["ORGANIZATION"]
token = ctx.obj["TOKEN"]

url = f"https://{organization}.storage.api2.merklebot.com/contents/{content_id}"
rqst = requests.get(url, headers={
"Authorization": token,
})
rez = get_content(organization, token, content_id, cli_mode=True)

click.echo(json.dumps(rez, indent=2))

check_response_for_error(rqst)
click.echo(json.dumps(rqst.json(), indent=2))
return rqst.json()


@content.command()
Expand All @@ -129,13 +118,9 @@ def delete(ctx, content_id):
organization = ctx.obj["ORGANIZATION"]
token = ctx.obj["TOKEN"]

url = f"https://{organization}.storage.api2.merklebot.com/contents/{content_id}"
rqst = requests.delete(url, headers={
"Authorization": token,
})
rez = delete_content(organization, token, content_id, cli_mode=True)

check_response_for_error(rqst)
click.echo(json.dumps(rqst.json(), indent=2))
click.echo(json.dumps(rez, indent=2))


@content.command()
Expand Down Expand Up @@ -168,13 +153,9 @@ def get_link(ctx, content_id):
organization = ctx.obj["ORGANIZATION"]
token = ctx.obj["TOKEN"]

url = f"https://{organization}.storage.api2.merklebot.com/contents/{content_id}/download"
rqst = requests.get(url, headers={
"Authorization": token,
})
rez = get_content_link(organization, token, content_id, cli_mode=True)

check_response_for_error(rqst)
click.echo(json.dumps(rqst.json(), indent=2))
click.echo(json.dumps(rez, indent=2))


@content.command()
Expand All @@ -189,14 +170,7 @@ def download(ctx, content_id, dest_file):
organization = ctx.obj["ORGANIZATION"]
token = ctx.obj["TOKEN"]

url = f"https://{organization}.storage.api2.merklebot.com/contents/{content_id}/download"
rqst = requests.get(url, headers={
"Authorization": token,
})

check_response_for_error(rqst)
download_url = rqst.json().get("url")
download_with_progressbar(download_url, dest_file)
download_content(organization, token, content_id, dest_file, cli_mode=True)


@content.command()
Expand All @@ -218,17 +192,6 @@ def restore(ctx, content_id, restore_days, web_hook):
organization = ctx.obj["ORGANIZATION"]
token = ctx.obj["TOKEN"]

json_data = {
'restoreDays': restore_days,
}
rez = restore_content(organization, token, content_id, restore_days, web_hook, cli_mode=True)

if web_hook:
json_data["webhookUrl"] = web_hook

url = f"https://{organization}.storage.api2.merklebot.com/contents/{content_id}/restore"
rqst = requests.post(url, headers={
"Authorization": token,
}, json=json_data)

check_response_for_error(rqst)
click.echo(json.dumps(rqst.json(), indent=2))
click.echo(json.dumps(rez, indent=2))
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "storagecli"
version = "0.1.2"
version = "0.1.3"
description = "CLI for Storage at app.merklebot.com"
authors = ["Arseniy Popov <[email protected]>"]
license = "MIT"
Expand All @@ -16,7 +16,7 @@ repository="https://github.com/merklebot/StorageCLI"
storagecli = 'StorageCLI.storage_io:cli'

[tool.poetry.dependencies]
python = "^3.10"
python = "^3.7"
requests = "^2.31.0"
click = "^8.1.7"
platformdirs = "^3.11.0"
Expand Down

0 comments on commit 495efc4

Please sign in to comment.