Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove endpoint POST /orders/{order_id}/statuses #126

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Endpoint `/orders/{order_id}/statuses` supporting `GET` for retrieving statuses. The entity returned by this conforms
to the change proposed in [stapi-spec#239](https://github.com/stapi-spec/stapi-spec/pull/239).
- Endpoint `/orders/{order_id}/statuses` supporting `POST` for updating current status
- RootBackend has new methods `get_order_statuses` and `set_order_status`

### Changed
Expand All @@ -25,7 +24,8 @@ none

### Removed

none
- Endpoint `/orders/{order_id}/statuses` supporting `POST` for updating current status was added and then
removed prior to release

### Fixed

Expand Down
19 changes: 2 additions & 17 deletions src/stapi_fastapi/backends/root_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
Order,
OrderCollection,
OrderStatus,
OrderStatusPayload,
)


class RootBackend[T: OrderStatusPayload, U: OrderStatus](Protocol): # pragma: nocover
class RootBackend[T: OrderStatus](Protocol): # pragma: nocover
async def get_orders(self, request: Request) -> ResultE[OrderCollection]:
"""
Return a list of existing orders.
Expand All @@ -34,7 +33,7 @@ async def get_order(self, order_id: str, request: Request) -> ResultE[Maybe[Orde

async def get_order_statuses(
self, order_id: str, request: Request
) -> ResultE[list[U]]:
) -> ResultE[list[T]]:
"""
Get statuses for order with `order_id`.

Expand All @@ -46,17 +45,3 @@ async def get_order_statuses(
A Failure[Exception] will result in a 500.
"""
...

async def set_order_status(
self, order_id: str, payload: T, request: Request
) -> ResultE[U]:
"""
Set statuses for order with `order_id`.

Should return returns.results.Success[OrderStatus] if successful.

Should return returns.results.Failure[Exception] if the status was not able to be set.

A Failure[Exception] will result in a 500.
"""
...
9 changes: 0 additions & 9 deletions src/stapi_fastapi/models/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,3 @@ class OrderPayload(BaseModel, Generic[ORP]):
order_parameters: ORP

model_config = ConfigDict(strict=True)


class OrderStatusPayload(BaseModel):
status_code: OrderStatusCode | None = None
reason_code: str | None = None
reason_text: str | None = None

# todo: rework generic types to allow subclasses to be used correctly, and remove extra=allow
model_config = ConfigDict(strict=True, extra="allow")
28 changes: 0 additions & 28 deletions src/stapi_fastapi/routers/root_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from fastapi import APIRouter, HTTPException, Request, status
from fastapi.datastructures import URL
from fastapi.responses import Response
from returns.maybe import Maybe, Some
from returns.result import Failure, Success

Expand All @@ -16,7 +15,6 @@
Order,
OrderCollection,
OrderStatuses,
OrderStatusPayload,
)
from stapi_fastapi.models.product import Product, ProductsCollection
from stapi_fastapi.models.root import RootResponse
Expand Down Expand Up @@ -101,14 +99,6 @@ def __init__(
tags=["Orders"],
)

self.add_api_route(
"/orders/{order_id}/statuses",
self.set_order_status,
methods=["POST"],
name=f"{self.name}:set-order-status",
tags=["Orders"],
)

def get_root(self, request: Request) -> RootResponse:
return RootResponse(
id="STAPI API",
Expand Down Expand Up @@ -245,24 +235,6 @@ async def get_order_statuses(
case _:
raise AssertionError("Expected code to be unreachable")

async def set_order_status(
self, order_id: str, payload: OrderStatusPayload, request: Request
) -> Response:
match await self.backend.set_order_status(order_id, payload, request):
case Success(_):
return Response(status_code=status.HTTP_202_ACCEPTED)
case Failure(e):
logger.error(
"An error occurred while setting order status: %s",
traceback.format_exception(e),
)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Error setting Order Status",
)
case x:
raise AssertionError(f"Expected code to be unreachable {x}")

def add_product(self: Self, product: Product) -> None:
# Give the include a prefix from the product router
product_router = ProductRouter(product, self)
Expand Down
13 changes: 1 addition & 12 deletions tests/application.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import defaultdict
from datetime import UTC, datetime, timezone
from datetime import datetime, timezone
from typing import Literal, Self
from uuid import uuid4

Expand All @@ -23,7 +23,6 @@
OrderPayload,
OrderStatus,
OrderStatusCode,
OrderStatusPayload,
)
from stapi_fastapi.models.product import (
Product,
Expand Down Expand Up @@ -61,16 +60,6 @@ async def get_order_statuses(
) -> ResultE[list[OrderStatus]]:
return Success(self._orders_db._statuses[order_id])

async def set_order_status(
self, order_id: str, payload: OrderStatusPayload, request: Request
) -> ResultE[OrderStatus]:
input = payload.model_dump()
input["timestamp"] = datetime.now(UTC)
order_status = OrderStatus.model_validate(input)
self._orders_db._orders[order_id].properties.status = order_status
self._orders_db._statuses[order_id].insert(0, order_status)
return Success(order_status)


class MockProductBackend(ProductBackend):
def __init__(self, orders: InMemoryOrderDB) -> None:
Expand Down
37 changes: 0 additions & 37 deletions tests/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,40 +131,3 @@ def test_order_status_after_create(
assert res.status_code == status.HTTP_200_OK
assert res.headers["Content-Type"] == "application/json"
assert len(res.json()["statuses"]) == 1


@pytest.mark.parametrize("product_id", ["test-spotlight"])
def test_order_status_after_update(
get_order_response: Response, stapi_client: TestClient
) -> None:
body = get_order_response.json()
statuses_url = find_link(body["links"], "monitor")["href"]

res = stapi_client.post(
statuses_url,
json={
"status_code": "accepted",
"reason_code": "REASON1",
"reason_text": "some reason",
},
)

assert res.status_code == status.HTTP_202_ACCEPTED

res = stapi_client.get(statuses_url)
assert res.status_code == status.HTTP_200_OK
assert res.headers["Content-Type"] == "application/json"
body = res.json()
assert len(body["statuses"]) == 2

s = body["statuses"][0]
assert s["reason_code"] == "REASON1"
assert s["reason_text"] == "some reason"
assert s["status_code"] == "accepted"
assert s["timestamp"]

s = body["statuses"][1]
assert s["reason_code"] is None
assert s["reason_text"] is None
assert s["status_code"] == "received"
assert s["timestamp"]
Loading