Code Monkey home page Code Monkey logo

apimodel's Introduction

APIModel

Downloads PyPI package Last Commit Coverage Documentation

Advanced models for non-standard modern JSON APIs.


Documentation: https://apimodel.readthedocs.io/en/latest/

Source Code: https://github.com/thesadru/apimodel


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.

import typing

import 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 values

class Event(apimodel.APIModel):
    # allow easily renaming fields
    time: datetime.datetime = apimodel.Field(alias="happening_at")
    # clean nested models
    attendees: list[User]

apimodel's People

Contributors

thesadru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

lunarmagpie

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.