Tin is a thin, configurable wrapper around requests, to interact with
Project description
Tin
Tin is a thin and minimal wrapper around python requests intended as a generic REST API client library. APIs are defined as YAML wherein API services, enpoints, methods and such that API specs can be updated easily without changing python code. A bit like a poor man's Swagger/OpenAPI.
On the subject of Swagger/OpenAPI
If the REST API you need to interact with publishes an OpenAPI spec, you very likely do not need Tin. There are a variety of much more robust OpenAPI tools that will generate API client code, among other functions. Tin was made with services that don't publish OpenAPI data in mind. That being said, it'll still work fine with any REST API that speaks JSON.
Features
- Supports multiple environment definitions per API
- Basic, header and parameter based authentication
- Credentials from a YML or JSON file, or environment vars
- A minimal model system where lists of json dicts returned from a service
can be instantiated as custom objects with canned CRUD methods such
as
.create()
and.save()
- Simple field validation for models, for specifying required and/or read-only fields.
- Requests session support
- Generally any option that can be passed to requests.
Usage
With an service, API and model files such as
Service definition
---
environments:
production:
host: yourhost.service.com
scheme: https
port: 443
credentials: ~/path/to/credentials.yml
auth_type: basic
ssl:
verify: true
api_file: path/to/service-api.yml
model_file: path/to/service-models.yml
common:
# Common settings apply to all environments
content_type: "application/json"
basepath: "/api/v2"
params:
may: ["links", "filter_by", "filter_value"]
API Definition
---
things:
response_data_key: "things" # if interesting data comes back under a toplevel key, return only what's under that key
objects_data_key: "othings" # Retain the original structure of the response, but look under this key for objects to be cast to models
model: thing # Optional: associated model from the models file
methods:
create:
method: POST
path: /things
object_method: create # this method will be associated with model_instance.create()
expect: 201
get:
method: GET
path: /things/:id
object_method: read # this method will be associated with model_instance.refresh()
list:
method: GET
path: /things
Models Definition
---
thing:
id_attr: id
read_only: # read-only
- id
must: # When saving a newly created model instance, these attrs are required
- name
- email
may: # Optional attrs that will be sent to the service API if set in the model instance
- description
- extra_stuff
Tin can be used like this
from tin.api import TinApi
myapi = TinApi(config_file='path/to/myapi_definition.yml', environment='production')
things = myapi.things.list() # returns {'things': [<myapi.things.thing>, <myapi.things.thing>...]}
things = myapi.things.list(nomodel=True) # returns {'things': [ {'id': 1, name: 'thingname', 'email': 'mail@example.com'} ... ]
thing = myapi.things.get(1)
thing = myapi.things.get(id=1)
thing.name # "thingname"
thing.name = "newname"
thing.save()
thing.name # "newname"
newthing = myapi.things.model({})
newthing.save() # fails validation as name and email aren't set
newthing.name = 'new thing'
newthing.email = 'mail@example.com'
newthing.save() # succeeds
newthing.update({'name': 'new name'}) # updates instance and makes Update API call
newthing.delete()
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
File details
Details for the file tin-1.5.4.tar.gz
.
File metadata
- Download URL: tin-1.5.4.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d71fe68929f6b329cafc3fb617d9790b3a48b2f5d63a0dd57992619bc8275b76 |
|
MD5 | 128ad65f69d6ede4ba1469d85d17bbbe |
|
BLAKE2b-256 | 2086a1f62cebdf62f15c88d48167ee4cd544284833ba87c12d12f8080a08f32e |