kopia lustrzana https://github.com/bugout-dev/moonstream
Merge pull request #459 from bugout-dev/fix-dashboard-update
Add fixes for work with json data insted form data.pull/462/head
commit
071f6666d4
|
@ -249,3 +249,8 @@ class DashboardResource(BaseModel):
|
||||||
class DashboardCreate(BaseModel):
|
class DashboardCreate(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
subscriptions: List[DashboardMeta]
|
subscriptions: List[DashboardMeta]
|
||||||
|
|
||||||
|
|
||||||
|
class DashboardUpdate(BaseModel):
|
||||||
|
name: Optional[str]
|
||||||
|
subscriptions: List[DashboardMeta] = Field(default_factory=list)
|
||||||
|
|
|
@ -7,7 +7,7 @@ from uuid import UUID
|
||||||
import boto3 # type: ignore
|
import boto3 # type: ignore
|
||||||
from bugout.data import BugoutResource, BugoutResources
|
from bugout.data import BugoutResource, BugoutResources
|
||||||
from bugout.exceptions import BugoutResponseException
|
from bugout.exceptions import BugoutResponseException
|
||||||
from fastapi import APIRouter, Request, Query
|
from fastapi import APIRouter, Request, Query, Body
|
||||||
|
|
||||||
from .. import actions
|
from .. import actions
|
||||||
from .. import data
|
from .. import data
|
||||||
|
@ -41,7 +41,7 @@ blockchain_by_subscription_id = {
|
||||||
|
|
||||||
@router.post("/", tags=["dashboards"], response_model=BugoutResource)
|
@router.post("/", tags=["dashboards"], response_model=BugoutResource)
|
||||||
async def add_dashboard_handler(
|
async def add_dashboard_handler(
|
||||||
request: Request, dashboard: data.DashboardCreate
|
request: Request, dashboard: data.DashboardCreate = Body(...)
|
||||||
) -> BugoutResource:
|
) -> BugoutResource:
|
||||||
"""
|
"""
|
||||||
Add subscription to blockchain stream data for user.
|
Add subscription to blockchain stream data for user.
|
||||||
|
@ -231,10 +231,7 @@ async def get_dashboard_handler(
|
||||||
|
|
||||||
@router.put("/{dashboard_id}", tags=["dashboards"], response_model=BugoutResource)
|
@router.put("/{dashboard_id}", tags=["dashboards"], response_model=BugoutResource)
|
||||||
async def update_dashboard_handler(
|
async def update_dashboard_handler(
|
||||||
request: Request,
|
request: Request, dashboard_id: str, dashboard: data.DashboardUpdate = Body(...)
|
||||||
dashboard_id: str,
|
|
||||||
name: Optional[str],
|
|
||||||
subscriptions: List[data.DashboardMeta],
|
|
||||||
) -> BugoutResource:
|
) -> BugoutResource:
|
||||||
"""
|
"""
|
||||||
Update dashboards mainly fully overwrite name and subscription metadata
|
Update dashboards mainly fully overwrite name and subscription metadata
|
||||||
|
@ -244,7 +241,7 @@ async def update_dashboard_handler(
|
||||||
|
|
||||||
user = request.state.user
|
user = request.state.user
|
||||||
|
|
||||||
dashboard_subscriptions = subscriptions
|
dashboard_subscriptions = dashboard.subscriptions
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
"type": BUGOUT_RESOURCE_TYPE_SUBSCRIPTION,
|
"type": BUGOUT_RESOURCE_TYPE_SUBSCRIPTION,
|
||||||
|
@ -277,7 +274,7 @@ async def update_dashboard_handler(
|
||||||
"bucket"
|
"bucket"
|
||||||
]
|
]
|
||||||
abi_path = available_subscriptions[dashboard_subscription.subscription_id][
|
abi_path = available_subscriptions[dashboard_subscription.subscription_id][
|
||||||
"abi_path"
|
"s3_path"
|
||||||
]
|
]
|
||||||
|
|
||||||
if bucket is None or abi_path is None:
|
if bucket is None or abi_path is None:
|
||||||
|
@ -306,8 +303,7 @@ async def update_dashboard_handler(
|
||||||
internal_error=e,
|
internal_error=e,
|
||||||
detail=f"We can't access the abi for subscription with id:{dashboard_subscription.subscription_id}.",
|
detail=f"We can't access the abi for subscription with id:{dashboard_subscription.subscription_id}.",
|
||||||
)
|
)
|
||||||
|
abi = json.loads(response["Body"].read())
|
||||||
abi = data.DashboardMeta(**response["Body"].read().decode("utf-8"))
|
|
||||||
|
|
||||||
actions.dashboards_abi_validation(
|
actions.dashboards_abi_validation(
|
||||||
dashboard_subscription, abi, s3_path=s3_path
|
dashboard_subscription, abi, s3_path=s3_path
|
||||||
|
@ -321,23 +317,25 @@ async def update_dashboard_handler(
|
||||||
|
|
||||||
dashboard_resource: Dict[str, Any] = {}
|
dashboard_resource: Dict[str, Any] = {}
|
||||||
|
|
||||||
if subscriptions:
|
if dashboard_subscriptions:
|
||||||
|
|
||||||
dashboard_resource["subscriptions"] = subscriptions
|
dashboard_resource["dashboard_subscriptions"] = json.loads(dashboard.json())[
|
||||||
|
"subscriptions"
|
||||||
|
]
|
||||||
|
|
||||||
if name is not None:
|
if dashboard.name is not None:
|
||||||
dashboard_resource["name"] = name
|
dashboard_resource["name"] = dashboard.name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resource: BugoutResource = bc.update_resource(
|
resource: BugoutResource = bc.update_resource(
|
||||||
token=token,
|
token=token,
|
||||||
resource_id=dashboard_id,
|
resource_id=dashboard_id,
|
||||||
resource_data=dashboard_resource,
|
resource_data=data.SubscriptionUpdate(update=dashboard_resource).dict(),
|
||||||
)
|
)
|
||||||
except BugoutResponseException as e:
|
except BugoutResponseException as e:
|
||||||
raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail)
|
raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error creating subscription resource: {str(e)}")
|
logger.error(f"Error updating subscription resource: {str(e)}")
|
||||||
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
||||||
|
|
||||||
return resource
|
return resource
|
||||||
|
|
Ładowanie…
Reference in New Issue