-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvapi.py
92 lines (79 loc) · 3.05 KB
/
vapi.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
86
87
88
89
90
91
import bottle
import vigo
from database import plugin
from bottle import request, response
from cors import EnableCors
from serializers import json_serial
import json
app = bottle.Bottle()
app.install(EnableCors())
app.install(plugin)
@app.route("/clientes", method="GET")
def listar_clientes(db):
response.content_type = 'application/json'
clientes = vigo.Clientes(db)
return json.dumps(clientes.json(), default=json_serial)
@app.route('/clientes/<id:int>', method='GET')
def obter_cliente(id, db):
response.content_type = 'application/json'
clientes = vigo.Clientes(db)
return json.dumps(clientes.find_by_id(id), default=json_serial)
@app.route("/clientes", method="POST")
def novo_cliente(db):
response.content_type = 'application/json'
clientes = vigo.Clientes(db)
try:
retorno = clientes.insert(request.json)
except vigo.clientes.ENumeroInvalido as e:
response.status = e.code
return {"errors": [{"code": e.code, "description": e.message}]}
return json.dumps(retorno, default=json_serial)
@app.route("/clientes/<numero:int>", method="PUT")
def atualizar_cliente(numero, db):
response.content_type = 'application/json'
clientes = vigo.Clientes(db)
retorno = clientes.update(numero, request.json)
return json.dumps(retorno, default=json_serial)
@app.route('/clientes/<id:int>', method='DELETE')
def remover_cliente(id, db):
clientes = vigo.Clientes(db)
try:
clientes.delete(id)
except vigo.DeleteError as e:
response.status = e.code
return {"errors": [{"code": e.code, "description": e.message}]}
@app.route("/chamados", method="GET")
def listar_atendimentos(db):
response.content_type = 'application/json'
atendimentos = vigo.Atendimentos(db)
return json.dumps(atendimentos.all(), default=json_serial, encoding='latin1')
@app.route("/chamados/<numero>", method="GET")
def chamado_por_numero(numero, db):
response.content_type = 'application/json'
atendimento = vigo.Atendimentos(db)
return json.dumps(atendimento.find_by_numero_os(numero), default=json_serial)
@app.route("/chamados", method="POST")
def inserir_atendimento(db):
response.content_type = 'application/json'
atendimento = vigo.Atendimentos(db)
retorno = atendimento.insert(request.json)
return json.dumps(retorno, default=json_serial)
@app.route("/chamados/<numero>", method="PUT")
def altera_chamado(numero, db):
response.content_type = 'application/json'
atendimento = vigo.Atendimentos(db)
retorno = atendimento.update(numero, request.json)
return json.dumps(retorno, default=json_serial)
@app.route("/chamados/<numero>", method="DELETE")
def remove_chamado(numero, db):
response.content_type = 'application/json'
atendimento = vigo.Atendimentos(db)
try:
retorno = atendimento.delete(numero)
except vigo.DeleteError as e:
response.status = e.code
return {"errors": [{"code": e.code, "description": e.message}]}
return json.dumps(retorno, default=json_serial)
if __name__=="__main__":
bottle.debug(True)
app.run()