/python-resource

Library for easily interacting with RESTful services (inspired by Angular's ngResource)

Primary LanguagePythonMIT LicenseMIT

Installation

(coming soon to a pip near you ..)

pip install py-resource

Usage

Build Status

Example Usage

Define the API you want to consume:

api.py

class GithubAPI(API):
	base_url = "https://api.github.com"
	headers = {"Authorization": "token {}" . format (os.environ.get('token'))}

class RepositoryRepo(Resource):
	api_class = GithubAPI
	resource_path = "/repos/{owner}/{repo}"
	
	# you only need to define resource_path, but 
	# if you need to, you can specify other paths too
	create_resource_path = "/user/repos"
	list_resource_path = "/users/{owner}/repos"
	
..

Now you can consume it:

List

keys = {"owner": user}
repos = RepositoryRepo().list(keys=keys)

Get a repo:

keys = {"owner": owner, "repo": name}
repo = RepositoryRepo().get(keys=keys)

Create a resource

data = {"name": repo_name}
repo = RepositoryRepo().create(data)

Partial update: (PATCH)

keys = {"owner": owner, "repo": repo}
data = {
	"name": repo,
	"private": private
	}
result = RepositoryRepo().partial_update(keys=keys, data=data)

Update: (PUT)

keys = {"owner": owner, "repo": repo}
data = {
	"name": repo,
	"private": private
}
result = RepositoryRepo().update(keys=keys, data=data)

Delete

keys = {"owner": owner, "repo": repo}
result = RepositoryRepo().delete(keys=keys)

Coming soon:

  • add update_or_create(keys, data, search_fields)

Extra goodies (todo):

  • Generate slate docs
    • Generate entire docs
    • Generate snippets
  • Generate API mocks
  • Build Resources from API
  • Install available clients

Features:

It can:

Basics:

  • GET a resource
  • update a resource
  • create a resource
  • delete a resource
  • update_or_create a resource if it doesnt exist
  • can query a list endpoint
  • provides access to the raw response
  • maps JSON map to a Python object for ease of access
  • supports nested json objects

Authentication and security

  • authenticate with token auth
  • authenticate with basic auth

Meta and ease of use

  • Can provide useful information if missing fields are not included
  • Can print what a request would look like as
    • curl: resource.get(out='curl')
    • python: resource.get(out='python')
    • javascript: resource.get(out='js')
    • ..
  • Generate Slate-style docs (python -m resources.cli generate docs)
  • Generate Slate-style mocks (python -m resources.cli generate mocks)

Low level

  • Accept custom headers
  • Accept custom params
  • Accept custom credentials
  • Optionally pass any kwargs to requests constructor