Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions openprocurement_client/api_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from requests import Session
from requests.auth import HTTPBasicAuth as BasicAuth
from simplejson import loads
from retrying import retry

logger = logging.getLogger(__name__)
IGNORE_PARAMS = ('uri', 'path')
Expand Down Expand Up @@ -154,6 +155,23 @@ def _get_resource_item(self, url, headers=None):
return munchify(loads(response_item.text))
raise InvalidResponse(response_item)

@retry(stop_max_attempt_number=5)
def _get_resource_items(self, params=None, feed='changes'):
_params = (params or {}).copy()
_params['feed'] = feed
self._update_params(_params)
response = self.request('GET',
self.prefix_path,
params_dict=self.params)
if response.status_code == 200:
resource_items_list = munchify(loads(response.text))
self._update_params(resource_items_list.next_page)
return resource_items_list.data
elif response.status_code == 404:
del self.params['offset']

raise InvalidResponse(response)

def _patch_resource_item(self, url, payload, headers=None):
_headers = self.headers.copy()
_headers.update(headers or {})
Expand Down Expand Up @@ -248,3 +266,13 @@ def patch_credentials(self, id, access_token):
payload=None,
headers={'X-Access-Token': access_token}
)

def create_resource_item(self, resource_item):
return self._create_resource_item(self.prefix_path, resource_item)

def patch_resource_item(self, resource_item):
return self._patch_resource_item(
'{}/{}'.format(self.prefix_path, resource_item['data']['id']),
payload=resource_item,
headers={'X-Access-Token': self._get_access_token(resource_item)}
)
59 changes: 59 additions & 0 deletions openprocurement_client/registry_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import logging

from .api_base_client import APIBaseClient
from .exceptions import InvalidResponse

from iso8601 import parse_date
from munch import munchify
from retrying import retry
from simplejson import loads


class LotsClient(APIBaseClient):
""" Client for Openregistry Lots """

api_version = '0.1'

def __init__(self,
key='',
resource='lots',
host_url=None,
api_version=None,
params=None,
ds_client=None,
user_agent=None):
super(LotsClient, self).__init__(
key=key, resource=resource, host_url=host_url, params=params,
api_version=api_version, ds_client=ds_client,
user_agent=user_agent)

def get_lot(self, lot_id, headers=None):
return self.get_resource_item(lot_id, headers=headers)

def get_lots(self, params=None, feed='changes'):
return self._get_resource_items(params=params, feed=feed)


class AssetsClient(APIBaseClient):
""" Client for Openregistry Assets """

api_version = '0.1'

def __init__(self,
key='',
resource='assets',
host_url=None,
api_version=None,
params=None,
ds_client=None,
user_agent=None):
super(AssetsClient, self).__init__(
key=key, resource=resource, host_url=host_url, params=params,
api_version=api_version, ds_client=ds_client,
user_agent=user_agent)

def get_asset(self, asset_id, headers=None):
return self.get_resource_item(asset_id, headers=headers)

def get_assets(self, params=None, feed='changes'):
return self._get_resource_items(params=params, feed=feed)
96 changes: 55 additions & 41 deletions openprocurement_client/tests/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
from simplejson import dumps, load
from openprocurement_client.document_service_client \
import DocumentServiceClient
from openprocurement_client.tests.data_dict import TEST_TENDER_KEYS, \
TEST_PLAN_KEYS, TEST_CONTRACT_KEYS
from openprocurement_client.tests.data_dict import (
TEST_TENDER_KEYS,
TEST_PLAN_KEYS,
TEST_CONTRACT_KEYS,
TEST_ASSET_KEYS,
TEST_LOT_KEYS
)
import magic
import os

Expand All @@ -23,10 +28,13 @@
CONTRACTS_PATH = API_PATH.format('contracts')
SPORE_PATH = API_PATH.format('spore')
DOWNLOAD_URL_EXTENSION = 'some_key_etc'
RESOURCE_DICT = \
{'tender': {'sublink': 'tenders', 'data': TEST_TENDER_KEYS},
'contract': {'sublink': 'contracts', 'data': TEST_CONTRACT_KEYS},
'plan': {'sublink': 'plans', 'data': TEST_PLAN_KEYS}}
RESOURCE_DICT = {
'tender': {'sublink': 'tenders', 'data': TEST_TENDER_KEYS},
'contract': {'sublink': 'contracts', 'data': TEST_CONTRACT_KEYS},
'plan': {'sublink': 'plans', 'data': TEST_PLAN_KEYS},
'asset': {'sublink': 'assets', 'data': TEST_ASSET_KEYS},
'lot': {'sublink': 'lots', 'data': TEST_LOT_KEYS}
}


def resource_filter(resource_name):
Expand Down Expand Up @@ -296,41 +304,47 @@ def contract_change_patch(contract_id, change_id):


routes_dict = {
"spore": (SPORE_PATH, 'HEAD', spore),
"offset_error": (API_PATH.format('<resource_name:resource_filter:tender>'), 'GET', offset_error),
"tenders": (API_PATH.format('<resource_name:resource_filter:tender>'), 'GET', resource_page_get),
"tender_create": (TENDERS_PATH, 'POST', resource_create),
"tender": (API_PATH.format('<resource_name:resource_filter:tender>') + '/<resource_id>', 'GET', resource_page),
"tender_patch": (API_PATH.format('<resource_name:resource_filter:tender>') + "/<resource_id>", 'PATCH', resource_patch),
"tender_document_create": (TENDERS_PATH + "/<tender_id>/documents", 'POST', tender_document_create),
"tender_subpage": (TENDERS_PATH + "/<tender_id>/<subpage_name>", 'GET', tender_subpage),
"tender_subpage_item_create": (TENDERS_PATH + "/<resource_id>/<subpage_name>", 'POST', resource_subpage_item_create),
"tender_award_documents": (TENDERS_PATH + "/<tender_id>/awards/<award_id>/documents", 'GET', tender_award_documents),
"tender_qualification_documents": (TENDERS_PATH + "/<tender_id>/qualifications/<qualification_id>/documents", 'GET', tender_qualification_documents),
"tender_subpage_document_create": (TENDERS_PATH + "/<tender_id>/<subpage_name>/<subpage_id>/<document_type>", 'POST', tender_subpage_document_create),
"tender_subpage_document_update": (TENDERS_PATH + "/<tender_id>/<subpage_name>/<subpage_id>/<document_type>/<document_id>", 'PUT', tender_subpage_document_update),
"tender_subpage_document_patch": (TENDERS_PATH + "/<tender_id>/<subpage_name>/<subpage_id>/<document_type>/<document_id>", 'PATCH', tender_subpage_document_patch),
"tender_subpage_item": (TENDERS_PATH + "/<tender_id>/<subpage_name>/<subpage_id>", 'GET', tender_subpage_item),
"tender_subpage_item_patch": (API_PATH.format('<resource_name:resource_filter:tender>') + '/<obj_id>/<subpage_name>/<subpage_id>', 'PATCH', object_subpage_item_patch),
"tender_subpage_item_delete": (TENDERS_PATH + "/<tender_id>/<subpage_name>/<subpage_id>", 'DELETE', tender_subpage_item_delete),
"tender_patch_credentials": (API_PATH.format('<resource_name:resource_filter:tender>') + '/<resource_id>/credentials', 'PATCH', patch_credentials),
"redirect": ('/redirect/<filename:path>', 'GET', get_file),
"download": ('/download/<filename:path>', 'GET', download_file),
"plans": (API_PATH.format('<resource_name:resource_filter:plan>'), 'GET', resource_page_get),
"plan_create": (PLANS_PATH, 'POST', resource_create),
"plan_patch": (API_PATH.format('<resource_name:resource_filter:plan>') + "/<resource_id>", 'PATCH', resource_patch),
"plan": (API_PATH.format('<resource_name:resource_filter:plan>') + '/<resource_id>', 'GET', resource_page),
"plan_offset_error": (API_PATH.format('<resource_name:resource_filter:plan>'), 'GET', offset_error),
"contracts": (API_PATH.format('<resource_name:resource_filter:contract>'), 'GET', resource_page_get),
"contract_create": (CONTRACTS_PATH, 'POST', resource_create),
"contract_document_create": (CONTRACTS_PATH + "/<contract_id>/documents", 'POST', contract_document_create),
"contract": (API_PATH.format('<resource_name:resource_filter:contract>') + '/<resource_id>', 'GET', resource_page),
"contract_subpage_item_create": (CONTRACTS_PATH + "/<resource_id>/<subpage_name>", 'POST', resource_subpage_item_create),
"contract_subpage_item_patch": (API_PATH.format('<resource_name:resource_filter:contract>') + '/<obj_id>/<subpage_name>/<subpage_id>', 'PATCH', object_subpage_item_patch),
"contract_change_patch": (API_PATH.format('contracts') + '/<contract_id>/changes/<change_id>', 'PATCH', contract_change_patch),
"contract_patch": (API_PATH.format('<resource_name:resource_filter:contract>') + "/<resource_id>", 'PATCH', resource_patch),
"contract_patch_credentials": (API_PATH.format('<resource_name:resource_filter:contract>') + '/<resource_id>/credentials', 'PATCH', patch_credentials),
}
"spore": (SPORE_PATH, 'HEAD', spore),
"offset_error": (API_PATH.format('<resource_name:resource_filter:tender>'), 'GET', offset_error),
"tenders": (API_PATH.format('<resource_name:resource_filter:tender>'), 'GET', resource_page_get),
"tender_create": (TENDERS_PATH, 'POST', resource_create),
"tender": (API_PATH.format('<resource_name:resource_filter:tender>') + '/<resource_id>', 'GET', resource_page),
"tender_patch": (API_PATH.format('<resource_name:resource_filter:tender>') + "/<resource_id>", 'PATCH', resource_patch),
"tender_document_create": (TENDERS_PATH + "/<tender_id>/documents", 'POST', tender_document_create),
"tender_subpage": (TENDERS_PATH + "/<tender_id>/<subpage_name>", 'GET', tender_subpage),
"tender_subpage_item_create": (TENDERS_PATH + "/<resource_id>/<subpage_name>", 'POST', resource_subpage_item_create),
"tender_award_documents": (TENDERS_PATH + "/<tender_id>/awards/<award_id>/documents", 'GET', tender_award_documents),
"tender_qualification_documents": (TENDERS_PATH + "/<tender_id>/qualifications/<qualification_id>/documents", 'GET', tender_qualification_documents),
"tender_subpage_document_create": (TENDERS_PATH + "/<tender_id>/<subpage_name>/<subpage_id>/<document_type>", 'POST', tender_subpage_document_create),
"tender_subpage_document_update": (TENDERS_PATH + "/<tender_id>/<subpage_name>/<subpage_id>/<document_type>/<document_id>", 'PUT', tender_subpage_document_update),
"tender_subpage_document_patch": (TENDERS_PATH + "/<tender_id>/<subpage_name>/<subpage_id>/<document_type>/<document_id>", 'PATCH', tender_subpage_document_patch),
"tender_subpage_item": (TENDERS_PATH + "/<tender_id>/<subpage_name>/<subpage_id>", 'GET', tender_subpage_item),
"tender_subpage_item_patch": (API_PATH.format('<resource_name:resource_filter:tender>') + '/<obj_id>/<subpage_name>/<subpage_id>', 'PATCH', object_subpage_item_patch),
"tender_subpage_item_delete": (TENDERS_PATH + "/<tender_id>/<subpage_name>/<subpage_id>", 'DELETE', tender_subpage_item_delete),
"tender_patch_credentials": (API_PATH.format('<resource_name:resource_filter:tender>') + '/<resource_id>/credentials', 'PATCH', patch_credentials),
"redirect": ('/redirect/<filename:path>', 'GET', get_file),
"download": ('/download/<filename:path>', 'GET', download_file),
"plans": (API_PATH.format('<resource_name:resource_filter:plan>'), 'GET', resource_page_get),
"plan_create": (PLANS_PATH, 'POST', resource_create),
"plan_patch": (API_PATH.format('<resource_name:resource_filter:plan>') + "/<resource_id>", 'PATCH', resource_patch),
"plan": (API_PATH.format('<resource_name:resource_filter:plan>') + '/<resource_id>', 'GET', resource_page),
"plan_offset_error": (API_PATH.format('<resource_name:resource_filter:plan>'), 'GET', offset_error),
"contracts": (API_PATH.format('<resource_name:resource_filter:contract>'), 'GET', resource_page_get),
"contract_create": (CONTRACTS_PATH, 'POST', resource_create),
"contract_document_create": (CONTRACTS_PATH + "/<contract_id>/documents", 'POST', contract_document_create),
"contract": (API_PATH.format('<resource_name:resource_filter:contract>') + '/<resource_id>', 'GET', resource_page),
"contract_subpage_item_create": (CONTRACTS_PATH + "/<resource_id>/<subpage_name>", 'POST', resource_subpage_item_create),
"contract_subpage_item_patch": (API_PATH.format('<resource_name:resource_filter:contract>') + '/<obj_id>/<subpage_name>/<subpage_id>', 'PATCH', object_subpage_item_patch),
"contract_change_patch": (API_PATH.format('contracts') + '/<contract_id>/changes/<change_id>', 'PATCH', contract_change_patch),
"contract_patch": (API_PATH.format('<resource_name:resource_filter:contract>') + "/<resource_id>", 'PATCH', resource_patch),
"contract_patch_credentials": (API_PATH.format('<resource_name:resource_filter:contract>') + '/<resource_id>/credentials', 'PATCH', patch_credentials),
"assets": (API_PATH.format('<resource_name:resource_filter:asset>'), 'GET', resource_page_get),
"asset": (API_PATH.format('<resource_name:resource_filter:asset>') + '/<resource_id>', 'GET', resource_page),
"asset_patch": (API_PATH.format('<resource_name:resource_filter:asset>') + "/<resource_id>", 'PATCH', resource_patch),
"lots": (API_PATH.format('<resource_name:resource_filter:lot>'), 'GET', resource_page_get),
"lot": (API_PATH.format('<resource_name:resource_filter:lot>') + '/<resource_id>', 'GET', resource_page),
"lot_patch": (API_PATH.format('<resource_name:resource_filter:lot>') + "/<resource_id>", 'PATCH', resource_patch),
}


def file_info(file_):
Expand Down
Loading