Spaces:
Sleeping
Sleeping
File size: 14,806 Bytes
62fe6d4 | 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | /**
* Composant APIDocumentation pour AfriDataHub
* Created by BlackBenAI Team - AfriDataHub Platform
*/
import { useState, useEffect } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import { Button } from '@/components/ui/button'
import {
Code,
Copy,
RefreshCw,
Terminal,
Globe,
Lock,
Check,
ExternalLink,
BookOpen,
Cpu,
Database
} from 'lucide-react'
import { API_URL } from '../config'
const APIDocumentation = () => {
const [token, setToken] = useState('')
const [loading, setLoading] = useState(false)
const [copied, setCopied] = useState(false)
const [activeTab, setActiveTab] = useState('python')
useEffect(() => {
fetchToken()
}, [])
const fetchToken = async () => {
try {
const storedToken = localStorage.getItem('token')
const response = await fetch(`${API_URL}auth/token/`, {
headers: {
'Authorization': `Token ${storedToken}`
}
})
const data = await response.json()
if (response.ok) {
setToken(data.token)
}
} catch (error) {
console.error('Erreur lors de la récupération du token:', error)
}
}
const regenerateToken = async () => {
if (!confirm('Êtes-vous sûr de vouloir régénérer votre token ? L\'ancien token ne fonctionnera plus.')) return
try {
setLoading(true)
const storedToken = localStorage.getItem('token')
const response = await fetch(`${API_URL}auth/token/`, {
method: 'POST',
headers: {
'Authorization': `Token ${storedToken}`
}
})
const data = await response.json()
if (response.ok) {
setToken(data.token)
}
} catch (error) {
console.error('Erreur lors de la régénération du token:', error)
} finally {
setLoading(false)
}
}
const copyToClipboard = (text) => {
navigator.clipboard.writeText(text)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
}
const endpoints = [
{
method: 'GET',
path: '/api/datasets/',
description: 'Liste tous les datasets publics disponibles.',
params: [
{ name: 'domain', type: 'string', desc: 'Filtrer par domaine (agriculture, health, etc.)' },
{ name: 'search', type: 'string', desc: 'Recherche textuelle dans le titre ou la description' }
]
},
{
method: 'GET',
path: '/api/datasets/{slug}/data/',
description: 'Récupère les points de données pour un dataset spécifique.',
params: [
{ name: 'country', type: 'string', desc: 'Code pays ISO (ex: BJ, NG)' },
{ name: 'limit', type: 'number', desc: 'Nombre maximum de résultats (défaut: 100)' }
]
},
{
method: 'GET',
path: '/api/analytics/comprehensive/',
description: 'Récupère des analyses consolidées et des statistiques.',
params: [
{ name: 'dataset_id', type: 'string', desc: 'ID ou Slug du dataset pour filtrer l\'analyse' }
]
}
]
const codeExamples = {
python: `import requests
url = "${API_URL}datasets/"
headers = {
"Authorization": "Token ${token || 'VOTRE_TOKEN_ICI'}"
}
response = requests.get(url, headers=headers)
data = response.json()
print(f"Nombre de datasets: {len(data['results'])}")`,
javascript: `const fetchDatasets = async () => {
const response = await fetch('${API_URL}datasets/', {
headers: {
'Authorization': 'Token ${token || 'VOTRE_TOKEN_ICI'}'
}
});
const data = await response.json();
console.log(data);
};`,
curl: `curl -X GET ${API_URL}datasets/ \\
-H "Authorization: Token ${token || 'VOTRE_TOKEN_ICI'}"`
}
return (
<div className="space-y-10 pb-20">
{/* En-tête */}
<motion.div
initial={{ opacity: 0, y: -20 }}
animate={{ opacity: 1, y: 0 }}
className="flex flex-col md:flex-row md:items-end md:justify-between gap-6"
>
<div>
<h1 className="text-4xl font-black text-foreground tracking-tight">API <span className="text-gradient">Developer Hub</span></h1>
<p className="text-muted-foreground font-medium mt-2 text-lg">
Intégrez les données d'AfriDataHub directement dans vos applications et flux de travail.
</p>
</div>
<div className="flex items-center space-x-3">
<Button variant="outline" className="rounded-2xl border-primary/20 hover:bg-primary/5 font-bold">
<BookOpen className="h-4 w-4 mr-2" />
Documentation Complète
</Button>
</div>
</motion.div>
{/* Section Token */}
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
className="glass rounded-[3rem] p-10 relative overflow-hidden"
>
<div className="absolute top-0 right-0 p-8 opacity-5 pointer-events-none">
<Lock className="w-48 h-48 text-primary" />
</div>
<div className="relative z-10">
<div className="flex items-center mb-8">
<div className="w-12 h-12 rounded-2xl bg-primary/10 flex items-center justify-center mr-4">
<Cpu className="h-6 w-6 text-primary" />
</div>
<div>
<h2 className="text-2xl font-black text-foreground">Votre Clé API</h2>
<p className="text-muted-foreground font-medium">Utilisez ce token pour authentifier vos requêtes.</p>
</div>
</div>
<div className="flex flex-col md:flex-row items-center gap-4">
<div className="flex-1 w-full relative group">
<div className="absolute inset-y-0 left-4 flex items-center pointer-events-none">
<Terminal className="h-5 w-5 text-muted-foreground" />
</div>
<input
type="text"
readOnly
value={token || 'Chargement...'}
className="w-full pl-12 pr-12 py-5 bg-background/50 border border-border rounded-2xl font-mono text-sm focus:ring-4 focus:ring-primary/10 outline-none transition-all"
/>
<button
onClick={() => copyToClipboard(token)}
className="absolute inset-y-0 right-4 flex items-center text-muted-foreground hover:text-primary transition-colors"
>
{copied ? <Check className="h-5 w-5 text-green-500" /> : <Copy className="h-5 w-5" />}
</button>
</div>
<Button
onClick={regenerateToken}
disabled={loading}
className="w-full md:w-auto px-8 py-7 rounded-2xl bg-primary text-white font-bold shadow-lg hover:shadow-primary/20 transition-all hover:scale-105"
>
<RefreshCw className={`h-5 w-5 mr-2 ${loading ? 'animate-spin' : ''}`} />
Régénérer le Token
</Button>
</div>
<div className="mt-6 p-4 bg-yellow-500/10 dark:bg-yellow-500/5 border border-yellow-500/20 dark:border-yellow-500/10 rounded-2xl flex items-start">
<div className="p-2 bg-yellow-500/20 rounded-lg mr-4 mt-1">
<Lock className="h-4 w-4 text-yellow-500" />
</div>
<p className="text-sm text-yellow-600 dark:text-yellow-400 font-medium">
Gardez votre token secret. Ne le partagez jamais dans des environnements publics ou côté client (frontend).
</p>
</div>
</div>
</motion.div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-10">
{/* Documentation des Endpoints */}
<motion.div
initial={{ opacity: 0, x: -20 }}
animate={{ opacity: 1, x: 0 }}
className="space-y-6"
>
<div className="flex items-center mb-4">
<Globe className="h-6 w-6 text-primary mr-3" />
<h2 className="text-2xl font-black text-foreground">Endpoints Publics</h2>
</div>
{endpoints.map((ep, i) => (
<div key={i} className="glass rounded-3xl p-6 border border-border hover:border-primary/30 transition-all group">
<div className="flex items-center justify-between mb-4">
<div className="flex items-center space-x-3">
<span className="px-3 py-1 bg-primary text-white text-xs font-black rounded-lg uppercase tracking-widest">
{ep.method}
</span>
<code className="text-sm font-bold text-foreground/80">{ep.path}</code>
</div>
<ExternalLink className="h-4 w-4 text-muted-foreground group-hover:text-primary transition-colors" />
</div>
<p className="text-muted-foreground text-sm font-medium mb-4">{ep.description}</p>
<div className="space-y-2">
<p className="text-[10px] font-black uppercase tracking-widest text-muted-foreground/60">Paramètres</p>
{ep.params.map((param, pi) => (
<div key={pi} className="flex items-start text-xs">
<span className="font-bold text-primary mr-2">{param.name}</span>
<span className="text-muted-foreground/60 mr-2">({param.type})</span>
<span className="text-foreground/70">{param.desc}</span>
</div>
))}
</div>
</div>
))}
</motion.div>
{/* Exemples de Code */}
<motion.div
initial={{ opacity: 0, x: 20 }}
animate={{ opacity: 1, x: 0 }}
className="space-y-6"
>
<div className="flex items-center mb-4">
<Code className="h-6 w-6 text-secondary mr-3" />
<h2 className="text-2xl font-black text-foreground">Exemples d'Intégration</h2>
</div>
<div className="glass rounded-[2rem] md:rounded-[3rem] p-6 md:p-10 border border-border relative overflow-hidden shadow-2xl">
<div className="flex border-b border-border bg-muted/30">
{['python', 'javascript', 'curl'].map((tab) => (
<button
key={tab}
onClick={() => setActiveTab(tab)}
className={`px-8 py-4 text-xs font-black uppercase tracking-widest transition-all ${activeTab === tab
? 'bg-primary text-white'
: 'text-muted-foreground hover:bg-white/10 dark:hover:bg-black/20'
}`}
>
{tab}
</button>
))}
</div>
<div className="p-8 bg-black/40 relative group">
<button
onClick={() => copyToClipboard(codeExamples[activeTab])}
className="absolute top-6 right-6 p-2 bg-white/10 hover:bg-white/20 rounded-xl text-white transition-all opacity-0 group-hover:opacity-100"
>
{copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
</button>
<pre className="font-mono text-sm text-blue-100 overflow-x-auto">
<code>{codeExamples[activeTab]}</code>
</pre>
</div>
<div className="p-8 bg-muted/20 border-t border-border">
<div className="flex items-center justify-between">
<div className="flex items-center">
<div className="w-10 h-10 rounded-xl bg-secondary/10 flex items-center justify-center mr-4">
<Database className="h-5 w-5 text-secondary" />
</div>
<div>
<p className="text-sm font-bold text-foreground">SDK AfriDataHub</p>
<p className="text-xs text-muted-foreground">Bientôt disponible sur PyPI et NPM.</p>
</div>
</div>
<Button variant="ghost" className="text-xs font-bold text-secondary hover:bg-secondary/10">
S'inscrire à la Beta
</Button>
</div>
</div>
</div>
</motion.div>
</div>
</div>
)
}
export default APIDocumentation
|