Files
gauvainboiche 0d071d2843 Semaine 11
2026-07-11 21:38:00 +02:00

306 lines
6.6 KiB
Markdown

# Exo 3.4 "Consommer l'API" - BOICHÉ Gauvain
## Requêtes type
### Lister
```
# Liste complète
curl -i http://127.0.0.1:5000/api/equipments
# Equipement précis
curl -i http://127.0.0.1:5000/api/equipments/1
# Equipement inexistant
curl -i http://127.0.0.1:5000/api/equipments/9999
# Sortie formatée
curl -s http://127.0.0.1:5000/api/equipments | python -m json.tool
# Compter les équipements
curl -s http://127.0.0.1:5000/api/equipments | python -c "import json,sys; print(len(json.load(sys.stdin)))"
# Filtrage par champ
curl -s http://127.0.0.1:5000/api/equipments | jq '.[] | select(.is_active==true) | {id, hostname}'
```
### Créer
```
# Normal
curl -i -X POST http://127.0.0.1:5000/api/equipments \
-H "Content-Type: application/json" \
-d '{"hostname":"curl-test-01","type":"router","ip_address":"10.0.50.1","is_active":true,"latency":12.5}'
# Mal formaté
curl -i -X POST http://127.0.0.1:5000/api/equipments \
-H "Content-Type: application/json" \
-d '{hostname: "sans-guillemets"}'
# Avec mentions obligatoires manquantes
curl -i -X POST http://127.0.0.1:5000/api/equipments \
-H "Content-Type: application/json" \
-d '{"type":"router"}'
```
### Patch partiel (actif <-> inactif)
```
curl -i -X PATCH http://127.0.0.1:5000/api/equipments/1/toggle
```
### Mise à jour
```
curl -i -X PUT http://127.0.0.1:5000/api/equipments/1 \
-H "Content-Type: application/json" \
-d '{"hostname":"serveur-prod-01","type":"server","ip_address":"10.0.10.1","is_active":true,"latency":8.3}'
```
### Supprimer
```
# Autorisé
curl -i -X DELETE http://127.0.0.1:5000/api/equipments/1
# Interdit
curl -i -X DELETE http://127.0.0.1:5000/api/equipments
```
## Réponses
### Lister
```
> curl -i http://127.0.0.1:5000/api/equipments
HTTP/1.1 200 OK
Server: Werkzeug/3.1.8 Python/3.14.6
Date: Wed, 08 Jul 2026 12:58:59 GMT
Content-Type: application/json
Content-Length: 6850
Connection: close
[
{
"hostname": "bird-dawn-peas-031",
"id": 1,
"interfaces": [
{
"id": 1,
"port": 25,
"service": "SMTP"
},
{
"id": 2,
"port": 21,
"service": "FTP"
},
{
"id": 3,
"port": 80,
"service": "HTTP"
},
{
"id": 4,
"port": 21,
"service": "FTP"
}
],
"ip_address": "242.48.158.201",
"is_active": true,
"latency": 33.6,
"type": "Router"
}, [...]
> curl -i http://127.0.0.1:5000/api/equipments/12
HTTP/1.1 200 OK
Server: Werkzeug/3.1.8 Python/3.14.6
Date: Wed, 08 Jul 2026 13:21:47 GMT
Content-Type: application/json
Content-Length: 314
Connection: close
{
"hostname": "barn-lamps-bottle-151",
"id": 12,
"interfaces": [
{
"id": 42,
"port": 53,
"service": "DNS"
},
{
"id": 43,
"port": 139,
"service": "NetBIOS"
}
],
"ip_address": "76.143.32.122",
"is_active": true,
"latency": 42.1,
"type": "Server"
}
> curl -i http://127.0.0.1:5000/api/equipments/1267
HTTP/1.1 404 NOT FOUND
Server: Werkzeug/3.1.8 Python/3.14.6
Date: Wed, 08 Jul 2026 13:22:01 GMT
Content-Type: application/json
Content-Length: 46
Connection: close
{
"error": "\u00c9quipement introuvable."
}
> curl -s http://127.0.0.1:5000/api/equipments | python -c "import json,sys; print(len(json.load(sys.stdin)))"
15
```
### Créer
```
> curl -i -X POST http://127.0.0.1:5000/api/equipments -H "Content-Type: application/json" -d '{"hostname":"curl-test-01","type":"Router","ip_address":"10.0.50.1","is_active":true,"latency":12.5}'
HTTP/1.1 201 CREATED
Server: Werkzeug/3.1.8 Python/3.14.6
Date: Wed, 08 Jul 2026 13:23:15 GMT
Content-Type: application/json
Content-Length: 154
Connection: close
{
"hostname": "curl-test-01",
"id": 16,
"interfaces": [],
"ip_address": "10.0.50.1",
"is_active": true,
"latency": 12.5,
"type": "Router"
}
> curl -i -X POST http://127.0.0.1:5000/api/equipments -H "Content-Type: application/json" -d '{"hostname":"curl-test-01","type":"router","ip_address":"10.0.50.1","is_active":true,"latency":12.5}'
HTTP/1.1 400 BAD REQUEST
Server: Werkzeug/3.1.8 Python/3.14.6
Date: Wed, 08 Jul 2026 13:23:04 GMT
Content-Type: application/json
Content-Length: 41
Connection: close
{
"error": "Type invalide : router."
}
> curl -i -X POST http://127.0.0.1:5000/api/equipments -H "Content-Type: application/json" -d '{"type":"Router"}'
HTTP/1.1 400 BAD REQUEST
Server: Werkzeug/3.1.8 Python/3.14.6
Date: Wed, 08 Jul 2026 13:23:58 GMT
Content-Type: application/json
Content-Length: 34
Connection: close
{
"error": "Hostname requis."
}
```
### Patch partiel (actif <-> inactif)
```
> curl -i -X PATCH http://127.0.0.1:5000/api/equipments/2/toggle
HTTP/1.1 200 OK
Server: Werkzeug/3.1.8 Python/3.14.6
Date: Wed, 08 Jul 2026 13:46:11 GMT
Content-Type: application/json
Content-Length: 387
Connection: close
{
"hostname": "gulf-manage-order-022",
"id": 2,
"interfaces": [
{
"id": 3,
"port": 21,
"service": "FTP"
},
{
"id": 4,
"port": 139,
"service": "NetBIOS"
},
{
"id": 5,
"port": 143,
"service": "IMAP"
}
],
"ip_address": "183.139.112.112",
"is_active": false,
"latency": 28.1,
"type": "Database"
}
```
### Mise à jour
```
> curl -i -X PUT http://127.0.0.1:5000/api/equipments/1 -H "Content-Type: application/json" -d '{"hostname":"serveur-prod-01","type":"Server","ip_address":"10.0.10.1","is_active":true,"latency":8.3}'
HTTP/1.1 200 OK
Server: Werkzeug/3.1.8 Python/3.14.6
Date: Wed, 08 Jul 2026 13:49:06 GMT
Content-Type: application/json
Content-Length: 297
Connection: close
{
"hostname": "serveur-prod-01",
"id": 1,
"interfaces": [
{
"id": 1,
"port": 53,
"service": "DNS"
},
{
"id": 2,
"port": 3389,
"service": "RDP"
}
],
"ip_address": "10.0.10.1",
"is_active": true,
"latency": 8.3,
"type": "Server"
}
```
### Supprimer
```
> curl -i -X DELETE http://127.0.0.1:5000/api/equipments/1
HTTP/1.1 200 OK
Server: Werkzeug/3.1.8 Python/3.14.6
Date: Wed, 08 Jul 2026 13:49:49 GMT
Content-Type: application/json
Content-Length: 80
Connection: close
{
"message": "\u00c9quipement \u00ab serveur-prod-01 \u00bb supprim\u00e9."
}
> curl -i -X DELETE http://127.0.0.1:5000/api/equipments
HTTP/1.1 405 METHOD NOT ALLOWED
Server: Werkzeug/3.1.8 Python/3.14.6
Date: Wed, 08 Jul 2026 13:50:04 GMT
Content-Type: text/html; charset=utf-8
Allow: OPTIONS, POST, HEAD, GET
Content-Length: 153
Connection: close
<!doctype html>
<html lang=en>
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
```