Skip to main content

UNKNOWN

Project description

Build and consume web services (aka APIs) in Python.

Features

  • Providers that work with Django, Flask and Twisted

  • Everything is signed (using itsdangerous)

  • Synchronous consumer (framework independant)

  • Asynchronous consumer (powered by Twisted)

Installation

Django (provider/consumer)

pip install webservices[django]

Flask (provider/consumer)

pip install webservices[flask]

Twisted (provider/consumer)

pip install webservices[twisted]

Synchronous consumer only

pip install webservices[consumer]

Quickstart

We’ll write an API that greets you with your name (or ‘hello world’ if not name is provided).

Provider

Django

We assume you have a setting API_KEYS which is a dictionary of public keys mapping to private keys.

myapi/urls.py:

from django.conf.urls import url, patterns
from webservices.sync import provider_for_django
from myapi.views import HelloProvider

urlpatterns = patterns('',
    url(r'hello/$', provider_for_django(HelloProvider())),
)

Your myapi/views.py:

from django.conf import settings
from webservices.models import Provider

class HelloProvider(Provider):
    def get_private_key(self, public_key):
        return settings.API_KEYS.get(public_key)

    def provide(self, data):
        name = data.get('name', 'world')
        return {'greeting': u'hello %s' % name}

Flask

app.py:

from flask import Flask
from webservices.sync import provider_for_flask
from webservices.models import Provider

app = Flask(__name__)

API_KEYS = {
    'publickey': 'privatekey', # your keys here
}

class HelloProvider(Provider):
    def get_private_key(self, public_key):
        return API_KEYS.get(public_key)

    def provide(self, data):
        name = data.get('name', 'world')
        return {'greeting': u'hello %s' % name}

provider_for_flask(app, '/hello/', HelloProvider())

Twisted

app.py:

from twisted.internet import reactor
from twisted.web.server import Site
from webservices.async import provider_for_twisted
from webservices.models import Provider

API_KEYS = {
    'publickey': 'privatekey', # your keys here
}

class HelloProvider(Provider):
    def get_private_key(self, public_key):
        return API_KEYS.get(public_key)

    def provide(self, data):
        name = data.get('name', 'world')
        return {'greeting': u'hello %s' % name}

resource = provider_for_twisted(HelloProvider())

site = Site(resource)
reactor.listenTCP(80, site)
reactor.run()

Noticed how the provider is basically the same for all three (other than get_private_key)? Neat, right?

Handling errors

To log errors (for example using raven) you can implement the report_exception method on Provider classes. This method is called whenever the provide method throws an exception. It takes no arguments.

Consumer

Synchronous

To consume that code (assuming it’s hosted on ‘https://api.example.org’):

from webservices.sync import SyncConsumer

consumer = SyncConsumer('https://api.example.org', 'mypublickey', 'myprivatekey')
result = consumer.consume('/hello/', {'name': 'webservices')
print result # prints 'hello webservices'

Asynchronous

Same as above, but async:

from webservices.async import TwistedConsumer
from twisted.internet import reactor

def callback(result):
    print result # prints 'hello webserivces'
    reactor.stop()

consumer = TwistedConsumer('https://api.example.org', 'mypublickey', 'myprivatekey')
deferred = consumer.consume('/hello/', {'name': 'webservices')
deferred.addCallback(callback)

reactor.run()

Data Source Name

You can create consumers from Data Source Names (eg 'http://public_key:private_key@api.example.org') using the from_dsn classmethod on consumers.

Example:

consumer = SyncConsumer.from_dsn(’https://public_key:private_key@api.example.org’)

License

This code is licensed under the 3-clause BSD license, see LICENSE.txt.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webservices-0.5.tar.gz (5.4 kB view details)

Uploaded Source

Built Distributions

webservices-0.5-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

webservices-0.5-py2-none-any.whl (7.0 kB view details)

Uploaded Python 2

File details

Details for the file webservices-0.5.tar.gz.

File metadata

  • Download URL: webservices-0.5.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for webservices-0.5.tar.gz
Algorithm Hash digest
SHA256 555451a555d007f142254dec73b9484e07d0fd032dd6ea8ac7352d189b4baf7b
MD5 3d82bd0a57eee46dd69b884d00c8918c
BLAKE2b-256 ce0d94bb6215cfd0bb58f0267f9eb0ae6401ad8344f19111aea16f27f339790c

See more details on using hashes here.

Provenance

File details

Details for the file webservices-0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for webservices-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 63b8507e7260d90daf0526f85469124579ad21767ed855de506ba8b95bb0b628
MD5 9e5ed4dd392dbacacc1908b0882c3070
BLAKE2b-256 85e38b7816b8417bcbad0b7ca5514fb7e9c2cf5cd6e8b89d0f9b338be4a8d09f

See more details on using hashes here.

Provenance

File details

Details for the file webservices-0.5-py2-none-any.whl.

File metadata

File hashes

Hashes for webservices-0.5-py2-none-any.whl
Algorithm Hash digest
SHA256 3a42eb0f10e3a10fcb2615dfa738a2810f962fa45388576bc4201c845170c14b
MD5 630164655a0f6e095213700a4e196d7a
BLAKE2b-256 92f46d3dea5b36f36fb887196f0ebcda55a09fb8b71e6295ce5253f7a876516a

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page