-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (80 loc) · 2.45 KB
/
main.py
File metadata and controls
99 lines (80 loc) · 2.45 KB
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
92
93
94
95
96
97
98
99
from __future__ import annotations
import time
import uuid
import random
from typing import Dict, Tuple
from fastapi import FastAPI, HTTPException, Query
from fastapi.responses import Response
app = FastAPI(title="REST Challenge (compat app.erikmd)")
# token -> (secret_number, expires_at_epoch_seconds)
TOKENS: Dict[str, Tuple[int, float]] = {}
TOKEN_TTL_SECONDS = 60
def _now() -> float:
return time.time()
def _purge_expired() -> None:
now = _now()
expired = [t for t, (_, exp) in TOKENS.items() if exp <= now]
for t in expired:
TOKENS.pop(t, None)
@app.get("/init")
def init(
quad: str = Query(
...,
pattern=r"^[A-Z]{2,4}$",
description="Chaîne [A-Z]{2,4}",
examples=["NAME"],
)
):
"""
GET /init?quad=NAME
Retourne du XML avec un token valable 60s.
"""
_purge_expired()
token = uuid.uuid4().hex
# Nombre à deviner : volontairement simple pour un TP (1..2^31-1)
secret = random.randint(1, 2_147_483_647)
expires_at = _now() + TOKEN_TTL_SECONDS
TOKENS[token] = (secret, expires_at)
# XML minimal "pédagogique" : root + token + validForSeconds
xml = f"""<?xml version="1.0" encoding="UTF-8"?>
<init>
<quad>{quad}</quad>
<token>{token}</token>
<validForSeconds>{TOKEN_TTL_SECONDS}</validForSeconds>
</init>
"""
return Response(content=xml, media_type="text/xml")
@app.get("/try")
def try_guess(
token: str = Query(..., description="Token renvoyé par /init"),
guess: int = Query(
...,
ge=1,
le=2_147_483_647,
description="Entier positif sur 32 bits",
),
):
"""
GET /try?token=...&guess=...
Retourne du JSON indiquant si guess est trop petit/grand/trouvé.
"""
_purge_expired()
if token not in TOKENS:
# 404 est pratique pédagogiquement (token inconnu/expiré)
raise HTTPException(status_code=404, detail="Unknown or expired token")
secret, expires_at = TOKENS[token]
if expires_at <= _now():
TOKENS.pop(token, None)
raise HTTPException(status_code=404, detail="Unknown or expired token")
if guess < secret:
return {"token": token, "guess": guess, "result": "TOO_SMALL"}
if guess > secret:
return {"token": token, "guess": guess, "result": "TOO_BIG"}
# trouvé
TOKENS.pop(token, None)
return {
"token": token,
"guess": guess,
"result": "FOUND",
"message": "Bravo, nombre trouvé !",
}