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
|
|
|
|
33
|
|
|
|
34
|
1 |
|
class WebserviceError(Exception): |
|
|
|
|
35
|
1 |
|
pass |
36
|
|
|
|
37
|
|
|
|
38
|
1 |
|
class WebserviceBase(): |
|
|
|
|
39
|
|
|
|
40
|
1 |
|
def __init__(self, env, id_correios, password, cert=False): |
41
|
|
|
''' Webservice initialization. |
42
|
|
|
|
43
|
|
|
Depending on the env get a different wsdl definition. |
44
|
|
|
New Correios SIGEP uses HTTPAuth to do requests. |
45
|
|
|
|
46
|
|
|
Args: |
47
|
|
|
env (str): Environment used to get the wsdl |
48
|
|
|
id_correios (str): IdCorreios given by correios website |
49
|
|
|
password (str): password vinculated to the IdCorreios |
50
|
|
|
''' |
51
|
|
|
|
52
|
|
|
''' Untrusted ssl certificate for homolog envs see more at: |
53
|
|
|
|
54
|
|
|
https://www.ssllabs.com/ssltest/analyze.html?d=apphom.correios.com.br |
55
|
|
|
''' |
|
|
|
|
56
|
1 |
|
if cert is False: |
57
|
1 |
|
verify = False |
58
|
|
|
else: |
59
|
|
|
verify = certifi.where() |
60
|
|
|
|
61
|
1 |
|
t = Transport( |
|
|
|
|
62
|
|
|
verify=verify, |
63
|
|
|
http_auth=(id_correios, password) |
64
|
|
|
) |
65
|
1 |
|
self.client = Client(wsdl=self.get_env(env), transport=t) |
66
|
|
|
|
67
|
1 |
|
def get_env(self, env): |
68
|
|
|
""" Get WSDL Url. |
69
|
|
|
|
70
|
|
|
Must be implemented in order to return the correct WSDL. |
71
|
|
|
|
72
|
|
|
Args: |
73
|
|
|
env (str): Environment used to get the wsdl |
74
|
|
|
|
75
|
|
|
Returns: |
76
|
|
|
str: WSDL Url of the corresponding environment |
77
|
|
|
""" |
78
|
1 |
|
raise NotImplementedError() |
79
|
|
|
|
80
|
1 |
|
def call(self, method, request): |
81
|
|
|
''' Call the webservice method with the validated request content |
82
|
|
|
|
83
|
|
|
Args: |
84
|
|
|
method (str): Name of the method available at the webservice |
85
|
|
|
request (RequestObject): Object containing all the info needed |
86
|
|
|
|
87
|
|
|
Returns: |
88
|
|
|
dict: Result of the service call |
89
|
|
|
''' |
90
|
1 |
|
service_method = getattr(self.client.service, method, None) |
91
|
|
|
|
92
|
1 |
|
try: |
93
|
1 |
|
return service_method(request) |
94
|
1 |
|
except ValueError: |
95
|
1 |
|
raise WebserviceError('The method you are trying to call does not ' |
96
|
|
|
'exists.') |
97
|
|
|
|
98
|
|
|
|
99
|
1 |
|
class EntityBase(dict): |
|
|
|
|
100
|
|
|
|
101
|
1 |
|
def __init__(self, **kargs): |
102
|
1 |
|
super(EntityBase, self).__init__(kargs) |
103
|
1 |
|
self.validate() |
104
|
|
|
|
105
|
1 |
|
try: |
106
|
1 |
|
raise getattr(self, 'error') |
107
|
1 |
|
except AttributeError: |
108
|
1 |
|
pass |
109
|
1 |
|
except Invalid as e: |
|
|
|
|
110
|
1 |
|
raise MultipleInvalid([e]) |
111
|
|
|
|
112
|
1 |
|
def get_schema(self): |
113
|
|
|
""" Returns validation schema |
114
|
|
|
|
115
|
|
|
Returns: |
116
|
|
|
voluptous.Schema: Schema with validation rules |
117
|
|
|
""" |
118
|
|
|
raise NotImplementedError("Every entitiy needs validation against a " + |
119
|
|
|
"Schema.") |
120
|
|
|
|
121
|
1 |
|
def validate(self): |
122
|
|
|
""" Validates content against schema |
123
|
|
|
|
124
|
|
|
In case of failure an error is kept inside the entity object, |
125
|
|
|
to be displayed by the client user if needed. |
126
|
|
|
|
127
|
|
|
Returns: |
128
|
|
|
bool: Validation result |
129
|
|
|
""" |
130
|
1 |
|
schema = self.get_schema() |
131
|
1 |
|
try: |
132
|
1 |
|
super(EntityBase, self).__init__(schema(self)) |
133
|
1 |
|
return True |
134
|
1 |
|
except Exception as e: |
|
|
|
|
135
|
1 |
|
self.error = e |
136
|
|
|
return False |
137
|
|
|
|
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.