Exemples Python – API de Santé et de Fitness¶
Python facilite l'interaction avec les API REST, telles que Health Fitness API. Vous trouverez ci-dessous des exemples utilisant plusieurs bibliothèques HTTP Python pour les requêtes synchrones et asynchrones.
1. Utilisation de requests (Synchrones, le plus courant)¶
import requests
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {
"height": 178,
"weight": 82,
"units": "metric",
"lang": "en"
}
headers = {"X-API-Key": API_KEY}
response = requests.get(url, headers=headers, params=params)
data = response.json()
print(data)
Exemple de requête POST :
import requests
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi/post"
payload = {
"height": 178,
"weight": 82,
"units": "metric"
}
headers = {
"X-API-Key": API_KEY,
"Content-Type": "application/json"
}
response = requests.post(url, headers=headers, json=payload)
data = response.json()
print(data)
2. Utilisation de httpx (Synchrones et Asynchrones)¶
Exemple de requête GET synchrone :
import httpx
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
headers = {"X-API-Key": API_KEY}
with httpx.Client() as client:
response = client.get(url, headers=headers, params=params)
data = response.json()
print(data)
Exemple de requête GET asynchrone :
import httpx
import asyncio
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
headers = {"X-API-Key": API_KEY}
async def fetch_bmi():
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers, params=params)
data = response.json()
print(data)
asyncio.run(fetch_bmi())
Exemple POST (asynchrone) :
async def post_bmi():
payload = {"height": 178, "weight": 82, "units": "metric"}
async with httpx.AsyncClient() as client:
response = await client.post(url + "/post", headers=headers, json=payload)
data = response.json()
print(data)
asyncio.run(post_bmi())
3. Utilisation de urllib (Bibliothèque standard)¶
import json
from urllib import request, parse
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
query_string = parse.urlencode(params)
full_url = f"{url}?{query_string}"
req = request.Request(full_url, headers={"X-API-Key": API_KEY})
with request.urlopen(req) as response:
data = json.load(response)
print(data)
Requête POST utilisant urllib :
payload = json.dumps({"height": 178, "weight": 82, "units": "metric"}).encode("utf-8")
req = request.Request(url + "/post", data=payload, method="POST")
req.add_header("Content-Type", "application/json")
req.add_header("X-API-Key", API_KEY)
with request.urlopen(req) as response:
data = json.load(response)
print(data)
4. Utilisation de aiohttp (Asynchrone, populaire pour les frameworks asynchrones)¶
import aiohttp
import asyncio
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
headers = {"X-API-Key": API_KEY}
async def fetch_bmi():
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers, params=params) as resp:
data = await resp.json()
print(data)
asyncio.run(fetch_bmi())
Exemple POST (aiohttp) :
async def post_bmi():
payload = {"height": 178, "weight": 82, "units": "metric"}
async with aiohttp.ClientSession() as session:
async with session.post(url + "/post", headers=headers, json=payload) as resp:
data = await resp.json()
print(data)
asyncio.run(post_bmi())
5. Notes et Meilleures Pratiques¶
- Remplacez toujours
YOUR_API_KEYpar votre clé API. - Utilisez le paramètre
langpour prendre en charge plusieurs langues (en,fr,de,es, etc.). - Pour la production, envisagez la gestion des connexions (client
httpx) et les requêtes asynchrones pour améliorer les performances. - Gérez les erreurs de manière appropriée : codes de statut HTTP, délais d'attente et exceptions.
- Les réponses incluent des signaux de risque, des plans d'action et des métadonnées d'entreprise lorsque disponibles.
Prochaines étapes¶
Consultez d'autres exemples spécifiques à chaque langage :
- PHP
- JavaScript
- NodeJs
- Kotlin / Android
- Flutter / Dart
- React Native / Expo
- Bash Shell
- Go / Golang
- Ruby / Rails
Commencez à intégrer l'API Health Fitness dans vos applications Python dès aujourd'hui !
Health Fitness API Alimenter l'infrastructure numérique de santé moderne