Commit fbc26353 authored by Trond Aasan's avatar Trond Aasan
Browse files

CIM-27 Refactor

Use CimEndpoints in CimClient constructor instead of delegating arguments
parent 4b4dec6b
......@@ -3,12 +3,13 @@
Python client for accessing the CIM-API.
from cim_client import CimClient
from cim_client import CimClient, CimEndpoints
from cim_client.models import Person, PersonList
c = CimClient(url='',
tokens={'api_key': {'X-Gravitee-API-Key': 'c-d-a-b'},})
c = CimClient(
tokens={'api_key': {'X-Gravitee-API-Key': 'c-d-a-b'},})
upd_schema = c.get_update_person_schema()
del_schema = c.get_delete_person_schema()
from .client import CimClient
from .client import CimClient, CimEndpoints
from .version import get_distribution
__all__ = ['CimClient']
__all__ = ['CimClient', 'CimEndpoints']
__version__ = get_distribution().version
......@@ -84,9 +84,7 @@ class CimClient(object):
def __init__(self,
url: str,
update_person_url: Union[str, None] = None,
delete_person_url: Union[str, None] = None,
endpoints: Union[CimEndpoints, dict],
tokens: dict = None,
headers: Union[None, dict] = None,
return_objects: bool = True,
......@@ -95,25 +93,17 @@ class CimClient(object):
CIM API client.
:param str url: Base API URL
:param str update_person_url: Relative URL to update person endpoint
:param str delete_person_url: Relative URL to delete person endpoint
:param CimEndpoints endpoints: API endpoints
:param dict tokens: Tokens for the different APIs
:param dict headers: Append extra headers to all requests
:param bool return_objects: Return objects instead of raw JSON
:param bool use_sessions: Keep HTTP connections alive (default True)
:param tuple auth: (username, password) for simple http auth
if update_person_url and delete_person_url:
self.urls = CimEndpoints(url, update_person_url, delete_person_url)
elif update_person_url:
self.urls = CimEndpoints(url, update_person_url)
elif delete_person_url:
self.urls = CimEndpoints(url, delete_person_url=delete_person_url)
self.urls = CimEndpoints(url)
if isinstance(endpoints, dict):
endpoints = CimEndpoints(**endpoints)
self.endpoints = endpoints
self.tokens = tokens if tokens else {}
self.auth = auth if auth else None
self.headers = merge_dicts(self.default_headers, headers)
......@@ -180,7 +170,7 @@ class CimClient(object):
checks whether the schema has changed lately so you don't update
using an outdated schema.
url = self.urls.get_update_person_schema()
url = self.endpoints.get_update_person_schema()
response = self.get(url,
headers=self.tokens.get('api_key', None))
......@@ -195,7 +185,7 @@ class CimClient(object):
checks whether the schema has changed lately so you don't update
using an outdated schema.
url = self.urls.get_delete_person_schema()
url = self.endpoints.get_delete_person_schema()
response = self.get(url,
headers=self.tokens.get('api_key', None))
......@@ -213,9 +203,9 @@ class CimClient(object):
someone. You will have to compare with what you posted.
if delete:
url = self.urls.delete_person()
url = self.endpoints.delete_person()
url = self.urls.update_person()
url = self.endpoints.update_person()
if type(persondata) is list:
persondata = PersonList(persons=persondata)
"_": "Config example for the cim_client command-line utility",
"client": {
"url": "",
"update_person_url": "/_webservices/?ws=contacts/upsert/1.0",
"delete_person_url": "/_webservices/?ws=contacts/delete/1.0",
"endpoints": {
"url": "",
"update_person_url": "/_webservices/?ws=contacts/upsert/1.0",
"delete_person_url": "/_webservices/?ws=contacts/delete/1.0"
"tokens": {
"api_key": "foooooo-bbbbbb-aaaaaa-rrrrr"
......@@ -25,7 +25,7 @@ def custom_endpoints(baseurl):
def client(baseurl):
return CimClient(baseurl)
return CimClient(CimEndpoints(baseurl))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment