/
service.py
85 lines (68 loc) · 2.86 KB
/
service.py
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
from api import API
import json
class SPService:
"""
Conecta com o webservice do Detran-SP.
"""
def __init__(self, **kwargs):
"""
Construtor.
"""
self.params = kwargs
# Converte a placa para o modelo padrão, caso esteja no modelo Mercosul.
plate_char = self.params["license_plate"][4]
if plate_char.isalpha():
with open("mercosul_plate_convertion.json", "r") as mp:
mp = json.loads(mp.read())
converted_num = mp[plate_char]
self.params["license_plate"] = f"{self.params['license_plate'][:4]}{converted_num}{self.params['license_plate'][5:]}"
def get_json_response(self, method):
"""
Pega a resposta da requisição em json.
"""
api = API(self.params["license_plate"], self.params["renavam"], method)
return api.fetch()
def debt_search(self):
"""
Pega os débitos de acordo com a opção passada.
"""
if self.params["debt_option"] == "ticket":
response_json = self.get_json_response("ConsultaMultas")
elif self.params["debt_option"] == "ipva":
response_json = self.get_json_response("ConsultaIPVA")
elif self.params["debt_option"] == "dpvat":
response_json = self.get_json_response("ConsultaDPVAT")
elif self.params["debt_option"] == "licensing":
response_json = self.get_json_response("ConsultaLicenciamento")
response_json = {
"Licenciamento": {
"Exercicio": response_json.get("Exercicio"),
"TaxaLicenciamento": response_json.get("TaxaLicenciamento"),
}
}
elif self.params["debt_option"] == "all":
ticket_json = self.get_json_response("ConsultaMultas")
ipva_json = self.get_json_response("ConsultaIPVA")
dpvat_json = self.get_json_response("ConsultaDPVAT")
licensing_json = self.get_json_response("ConsultaLicenciamento")
response_json = {
"Multas": ticket_json.get("Multas"),
"IPVAs": ipva_json.get("IPVAs"),
"DPVATs": dpvat_json.get("DPVATs"),
"Licenciamento": {
"Exercicio": licensing_json.get("Exercicio"),
"TaxaLicenciamento": licensing_json.get("TaxaLicenciamento"),
},
}
else:
raise Exception("opção inválida")
debts = {
"IPVAs": response_json.get("IPVAs") or {},
"DPVATs": response_json.get("DPVATs") or {},
"Multas": response_json.get("Multas") or {},
"Licenciamento": response_json.get("Licenciamento") or {},
}
for debt in debts:
if debts[debt] == {}:
debts[debt] = None
return debts