forked from vintasoftware/tapioca-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapters.py
More file actions
117 lines (79 loc) · 3.35 KB
/
adapters.py
File metadata and controls
117 lines (79 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# coding: utf-8
import json
from .tapioca import TapiocaInstantiator
from .exceptions import (
ResponseProcessException, ClientError, ServerError)
from .serializers import SimpleSerializer
def generate_wrapper_from_adapter(adapter_class):
return TapiocaInstantiator(adapter_class)
class TapiocaAdapter(object):
serializer_class = SimpleSerializer
def __init__(self, serializer_class=None, *args, **kwargs):
if serializer_class:
self.serializer = serializer_class()
else:
self.serializer = self.get_serializer()
def _get_to_native_method(self, method_name, value):
if not self.serializer:
raise NotImplementedError("This client does not have a serializer")
def to_native_wrapper(**kwargs):
return self._value_to_native(method_name, value, **kwargs)
return to_native_wrapper
def _value_to_native(self, method_name, value, **kwargs):
return self.serializer.deserialize(method_name, value, **kwargs)
def get_serializer(self):
if self.serializer_class:
return self.serializer_class()
def get_api_root(self, api_params):
return self.api_root
def fill_resource_template_url(self, template, params):
return template.format(**params)
def get_request_kwargs(self, api_params, *args, **kwargs):
serialized = self.serialize_data(kwargs.get('data'))
kwargs.update({
'data': self.format_data_to_request(serialized),
})
return kwargs
def process_response(self, response):
if str(response.status_code).startswith('5'):
raise ResponseProcessException(ServerError, None)
data = self.response_to_native(response)
if str(response.status_code).startswith('4'):
raise ResponseProcessException(ClientError, data)
return data
def serialize_data(self, data):
if self.serializer:
return self.serializer.serialize(data)
return data
def format_data_to_request(self, data):
raise NotImplementedError()
def response_to_native(self, response):
raise NotImplementedError()
def get_iterator_list(self, response_data):
raise NotImplementedError()
def get_iterator_next_request_kwargs(self, iterator_request_kwargs,
response_data, response):
raise NotImplementedError()
def is_authentication_expired(self, exception, *args, **kwargs):
return False
def refresh_authentication(self, api_params, *args, **kwargs):
raise NotImplementedError()
class FormAdapterMixin(object):
def format_data_to_request(self, data):
return data
def response_to_native(self, response):
return {'text': response.text}
class JSONAdapterMixin(object):
def get_request_kwargs(self, api_params, *args, **kwargs):
arguments = super(JSONAdapterMixin, self).get_request_kwargs(
api_params, *args, **kwargs)
if 'headers' not in arguments:
arguments['headers'] = {}
arguments['headers']['Content-Type'] = 'application/json'
return arguments
def format_data_to_request(self, data):
if data:
return json.dumps(data)
def response_to_native(self, response):
if response.content.strip():
return response.json()