GithubHelp home page GithubHelp logo

dwolla / dwolla-v2-python Goto Github PK

View Code? Open in Web Editor NEW
25.0 19.0 17.0 115 KB

Official Python Wrapper for Dwolla's API

Home Page: https://developers.dwolla.com

License: MIT License

Python 99.69% Dockerfile 0.31%
python dwolla api sdk sdk-python

dwolla-v2-python's Introduction

Dwolla SDK for Python

This repository contains the source code for Dwolla's Python-based SDK, which allows developers to interact with Dwolla's server-side API via a Python API. Any action that can be performed via an HTTP request can be made using this SDK when executed within a server-side environment.

Table of Contents

Getting Started

Installation

To begin using this SDK, you will first need to download it to your machine. We use PyPi to distribute this package from where you can automagically download it via pip.

$ pip install dwollav2

Initialization

Before any API requests can be made, you must first determine which environment you will be using, as well as fetch the application key and secret. To fetch your application key and secret, please visit one of the following links:

Finally, you can create an instance of Client with key and secret replaced with the application key and secret that you fetched from one of the aforementioned links, respectively.

client = dwollav2.Client(
  key = os.environ['DWOLLA_APP_KEY'],
  secret = os.environ['DWOLLA_APP_SECRET'],
  environment = 'sandbox', # defaults to 'production'
  requests = {'timeout': 0.001}
)
Configure an on_grant callback (optional)

An on_grant callback is useful for storing new tokens when they are granted. The on_grant callback is called with the Token that was just granted by the server.

client = dwollav2.Client(
  key = os.environ['DWOLLA_APP_KEY'],
  secret = os.environ['DWOLLA_APP_SECRET'],
  on_grant = lambda t: save(t)
)

It is highly recommended that you encrypt any token data you store.

Tokens

Generating New Access Tokens

Application access tokens are used to authenticate against the API on behalf of an application. Application tokens can be used to access resources in the API that either belong to the application itself (webhooks, events, webhook-subscriptions) or the Dwolla Account that owns the application (accounts, customers, funding-sources, etc.). Application tokens are obtained by using the client_credentials OAuth grant type:

application_token = client.Auth.client()

Application access tokens are short-lived: 1 hour. They do not include a refresh_token. When it expires, generate a new one using client.Auth.client().

Initializing Pre-Existing Tokens:

The Dwolla Sandbox Dashboard allows you to generate tokens for your application. A Token can be initialized with the following attributes:

client.Token(access_token = '...',
             expires_in = 123)

Making Requests

Once you've created a Token, currently, you can make low-level HTTP requests.

Low-level Requests

To make low-level HTTP requests, you can use the get(), post(), and delete() methods. These methods will return a Response object.

GET

# GET api.dwolla.com/resource?foo=bar
token.get('resource', foo = 'bar')

# GET requests can also use objects as parameters
# GET api.dwolla.com/resource?foo=bar
token.get('resource', {'foo' = 'bar', 'baz' = 'foo'})

POST

# POST api.dwolla.com/resource {"foo":"bar"}
token.post('resource', foo = 'bar')

# POST api.dwolla.com/resource multipart/form-data foo=...
token.post('resource', foo = ('mclovin.jpg', open('mclovin.jpg', 'rb'), 'image/jpeg'))

DELETE

# DELETE api.dwolla.com/resource
token.delete('resource')

Setting headers

To set additional headers on a request you can pass a dict of headers as the 3rd argument.

For example:

token.post('customers', { 'firstName': 'John', 'lastName': 'Doe', 'email': '[email protected]' },
                        { 'Idempotency-Key': 'a52fcf63-0730-41c3-96e8-7147b5d1fb01' })

Responses

The following snippets demonstrate successful and errored responses from the Dwolla API.

An errored response is returned when Dwolla's servers respond with a status code that is greater than or equal to 400, whereas a successful response is when Dwolla's servers respond with a 200-level status code.

Success
res = token.get('/')

res.status
# => 200

res.headers
# => {'server'=>'cloudflare-nginx', 'date'=>'Mon, 28 Mar 2016 15:30:23 GMT', 'content-type'=>'application/vnd.dwolla.v1.hal+json; charset=UTF-8', 'content-length'=>'150', 'connection'=>'close', 'set-cookie'=>'__cfduid=d9dcd0f586c166d36cbd45b992bdaa11b1459179023; expires=Tue, 28-Mar-17 15:30:23 GMT; path=/; domain=.dwolla.com; HttpOnly', 'x-request-id'=>'69a4e612-5dae-4c52-a6a0-2f921e34a88a', 'cf-ray'=>'28ac1f81875941e3-MSP'}

res.body['_links']['events']['href']
# => 'https://api-sandbox.dwolla.com/events'
Error

If the server returns an error, a dwollav2.Error (or one of its subclasses) will be raised. dwollav2.Errors are similar to Responses.

try:
  token.get('/not-found')
except dwollav2.NotFoundError as e:
  e.status
  # => 404

  e.headers
  # => {"server"=>"cloudflare-nginx", "date"=>"Mon, 28 Mar 2016 15:35:32 GMT", "content-type"=>"application/vnd.dwolla.v1.hal+json; profile=\"http://nocarrier.co.uk/profiles/vnd.error/\"; charset=UTF-8", "content-length"=>"69", "connection"=>"close", "set-cookie"=>"__cfduid=da1478bfdf3e56275cd8a6a741866ccce1459179332; expires=Tue, 28-Mar-17 15:35:32 GMT; path=/; domain=.dwolla.com; HttpOnly", "access-control-allow-origin"=>"*", "x-request-id"=>"667fca74-b53d-43db-bddd-50426a011881", "cf-ray"=>"28ac270abca64207-MSP"}

  e.body.code
  # => "NotFound"
except dwollav2.Error:
  # ...
dwollav2.Error subclasses:

See https://developers.dwolla.com/api-reference#errors for more info.

  • dwollav2.AccessDeniedError
  • dwollav2.InvalidCredentialsError
  • dwollav2.NotFoundError
  • dwollav2.BadRequestError
  • dwollav2.InvalidGrantError
  • dwollav2.RequestTimeoutError
  • dwollav2.ExpiredAccessTokenError
  • dwollav2.InvalidRequestError
  • dwollav2.ServerError
  • dwollav2.ForbiddenError
  • dwollav2.InvalidResourceStateError
  • dwollav2.TemporarilyUnavailableError
  • dwollav2.InvalidAccessTokenError
  • dwollav2.InvalidScopeError
  • dwollav2.UnauthorizedClientError
  • dwollav2.InvalidAccountStatusError
  • dwollav2.InvalidScopesError
  • dwollav2.UnsupportedGrantTypeError
  • dwollav2.InvalidApplicationStatusError
  • dwollav2.InvalidVersionError
  • dwollav2.UnsupportedResponseTypeError
  • dwollav2.InvalidClientError
  • dwollav2.MethodNotAllowedError
  • dwollav2.ValidationError
  • dwollav2.TooManyRequestsError
  • dwollav2.ConflictError

Example App

Take a look at the Sample Application for examples on how to use this SDK to call the Dwolla API. Before you can begin using the app, however, you will need to specify a DWOLLA_APP_KEY and DWOLLA_APP_SECRET environment variable.

Changelog

  • 2.2.1
    • Add extra check in URL's to ensure they are clean. #36.
  • 2.2.0
    • Update JSON request bodies to serialize via simplejson so datatypes like Decimal still serialize like they did pre 2.0.0
  • 2.1.0
    • Do not share requests.session() across instances of dwollav2.Client
  • 2.0.0
    • JSON request bodies now contain sorted keys to ensure the same request body for a given set of arguments, no matter the order they are passed to dwolla.post. This ensures the Idempotency-Key header will work as intended without additional effort by developers.
    • NOTE: Because this change alters the formatting of JSON request bodies, we are releasing it as a major new version. The request body of a request made with 1.6.0 will not match the request body of the same request made in 2.0.0. This will nullify the effect of the Idempotency-Key header when upgrading, so please take this into account. If you have any questions please reach out to us! There are no other changes since 1.6.0.
  • 1.6.0 Allow configuration of requests options on dwollav2.Client.
  • 1.5.0 Add integrations auth functionality
  • 1.4.0 Pass kwargs from get, post, and delete methods to underlying requests methods. (Removed in v1.6)
  • 1.3.0 Change token URLs, update dependencies.
  • 1.2.4 Create a new session for each Token.
  • 1.2.3 Check if IOBase when checking to see if something is a file.
  • 1.2.2 Strip domain from URLs provided to token.* methods.
  • 1.2.1 Update sandbox URLs from uat => sandbox.
  • 1.2.0 Refer to Client id as key.
  • 1.1.8 Support verified_account and dwolla_landing auth flags
  • 1.1.7 Use session over connections for performance improvement (#8 - Thanks @bfeeser!
  • 1.1.5 Fix file upload bug when using with Python 2 (#6)
  • 1.1.2 Add TooManyRequestsError and ConflictError
  • 1.1.1 Add MANIFEST.in
  • 1.1.0 Support per-request headers

Community

  • If you have any feedback, please reach out to us on our forums or by creating a GitHub issue.
  • If you would like to contribute to this library, bug reports and pull requests are always appreciated!
    • After checking out the repo, run pip install -r requirements.txt to install dependencies. Then, run python setup.py test to run the tests.
    • To install this gem onto your local machine, run pip install -e ..

Docker

If you prefer to use Docker to run dwolla-v2-python locally, a Dockerfile is included at the root directory. Follow these instructions from Docker's website to create a Docker image from the Dockerfile, and run it.

Additional Resources

To learn more about Dwolla and how to integrate our product with your application, please consider visiting the following resources and becoming a member of our community!

dwolla-v2-python's People

Contributors

bfeeser avatar gcbh avatar justmobilize avatar nleeper avatar rossdeane avatar sausman avatar shreyathapa avatar spencerhunter avatar tarricsookdeo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dwolla-v2-python's Issues

pip install not working

Collecting dwollav2
  Using cached dwollav2-1.1.0.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/cg/v9dhxy1x42j_q_zsfgb1blsh0000gn/T/pip-build-0mANaF/dwollav2/setup.py", line 23, in <module>
        long_description=open('README.md').read(),
    IOError: [Errno 2] No such file or directory: 'README.md'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/cg/v9dhxy1x42j_q_zsfgb1blsh0000gn/T/pip-build-0mANaF/dwollav2/

ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

When running a long process it appears that the underlying request library throws an error.

Sample code:

        token = dwollav2.Client(
                key = self.app.config.get('DWOLLA_KEY'),
                secret = self.app.config.get('DWOLLA_SECRET'),
                environment = self.app.config.get('DWOLLA_ENV')
            ).Auth.client()

The errors:

ConnectionError
('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
requests/adapters.py in send at line 490

                    # Then, reraise so that we can handle the actual exception.
                    low_conn.close()
                    raise
        except (ProtocolError, socket.error) as err:
            raise ConnectionError(err, request=request)
        except MaxRetryError as e:
            if isinstance(e.reason, ConnectTimeoutError):
                # TODO: Remove this in 3.0.0: see #2811
                if not isinstance(e.reason, NewConnectionError):

ConnectionResetError
[Errno 104] Connection reset by peer
python3.6/ssl.py in read at line 625

        If 'buffer' is provided, read into this buffer and return the number of
        bytes read.
        """
        if buffer is not None:
            v = self._sslobj.read(len, buffer)
        else:
            v = self._sslobj.read(len)
        return v
    def write(self, data):

From our logs it appears that the initial handshake makes a call to get an access_token and subsequent calls always create a new HTTPS connection for api.dwolla.com but not for the www.dwollo.com domain.

  1. Request
[DEBUG] Starting new HTTPS connection (1): www.dwolla.com
[DEBUG] https://www.dwolla.com:443 "POST /oauth/v2/token HTTP/1.1" 200 None
[DEBUG] Starting new HTTPS connection (1): api.dwolla.com
  1. Request (several minutes later)
[DEBUG] https://www.dwolla.com:443 "POST /oauth/v2/token HTTP/1.1" 200 None
[DEBUG] Starting new HTTPS connection (1): api.dwolla.com
[DEBUG] https://api.dwolla.com:443 "GET /mass-payments/XXXXX/items HTTP/1.1" 200 2331
[DEBUG] https://www.dwolla.com:443 "POST /oauth/v2/token HTTP/1.1" 200 None
[DEBUG] Starting new HTTPS connection (1): api.dwolla.com
[DEBUG] https://api.dwolla.com:443 "GET /transfers/XXXXX HTTP/1.1" 200 1247

Notice there is no subsequent connection made for www.dwolla.com. It appears www.dwolla.com, unlike api.dwolla.com becomes cached by the request library and eventually the connection drops raising an error.

Future logs, after a connection error, show the connection being reestablished as follows. Note (2): www.dwolla.com. Where (2) is the num of connections made to that host during a process. I've noticed that after every error the connection count increases.

[DEBUG] Starting new HTTPS connection (2): www.dwolla.com
[DEBUG] https://www.dwolla.com:443 "POST /oauth/v2/token HTTP/1.1" 200 None
[DEBUG] Starting new HTTPS connection (1): api.dwolla.com
[DEBUG] https://api.dwolla.com:443 "GET /mass-payments/XXX/items HTTP/1.1" 200 

It seems that the issues would resolve if the library did not cache the www.dwolla.com host or a new connection was established per request.

As a work around I've tried to add a try catch block around the token code until I can come up with a better solution but have had mixed results thus far.

Sending multipart as describe in the README doesn't seem to work.

It appears this might not have been implemented, or am I doing it wrong?

token.post('documents/%s' % id, documentType = 'other', file = ('rollbar.png', open('rollbar.png', 'rb'), 'image/png'))

Results in:

TypeError: <open file 'rollbar.png', mode 'rb' at 0x10c6ea4b0> is not JSON serializable

And

token.post('documents/%s' % id, documentType = 'other', file = ('rollbar.png', open('rollbar.png', 'rb').read(), 'image/png'))

Results in:

UnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 0: invalid start byte

Using an expired Plaid token gets misleading error

When using Dwolla/Plaid tokenization to create a new funding source, if that token is no longer valide, a generic error happens :

dwollav2.error.Error: {"code":"UpdateCredentials","message":"The login details for this token have changed."}

The Error class is a little bit misleading, it will be easier to isolate this issue if the error was named .error.PlaidError or TokenError or even ValidationError like the rest.

Upload of document fails with TypeError in version 1.5.0

Upload of document fails with TypeError in version 1.5.0.

After we reverted back to version 1.3.0 the error went away, and that's the lib we have in prod right now.

We didn't test version 1.4.0 so I don't know if the error would be present in that version

Here's the stacktrace:

TypeError: request() got an unexpected keyword argument 'file'
  at post (/base/data/home/apps/s~astra-delta-production-205714/20191114t162658.422449984953391035/lib/requests/sessions.py:581)
  at post (/base/data/home/apps/s~astra-delta-production-205714/20191114t162658.422449984953391035/lib/dwollav2/token.py:63)
  at upload_document (/base/data/home/apps/s~astra-delta-production-205714/20191114t162658.422449984953391035/transfers/dwolla.py:225)
  at send_document_dwolla (/base/data/home/apps/s~astra-delta-production-205714/20191114t162658.422449984953391035/transfers/controllers.py:723)
  at send_document_dwolla (/base/data/home/apps/s~astra-delta-production-205714/20191114t162658.422449984953391035/main.py:398)
  at invoke_remote_method (/base/alloc/tmpfs/dynamic_runtimes/python27g/7633deef4a2907e3/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/remote.py:414)
  at invoke_remote (/base/data/home/apps/s~astra-delta-production-205714/20191114t162658.422449984953391035/lib/endpoints/api_config.py:1351)
  at protorpc_service_app (/base/alloc/tmpfs/dynamic_runtimes/python27g/7633deef4a2907e3/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/wsgi/service.py:181)

Here's the code we use for the upload:

    def upload_document(self, customer_id, fd_file, document_type):
        customer_url = "customers/%s" % customer_id
        document = self.app_token.post('%s/documents' % customer_url,
                                       file=fd_file,
                                       documentType=document_type)
        document_url = document.headers['location']
        return document_url

Support for Cloudfare captcha

Hello. I work for Astra, one of your clients, and my office is in Brazil.

It seems Cloudfare requires captcha from Brazilian IP addresses.
I asked dwolla support for the whitelist of my IP address a few times, but since it's dynamic, I keep needing to ask over and over again.
image

Is there a way I could solve the captcha once, and have the dwolla python library read that token/cookie from somewhere?

Please note that I only plan to access to sandbox environment from my IP, and I completely understand if keep being "strict" with IPs outside the USA for the production environment. That said being so strict with the sandbox doesn't make a lot of sense to me.

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.