kopia lustrzana https://github.com/bugout-dev/moonstream
Add initial subscription backend.
rodzic
50952d1c29
commit
3814fdef23
|
@ -1,11 +1,41 @@
|
||||||
"""
|
"""
|
||||||
Pydantic schemas for the Moonstream HTTP API
|
Pydantic schemas for the Moonstream HTTP API
|
||||||
"""
|
"""
|
||||||
from typing import List
|
from enum import Enum
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
|
||||||
|
class SubscriptionTypeResourceData(BaseModel):
|
||||||
|
id: str
|
||||||
|
name: str
|
||||||
|
description: str
|
||||||
|
subscription_plan_id: Optional[str] = None
|
||||||
|
active: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
class SubscriptionTypesListResponce(BaseModel):
|
||||||
|
subscriptions: List[SubscriptionTypeResourceData] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
class SubscriptionResourceData(BaseModel):
|
||||||
|
id: str
|
||||||
|
address: str
|
||||||
|
color: str
|
||||||
|
label: str
|
||||||
|
user_id: str
|
||||||
|
subscription_type_id: str
|
||||||
|
|
||||||
|
|
||||||
|
class CreateSubscriptionRequest(BaseModel):
|
||||||
|
address: str
|
||||||
|
color: str
|
||||||
|
label: str
|
||||||
|
subscription_type_id: str
|
||||||
|
|
||||||
|
|
||||||
class PingResponse(BaseModel):
|
class PingResponse(BaseModel):
|
||||||
"""
|
"""
|
||||||
Schema for ping response
|
Schema for ping response
|
||||||
|
@ -40,4 +70,4 @@ class SubscriptionResponse(BaseModel):
|
||||||
|
|
||||||
|
|
||||||
class SubscriptionsListResponse(BaseModel):
|
class SubscriptionsListResponse(BaseModel):
|
||||||
subscriptions: List[SubscriptionResponse] = Field(default_factory=list)
|
subscriptions: List[SubscriptionResourceData] = Field(default_factory=list)
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
The Moonstream subscriptions HTTP API
|
The Moonstream subscriptions HTTP API
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
from typing import Dict
|
from typing import Dict, List
|
||||||
|
|
||||||
from bugout.data import BugoutResource, BugoutResources
|
from bugout.data import BugoutResource, BugoutResources
|
||||||
from bugout.exceptions import BugoutResponseException
|
from bugout.exceptions import BugoutResponseException
|
||||||
|
@ -49,18 +49,47 @@ whitelist_paths.update(DOCS_PATHS)
|
||||||
app.add_middleware(BroodAuthMiddleware, whitelist=whitelist_paths)
|
app.add_middleware(BroodAuthMiddleware, whitelist=whitelist_paths)
|
||||||
|
|
||||||
|
|
||||||
@app.post("/", tags=["subscriptions"], response_model=data.SubscriptionResponse)
|
@app.post("/", tags=["subscriptions"], response_model=data.SubscriptionResourceData)
|
||||||
async def add_subscription_handler(
|
async def add_subscription_handler(
|
||||||
request: Request, subscription_data: data.SubscriptionRequest = Body(...)
|
request: Request, subscription_data: data.CreateSubscriptionRequest = Body(...)
|
||||||
) -> data.SubscriptionResponse:
|
) -> data.SubscriptionResourceData:
|
||||||
"""
|
"""
|
||||||
Add subscription to blockchain stream data for user.
|
Add subscription to blockchain stream data for user.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
token = request.state.token
|
token = request.state.token
|
||||||
|
|
||||||
|
params = {"type": "subscription_type"}
|
||||||
|
|
||||||
|
# request availble subscriptions
|
||||||
|
try:
|
||||||
|
subscription_resources: BugoutResources = bc.list_resources(
|
||||||
|
token=token, params=params
|
||||||
|
)
|
||||||
|
except BugoutResponseException as e:
|
||||||
|
raise HTTPException(status_code=e.status_code, detail=e.detail)
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500)
|
||||||
|
|
||||||
|
# allowed subscriptions
|
||||||
|
subscription_ids_list = [
|
||||||
|
resource.resource_data["id"] for resource in subscription_resources.resources
|
||||||
|
]
|
||||||
|
|
||||||
|
if subscription_data.subscription_type_id not in subscription_ids_list:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=403, detail="Subscription type is not avilable."
|
||||||
|
)
|
||||||
|
|
||||||
user = request.state.user
|
user = request.state.user
|
||||||
|
|
||||||
|
# chek if that contract not already setted up
|
||||||
|
|
||||||
resource_data = {"user_id": str(user.id)}
|
resource_data = {"user_id": str(user.id)}
|
||||||
resource_data.update(subscription_data)
|
resource_data.update(subscription_data)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
resource: BugoutResource = bc.create_resource(
|
resource: BugoutResource = bc.create_resource(
|
||||||
token=token,
|
token=token,
|
||||||
application_id=MOONSTREAM_APPLICATION_ID,
|
application_id=MOONSTREAM_APPLICATION_ID,
|
||||||
|
@ -70,9 +99,14 @@ async def add_subscription_handler(
|
||||||
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:
|
||||||
raise HTTPException(status_code=500)
|
raise HTTPException(status_code=500)
|
||||||
return data.SubscriptionResponse(
|
|
||||||
|
return data.SubscriptionResourceData(
|
||||||
|
id=str(resource.id),
|
||||||
user_id=resource.resource_data["user_id"],
|
user_id=resource.resource_data["user_id"],
|
||||||
blockchain=resource.resource_data["blockchain"],
|
address=resource.resource_data["address"],
|
||||||
|
color=resource.resource_data["color"],
|
||||||
|
label=resource.resource_data["label"],
|
||||||
|
subscription_type_id=resource.resource_data["subscription_type_id"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -86,15 +120,48 @@ async def get_subscriptions_handler(request: Request) -> data.SubscriptionsListR
|
||||||
try:
|
try:
|
||||||
resources: BugoutResources = bc.list_resources(token=token, params=params)
|
resources: BugoutResources = bc.list_resources(token=token, params=params)
|
||||||
except BugoutResponseException as e:
|
except BugoutResponseException as e:
|
||||||
|
if e.detail == "Resources not found":
|
||||||
|
return data.SubscriptionsListResponse(subscriptions=[])
|
||||||
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:
|
||||||
raise HTTPException(status_code=500)
|
raise HTTPException(status_code=500)
|
||||||
|
|
||||||
return data.SubscriptionsListResponse(
|
return data.SubscriptionsListResponse(
|
||||||
subscriptions=[
|
subscriptions=[
|
||||||
data.SubscriptionResponse(
|
data.SubscriptionResourceData(
|
||||||
|
id=str(resource.id),
|
||||||
user_id=resource.resource_data["user_id"],
|
user_id=resource.resource_data["user_id"],
|
||||||
blockchain=resource.resource_data["blockchain"],
|
address=resource.resource_data["address"],
|
||||||
|
color=resource.resource_data["color"],
|
||||||
|
label=resource.resource_data["label"],
|
||||||
|
subscription_type_id=resource.resource_data["subscription_type_id"],
|
||||||
)
|
)
|
||||||
for resource in resources.resources
|
for resource in resources.resources
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get(
|
||||||
|
"/types", tags=["subscriptions"], response_model=data.SubscriptionTypesListResponce
|
||||||
|
)
|
||||||
|
async def get_available_subscriptions_type(
|
||||||
|
request: Request,
|
||||||
|
) -> data.SubscriptionTypesListResponce:
|
||||||
|
|
||||||
|
"""
|
||||||
|
Get available's subscriptions types.
|
||||||
|
"""
|
||||||
|
token = request.state.token
|
||||||
|
params = {"type": "subscription_type"}
|
||||||
|
try:
|
||||||
|
resources: BugoutResources = bc.list_resources(token=token, params=params)
|
||||||
|
except BugoutResponseException as e:
|
||||||
|
raise HTTPException(status_code=e.status_code, detail=e.detail)
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500)
|
||||||
|
return data.SubscriptionTypesListResponce(
|
||||||
|
subscriptions=[
|
||||||
|
data.SubscriptionTypeResourceData.validate(resource.resource_data)
|
||||||
|
for resource in resources.resources
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
|
@ -3,7 +3,7 @@ import os
|
||||||
from bugout.app import Bugout
|
from bugout.app import Bugout
|
||||||
|
|
||||||
# Bugout
|
# Bugout
|
||||||
bugout_client = Bugout()
|
bugout_client = Bugout(brood_api_url="http://127.0.0.1:7474", spire_api_url="http://127.0.0.1:7475")
|
||||||
|
|
||||||
MOONSTREAM_APPLICATION_ID = os.environ.get("MOONSTREAM_APPLICATION_ID")
|
MOONSTREAM_APPLICATION_ID = os.environ.get("MOONSTREAM_APPLICATION_ID")
|
||||||
if MOONSTREAM_APPLICATION_ID is None:
|
if MOONSTREAM_APPLICATION_ID is None:
|
||||||
|
|
Ładowanie…
Reference in New Issue