You've already forked akaunting-py
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2c6c1f6f34 | |||
| d06b7d0a76 | |||
| c92fb0d1ee |
1
.idea/akaunting-py.iml
generated
1
.idea/akaunting-py.iml
generated
@@ -4,6 +4,7 @@
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.pytest_cache" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import requests
|
||||
from requests.auth import HTTPBasicAuth
|
||||
from setuptools_scm import get_version
|
||||
|
||||
from akauntingpy import exceptions
|
||||
from akauntingpy.helpers import *
|
||||
|
||||
__version__ = get_version(root='..', relative_to=__file__)
|
||||
__version__ = "1.0.3"
|
||||
|
||||
|
||||
class Client(object):
|
||||
@@ -19,16 +18,21 @@ class Client(object):
|
||||
username,
|
||||
password,
|
||||
company_id,
|
||||
ssl_verify=True,
|
||||
currency_code="NZD",
|
||||
currency_rate="1.0"):
|
||||
"""
|
||||
Create a new instance.
|
||||
Args:
|
||||
url (str): The URL to the Akaunting api.
|
||||
username (str): The username of the Akaunting credentials.
|
||||
password (str): The password of the Akaunting credentials.
|
||||
url (str): The URL to the Akaunting api. ** required **
|
||||
username (str): The username of the Akaunting credentials. ** required **
|
||||
password (str): The password of the Akaunting credentials. ** required **
|
||||
company_id (str): The company ID from Akaunting
|
||||
currency_code (str): The currency code. default is NZD
|
||||
currency_rate (str): The currency rate. default is "1.0"
|
||||
"""
|
||||
self.url = url
|
||||
self.ssl_verify = ssl_verify
|
||||
self.authentication = HTTPBasicAuth(username, password)
|
||||
self.headers = {
|
||||
'User-Agent': 'AkauntingPy v' + __version__,
|
||||
@@ -45,7 +49,8 @@ class Client(object):
|
||||
url=self.url + "/" + endpoint,
|
||||
headers=self.headers,
|
||||
auth=self.authentication,
|
||||
params=MergeDict(self.default_params, params)
|
||||
params=MergeDict(self.default_params, params),
|
||||
verify=self.ssl_verify
|
||||
)
|
||||
|
||||
response_ = response.json()
|
||||
@@ -74,17 +79,45 @@ class Client(object):
|
||||
|
||||
if params.get('search', False):
|
||||
# Check if there is an account returned
|
||||
if data['meta']['pagination'].get('count') == 0:
|
||||
# No account found
|
||||
raise exceptions.AccountNotFound("Sorry, account not found matching search parameters: %s".format(
|
||||
params.get('search')
|
||||
))
|
||||
try:
|
||||
if data['meta']['pagination'].get('count') == 0:
|
||||
# No account found
|
||||
raise exceptions.AccountNotFound("Sorry, account not found matching search parameters: %s".format(
|
||||
params.get('search')
|
||||
))
|
||||
except KeyError as e:
|
||||
# New API 3.0
|
||||
if data['meta']['total'] == 0:
|
||||
raise exceptions.AccountNotFound("Sorry, account not found matching search parameters: %s".format(
|
||||
params.get('search')
|
||||
))
|
||||
|
||||
return data['data']
|
||||
|
||||
def get_contact(self, **params):
|
||||
data = self.call(endpoint="contacts", **params)
|
||||
print(data)
|
||||
if params.get('search', False):
|
||||
try:
|
||||
# Check if there is an account returned
|
||||
if data['meta']['pagination'].get('count') == 0:
|
||||
# No account found
|
||||
raise exceptions.AccountNotFound("Sorry, contact not found matching search parameters: %s".format(
|
||||
params.get('search')
|
||||
))
|
||||
except KeyError as e:
|
||||
# New API 3.0
|
||||
if data['meta']['total'] == 0:
|
||||
raise exceptions.AccountNotFound("Sorry, contact not found matching search parameters: %s".format(
|
||||
params.get('search')
|
||||
))
|
||||
|
||||
return data['data']
|
||||
|
||||
def create_transaction(self,
|
||||
transaction_type='income', # Payment method type
|
||||
account_id=None, # Account ID to assign
|
||||
number="NULL", # Transaction number
|
||||
category_id=None, # Category ID to assign
|
||||
contact_id=None, # Contact ID/Client to assign
|
||||
description=None, # Description
|
||||
@@ -107,6 +140,7 @@ class Client(object):
|
||||
data = self.call(endpoint="transactions",
|
||||
method="POST",
|
||||
search="type:" + transaction_type,
|
||||
number=number,
|
||||
type=transaction_type,
|
||||
account_id=account_id,
|
||||
category_id=category_id,
|
||||
@@ -116,8 +150,28 @@ class Client(object):
|
||||
reference=reference,
|
||||
currency_code=currency_code,
|
||||
currency_rate=currency_rate,
|
||||
amount=amount,
|
||||
amount=EnsurePositiveInteger(amount),
|
||||
description=description,
|
||||
**params
|
||||
)
|
||||
return data
|
||||
|
||||
def create_transfer(self,
|
||||
from_account_id=None, # Account ID to create transfer from
|
||||
to_account_id=None, # Account ID to create transfer to
|
||||
transferred_at=None, # Date of expense/transfer or income
|
||||
payment_method="Bank Transfer", # Payment method
|
||||
amount=None, # Amount received/paid
|
||||
**params # Any additional parameters
|
||||
):
|
||||
|
||||
data = self.call(endpoint="transfers",
|
||||
method="POST",
|
||||
from_account_id=from_account_id,
|
||||
to_account_id=to_account_id,
|
||||
transferred_at=transferred_at,
|
||||
payment_method=payment_method,
|
||||
amount=EnsurePositiveInteger(amount),
|
||||
**params
|
||||
)
|
||||
return data
|
||||
|
||||
@@ -26,3 +26,11 @@ class AccountNotFound(Error):
|
||||
Args:
|
||||
Error (_type_): _description_
|
||||
"""
|
||||
|
||||
class ContactNotFound(Error):
|
||||
"""
|
||||
Account not found
|
||||
|
||||
Args:
|
||||
Error (_type_): _description_
|
||||
"""
|
||||
@@ -7,3 +7,6 @@ def RemoveFromString(items, string):
|
||||
for item in items:
|
||||
string = string.replace(item, '')
|
||||
return string
|
||||
|
||||
def EnsurePositiveInteger(number):
|
||||
return float(number) if float(number) > 0 else (float(number) * -1)
|
||||
17
data/CreateTransferSuccess.json
Normal file
17
data/CreateTransferSuccess.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"data":{
|
||||
"id":2,
|
||||
"company_id":1,
|
||||
"from_account":"Some Account",
|
||||
"from_account_id":1,
|
||||
"to_account":"Some Account New",
|
||||
"to_account_id":2,
|
||||
"paid_at":"2022-05-16T11:57:51+12:00",
|
||||
"amount":100,
|
||||
"amount_formatted":"$100.00",
|
||||
"currency_code":"NZD",
|
||||
"created_by":1,
|
||||
"created_at":"2022-05-16T11:57:51+12:00",
|
||||
"updated_at":"2022-05-16T11:57:51+12:00"
|
||||
}
|
||||
}
|
||||
@@ -11,24 +11,24 @@ from requests.auth import HTTPBasicAuth
|
||||
class TestAPI:
|
||||
@pytest.fixture()
|
||||
def setUp(self):
|
||||
c = akauntingpy.Client("https://akaunting.guise.net.nz/api",
|
||||
"aaron@guise.net.nz",
|
||||
"L3Tm31N0w",
|
||||
c = akauntingpy.Client("https://someakaunting-url/api",
|
||||
"some-emailaddress@somewhere.com",
|
||||
"aPassWord",
|
||||
1)
|
||||
yield c
|
||||
|
||||
@pytest.fixture()
|
||||
def setUpFailed(self):
|
||||
c = akauntingpy.Client("https://akaunting.guise.net.nz/api",
|
||||
"aaron@guise.net.nz",
|
||||
"L3Tm31N0w1",
|
||||
c = akauntingpy.Client("https://someakaunting-url/api",
|
||||
"some-emailaddress@somewhere.com",
|
||||
"aWrongPassWord",
|
||||
1)
|
||||
yield c
|
||||
|
||||
def test_init(self, setUp):
|
||||
c = setUp
|
||||
assert isinstance(c, akauntingpy.Client)
|
||||
assert c.url == "https://akaunting.guise.net.nz/api"
|
||||
assert c.url == "https://someakaunting-url/api"
|
||||
assert isinstance(c.authentication, HTTPBasicAuth)
|
||||
|
||||
def test_ping_success(self, setUp, requests_mock):
|
||||
@@ -55,16 +55,29 @@ class TestAPI:
|
||||
json=RetrieveJSONFromFile("data/GetAccountsList.json"))
|
||||
data = c.get_accounts(params={'page': 1, 'limit': 200})
|
||||
|
||||
def test_get_account_search(self, setUp, requests_mock):
|
||||
def test_get_account_search_v2(self, setUp, requests_mock):
|
||||
c = setUp
|
||||
requests_mock.get(c.url + "/accounts?search=number%3A38-9011-0510023-03¶ms=page¶ms=limit&company_id=1",
|
||||
json=RetrieveJSONFromFile("data/GetAccountsSearch.json"))
|
||||
data = c.get_accounts(search="number:38-9011-0510023-03", params={'page': 1, 'limit': 200})
|
||||
requests_mock.get(c.url + "/accounts?search=number%3A00-0000-0000000-00¶ms=page¶ms=limit&company_id=1",
|
||||
json=RetrieveJSONFromFile("data/v2/GetAccountsSearch.json"))
|
||||
data = c.get_accounts(search="number:00-0000-0000000-00", params={'page': 1, 'limit': 200})
|
||||
|
||||
def test_get_account_search_not_found(self, setUp, requests_mock):
|
||||
def test_get_account_search_v3(self, setUp, requests_mock):
|
||||
c = setUp
|
||||
requests_mock.get(c.url + "/accounts?search=number%3A00-0000-0000000-00¶ms=page¶ms=limit&company_id=1",
|
||||
json=RetrieveJSONFromFile("data/v3/GetAccountsSearch.json"))
|
||||
data = c.get_accounts(search="number:00-0000-0000000-00", params={'page': 1, 'limit': 200})
|
||||
|
||||
def test_get_account_search_not_found_v2(self, setUp, requests_mock):
|
||||
c = setUp
|
||||
requests_mock.get(c.url + "/accounts?search=number%3Aarandomvalue&company_id=1",
|
||||
json=RetrieveJSONFromFile("data/GetAccountsSearchNotFound.json"))
|
||||
json=RetrieveJSONFromFile("data/v2/GetAccountsSearchNotFound.json"))
|
||||
with pytest.raises(AccountNotFound):
|
||||
data = c.get_accounts(search="number:arandomvalue")
|
||||
|
||||
def test_get_account_search_not_found_v3(self, setUp, requests_mock):
|
||||
c = setUp
|
||||
requests_mock.get(c.url + "/accounts?search=number%3Aarandomvalue&company_id=1",
|
||||
json=RetrieveJSONFromFile("data/v3/GetAccountsSearchNotFound.json"))
|
||||
with pytest.raises(AccountNotFound):
|
||||
data = c.get_accounts(search="number:arandomvalue")
|
||||
|
||||
@@ -145,7 +158,7 @@ class TestAPI:
|
||||
|
||||
def test_create_transaction_expense_success(self, setUp, requests_mock):
|
||||
c = setUp
|
||||
requests_mock.post(c.url + "/transactions",
|
||||
requests_mock.post(c.url + "/transactions?search=type%3Aexpense&type=expense&account_id=3&category_id=4&paid_at=2022-05-16&payment_method=Bank+Transfer¤cy_code=NZD¤cy_rate=1&amount=100.0&description=Some+expenditures&company_id=1",
|
||||
json=RetrieveJSONFromFile("data/CreateTransactionExpenseSuccess.json"),
|
||||
status_code=201)
|
||||
data = c.create_transaction(transaction_type="expense",
|
||||
@@ -158,3 +171,19 @@ class TestAPI:
|
||||
category_id="4",
|
||||
description="Some expenditures"
|
||||
)
|
||||
|
||||
|
||||
def test_create_transfer_success(self, setUp, requests_mock):
|
||||
c = setUp
|
||||
requests_mock.post(c.url + "/transfers",
|
||||
json=RetrieveJSONFromFile("data/CreateTransferSuccess.json"),
|
||||
status_code=201)
|
||||
data = c.create_transfer( amount=100.00,
|
||||
account_id=3,
|
||||
paid_at="2022-05-16",
|
||||
currency_rate=1,
|
||||
currency_code="NZD",
|
||||
payment_method="Bank Transfer",
|
||||
category_id="4",
|
||||
description="Some expenditures"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user