This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

Britney is a module that implements the SPORE specification. It is based on Spyre. This module requires a SPORE description of an API. Some descriptions for known services are available here. You can write your own with this guide.

Install

Britney is working under Python 2.7 and Python >= 3.2. To install the module, you should use pip or easy_install :

$> pip install britney

or

$> easy_install britney

Create your client

Basics

You must create your client with the new method that reads a json file containing the API description or an URI exposing the description. This can be done like that :

import britney

# from a description file
client = britney.new('/path/to/api_desc.json')

# from an URI
client = britney.new('http://my-server/ws/api_desc.json')

The base URL

If your API description file doesn’t specify the base URL of the service, you can pass it to the new as the named argument base_url :

import britney

client = britney.new('/path/to/api_desc.json', base_url='http://my-server/ws/api/')

Middlewares

Sometimes, services need credentials to let you access the data or even send data. The client you created can enable middlewares at runtime and also extend his usage, by doing this :

import britney
from britney.middleware import auth

client = britney.new('http://my-server/ws/api_desc.json')
client.enable(auth.Basic, username='login', password='xxxxxx')

Sometimes, you want to enable middlewares on certain conditions. Another method called enable_if can take a callable predicate as argument that can check the environment parameters of the request :

import britney
from britney.middleware import auth

client = britney.new('http://my-server/ws/api_desc.json')
client.enable_if(lambda request: request['payload'] != '', auth.Basic, username='login', password='xxxxxx')

Use your client

Access data

Send data

A full example

Create your own middleware

Basics

Creating and enabling middlewares let you control how request are send or response are received by adding authentification or formatting data. To create your middleware, you should :

  • inherit from britney.middleware.Middleware
  • define how to instantiate your middleware
  • implement the process_request method to process the request
  • implement the process_response method to process the response

Here is a base class example to perform runtime on request :

from britney.middleware import Middleware


class Runtime(Middleware):

    def __init__(self, runtime_header):
        self.runtime_header  = runtime_header

    def process_request(self, environ):
        pass

    def process_response(self, response):
        pass

Processing request

With this method, you can access all of the keys and values of the request’s base environment. By the way, you can add keys and values, change them or even delete them. Most of time, this method doesn’t return data but if you return a requests.Response object, the process will stop and return this response. The result environment data will be used to build the request :

import datetime

[...]

def process_request(self, environ):
    self.start_time = datetime.datetime.now()
    environ[self.runtime_key] = 0

Processing response

With this method, you can access data from the response, change or format content or even check headers or status :

[...]

def process_response(self, reponse):
    request_time = datetime.datetime.now() - self.start_time
    response.environ[self.runtime_key] = self.request_time.seconds

Use it

When you create your client, you only should enable your middleware and pass appopriate named arguments to the enable method :

import britney
from your_module.middleware import Runtime

client = britney.new('http:://server.org/ws/api.json')
client.enable(Runtime, runtime_key='X-Spore-Runtime')

That’s all !

Test it

A mock middleware and a function to fake Requests response are available to test the middlewares you created by faking a server. To test the Runtime middleware, you can do as follow :

import datetime
import unittest
import britney
from britney.middleware import utils
from your_module.middleware import Runtime

def test_response(request):
    return utils.fake_response(request, 'OK')

class TestRuntime(unittest.TestCase):

    def setUp(self):
        self.fake_server = {'/test', test_response}
        self.client = britney.new('/path/to/api.json')
        self.runtime_key = 'X-Spore-Runtime'

    def test_runtime(self):
        self.client.enable(Runtime, runtime_header=self.runtime_header)
        self.client.enable(utils.Mock, fakes=self.fake_server, middlewares=self.client.middlewares)
        start = datetime.datetime.now()
        result = self.client.test()
        stop = datetime.datetime.now()

        self.assertIn(result.environ, self.runtime_key)
        self.assertAlmostEqual(result.environ[self.runtime_key], (stop - start).seconds)
Release History

Release History

0.5.0

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.4.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.3.3

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.3.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.3.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.3.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
britney-0.5.0.tar.gz (13.3 kB) Copy SHA256 Checksum SHA256 Source Jul 20, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting