Connects Xero to a database connector to transfer information to and fro.
Project description
Xero Database Connector
Connects Xero to a database to transfer information to and fro.
Installation
This project requires Python 3+.
-
Download this project and use it (copy it in your project, etc).
-
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
- Create a file
- 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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file xero-db-connector-0.3.4.tar.gz
.
File metadata
- Download URL: xero-db-connector-0.3.4.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccb361bc78c6aa310adf59278abb3fdce640f52ad31eacb3ebcf8f936bf9be17 |
|
MD5 | a1ea374a8dbbb433b8709fa8a72880e4 |
|
BLAKE2b-256 | 1b2bebedc6233d71c828b57fe0de0cdfd674609bc48e5d5a91f70d0859b21b61 |
File details
Details for the file xero_db_connector-0.3.4-py3-none-any.whl
.
File metadata
- Download URL: xero_db_connector-0.3.4-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5a1cfefffd88a0763774fdcfa8d1406fba76ad4d578a7aca0e2c7d6c9f2967b |
|
MD5 | 47cbed6d21c8280bed69946dc33de52e |
|
BLAKE2b-256 | e72a3a2a5a885cd5c230526abd8eabb7beb024096c8962ca6d9c4afcbfe12f5e |