kopia lustrzana https://github.com/bugout-dev/moonstream
				
				
				
			Merge pull request #95 from bugout-dev/edit-subscription-endpoint
Add update subscription.pull/96/head
						commit
						5eee300537
					
				| 
						 | 
				
			
			@ -2,7 +2,7 @@
 | 
			
		|||
Pydantic schemas for the Moonstream HTTP API
 | 
			
		||||
"""
 | 
			
		||||
from enum import Enum
 | 
			
		||||
from typing import List, Optional
 | 
			
		||||
from typing import List, Optional, Dict, Any
 | 
			
		||||
 | 
			
		||||
from sqlalchemy.sql.operators import notendswith_op
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -25,8 +25,8 @@ class SubscriptionTypesListResponce(BaseModel):
 | 
			
		|||
class SubscriptionResourceData(BaseModel):
 | 
			
		||||
    id: str
 | 
			
		||||
    address: str
 | 
			
		||||
    color: str
 | 
			
		||||
    label: str
 | 
			
		||||
    color: Optional[str]
 | 
			
		||||
    label: Optional[str]
 | 
			
		||||
    user_id: str
 | 
			
		||||
    subscription_type_id: str
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -54,21 +54,9 @@ class VersionResponse(BaseModel):
 | 
			
		|||
    version: str
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SubscriptionRequest(BaseModel):
 | 
			
		||||
    """
 | 
			
		||||
    Schema for data retrieving from frontend about subscription.
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    blockchain: str
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SubscriptionResponse(BaseModel):
 | 
			
		||||
    """
 | 
			
		||||
    User subscription storing in Bugout resources.
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    user_id: str
 | 
			
		||||
    blockchain: str
 | 
			
		||||
class SubscriptionUpdate(BaseModel):
 | 
			
		||||
    update: Dict[str, Any]
 | 
			
		||||
    drop_keys: List[str] = Field(default_factory=list)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SubscriptionsListResponse(BaseModel):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,7 +2,7 @@
 | 
			
		|||
The Moonstream subscriptions HTTP API
 | 
			
		||||
"""
 | 
			
		||||
import logging
 | 
			
		||||
from typing import Dict, List
 | 
			
		||||
from typing import Dict, List, Optional
 | 
			
		||||
 | 
			
		||||
from bugout.data import BugoutResource, BugoutResources
 | 
			
		||||
from bugout.exceptions import BugoutResponseException
 | 
			
		||||
| 
						 | 
				
			
			@ -180,6 +180,54 @@ async def get_subscriptions_handler(request: Request) -> data.SubscriptionsListR
 | 
			
		|||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.put(
 | 
			
		||||
    "/{subscription_id}",
 | 
			
		||||
    tags=["subscriptions"],
 | 
			
		||||
    response_model=data.SubscriptionResourceData,
 | 
			
		||||
)
 | 
			
		||||
async def update_subscriptions_handler(
 | 
			
		||||
    request: Request,
 | 
			
		||||
    subscription_id: str,
 | 
			
		||||
    color: Optional[str] = Form(None),
 | 
			
		||||
    label: Optional[str] = Form(None),
 | 
			
		||||
) -> data.SubscriptionResourceData:
 | 
			
		||||
    """
 | 
			
		||||
    Get user's subscriptions.
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    token = request.state.token
 | 
			
		||||
 | 
			
		||||
    update = {}
 | 
			
		||||
 | 
			
		||||
    if color:
 | 
			
		||||
        update["color"] = color
 | 
			
		||||
 | 
			
		||||
    if label:
 | 
			
		||||
        update["label"] = label
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        resource: BugoutResource = bc.update_resource(
 | 
			
		||||
            token=token,
 | 
			
		||||
            resource_id=subscription_id,
 | 
			
		||||
            resource_data=data.SubscriptionUpdate(
 | 
			
		||||
                update=update,
 | 
			
		||||
            ).dict(),
 | 
			
		||||
        )
 | 
			
		||||
    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.SubscriptionResourceData(
 | 
			
		||||
        id=str(resource.id),
 | 
			
		||||
        user_id=resource.resource_data["user_id"],
 | 
			
		||||
        address=resource.resource_data["address"],
 | 
			
		||||
        color=resource.resource_data["color"],
 | 
			
		||||
        label=resource.resource_data["label"],
 | 
			
		||||
        subscription_type_id=resource.resource_data["subscription_type_id"],
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.get(
 | 
			
		||||
    "/types", tags=["subscriptions"], response_model=data.SubscriptionTypesListResponce
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -66,10 +66,9 @@ export const modifySubscription =
 | 
			
		|||
    const data = new FormData();
 | 
			
		||||
    color && data.append("color", color);
 | 
			
		||||
    label && data.append("label", label);
 | 
			
		||||
    data.append("id", id);
 | 
			
		||||
    return http({
 | 
			
		||||
      method: "POST",
 | 
			
		||||
      url: `${API}/subscription/${id}`,
 | 
			
		||||
      method: "PUT",
 | 
			
		||||
      url: `${API}/subscriptions/${id}`,
 | 
			
		||||
      data,
 | 
			
		||||
    });
 | 
			
		||||
  };
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Ładowanie…
	
		Reference in New Issue