diff --git a/openprocurement_client/api_base_client.py b/openprocurement_client/api_base_client.py index 8a3f1e2..f25c15a 100644 --- a/openprocurement_client/api_base_client.py +++ b/openprocurement_client/api_base_client.py @@ -151,7 +151,10 @@ def _get_resource_item(self, url, headers=None): _headers.update(headers or {}) response_item = self.request('GET', url, headers=_headers) if response_item.status_code == 200: - return munchify(loads(response_item.text)) + data = loads(response_item.text) + if "x-revision-n" in response_item.headers: + data["x_revision_n"] = response_item.headers.get("x-revision-n") + return munchify(data) raise InvalidResponse(response_item) def _patch_resource_item(self, url, payload, headers=None): @@ -238,10 +241,16 @@ def upload_document(self, file_, obj, use_ds_client=True, doc_registration=doc_registration ) - def get_resource_item(self, id, headers=None): - return self._get_resource_item('{}/{}'.format(self.prefix_path, id), + def get_resource_item(self, item_id, headers=None): + return self._get_resource_item('{}/{}'.format(self.prefix_path, item_id), headers=headers) + def get_resource_item_historical(self, item_id, revision="", headers=None): + _headers = {"x-revision-n": str(revision)} + if headers: + _headers.update(headers) + return self._get_resource_item('{}/{}/historical'.format(self.prefix_path, item_id), headers=_headers) + def patch_credentials(self, id, access_token): return self._patch_resource_item( '{}/{}/credentials'.format(self.prefix_path, id), diff --git a/openprocurement_client/tests/main.py b/openprocurement_client/tests/main.py index 17c9333..d5dec6c 100644 --- a/openprocurement_client/tests/main.py +++ b/openprocurement_client/tests/main.py @@ -1,12 +1,13 @@ import unittest -from openprocurement_client.tests import tests, tests_sync +from openprocurement_client.tests import tests, tests_sync, tests_api_base_client def suite(): suite = unittest.TestSuite() suite.addTest(tests.suite()) suite.addTest(tests_sync.suite()) + suite.addTest(tests_api_base_client.suite()) return suite diff --git a/openprocurement_client/tests/tests_api_base_client.py b/openprocurement_client/tests/tests_api_base_client.py new file mode 100644 index 0000000..18e088c --- /dev/null +++ b/openprocurement_client/tests/tests_api_base_client.py @@ -0,0 +1,69 @@ +import unittest +import uuid +from mock import MagicMock +from json import dumps +from copy import deepcopy + +from openprocurement_client.api_base_client import APIBaseClient +from openprocurement_client.exceptions import InvalidResponse + + +CLIENT_CONFIG = { + "key": "", + "resource": "tenders", + "host_url": "http://lb.api-sandbox.openprocurement.org/", + "api_version": "2.4" +} + + +class TestAPIBaseClient(unittest.TestCase): + def setUp(self): + self.client = APIBaseClient(**CLIENT_CONFIG) + + def test_get_resource_item_historical(self): + class Response(object): + def __init__(self, status_code, text=None, headers=None): + self.status_code = status_code + self.text = text + self.headers = headers + + revisions_limit = 42 + response_text = { + "id": uuid.uuid4().hex, + "rev": 24 + } + + side_effect = [ + Response(200, dumps(response_text), {"x-revision-n": str(revisions_limit)}), + Response(200, dumps(response_text), {"x-revision-n": str(revisions_limit)}), + Response(200, dumps(response_text), {"x-revision-n": str(revisions_limit - 1)}), + Response(404), + Response(404), + Response(404), + ] + + self.client.request = MagicMock(side_effect=side_effect) + + actual_response = deepcopy(response_text) + actual_response["x_revision_n"] = str(revisions_limit) + item_id = response_text["id"] + self.assertEqual(self.client.get_resource_item_historical(item_id, revision=""), actual_response) + self.assertEqual(self.client.get_resource_item_historical(item_id, revision=revisions_limit), actual_response) + actual_response["x_revision_n"] = str(revisions_limit - 1) + self.assertEqual(self.client.get_resource_item_historical( + item_id, revision=revisions_limit - 1), actual_response) + + for revision in (0, revisions_limit + 1, None): + with self.assertRaises(InvalidResponse) as e: + self.client.get_resource_item_historical(item_id, revision=revision) + self.assertEqual(e.exception.status_code, 404) + + +def suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(TestAPIBaseClient)) + return suite + + +if __name__ == "__main__": + unittest.main(defaultTest='suite') \ No newline at end of file