Code Monkey home page Code Monkey logo

refreshbooks's Introduction

Refreshbooks provides a simple synchronous API for manipulating FreshBooks invoices, clients, and other data:

from refreshbooks import api

c = api.OAuthClient(
    'example.freshbooks.com',
    'consumerkey',
    'My Consumer Secret',
    'An existing token',
    'An existing token secret',
    user_agent='Example/1.0'
)

                                      # XML structure inferred from args
response = c.invoice.create(          # <request method="invoice.create">
    invoice=dict(                     #   <invoice>
        client_id='8',                #     <client_id>8</client_id>
        lines=[                       #     <lines>
            api.types.line(           #       <line>
                name='Yard Work',     #         <name>Yard Work</name>
                unit_cost='10',       #         <unit_cost>10</unit_cost>
                quantity='4'          #         <quantity>4</quantity>
            )                         #       </line>
        ]                             #     </lines>
    )                                 #   </invoice>
)                                     # </request>

invoice_response = c.invoice.get(     # <request method="invoice.get">
    invoice_id=response.invoice_id    #     <invoice_id>...</invoice_id>
)                                     # </request>

print "New invoice created: #%s (id %s)" % (
    invoice_response.invoice.number,
    invoice_response.invoice.invoice_id
)

invoices_response = c.invoice.list() # <request method="invoice.list" />

print "There are %s pages of invoices." % (
    invoices_response.invoices.attrib['pages'],
)

for invoice in invoices_response.invoices.invoice:
    print "Invoice %s total: %s" % (
        invoice.invoice_id,
        invoice.amount
    )

Consumer keys and secrets can be obtained from FreshBooks. This library does not handle negotiating for an OAuth token+secret pair; see the oauth module or the OAuth specification for details.

This library also supports the older token-based API authorization scheme:

c = api.TokenClient(
    'example.freshbooks.com',
    'My API token',
    user_agent='Example/1.0'
)

# ... as above ...

API methods return lxml.objectify.ObjectifiedDataElement trees, which can be manipulated as Python objects with the same structure as the underlying XML.

If you are having trouble accessing items as in:

items_response = c.items.list() for item in items_response.items.item:

print item.item_id

Adjust your syntax to use dictionary item lookup:

items_response = c.items.list() for item in items_response['items'].item:

print item.item_id

ObjectifiedDataElement provides a method named items which shadows the items element in the response. Accessing items with dictionary lookup syntax is the known work-around.

To run tests:

python setup.py nosetests

To run network-accessing integration tests against httpstat.us:

python setup.py nosetests --attr=integration

References:

refreshbooks's People

Contributors

ipmb avatar ojacobson avatar rbdcti avatar alfredpang avatar benhoyt avatar

Watchers

Gary Young 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.