Code Monkey home page Code Monkey logo

btnamespace's Introduction

btnamespace

image

[not actively supported outside of internal Venmo usage]

A Braintree namespace isolates state on the Braintree gateway:

import braintree
import btnamespace

with btnamespace.Namespace():
    customer = braintree.Customer.create({"id": "123"})
    assert customer.id == "123"
    braintree.Customer.find("123")  # success

braintree.Customer.find("123")  # NotFound exception

This is primarily useful during integration tests:

def setUp(self):
    self.namespace = btnamespace.Namespace()
    self.namespace.__enter__()

def test_some_sandbox_integration(self):
    #...

def tearDown(self):
    self.namespace.__exit__()

Compared to calling eg braintree.Customer.delete during tearDown, this has a number of advantages:

  • it's faster, since no teardown is needed
  • it's simpler, since it doesn't require any bookkeeping
  • it's robust, since tests can be written without any state assumptions

You can install it with $ pip install btnamespace.

What's supported

  • Customer create, update, find, delete
  • CreditCard create, update, find, delete
  • Transaction create, find

All operations involving subresources - eg creating a CreditCard and Customer in one call - work as expected.

Adding support for other operations is easy; we just haven't needed them yet. Contributions welcome!

How it Works

Under the hood, a Namespace globally patches the braintree client library.

During create operations, any provided ids are removed. This forces the gateway to respond with unique ids, which are later mapped back to the originally-provided ids. Here's an example:

  • on a call to braintree.Customer.create({'id': '123', ...}), '123' is stored as a Customer id and the call becomes braintree.Customer.create({...}).
  • then, the server returns a unique id 'abcde' for the Customer. '123' is mapped to 'abcde', and the resulting Customer object's id is set to '123'.
  • later, a call to braintree.Customer.find('123') becomes braintree.Customer.find('abcde').

Contributing

Inside your vitualenv:

$ cd btnamespace
$ pip install -e .
$ pip install -r requirements.txt

To run the tests, first add your sandbox credentials:

$ export BT_MERCHANT_ID=merchant-id
$ export BT_PUBLIC_KEY=public-id
$ export BT_PRIVATE_KEY=private-key

Then run $ python tests/test_integration.py.

btnamespace's People

Contributors

simon-weber avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  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.