1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
# ############################################################################# |
3
|
|
|
# |
4
|
|
|
# The MIT License (MIT) |
5
|
|
|
# |
6
|
|
|
# Copyright (c) 2016 Trocafone |
7
|
|
|
# |
8
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
# in the Software without restriction, including without limitation the rights |
11
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
# furnished to do so, subject to the following conditions: |
14
|
|
|
# |
15
|
|
|
# The above copyright notice and this permission notice shall be included in |
16
|
|
|
# all copies or substantial portions of the Software. |
17
|
|
|
# |
18
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
24
|
|
|
# SOFTWARE. |
25
|
|
|
# |
26
|
|
|
############################################################################### |
27
|
|
|
|
28
|
1 |
|
from zeep import Client |
29
|
1 |
|
from zeep.transports import Transport |
30
|
1 |
|
from voluptuous import Invalid, MultipleInvalid |
31
|
1 |
|
import certifi |
32
|
1 |
|
import logging.config |
33
|
|
|
|
34
|
|
|
|
35
|
1 |
|
class WebserviceError(Exception): |
|
|
|
|
36
|
1 |
|
pass |
37
|
|
|
|
38
|
|
|
|
39
|
1 |
|
class WebserviceBase(): |
|
|
|
|
40
|
|
|
|
41
|
1 |
|
def __init__(self, env, id_correios, password, |
|
|
|
|
42
|
|
|
cert=False, log_config=None): |
43
|
|
|
''' Webservice initialization. |
44
|
|
|
|
45
|
|
|
Depending on the env get a different wsdl definition. |
46
|
|
|
New Correios SIGEP uses HTTPAuth to do requests. |
47
|
|
|
|
48
|
|
|
Args: |
49
|
|
|
env (str): Environment used to get the wsdl |
50
|
|
|
id_correios (str): IdCorreios given by correios website |
51
|
|
|
password (str): password vinculated to the IdCorreios |
52
|
|
|
log_config (dict): Dictionary configurations of logging |
53
|
|
|
''' |
54
|
|
|
|
55
|
|
|
''' Untrusted ssl certificate for homolog envs see more at: |
56
|
|
|
|
57
|
|
|
https://www.ssllabs.com/ssltest/analyze.html?d=apphom.correios.com.br |
58
|
|
|
''' |
|
|
|
|
59
|
1 |
|
if cert is False: |
60
|
1 |
|
verify = False |
61
|
|
|
else: |
62
|
|
|
verify = certifi.where() |
63
|
|
|
|
64
|
1 |
|
if log_config is not None and isinstance(log_config, dict): |
65
|
|
|
""" Example config from zeep documentation: |
66
|
|
|
|
67
|
|
|
{ |
68
|
|
|
'version': 1, |
69
|
|
|
'formatters': { |
70
|
|
|
'verbose': { |
71
|
|
|
'format': '%(name)s: %(message)s' |
72
|
|
|
} |
73
|
|
|
}, |
74
|
|
|
'handlers': { |
75
|
|
|
'console': { |
76
|
|
|
'level': 'DEBUG', |
77
|
|
|
'class': 'logging.StreamHandler', |
78
|
|
|
'formatter': 'verbose', |
79
|
|
|
}, |
80
|
|
|
}, |
81
|
|
|
'loggers': { |
82
|
|
|
'zeep.transports': { |
83
|
|
|
'level': 'DEBUG', |
84
|
|
|
'propagate': True, |
85
|
|
|
'handlers': ['console'], |
86
|
|
|
}, |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
""" |
|
|
|
|
90
|
|
|
logging.config.dictConfig(log_config) |
91
|
|
|
|
92
|
1 |
|
t = Transport( |
|
|
|
|
93
|
|
|
verify=verify, |
94
|
|
|
http_auth=(id_correios, password) |
95
|
|
|
) |
96
|
1 |
|
self.client = Client(wsdl=self.get_env(env), transport=t) |
97
|
|
|
|
98
|
1 |
|
def get_env(self, env): |
99
|
|
|
""" Get WSDL Url. |
100
|
|
|
|
101
|
|
|
Must be implemented in order to return the correct WSDL. |
102
|
|
|
|
103
|
|
|
Args: |
104
|
|
|
env (str): Environment used to get the wsdl |
105
|
|
|
|
106
|
|
|
Returns: |
107
|
|
|
str: WSDL Url of the corresponding environment |
108
|
|
|
""" |
109
|
1 |
|
raise NotImplementedError() |
110
|
|
|
|
111
|
1 |
|
def call(self, method, request): |
112
|
|
|
''' Call the webservice method with the validated request content |
113
|
|
|
|
114
|
|
|
Args: |
115
|
|
|
method (str): Name of the method available at the webservice |
116
|
|
|
request (RequestObject): Object containing all the info needed |
117
|
|
|
|
118
|
|
|
Returns: |
119
|
|
|
dict: Result of the service call |
120
|
|
|
''' |
121
|
1 |
|
service_method = getattr(self.client.service, method, None) |
122
|
|
|
|
123
|
1 |
|
req_type = 'Request' + method[0].upper() + method[1:] |
124
|
1 |
|
req_instance = request.__class__.__name__ |
125
|
|
|
|
126
|
1 |
|
if req_type != req_instance: |
127
|
1 |
|
raise WebserviceError( |
128
|
|
|
'Request must be an instance of {0}, given {1}'.format( |
129
|
|
|
req_type, |
130
|
|
|
req_instance |
131
|
|
|
) |
|
|
|
|
132
|
|
|
) |
133
|
|
|
|
134
|
1 |
|
import sys |
135
|
1 |
|
if sys.version_info <= (3, 0): |
136
|
|
|
reload(sys) |
137
|
|
|
sys.setdefaultencoding('utf-8') |
|
|
|
|
138
|
|
|
|
139
|
1 |
|
try: |
140
|
1 |
|
return service_method(**request) |
|
|
|
|
141
|
|
|
except ValueError as e: |
142
|
|
|
raise WebserviceError(e) |
143
|
|
|
|
144
|
|
|
|
145
|
1 |
|
class EntityBase(dict): |
|
|
|
|
146
|
|
|
|
147
|
1 |
|
def __init__(self, **kargs): |
148
|
1 |
|
super(EntityBase, self).__init__(kargs) |
149
|
1 |
|
self.validate() |
150
|
|
|
|
151
|
1 |
|
try: |
152
|
1 |
|
raise getattr(self, 'error') |
153
|
1 |
|
except AttributeError: |
154
|
1 |
|
pass |
155
|
1 |
|
except Invalid as e: |
156
|
1 |
|
raise MultipleInvalid([e]) |
157
|
|
|
|
158
|
1 |
|
def get_schema(self): |
159
|
|
|
""" Returns validation schema |
160
|
|
|
|
161
|
|
|
Returns: |
162
|
|
|
voluptous.Schema: Schema with validation rules |
163
|
|
|
""" |
164
|
|
|
raise NotImplementedError("Every entitiy needs validation against a " + |
165
|
|
|
"Schema.") |
166
|
|
|
|
167
|
1 |
|
def validate(self): |
168
|
|
|
""" Validates content against schema |
169
|
|
|
|
170
|
|
|
In case of failure an error is kept inside the entity object, |
171
|
|
|
to be displayed by the client user if needed. |
172
|
|
|
|
173
|
|
|
Returns: |
174
|
|
|
bool: Validation result |
175
|
|
|
""" |
176
|
1 |
|
schema = self.get_schema() |
177
|
1 |
|
try: |
178
|
1 |
|
super(EntityBase, self).__init__(schema(self)) |
179
|
1 |
|
return True |
180
|
1 |
|
except Exception as e: |
|
|
|
|
181
|
1 |
|
self.error = e |
182
|
|
|
return False |
183
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.