GithubHelp home page GithubHelp logo

nave91 / finch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jaimegildesagredo/finch

0.0 2.0 0.0 427 KB

Asynchronous RESTful API consumer for Python

License: Other

Python 93.13% Gherkin 3.01% JavaScript 3.86%

finch's Introduction

Finch: RESTful API consumer

Latest version Number of PyPI downloads https://secure.travis-ci.org/jaimegildesagredo/finch.svg?branch=master

Finch is an asynchronous RESTful API consumer for Python. Finch is focused on remove all of the boilerplate related to consuming http based APIs and provide a high level abstraction to develop API clients. Finch is released under the Apache 2 license, so you can fork, contribute and also report errors and suggestions to improve it.

Usage

To start consuming a REST API you first should define the resources you are going to consume. For resources modeling we use the booby data modeling library. So, for example, to get your repos from github.com you should define the Repo model and the Repos collection.

from booby import Model, fields
from finch import Collection

class Repo(Model):
    id = fields.Integer()
    name = fields.String()
    owner = fields.String()
    is_private = fields.Boolean()

    def decode(self, response):
        return parse_repo(json.loads(response.body))

    def __repr__(self):
        return 'Repo({}/{})'.format(self.owner, self.name)

class Repos(Collection):
    model = Repo

    def __init__(self, username, *args, **kwargs):
        self.username = username

        super(Repos, self).__init__(*args, **kwargs)

    @property
    def url(self):
        return 'https://api.github.com/users/{}/repos'.format(self.username)

    def decode(self, response):
        return [parse_repo(r) for r in json.loads(response.body)]


def parse_repo(raw):
    return {
        'id': raw['id'],
        'name': raw['name'],
        'owner': raw['owner']['login'],
        'is_private': raw['private']
    }

Now you can fetch your public repos (and also your private repos if you're authenticated).

from tornado import httpclient, ioloop

def on_repos(repos, error):
    ioloop.IOLoop.instance().stop()

    if error:
        raise error

    for repo in repos:
        print repo

repos = Repos('jaimegildesagredo', httpclient.AsyncHTTPClient())
repos.all(on_repos)

ioloop.IOLoop.instance().start()

Installation

You can install the last stable release of Finch from PyPI using pip or easy_install.

$ pip install finch

Also you can install the latest sources from Github.

$ pip install -e git+git://github.com/jaimegildesagredo/finch.git#egg=finch

Tests

To run the Finch tests suite you should install the test requirements and run nosetests.

$ pip install -r test-requirements.txt
$ nosetests tests/unit

Status

Finch is under active development and there is not a complete documentation yet. By the moment you can read the examples in this repository and read the tests, that are the most up to date documentation. Also I'm working on create a complete API client using Finch and create a good documentation.

finch's People

Contributors

hayd avatar jaimegildesagredo avatar paulmelnikow avatar

Watchers

 avatar  avatar

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.