Skip to main content

Xero Database Connector

Connects Xero to a database to transfer information to and fro.

Installation

This project requires Python 3+.

  1. Download this project and use it (copy it in your project, etc).

  2. Install it from pip.

     $ pip install xero-db-connector
    

Usage

To use this connector you'll need Xero credentials - specifically the keyfile and consumer key.

Here's example usage.

from xero_db_connector.extract import XeroExtractConnector
from xero_db_connector.load import XeroLoadConnector
import sqlite3
import logging
import os
from xero import Xero
from xero.auth import PrivateCredentials

def xero_connect():
    XERO_PRIVATE_KEYFILE = os.environ.get('XERO_PRIVATE_KEYFILE', None)
    XERO_CONSUMER_KEY = os.environ.get('XERO_CONSUMER_KEY', None)
    if XERO_PRIVATE_KEYFILE is None:
        raise Exception('XERO_PRIVATE_KEYFILE is not set')
    if XERO_CONSUMER_KEY is None:
        raise Exception('XERO_CONSUMER_KEY is not set')
    with open(XERO_PRIVATE_KEYFILE) as keyfile:
        rsa_key = keyfile.read()
    credentials = PrivateCredentials(XERO_CONSUMER_KEY, rsa_key)
    # used to connect to xero
    return Xero(credentials)

dbconn = sqlite3.connect('/tmp/xero.db', detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
xero = xero_connect()
x = XeroExtractConnector(xero=xero, dbconn=dbconn)
x.create_tables()
y = XeroLoadConnector(xero=xero, dbconn=dbconn)
y.create_tables()


x.extract_contacts()
x.extract_tracking_categories()
x.extract_accounts()

# do some transformations and populated invoice tables xero_load_invoices and xero_load_invoice_lineitems
for invoice_id in y.load_invoices_generator():
    xero_invoice_id = y.get_xero_invoice_id(invoice_id=invoice_id)
    print(f'posted invoice {invoice_id} for which xero returned {xero_invoice_id}')

Contribute

To contribute to this project follow the steps

  • Fork and clone the repository.
  • Run pip install -r requirements.txt
  • Setup pylint precommit hook
    • Create a file .git/hooks/pre-commit
    • Copy and paste the following lines in the file -
      #!/usr/bin/env bash 
      git-pylint-commit-hook
      
  • Make necessary changes
  • Run unit tests to ensure everything is fine

Unit Tests

To run unit tests, run pytest in the following manner:

python -m pytest test/unit

You should see something like this:

================================================================== test session starts ==================================================================
platform darwin -- Python 3.7.4, pytest-5.2.2, py-1.8.0, pluggy-0.13.0
rootdir: /Users/siva/src/xero-db-connector, inifile: pytest.ini
plugins: mock-1.11.2, cov-2.8.1
collected 3 items                                                                                                                                       

test/unit/test_mocks.py::test_xero_mock_setup PASSED                                                                                              [ 33%]
test/unit/test_mocks.py::test_dbconn_mock_setup PASSED                                                                                            [ 66%]
test/unit/test_mocks.py::test_xec_mock_setup PASSED                                                                                               [100%]

=================================================================== 3 passed in 0.10s ===================================================================

Integration Tests

To run unit tests, you will need a mechanism to connect to a real Xero account. Specifically, you'll need a keyfile and a consumer key, both of which can be obtained from the xero developer portal. Set the following environment variables before running the integration tests:

export XERO_PRIVATE_KEYFILE=<path_to_keyfile>
export XERO_CONSUMER_KEY=<string>

python -m pytest test/integration

Code coverage

To get code coverage report, run this command:

python -m pytest --cov=xero_db_connector

<snipped output>

Name                            Stmts   Miss  Cover
---------------------------------------------------
xero_db_connector/__init__.py       0      0   100%
xero_db_connector/extract.py      106      0   100%
xero_db_connector/load.py          52      0   100%
---------------------------------------------------
TOTAL                             158      0   100%

To get an html report, run this command:

python -m pytest --cov=xero_db_connector --cov-report html:cov_html

We want to maintain code coverage of more than 95% for this project at all times.

License

This project is licensed under the MIT License - see the LICENSE file for details

Metadata

Release files for xero-db-connector 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xero-db-connector 0.3.4
File Size Uploaded
xero-db-connector-0.3.4.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xero-db-connector 0.3.4
File Interpreter ABI Platform
xero_db_connector-0.3.4-py3-none-any.whl Python 3 none any Details

Total release size: 15.0 kB

Release files / xero-db-connector-0.3.4.tar.gz

Download URL xero-db-connector-0.3.4.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ccb361bc78c6aa310adf59278abb3fdce640f52ad31eacb3ebcf8f936bf9be17
BLAKE2b-256 checksum
How to use checksums
1b2bebedc6233d71c828b57fe0de0cdfd674609bc48e5d5a91f70d0859b21b61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.4

Release files / xero_db_connector-0.3.4-py3-none-any.whl

Download URL xero_db_connector-0.3.4-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e5a1cfefffd88a0763774fdcfa8d1406fba76ad4d578a7aca0e2c7d6c9f2967b
BLAKE2b-256 checksum
How to use checksums
e72a3a2a5a885cd5c230526abd8eabb7beb024096c8962ca6d9c4afcbfe12f5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.3.4 This release

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page