Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thesadru/apimodel
Advanced models made for non-standard modern JSON APIs.
https://github.com/thesadru/apimodel
deserialization parsing python validation
Last synced: 3 months ago
JSON representation
Advanced models made for non-standard modern JSON APIs.
- Host: GitHub
- URL: https://github.com/thesadru/apimodel
- Owner: thesadru
- Created: 2022-05-26T21:23:10.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-07-23T19:56:16.000Z (over 2 years ago)
- Last Synced: 2024-10-13T11:46:19.808Z (4 months ago)
- Topics: deserialization, parsing, python, validation
- Language: Python
- Homepage: https://apimodel.readthedocs.io
- Size: 82 KB
- Stars: 8
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
Awesome Lists containing this project
README
# APIModel
[![Downloads](https://pepy.tech/badge/apimodels)](https://pepy.tech/project/apimodels)
[![PyPI package](https://img.shields.io/pypi/v/apimodels)](https://pypi.org/project/apimodels/)
[![Last Commit](https://img.shields.io/github/last-commit/thesadru/apimodel)](https://github.com/thesadru/apimodel/commits/master)
[![Coverage](https://img.shields.io/codeclimate/coverage/thesadru/apimodel)](https://codeclimate.com/github/thesadru/apimodel)
[![Documentation](https://readthedocs.org/projects/apimodel/badge)](https://apimodel.readthedocs.io/en/latest)Advanced models for non-standard modern JSON APIs.
---
Documentation:
Source Code:
---
Advanced models for non-standard modern JSON APIs. Supports extensive conversion and validation with several tools to help speed up development.
Works with both synchronous and asynchronous models seamlessly.
Fully tested and type-hinted.## Key Features
- Seamless data parsing through annotations.
- Supports extensive conversion and validation.
- Automatic code generation from json to speed up development.
- Inspired by pydantic to ensure familiarity.
- Localization support.
- Fully tested and type-hinted.
- No requirements.## Example
You can see more examples in the docs.
```py
import typingimport datetime
import apimodel
class User(apimodel.APIModel):
id: int
username: str
profile: str# UNIX or ISO parsing
created_at: datetime.datetime@apimodel.validator()
def _complete_profile_url(self, profile: str) -> str:
"""Take the raw profile ID and turn it into a full url"""
return f"https://example.com/assets/profile/{profile}.png"# inheritance
class Attendee(User):
status: typing.Literal["attending", "interested", "absent"] | None = None@apimodel.root_validator()
def _parse_status(self, values: dict[str, typing.Any]) -> dict[str, typing.Any]:
"""Take the entire JSON and update it accordingly"""
# made for non-standard APIs
if "attending" in values:
values["status"] = "attending" if values["attending"] else "absent"
elif "interested" in values:
values["status"] = "interested" if values["interested"] else "absent"# excess values get thrown away
return valuesclass Event(apimodel.APIModel):
# allow easily renaming fields
time: datetime.datetime = apimodel.Field(alias="happening_at")
# clean nested models
attendees: list[User]
```