A Python SDK for working with JSON:API specifications
Project description
Midil Kit
A Python SDK for working with JSON:API specifications. This library provides a comprehensive set of tools for creating, validating, and manipulating JSON:API documents according to the JSON:API specification.
Features
- JSON:API Document Creation: Easily create JSON:API compliant documents
- Validation: Built-in validation for JSON:API structures
- Error Handling: Comprehensive error document creation
- Type Safety: Full type hints and Pydantic models
- Utility Functions: Helper functions for common JSON:API operations
Installation
Using Poetry (Recommended)
poetry add midil-kit
Using pip
pip install midil-kit
Quick Start
from jsonapi import (
JSONAPIDocument,
create_success_document,
create_error_document,
JSONAPIError
)
# Create a success document
data = {"id": "1", "type": "articles", "attributes": {"title": "JSON:API"}}
document = create_success_document(data)
# Create an error document
error = JSONAPIError(
status="422",
title="Validation Error",
detail="The request was invalid"
)
error_document = create_error_document([error])
# Access document properties
print(document.jsonapi.version) # "1.0"
print(document.data.attributes["title"]) # "JSON:API"
Usage Examples
Creating a Resource Document
from jsonapi import JSONAPIDocument, ResourceObject
# Create a resource object
resource = ResourceObject(
id="1",
type="articles",
attributes={"title": "JSON:API", "content": "A specification for APIs"}
)
# Create a document with the resource
document = JSONAPIDocument(data=resource)
Working with Relationships
from jsonapi import JSONAPIDocument, ResourceObject, Relationship
# Create related resources
author = ResourceObject(id="1", type="authors", attributes={"name": "John Doe"})
article = ResourceObject(
id="1",
type="articles",
attributes={"title": "JSON:API"},
relationships={
"author": Relationship(data=author)
}
)
document = JSONAPIDocument(data=article)
Error Handling
from jsonapi import create_error_document, JSONAPIError, ErrorSource
# Create detailed error
error = JSONAPIError(
status="422",
title="Validation Error",
detail="The request was invalid",
source=ErrorSource(pointer="/data/attributes/title")
)
error_document = create_error_document([error])
API Reference
Core Classes
JSONAPIDocument: Main document class for JSON:API responsesResourceObject: Represents a JSON:API resourceJSONAPIError: Represents an error in a JSON:API responseRelationship: Represents relationships between resources
Utility Functions
create_success_document(): Create a success document with datacreate_error_document(): Create an error documentcreate_resource_identifier(): Create a resource identifier object
Development
Setup
- Clone the repository:
git clone <repository-url>
cd midil-kit
- Install dependencies:
poetry install
- Install pre-commit hooks:
poetry run pre-commit install
Running Tests
poetry run pytest
Code Formatting
poetry run black .
poetry run isort .
Type Checking
poetry run mypy .
Changelog Management
The project includes automated changelog generation based on conventional commit messages.
Preview changelog changes:
make changelog-preview
Update changelog with new commits:
make changelog
Create a new release:
make create-release
Bump version and prepare release:
make release
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
Commit Message Format
This project follows the Conventional Commits specification. Please use the following format for commit messages:
<type>(<scope>): <description>
[optional body]
[optional footer(s)]
Types:
feat: A new featurefix: A bug fixdocs: Documentation only changesstyle: Changes that do not affect the meaning of the coderefactor: A code change that neither fixes a bug nor adds a featuretest: Adding missing tests or correcting existing testschore: Changes to the build process or auxiliary tools
Scopes:
jsonapi: JSON:API core functionalitydocs: Documentationci: Continuous integrationdeps: Dependencies
Examples:
feat(jsonapi): add support for sparse fieldsets
fix(jsonapi): resolve validation error in relationship serialization
docs: update README with usage examples
test: add tests for error document creation
chore: update dependencies
For breaking changes, start the commit body with BREAKING CHANGE::
feat(jsonapi): remove deprecated ResourceObject constructor
BREAKING CHANGE: The ResourceObject constructor signature has changed
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
0.1.0
- Initial release
- Basic JSON:API document creation and validation
- Error handling utilities
- Type-safe Pydantic models
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file midil_kit-2.0.0.tar.gz.
File metadata
- Download URL: midil_kit-2.0.0.tar.gz
- Upload date:
- Size: 24.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.13.2 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54b43f65bb30bdd367509a1d24f4bfe71698f5b15a058c4408bbf1614028ace6
|
|
| MD5 |
7b2306f65fbcc6cb7dad92c9bcef21f0
|
|
| BLAKE2b-256 |
c36356b5baf15829dd3a20ac9520deab622e48f879c5a5fcb3562dcc837cf15a
|
File details
Details for the file midil_kit-2.0.0-py3-none-any.whl.
File metadata
- Download URL: midil_kit-2.0.0-py3-none-any.whl
- Upload date:
- Size: 32.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.13.2 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff3067eef7faf7ec953a0967aa366a689415cc864df273361383c796edff0401
|
|
| MD5 |
70b6bf44c1ad1f4e86b37b1322e1ef42
|
|
| BLAKE2b-256 |
53b2f511ea5756e83a377fcf16e606199a151ebff934107999d1a0e2d1e05d58
|