-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
42 lines (31 loc) · 1.2 KB
/
api.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
import json
from flask import Flask, Response, request
from cliente import Cliente
maico = Cliente('Maico', 232323232)
daniel = Cliente('Daniel', 757575757)
clientes = [maico.__dict__, daniel.__dict__]
app = Flask(__name__)
@app.get('/clientes')
def obterClientes():
return Response(json.dumps(clientes), mimetype="application/json")
@app.get('/clientes/<idCliente>')
def obterClientePorId(idCliente):
for cliente in clientes:
if cliente['id'] == idCliente:
return Response(json.dumps(cliente), mimetype="application/json")
return Response(None, status=404)
@app.post('/clientes')
def criarCliente():
for cliente in clientes:
if cliente['documento'] == request.get_json()['documento']:
erro = {
'mensagem': f'Cliente com documento {request.get_json()["documento"]} ja cadastrado.'
}
return Response(json.dumps(erro), mimetype="application/json", status=400)
novoCliente = Cliente(
request.get_json()['nome'],
request.get_json()['documento']
)
clientes.append(novoCliente.__dict__)
return Response(json.dumps(novoCliente.__dict__), mimetype="application/json", status=201)
app.run(host="0.0.0.0", port=5000)