kopia lustrzana https://github.com/bugout-dev/moonstream
Merge pull request #23 from bugout-dev/add-subsription-cli
Add init version of subscription cli.pull/46/head
commit
c8869cbb2d
|
@ -0,0 +1,135 @@
|
||||||
|
"""
|
||||||
|
Moonstream CLI
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
from distutils.util import strtobool
|
||||||
|
import json
|
||||||
|
from typing import List
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from bugout.data import BugoutResource, BugoutResources
|
||||||
|
from bugout.exceptions import BugoutResponseException
|
||||||
|
|
||||||
|
from .settings import (
|
||||||
|
MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
||||||
|
MOONSTREAM_APPLICATION_ID,
|
||||||
|
bugout_client as bc,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class BroodResourcesInteractionException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class UnExpectedException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def add_subscription_handler(args: argparse.Namespace) -> None:
|
||||||
|
"""
|
||||||
|
Handler for "groups subscription add" subcommand.
|
||||||
|
"""
|
||||||
|
new_subscription_id = 0
|
||||||
|
params = {"type": "subscription_type"}
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
# resolve index
|
||||||
|
try:
|
||||||
|
resources: BugoutResources = bc.list_resources(
|
||||||
|
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, params=params
|
||||||
|
)
|
||||||
|
new_subscription_id = (
|
||||||
|
max(
|
||||||
|
[
|
||||||
|
int(resource.resource_data["id"])
|
||||||
|
for resource in resources.resources
|
||||||
|
]
|
||||||
|
)
|
||||||
|
+ 1
|
||||||
|
)
|
||||||
|
except BugoutResponseException as e:
|
||||||
|
if e.detail != "Resources not found":
|
||||||
|
raise BroodResourcesInteractionException(
|
||||||
|
f"status_code={e.status_code}, detail={e.detail}"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print("Unexpected Exception on request to brood")
|
||||||
|
|
||||||
|
subscription_data = {
|
||||||
|
"id": str(new_subscription_id),
|
||||||
|
"name": args.name,
|
||||||
|
"description": args.description,
|
||||||
|
"subscription_plan_id": args.subscription_plan_id,
|
||||||
|
"active": args.active,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add subscriptions
|
||||||
|
|
||||||
|
resource_data = {"type": "subscription_type"}
|
||||||
|
resource_data.update(subscription_data)
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
resource: BugoutResource = bc.create_resource(
|
||||||
|
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
||||||
|
application_id=MOONSTREAM_APPLICATION_ID,
|
||||||
|
resource_data=resource_data,
|
||||||
|
)
|
||||||
|
except BugoutResponseException as e:
|
||||||
|
print(f"status_code={e.status_code}, detail={e.detail}")
|
||||||
|
raise BroodResourcesInteractionException(
|
||||||
|
f"status_code={e.status_code}, detail={e.detail}"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Exception in create brood resource error:{e}")
|
||||||
|
raise UnExpectedException("Error in resource creating")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(description="Moonstream CLI")
|
||||||
|
parser.set_defaults(func=lambda _: parser.print_help())
|
||||||
|
subcommands = parser.add_subparsers(description="Moonstream commands")
|
||||||
|
|
||||||
|
parser_subscription = subcommands.add_parser(
|
||||||
|
"subscription", description="Moonstream subscription"
|
||||||
|
)
|
||||||
|
parser_subscription.set_defaults(func=lambda _: parser_subscription.print_help())
|
||||||
|
subcommands_subscription = parser_subscription.add_subparsers(
|
||||||
|
description="Moonstream subscription commands"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Subscriptions command parser
|
||||||
|
parser_subscription_create = subcommands_subscription.add_parser(
|
||||||
|
"create", description="Create Moonstream subscription"
|
||||||
|
)
|
||||||
|
parser_subscription_create.add_argument(
|
||||||
|
"-n",
|
||||||
|
"--name",
|
||||||
|
required=True,
|
||||||
|
type=str,
|
||||||
|
help="Title of that subscription",
|
||||||
|
)
|
||||||
|
parser_subscription_create.add_argument(
|
||||||
|
"-d",
|
||||||
|
"--descriptions",
|
||||||
|
required=True,
|
||||||
|
type=str,
|
||||||
|
help="Description for user",
|
||||||
|
)
|
||||||
|
parser_subscription_create.add_argument(
|
||||||
|
"-s",
|
||||||
|
"--subscription_plan_id",
|
||||||
|
required=False,
|
||||||
|
type=str,
|
||||||
|
help="Stripe sibscription id",
|
||||||
|
)
|
||||||
|
parser_subscription_create.add_argument(
|
||||||
|
"--active",
|
||||||
|
action="store_true",
|
||||||
|
help="Set this flag to create a verified user",
|
||||||
|
)
|
||||||
|
parser_subscription_create.set_defaults(func=add_subscription_handler)
|
|
@ -12,10 +12,11 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||||
from .. import data
|
from .. import data
|
||||||
from ..middleware import BroodAuthMiddleware
|
from ..middleware import BroodAuthMiddleware
|
||||||
from ..settings import (
|
from ..settings import (
|
||||||
MOONSTREAM_APPLICATION_ID,
|
|
||||||
DOCS_TARGET_PATH,
|
DOCS_TARGET_PATH,
|
||||||
ORIGINS,
|
|
||||||
DOCS_PATHS,
|
DOCS_PATHS,
|
||||||
|
MOONSTREAM_APPLICATION_ID,
|
||||||
|
MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
||||||
|
ORIGINS,
|
||||||
bugout_client as bc,
|
bugout_client as bc,
|
||||||
)
|
)
|
||||||
from ..version import MOONSTREAM_VERSION
|
from ..version import MOONSTREAM_VERSION
|
||||||
|
@ -46,6 +47,11 @@ app.add_middleware(
|
||||||
|
|
||||||
whitelist_paths: Dict[str, str] = {}
|
whitelist_paths: Dict[str, str] = {}
|
||||||
whitelist_paths.update(DOCS_PATHS)
|
whitelist_paths.update(DOCS_PATHS)
|
||||||
|
whitelist_paths.update(
|
||||||
|
{
|
||||||
|
"/subscriptions/types": "GET"
|
||||||
|
}
|
||||||
|
)
|
||||||
app.add_middleware(BroodAuthMiddleware, whitelist=whitelist_paths)
|
app.add_middleware(BroodAuthMiddleware, whitelist=whitelist_paths)
|
||||||
|
|
||||||
|
|
||||||
|
@ -191,7 +197,9 @@ async def get_available_subscriptions_type(
|
||||||
token = request.state.token
|
token = request.state.token
|
||||||
params = {"type": "subscription_type"}
|
params = {"type": "subscription_type"}
|
||||||
try:
|
try:
|
||||||
resources: BugoutResources = bc.list_resources(token=token, params=params)
|
resources: BugoutResources = bc.list_resources(
|
||||||
|
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, params=params
|
||||||
|
)
|
||||||
except BugoutResponseException as e:
|
except BugoutResponseException as e:
|
||||||
raise HTTPException(status_code=e.status_code, detail=e.detail)
|
raise HTTPException(status_code=e.status_code, detail=e.detail)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
@ -14,6 +14,14 @@ MOONSTREAM_DATA_JOURNAL_ID = os.environ.get("MOONSTREAM_DATA_JOURNAL_ID")
|
||||||
if MOONSTREAM_DATA_JOURNAL_ID is None:
|
if MOONSTREAM_DATA_JOURNAL_ID is None:
|
||||||
raise ValueError("MOONSTREAM_DATA_JOURNAL_ID environment variable must be set")
|
raise ValueError("MOONSTREAM_DATA_JOURNAL_ID environment variable must be set")
|
||||||
|
|
||||||
|
MOONSTREAM_ADMIN_ACCESS_TOKEN = os.environ.get(
|
||||||
|
"MOONSTREAM_ADMIN_ACCESS_TOKEN"
|
||||||
|
)
|
||||||
|
if MOONSTREAM_ADMIN_ACCESS_TOKEN is None:
|
||||||
|
raise ValueError(
|
||||||
|
"MOONSTREAM_ADMIN_ACCESS_TOKEN environment variable must be set"
|
||||||
|
)
|
||||||
|
|
||||||
# Origin
|
# Origin
|
||||||
RAW_ORIGINS = os.environ.get("MOONSTREAM_CORS_ALLOWED_ORIGINS")
|
RAW_ORIGINS = os.environ.get("MOONSTREAM_CORS_ALLOWED_ORIGINS")
|
||||||
if RAW_ORIGINS is None:
|
if RAW_ORIGINS is None:
|
||||||
|
|
|
@ -4,3 +4,5 @@ export MOONSTREAM_APPLICATION_ID="<issued_bugout_application_id>"
|
||||||
export MOONSTREAM_DATA_JOURNAL_ID="<bugout_journal_id_to_store_blockchain_data>"
|
export MOONSTREAM_DATA_JOURNAL_ID="<bugout_journal_id_to_store_blockchain_data>"
|
||||||
export MOONSTREAM_DB_URI="postgresql://<username>:<password>@<db_host>:<db_port>/<db_name>"
|
export MOONSTREAM_DB_URI="postgresql://<username>:<password>@<db_host>:<db_port>/<db_name>"
|
||||||
export MOONSTREAM_POOL_SIZE=0
|
export MOONSTREAM_POOL_SIZE=0
|
||||||
|
export MOONSTREAM_AUTO_USER_TOKEN="<Access token to application resources>"
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue