Skip to content

Instantly share code, notes, and snippets.

@urigoren
Last active August 25, 2020 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urigoren/682c82e706063497f351ce2059a2426d to your computer and use it in GitHub Desktop.
Save urigoren/682c82e706063497f351ce2059a2426d to your computer and use it in GitHub Desktop.
"""
A python wrapper for the icount.co.il api
https://www.icount.co.il/api-v3/
"""
import json
from urllib import request, parse
def post(url, data):
req = request.Request(url, data=parse.urlencode(data).encode())
resp = request.urlopen(req)
ret = json.loads(resp.read().decode('utf-8'))
if not ret["status"]:
raise Exception(ret["error_description"])
return ret
class iCountSession:
def __init__(self, company, username, password):
self.sid = None
self.login(company, username, password)
def __del__(self):
self.logout()
def __enter__(self):
assert self.sid is not None
return self
def __exit__(self, exc_type, exc_value, traceback):
self.logout()
def login(self, company, username, password):
self.logout()
endpoint = "https://api.icount.co.il/api/v3.php/auth/login"
self.sid = post(endpoint, {"user": username, "cid": company, "pass": password})["sid"]
def logout(self):
if not self.sid:
return
endpoint = "https://api.icount.co.il/api/v3.php/auth/logout"
post(endpoint, {"sid": self.sid})
self.sid = None
def expense_lookup(self, start_date, end_date):
endpoint = "https://api.icount.co.il/api/v3.php/expense/search"
return post(endpoint, {"sid": self.sid, "start_date": start_date, "end_date": end_date})["results_list"]
def expense_types(self):
endpoint = "https://api.icount.co.il/api/v3.php/expense/types"
return post(endpoint, {"sid": self.sid})["expense_types"]
def expense_doctypes(self):
endpoint = "https://api.icount.co.il/api/v3.php/expense/doctypes"
return post(endpoint, {"sid": self.sid})["expense_doctypes"]
def expense_create(self, supplier_id, expense_type_id, expense_doctype, expense_docnum, expense_sum, date):
endpoint = "https://api.icount.co.il/api/v3.php/expense/create"
return post(endpoint, {"sid": self.sid, "supplier_id": supplier_id, "expense_type_id": expense_type_id,
"expense_doctype": expense_doctype, "expense_docnum": expense_docnum,
"expense_sum": expense_sum,
"invoice_date": date, "expense_date": date, "expense_paid_date": date,
"expense_paid": 1})["status"]
def supplier_info(self, supplier_id):
endpoint = "https://api.icount.co.il/api/v3.php/supplier/info"
return post(endpoint, {"sid": self.sid, "supplier_id": supplier_id})["supplier_info"]
def supplier_list(self):
endpoint = "https://api.icount.co.il/api/v3.php/supplier/get_list"
return post(endpoint, {"sid": self.sid, "detail_level": 10})["suppliers"]
def supplier_add(self, supplier_name, vat_id):
endpoint = "https://api.icount.co.il/api/v3.php/supplier/add"
return post(endpoint, {"sid": self.sid, "supplier_name": supplier_name, "vat_id": vat_id})["supplier_id"]
def supplier_update(self, supplier_id, **kwargs):
endpoint = "https://api.icount.co.il/api/v3.php/supplier/update"
return post(endpoint, dict(kwargs, sid=self.sid, supplier_id=supplier_id))["status"]
def client_list(self):
endpoint = "https://api.icount.co.il/api/v3.php/client/get_list"
return post(endpoint, {"sid": self.sid, "detail_level": 10})["clients"]
def doc_types(self):
endpoint = "https://api.icount.co.il/api/v3.php/doc/types"
return list(post(endpoint, {"sid": self.sid})["doctypes"].keys())
def doc_search(self, **kwargs):
endpoint = "https://api.icount.co.il/api/v3.php/doc/search"
return post(endpoint, dict(kwargs, sid=self.sid))["results_list"]
if __name__ == "__main__":
with iCountSession("company", "username", "password") as ic:
print(ic.expense_lookup("20190101", "20200101"))
print(ic.supplier_list())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment