1 Extending kamaki.clients
2 ========================
4 By default, kamaki clients are REST clients (they manage HTTP requests and responses to communicate with services). This is achieved by importing the connection module, which is an httplib wrapper.
9 The connection module features an error handling and logging system, a lazy response mechanism, a pooling mechanism as well as concurrency control for thread-demanding client functions (e.g. store upload).
14 All service clients consist of a subclass of the Client class and implement separate client functionalities as member methods. There is also an error class to raise exceptions that can be handled by kamaki interfaces.
16 .. code-block:: python
18 # ${KAMAKI_PATH}/kamaki/clients/mynewclient.py
20 from kamaki.clients import Client, ClientError
22 class MyNewClient(Client):
23 """MyNewClient Description Here"""
25 def my_first_method(self, **args):
26 """Method description"""
31 except SomeKnownException as e1:
32 raise ClientError('MyError: %s' % e1)
33 except SomeOtherException as e2:
34 raise ClientError('MyError: %s' % e2)
36 def my_second_method(self, **params):
37 """Method description"""
40 Custom clients can use a set of convenience methods for easy HTTP requests
42 .. code-block:: python
44 def get(self, path, **kwargs)
45 def head(self, path, **kwargs)
46 def post(self, path, **kwargs)
47 def put(self, path, **kwargs)
48 def delete(self, path, **kwargs)
49 def copy(self, path, **kwargs)
50 def move(self, path, **kwargs)
52 How to use your client
53 ----------------------
55 External applications must instantiate a MyNewClient object.
57 .. code-block:: python
59 from kamaki.clients import ClientError
60 from kamaki.clients.mynewclient import MyNewClient
64 cl = MyNewClient(args)
65 cl.my_first_method(other_args)
66 except ClientError as cle:
67 print('Client Error: %s' % cle)
73 Kamaki clients may handle multiple requests at once, using threads. In that case, users might implement their own thread handling mechanism, use an external solution or take advantage of the mechanism featured in kamaki.clients
75 .. code-block:: python
77 from threading import enumerate
78 from kamaki.clients import SilentEvent
81 class MyNewClient(Client):
84 def _single_threaded_method(self, **args):
89 def multithread_method(self):
91 self._init_thread_limit()
92 while some_condition or thread_list:
94 event = SilentEvent(self._single_threaded_method, **args)
96 thread_list.append(event)
97 thread_list = self._watch_thread_limit(thread_list)