Skip to content

Commit

Permalink
Add default value
Browse files Browse the repository at this point in the history
  • Loading branch information
ehooo committed Oct 8, 2024
1 parent a2734c0 commit c00b775
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/lima_api/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import contextlib
import json
from typing import Optional, TypeVar, Union
from typing import Optional, TypeVar, Union, Any

import pydantic

Expand Down Expand Up @@ -31,20 +31,24 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return self.detail

def json(self):
def json(self, default: Optional[Any] = dict):
if self.content is None:
return default() if callable(default) else default
return json.loads(self.content.decode())

def object(self):
return parse_data(self.model, self.content)

def response(self) -> Union[T, dict, bytes]:
def response(self, default: Optional[Any] = dict) -> Union[bytes, Any, T]:
response = self.content
if self.content:
if self.content is not None:
with contextlib.suppress(json.JSONDecodeError):
response = self.json()
response = self.json(default=default)
if self.model:
with contextlib.suppress(pydantic.ValidationError):
response = self.object()
else:
response = default() if callable(default) else default
return response


Expand Down

0 comments on commit c00b775

Please sign in to comment.