Statistics
| Branch: | Tag: | Revision:

root / docs / developers / extending-clients-api.rst @ b213b9a0

History | View | Annotate | Download (3 kB)

1
Extending kamaki.clients
2
========================
3

    
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.
5

    
6
Connection
7
----------
8

    
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).
10

    
11
How to build a client
12
---------------------
13

    
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.
15

    
16
.. code-block:: python
17
    
18
    #  ${KAMAKI_PATH}/kamaki/clients/mynewclient.py
19

    
20
    from kamaki.clients import Client, ClientError
21

    
22
    class MyNewClient(Client):
23
        """MyNewClient Description Here"""
24

    
25
        def my_first_method(self, **args):
26
            """Method description"""
27
            try:
28
                ...
29
                method code
30
                ...
31
            except SomeKnownException as e1:
32
                raise ClientError('MyError: %s' % e1)
33
            except SomeOtherException as e2:
34
                raise ClientError('MyError: %s' % e2)
35

    
36
        def my_second_method(self, **params):
37
            """Method description"""
38
            ...
39

    
40
Custom clients can use a set of convenience methods for easy HTTP requests
41

    
42
.. code-block:: python
43

    
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)
51

    
52
How to use your client
53
----------------------
54

    
55
External applications must instantiate a MyNewClient object.
56

    
57
.. code-block:: python
58

    
59
    from kamaki.clients import ClientError
60
    from kamaki.clients.mynewclient import MyNewClient
61

    
62
    ...
63
    try:
64
        cl = MyNewClient(args)
65
        cl.my_first_method(other_args)
66
    except ClientError as cle:
67
        print('Client Error: %s' % cle)
68
    ...
69

    
70
Concurrency control
71
-------------------
72

    
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
74

    
75
.. code-block:: python
76

    
77
    from threading import enumerate
78
    from kamaki.clients import SilentEvent
79
    ...
80

    
81
    class MyNewClient(Client):
82
        ...
83

    
84
        def _single_threaded_method(self, **args):
85
            ...
86
            request code
87
            ...
88

    
89
        def multithread_method(self):
90
            thread_list = []
91
            self._init_thread_limit()
92
            while some_condition or thread_list:
93
                ...
94
                event = SilentEvent(self._single_threaded_method, **args)
95
                event.start()
96
                thread_list.append(event)
97
                thread_list = self._watch_thread_limit(thread_list)