from bit2me.types import OrderResponse
from pydantic import TypeAdapter
from bit2me.core import Endpoint

adapter = TypeAdapter(OrderResponse)

class Cancel(Endpoint):
  async def cancel(self, id: str, *, validate: bool = True) -> OrderResponse:
    """Cancel the order by identifier if this order is open or inactive.
    
    - `id`
    - `validate`: Whether to validate the response against the expected schema (default: True) (default: None)
    
    [Official docs](https://api.bit2me.com/trading-spot-rest#tag/trading/DELETE/v1/trading/order/{id})"""
    r = await self.authed_request('DELETE', f'/v1/trading/order/{id}')
    
    if r.status_code != 200:
      self.raise_error(r)
    return adapter.validate_json(r.text) if self.should_validate(validate) else r.json()
