Skip to content

rest

This module contains Rest api utilities, Mainly the RestClient, which you can use to easily pythonify a rest api.

based on https://github.com/jpaugh/agithub/commit/1e2575825b165c1cb7cbd85c22e2561fc4d434d3

Authors:

  • Jonathan Paugh
  • Jens Timmerman

Client

Bases: object

An implementation of a REST client

__init__(url, username=None, password=None, token=None, token_type='Token', user_agent=None, append_slash=False)

Create a Client object, this client can consume a REST api hosted at host/endpoint

If a username is given a password or a token is required. You can not use a password and a token. token_type is the typoe fo th the authorization token text in the http authentication header, defaults to Token This should be set to 'Bearer' for certain OAuth implementations.

delete(url, headers=None, body=None, **params)

Do a http delete request on the given url with given headers, body and parameters Parameters is a dictionary that will will be urlencoded

get(url, headers=None, **params)

Do a http get request on the given url with given headers and parameters Parameters is a dictionary that will will be urlencoded

head(url, headers=None, **params)

Do a http head request on the given url with given headers and parameters Parameters is a dictionary that will will be urlencoded

patch(url, body=None, headers=None, **params)

Do a http patch request on the given url with given body, headers and parameters Parameters is a dictionary that will will be urlencoded

post(url, body=None, headers=None, **params)

Do a http post request on the given url with given body, headers and parameters Parameters is a dictionary that will will be urlencoded

put(url, body=None, headers=None, **params)

Do a http put request on the given url with given body, headers and parameters Parameters is a dictionary that will will be urlencoded

request(method, url, body, headers, content_type=None)

Low-level networking. All HTTP-method methods call this

RequestBuilder

Bases: object

RequestBuilder(client).path.to.resource.method(...) stands for RequestBuilder(client).client.method('path/to/resource, ...)

Also, if you use an invalid path, too bad. Just be ready to catch a You can use item access instead of attribute access. This is convenient for using variables' values and required for numbers. bad status from github.com. (Or maybe an httplib.error...)

To understand the method(...) calls, check out github.client.Client.

__getattr__(key)

Overwrite getattr to build up the equest url this enables us to do bla.some.path['something'] and get the url bla/some/path/something

__init__(client)

Constructor

__str__()

If you ever stringify this, you've (probably) messed up somewhere. So let's give a semi-helpful message.

RestClient

Bases: object

A client with a request builder, so you can easily create rest requests e.g. to create a github Rest API client just do

g = RestClient('https://api.github.com', username='user', password='pass') g = RestClient('https://api.github.com', token='oauth token') status, data = g.issues.get(filter='subscribed') data ... [ list_, of, stuff ] status, data = g.repos.jpaugh64.repla.issues[1].get() data ... { 'dict': 'my issue data', } name, repo = 'jpaugh64', 'repla' status, data = g.repos[name][repo].issues[1].get() ... same thing status, data = g.funny.I.donna.remember.that.one.get() status ... 404

That's all there is to it. (blah.post() should work, too.)

NOTE: It is up to you to spell things correctly. Github doesn't even try to validate the url you feed it. On the other hand, it automatically supports the full API--so why should you care?

__getattr__(key)

Get an attribute, we will build a request with it

__init__(*args, **kwargs)

We create a client with the given arguments