1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import unicode_literals, print_function |
3
|
|
|
|
4
|
|
|
from abc import ABCMeta, abstractproperty, abstractmethod |
5
|
|
|
from six import with_metaclass |
6
|
|
|
|
7
|
|
|
from enum import Enum as BaseEnum |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class Enum(BaseEnum): |
11
|
|
|
@classmethod |
12
|
|
|
def get_by_name(cls, name): |
13
|
|
|
return getattr(cls, name, None) |
14
|
|
|
|
15
|
|
|
@classmethod |
16
|
|
|
def get_by_value(cls, value): |
17
|
|
|
return cls(value) |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class APIArgumentSerializer(with_metaclass(ABCMeta, object)): # pragma: no cover |
21
|
|
|
@abstractmethod |
22
|
|
|
def to_api(self, python_value): |
23
|
|
|
pass |
24
|
|
|
|
25
|
|
|
@abstractmethod |
26
|
|
|
def to_python(self, api_value): |
27
|
|
|
pass |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class APIArgument(with_metaclass(ABCMeta, object)): |
31
|
|
|
def __init__(self, name, description=None, required=True): |
32
|
|
|
self.name = name |
33
|
|
|
self.description = description |
34
|
|
|
self.required = required |
35
|
|
|
|
36
|
|
|
@abstractproperty |
37
|
|
|
def serializer_class(self): # pragma: no cover |
38
|
|
|
pass |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
class API(with_metaclass(ABCMeta, object)): |
42
|
|
|
@abstractproperty |
43
|
|
|
def method(self): # pragma: no cover |
44
|
|
|
""" |
45
|
|
|
Returns the exact name of the XMLRPC API method to call |
46
|
|
|
:rtype: str |
47
|
|
|
:return: Name of the XMLRPC API method to call |
48
|
|
|
""" |
49
|
|
|
|
50
|
|
|
@abstractproperty |
51
|
|
|
def arguments(self): # pragma: no cover |
52
|
|
|
""" |
53
|
|
|
Returns an (ordered) list of APIArgument objects. |
54
|
|
|
:rtype: Iterable |
55
|
|
|
:return: An iterable containing the arguments |
56
|
|
|
""" |
57
|
|
|
|
58
|
|
|
@property |
59
|
|
|
def authenticate(self): |
60
|
|
|
""" |
61
|
|
|
Defines whether or not we should authenticate when calling this API |
62
|
|
|
:return: |
63
|
|
|
""" |
64
|
|
|
return True |
65
|
|
|
|
66
|
|
|
def transform_arguments(self, **kwargs): |
67
|
|
|
""" |
68
|
|
|
Handler method to transform an argument before processing |
69
|
|
|
:param kwargs: Named argument dictionary |
70
|
|
|
:type kwargs: dict |
71
|
|
|
:rtype: dict |
72
|
|
|
:return: |
73
|
|
|
""" |
74
|
|
|
return kwargs |
75
|
|
|
|
76
|
|
|
def validate_response(self, api, arguments, response): |
77
|
|
|
""" |
78
|
|
|
Handler to validate the API response. Can be used to raise an exception to indicate failure. If it does not |
79
|
|
|
raise an exception, the pipeline will continue with the 'transform_response' method. |
80
|
|
|
:param api: The api object that has been used for the call |
81
|
|
|
:type api: py:class:API |
82
|
|
|
:param arguments: The dictionary containing the arguments that have ben used to perform the call |
83
|
|
|
:type arguments: dict |
84
|
|
|
:param response: |
85
|
|
|
:type response: object |
86
|
|
|
:rtype: object |
87
|
|
|
:return: |
88
|
|
|
""" |
89
|
|
|
|
90
|
|
|
def transform_response(self, api, arguments, response): |
91
|
|
|
""" |
92
|
|
|
Handler method to process the response. The output of this method will be returned as the output of the API |
93
|
|
|
call. |
94
|
|
|
:param api: The api object that has been used for the call |
95
|
|
|
:type api: py:class:API |
96
|
|
|
:param arguments: The dictionary containing the arguments that have ben used to perform the call |
97
|
|
|
:type arguments: dict |
98
|
|
|
:param response: |
99
|
|
|
:type response: object |
100
|
|
|
:rtype: object |
101
|
|
|
:return: |
102
|
|
|
""" |
103
|
|
|
return response |
104
|
|
|
|