From 8e7249b93922fca6d4c525f03d76c65bd740b459 Mon Sep 17 00:00:00 2001
From: Petr Kalashnikov <pka065@it6100016.klientdrift.uib.no>
Date: Fri, 19 Feb 2021 16:30:16 +0100
Subject: [PATCH] Support batch_complete endpoint from setra

---
 example-config.json                        |  1 +
 setra_client/client.py                     | 26 ++++++-
 setra_client/models.py                     |  4 +-
 tests/conftest.py                          |  8 ++-
 tests/fixtures/batch_complete_fixture.json | 79 ++++++++++++++++++++++
 tests/test_endpoints.py                    |  5 ++
 tests/test_models.py                       |  4 ++
 7 files changed, 123 insertions(+), 4 deletions(-)
 create mode 100644 tests/fixtures/batch_complete_fixture.json

diff --git a/example-config.json b/example-config.json
index f9ce496..6ea81f6 100644
--- a/example-config.json
+++ b/example-config.json
@@ -6,6 +6,7 @@
     "transaction_url": "/transaction/",
     "voucher_url": "/voucher/",
     "multi_url": "/addtrans/",
+    "batch_complete_url": "/batch_complete/",
     "headers": {
       "X-Gravitee-Api-Key": "..."
     },
diff --git a/setra_client/client.py b/setra_client/client.py
index 2d4c915..f9f984c 100644
--- a/setra_client/client.py
+++ b/setra_client/client.py
@@ -37,13 +37,15 @@ class SetraEndpoints:
                  batch_url='api/batch/',
                  transaction_url='api/transaction/',
                  voucher_url='api/voucher/',
-                 new_batch_url='api/addtrans/'
+                 new_batch_url='api/addtrans/',
+                 batch_complete_url='api/batch_complete/'
                  ):
         self.baseurl = url
         self.batch_url = batch_url
         self.transaction_url = transaction_url
         self.voucher_url = voucher_url
         self.new_batch_url = new_batch_url
+        self.batch_complete_url = batch_complete_url
 
     """ Get endpoints relative to the SETRA API URL. """
 
@@ -86,6 +88,16 @@ class SetraEndpoints:
     def post_new_batch(self):
         return urllib.parse.urljoin(self.baseurl, self.new_batch_url)
 
+    def batch_complete(self, batch_id: str = None):
+        """
+        URL for Batch endpoint
+        """
+        if batch_id is None:
+            return urllib.parse.urljoin(self.baseurl, self.batch_complete_url)
+        else:
+            return urllib.parse.urljoin(self.baseurl,
+                                        '/'.join((self.batch_complete_url, batch_id)))
+
 
 class SetraClient(object):
     default_headers = {
@@ -209,6 +221,18 @@ class SetraClient(object):
                              return_response=return_response)
         return response
 
+    def get_batch_complete(self, batch_id: int = None):
+        """
+        GETs one or all batches from SETRA
+        """
+        if batch_id is not None:
+            batch_id = str(batch_id)
+
+        url = self.urls.batch_complete(batch_id)
+
+        response = self.get(url)
+        return response.json()
+
 
 def get_client(config_dict):
     """
diff --git a/setra_client/models.py b/setra_client/models.py
index a121b2c..c710baf 100644
--- a/setra_client/models.py
+++ b/setra_client/models.py
@@ -35,7 +35,7 @@ class BaseModel(pydantic.BaseModel):
 class Transaction(BaseModel):
     account: str
     amount: float
-    transdate: Optional[datetime.date]
+    transdate: Optional[datetime.datetime]
     curamount: Optional[float]
     currency: Optional[str]
     description: str
@@ -53,7 +53,7 @@ class Transaction(BaseModel):
 
 
 class Voucher(BaseModel):
-    voucherdate: Optional[datetime.date]
+    voucherdate: Optional[datetime.datetime]
     exref: Optional[str]
     voucherno: int
     transactions: typing.List[Transaction]
diff --git a/tests/conftest.py b/tests/conftest.py
index 8a225c0..6added8 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -33,7 +33,8 @@ def custom_endpoints(baseurl):
                           '/custom/batch/',
                           '/custom/transaction/',
                           '/custom/voucher/',
-                          '/custom/addtrans/'
+                          '/custom/addtrans/',
+                          '/custom/batch_complete/'
                           )
 
 
@@ -80,3 +81,8 @@ def batch_with_voucher_fixture():
 @pytest.fixture
 def batch_fail_fixture():
     return load_json_file('batch_fail_fixture.json')
+
+
+@pytest.fixture
+def batch_complete_fixture():
+    return load_json_file('batch_complete_fixture.json')
diff --git a/tests/fixtures/batch_complete_fixture.json b/tests/fixtures/batch_complete_fixture.json
new file mode 100644
index 0000000..e49a6f7
--- /dev/null
+++ b/tests/fixtures/batch_complete_fixture.json
@@ -0,0 +1,79 @@
+{
+    "created": "2020-01-01T00:00:00Z",
+    "batchid": "252",
+    "period": 201902,
+    "interface": "HB",
+    "client": "72",
+    "vouchertype": "3",
+    "batch_validated_ok_date": null,
+    "batch_rejected_code": "",
+    "sent_date": null,
+    "http_response_content": "",
+    "http_response_code": null,
+    "orderno": null,
+    "polling_statuscode": "1",
+    "polling_statuscode_date": null,
+    "getresult_date": null,
+    "getresult_statuscode": "1",
+    "getresult_logg": "",
+    "getresult_report": "",
+    "vouchers": [
+        {
+            "id": 252,
+            "batch_id": 252,
+            "created": "2020-01-01T00:00:00Z",
+            "voucherdate": "2020-01-01T00:00:00Z",
+            "exref": "",
+            "voucherno": 252,
+            "voucherno_ubw": null,
+            "voucherno_ubw_wflow": null,
+            "vouchertype": "HB",
+            "transactions": [
+                {
+                    "id": 3,
+                    "voucher_id": 252,
+                    "created": "2020-01-01T00:00:00Z",
+                    "account": "72",
+                    "amount": 50.0,
+                    "transdate": "2020-01-01T00:00:00Z",
+                    "curamount": 50.0,
+                    "currency": "NOK",
+                    "description": "50 NOK into 72",
+                    "dim1": "",
+                    "dim2": "",
+                    "dim3": "",
+                    "dim4": "",
+                    "dim5": "",
+                    "dim6": "",
+                    "dim7": "",
+                    "sequenceno": 1,
+                    "taxcode": "MVA",
+                    "transtype": "GL",
+                    "extinvref": ""
+                },
+                {
+                    "id": 4,
+                    "voucher_id": 252,
+                    "created": "2020-01-01T00:00:00Z",
+                    "account": "72",
+                    "amount": 50.0,
+                    "transdate": "2020-01-01T00:00:00Z",
+                    "curamount": 50.0,
+                    "currency": "NOK",
+                    "description": "50 NOK into 72",
+                    "dim1": "",
+                    "dim2": "",
+                    "dim3": "",
+                    "dim4": "",
+                    "dim5": "",
+                    "dim6": "",
+                    "dim7": "",
+                    "sequenceno": 1,
+                    "taxcode": "MVA",
+                    "transtype": "GL",
+                    "extinvref": ""
+                }
+            ]
+        }
+    ]
+}
\ No newline at end of file
diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py
index 349cee7..8923c22 100644
--- a/tests/test_endpoints.py
+++ b/tests/test_endpoints.py
@@ -35,3 +35,8 @@ def test_init_transaction_without_value(baseurl):
     endpoints = SetraEndpoints(baseurl)
     assert endpoints.transaction() == baseurl + "/api/transaction/"
 
+
+def test_init_batch_complete_without_value(baseurl):
+    endpoints = SetraEndpoints(baseurl)
+    assert endpoints.batch_complete() == baseurl + "/api/batch_complete/"
+
diff --git a/tests/test_models.py b/tests/test_models.py
index aa0e46e..8f9997c 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -8,6 +8,10 @@ def test_batch(batch_fixture):
     assert Batch(**batch_fixture)
 
 
+def test_batch_complete(batch_complete_fixture):
+    assert Batch(**batch_complete_fixture)
+
+
 def test_voucher(voucher_fixture):
     assert Voucher(**voucher_fixture)
 
-- 
GitLab