Skip to content

Commit

Permalink
Merge pull request #2 from hotgluexyz/feature/transactions
Browse files Browse the repository at this point in the history
added transactions push
  • Loading branch information
hsyyid authored Jun 2, 2022
2 parents a27e8cc + b6f3de0 commit 8af49e9
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 6 deletions.
106 changes: 106 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,109 @@
.DS_Store
tests/
hidden-tests/
.secrets

todo.org
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# IPython Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# dotenv
.env

# virtualenv
venv/
ENV/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject

# Mac
._*
.DS_Store

# Custom stuff
env.sh
config.json
.autoenv.zsh

rsa-key
tags
singer-check-tap-data
state.json
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

setup(
name='target-xero',
version='1.0.1',
version='1.0.2',
description='hotglue target for exporting data to Xero API',
author='hotglue',
url='https://hotglue.xyz',
classifiers=['Programming Language :: Python :: 3 :: Only'],
py_modules=['target_xero'],
install_requires=[
'requests==2.20.0',
'pandas==1.1.3',
'pandas==1.3.5',
'argparse==1.4.0',
"singer-python==5.9.0"
],
Expand Down
37 changes: 34 additions & 3 deletions target_xero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import json
import sys
import argparse
import requests
import base64
import json
import pandas as pd
import logging
import re

from .client import XeroClient
from target_xero.client import XeroClient

logger = logging.getLogger("target-xero")
logging.basicConfig(level=logging.DEBUG,
Expand Down Expand Up @@ -247,11 +246,43 @@ def upload_journals(config, client):
post_journal_entries(journals, client)


def upload_transactions(config, client):

input_path = f"{config['input_path']}/Transactions.json"
with open(input_path) as f:
transactions = json.load(f)

acc_list = client.filter("Accounts")
contact_list = client.filter("Contacts")

pushed_ids = []
try:
for transaction in transactions:
bank = [acc["AccountID"] for acc in acc_list if acc["Name"]==transaction["Bank"] and acc["Type"]=="BANK"][0]
transaction["BankAccount"] = dict(AccountID=bank)
for line in transaction["LineItems"]:
code = [acc["Code"] for acc in acc_list if acc["Name"]==line["AccountName"]][0]
line["AccountCode"] = code
contact = [contact["ContactID"] for contact in contact_list if contact["Name"]==transaction["Contact"]][0]
transaction["Contact"] = dict(ContactID=contact)
res = client.push("Bank_Transactions", transaction)
pushed_ids.extend([transaction['BankTransactionID'] for transaction in res['BankTransactions']])
except:
logger.warning(f"Invalid Payload: {json.dumps(transaction)}")
logger.info("Deleting posted transactions")
for id in pushed_ids:
client.push("Bank_Transactions", dict(BankTransactionID=id, Status="DELETED"))

def upload(config, args):
# Login update tap config with new refresh token if necessary
client = XeroClient(config)
client.refresh_credentials(config, args.config_path)

if os.path.exists(f"{config['input_path']}/Transactions.json"):
logger.info("Found Transactions.json, uploading...")
upload_transactions(config, client)
logger.info("Transactions.json uploaded!")

if os.path.exists(f"{config['input_path']}/JournalEntries.csv"):
logger.info("Found JournalEntries.csv, uploading...")
upload_journals(config, client)
Expand Down
2 changes: 1 addition & 1 deletion target_xero/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def push(self, tap_stream_id, payload):
response = self.session.send(request.prepare())

if response.status_code != 200:
raise_for_error(response)
raise_for_error(response.json())
return None
else:
return response.json()
Expand Down

0 comments on commit 8af49e9

Please sign in to comment.