File size: 18,984 Bytes
426ef14 |
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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 |
# 📱 راهنمای یکپارچهسازی کلاینت
## نگاه کلی
این راهنما برای توسعهدهندگان Frontend است که میخواهند از API های پروژه استفاده کنند.
---
## 🎯 پشتیبانی از Client Applications
### ✅ پلتفرمهای پشتیبانی شده:
```
✅ Web (JavaScript/TypeScript)
✅ React / Next.js
✅ Vue.js
✅ Angular
✅ Mobile (React Native)
✅ iOS (Swift)
✅ Android (Kotlin/Java)
✅ Desktop (Electron)
✅ Python Scripts
✅ Any HTTP/WebSocket Client
```
---
## 🔌 روشهای اتصال
### 1. REST API (HTTP/HTTPS)
**Base URL:**
```
Development: http://localhost:7860
Production: https://your-domain.com
```
**Headers مورد نیاز:**
```http
Content-Type: application/json
Accept: application/json
Origin: https://your-domain.com (برای CORS)
```
**Headers اختیاری:**
```http
Authorization: Bearer YOUR_TOKEN (برای endpoints محافظت شده)
X-Client-Version: 1.0.0
User-Agent: YourApp/1.0
```
---
### 2. WebSocket (Real-time)
**URLs:**
```
ws://localhost:7860/ws/master
ws://localhost:7860/ws/market_data
ws://localhost:7860/ws/news
wss://your-domain.com/ws/... (برای HTTPS)
```
**Protocol:**
- JSON-based messaging
- Subscribe/Unsubscribe patterns
- Auto-reconnect recommended
---
## 📚 نمونه کدها
### JavaScript/TypeScript
#### Basic HTTP Request:
```typescript
// استفاده از fetch API
async function getBTCPrice(): Promise<number> {
try {
const response = await fetch('http://localhost:7860/api/resources/market/price/BTC');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.price;
} catch (error) {
console.error('Error fetching BTC price:', error);
throw error;
}
}
// استفاده
const price = await getBTCPrice();
console.log(`BTC Price: $${price}`);
```
#### با Axios:
```typescript
import axios from 'axios';
const API_BASE = 'http://localhost:7860';
// تنظیم instance
const apiClient = axios.create({
baseURL: API_BASE,
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
});
// قیمت BTC
export async function getBTCPrice() {
const { data } = await apiClient.get('/api/resources/market/price/BTC');
return data.price;
}
// قیمت چندتا ارز
export async function getMultiplePrices(symbols: string[]) {
const { data } = await apiClient.get('/api/resources/market/prices', {
params: { symbols: symbols.join(',') }
});
return data.data;
}
// اخبار
export async function getLatestNews(limit = 20) {
const { data } = await apiClient.get('/api/resources/news/latest', {
params: { limit }
});
return data.news;
}
```
---
### React Hook
```typescript
import { useState, useEffect } from 'react';
import axios from 'axios';
interface PriceData {
symbol: string;
price: number;
source: string;
timestamp: string;
}
export function useCryptoPrice(symbol: string, refreshInterval = 5000) {
const [price, setPrice] = useState<PriceData | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const fetchPrice = async () => {
try {
setLoading(true);
const { data } = await axios.get(
`http://localhost:7860/api/resources/market/price/${symbol}`
);
setPrice(data);
setError(null);
} catch (err: any) {
setError(err.message);
} finally {
setLoading(false);
}
};
// اولین بار
fetchPrice();
// Polling برای بروزرسانی
const interval = setInterval(fetchPrice, refreshInterval);
return () => clearInterval(interval);
}, [symbol, refreshInterval]);
return { price, loading, error };
}
// استفاده در کامپوننت
function BTCPriceDisplay() {
const { price, loading, error } = useCryptoPrice('BTC');
if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error}</div>;
return (
<div>
<h2>Bitcoin Price</h2>
<p>${price?.price.toLocaleString()}</p>
<small>Source: {price?.source}</small>
</div>
);
}
```
---
### WebSocket در React
```typescript
import { useEffect, useState } from 'react';
interface MarketUpdate {
symbol: string;
price: number;
change: number;
timestamp: string;
}
export function useWebSocket(url: string) {
const [data, setData] = useState<MarketUpdate | null>(null);
const [connected, setConnected] = useState(false);
const [ws, setWs] = useState<WebSocket | null>(null);
useEffect(() => {
const websocket = new WebSocket(url);
websocket.onopen = () => {
console.log('WebSocket connected');
setConnected(true);
// Subscribe به market data
websocket.send(JSON.stringify({
action: 'subscribe',
service: 'market_data'
}));
};
websocket.onmessage = (event) => {
const message = JSON.parse(event.data);
if (message.type === 'market_update') {
setData(message.data);
}
};
websocket.onerror = (error) => {
console.error('WebSocket error:', error);
};
websocket.onclose = () => {
console.log('WebSocket disconnected');
setConnected(false);
// Auto-reconnect بعد از 5 ثانیه
setTimeout(() => {
console.log('Attempting to reconnect...');
// Recreate WebSocket
}, 5000);
};
setWs(websocket);
return () => {
websocket.close();
};
}, [url]);
const sendMessage = (message: any) => {
if (ws && connected) {
ws.send(JSON.stringify(message));
}
};
return { data, connected, sendMessage };
}
// استفاده
function LivePriceDisplay() {
const { data, connected } = useWebSocket('ws://localhost:7860/ws/market_data');
return (
<div>
<div>Status: {connected ? '🟢 Connected' : '🔴 Disconnected'}</div>
{data && (
<div>
<h3>{data.symbol}</h3>
<p>${data.price}</p>
<p className={data.change >= 0 ? 'green' : 'red'}>
{data.change >= 0 ? '+' : ''}{data.change}%
</p>
</div>
)}
</div>
);
}
```
---
### Vue.js Composable
```typescript
// composables/useCryptoAPI.ts
import { ref, onMounted, onUnmounted } from 'vue';
import axios from 'axios';
export function useCryptoPrice(symbol: string) {
const price = ref(null);
const loading = ref(true);
const error = ref(null);
let intervalId: number;
const fetchPrice = async () => {
try {
loading.value = true;
const { data } = await axios.get(
`http://localhost:7860/api/resources/market/price/${symbol}`
);
price.value = data;
error.value = null;
} catch (err: any) {
error.value = err.message;
} finally {
loading.value = false;
}
};
onMounted(() => {
fetchPrice();
intervalId = setInterval(fetchPrice, 5000);
});
onUnmounted(() => {
clearInterval(intervalId);
});
return { price, loading, error };
}
// استفاده در component
<script setup>
import { useCryptoPrice } from '@/composables/useCryptoAPI';
const { price, loading, error } = useCryptoPrice('BTC');
</script>
<template>
<div>
<div v-if="loading">Loading...</div>
<div v-else-if="error">Error: {{ error }}</div>
<div v-else>
<h2>{{ price.symbol }}</h2>
<p>${{ price.price }}</p>
</div>
</div>
</template>
```
---
### Python Client
```python
import requests
import asyncio
import websockets
import json
class CryptoAPIClient:
"""Python client برای Crypto API"""
def __init__(self, base_url='http://localhost:7860'):
self.base_url = base_url
self.session = requests.Session()
self.session.headers.update({
'Content-Type': 'application/json',
'User-Agent': 'PythonClient/1.0'
})
def get_price(self, symbol):
"""دریافت قیمت یک ارز"""
response = self.session.get(
f'{self.base_url}/api/resources/market/price/{symbol}'
)
response.raise_for_status()
return response.json()
def get_multiple_prices(self, symbols):
"""دریافت قیمت چند ارز"""
response = self.session.get(
f'{self.base_url}/api/resources/market/prices',
params={'symbols': ','.join(symbols)}
)
response.raise_for_status()
return response.json()['data']
def get_news(self, limit=20):
"""دریافت آخرین اخبار"""
response = self.session.get(
f'{self.base_url}/api/resources/news/latest',
params={'limit': limit}
)
response.raise_for_status()
return response.json()['news']
def get_fear_greed_index(self):
"""دریافت شاخص ترس و طمع"""
response = self.session.get(
f'{self.base_url}/api/resources/sentiment/fear-greed'
)
response.raise_for_status()
return response.json()
async def connect_websocket(self, on_message_callback):
"""اتصال به WebSocket"""
uri = self.base_url.replace('http', 'ws') + '/ws/master'
async with websockets.connect(uri) as websocket:
# Subscribe
await websocket.send(json.dumps({
'action': 'subscribe',
'service': 'market_data'
}))
# دریافت پیامها
async for message in websocket:
data = json.loads(message)
await on_message_callback(data)
# استفاده
client = CryptoAPIClient()
# REST API
btc_price = client.get_price('BTC')
print(f"BTC Price: ${btc_price['price']}")
prices = client.get_multiple_prices(['BTC', 'ETH', 'BNB'])
for price_data in prices:
print(f"{price_data['symbol']}: ${price_data['price']}")
# WebSocket
async def handle_message(data):
print(f"Received: {data}")
asyncio.run(client.connect_websocket(handle_message))
```
---
### React Native
```typescript
import { useEffect, useState } from 'react';
import { View, Text, ActivityIndicator } from 'react-native';
export function PriceScreen() {
const [price, setPrice] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
const fetchPrice = async () => {
try {
const response = await fetch(
'http://your-api.com/api/resources/market/price/BTC'
);
const data = await response.json();
setPrice(data.price);
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};
fetchPrice();
const interval = setInterval(fetchPrice, 5000);
return () => clearInterval(interval);
}, []);
if (loading) {
return <ActivityIndicator />;
}
return (
<View>
<Text>BTC Price</Text>
<Text>${price}</Text>
</View>
);
}
```
---
## 🔒 Authentication (در صورت نیاز)
### JWT Token Based:
```typescript
// دریافت توکن (login)
async function login(username: string, password: string) {
const response = await fetch('http://localhost:7860/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
const data = await response.json();
// ذخیره توکن
localStorage.setItem('token', data.token);
return data.token;
}
// استفاده از توکن در درخواستها
async function getProtectedData() {
const token = localStorage.getItem('token');
const response = await fetch('http://localhost:7860/api/protected/data', {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
return response.json();
}
```
---
## ⚡ بهینهسازی Performance
### 1. Caching در Client:
```typescript
class CachedAPIClient {
private cache = new Map<string, { data: any; timestamp: number }>();
private cacheTTL = 5000; // 5 seconds
async get(url: string) {
const cached = this.cache.get(url);
// بررسی cache
if (cached && Date.now() - cached.timestamp < this.cacheTTL) {
return cached.data;
}
// درخواست جدید
const response = await fetch(url);
const data = await response.json();
// ذخیره در cache
this.cache.set(url, {
data,
timestamp: Date.now()
});
return data;
}
}
```
### 2. Request Batching:
```typescript
class BatchedAPIClient {
private pendingRequests: Map<string, Promise<any>> = new Map();
async get(url: string) {
// اگر همین درخواست در حال انجام است، همان را برگردان
if (this.pendingRequests.has(url)) {
return this.pendingRequests.get(url);
}
// درخواست جدید
const promise = fetch(url).then(r => r.json());
this.pendingRequests.set(url, promise);
try {
const data = await promise;
return data;
} finally {
this.pendingRequests.delete(url);
}
}
}
```
### 3. Debouncing:
```typescript
function debounce<T extends (...args: any[]) => any>(
func: T,
wait: number
): (...args: Parameters<T>) => void {
let timeout: NodeJS.Timeout;
return function executedFunction(...args: Parameters<T>) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// استفاده
const debouncedSearch = debounce(async (query: string) => {
const results = await fetch(`/api/search?q=${query}`);
// ...
}, 300);
// در input
<input onChange={(e) => debouncedSearch(e.target.value)} />
```
---
## 🚨 Error Handling
### Retry Logic:
```typescript
async function fetchWithRetry(
url: string,
options: RequestInit = {},
retries = 3,
delay = 1000
): Promise<any> {
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return await response.json();
} catch (error) {
if (retries > 0) {
console.log(`Retrying... (${retries} attempts left)`);
await new Promise(resolve => setTimeout(resolve, delay));
return fetchWithRetry(url, options, retries - 1, delay * 2);
}
throw error;
}
}
```
### Global Error Handler:
```typescript
class APIClient {
async request(url: string, options?: RequestInit) {
try {
const response = await fetch(url, options);
if (response.status === 401) {
// Token منقضی شده
await this.refreshToken();
return this.request(url, options); // Retry
}
if (response.status === 429) {
// Rate limit
const retryAfter = response.headers.get('Retry-After');
await new Promise(r => setTimeout(r, parseInt(retryAfter || '5') * 1000));
return this.request(url, options); // Retry
}
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Request failed');
}
return await response.json();
} catch (error) {
// Log to monitoring service
this.logError(error);
throw error;
}
}
}
```
---
## 📊 Rate Limiting
**سمت سرور:**
```
✅ 100 requests/minute per IP
✅ Headers شامل rate limit info
```
**Response Headers:**
```
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1702027200
```
**Handle در Client:**
```typescript
async function checkRateLimit(response: Response) {
const limit = response.headers.get('X-RateLimit-Limit');
const remaining = response.headers.get('X-RateLimit-Remaining');
const reset = response.headers.get('X-RateLimit-Reset');
if (response.status === 429) {
const retryAfter = parseInt(reset!) - Date.now() / 1000;
throw new Error(`Rate limit exceeded. Retry after ${retryAfter}s`);
}
return {
limit: parseInt(limit!),
remaining: parseInt(remaining!),
reset: new Date(parseInt(reset!) * 1000)
};
}
```
---
## ✅ Best Practices
### 1. همیشه Error Handling داشته باشید
```typescript
try {
const data = await apiCall();
} catch (error) {
// Handle error
console.error(error);
showErrorToUser(error.message);
}
```
### 2. Timeout تنظیم کنید
```typescript
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 10000);
fetch(url, { signal: controller.signal })
.finally(() => clearTimeout(timeout));
```
### 3. Loading States نشان دهید
```typescript
const [loading, setLoading] = useState(false);
setLoading(true);
try {
await apiCall();
} finally {
setLoading(false);
}
```
### 4. Cache استفاده کنید
```typescript
// React Query
const { data } = useQuery('prices', fetchPrices, {
staleTime: 5000,
cacheTime: 10000
});
```
---
## 📱 پلتفرمهای خاص
### iOS (Swift):
```swift
import Foundation
class CryptoAPIClient {
let baseURL = "http://localhost:7860"
func getPrice(symbol: String, completion: @escaping (Result<Double, Error>) -> Void) {
guard let url = URL(string: "\(baseURL)/api/resources/market/price/\(symbol)") else {
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
completion(.failure(error))
return
}
guard let data = data else {
return
}
do {
let json = try JSONDecoder().decode(PriceResponse.self, from: data)
completion(.success(json.price))
} catch {
completion(.failure(error))
}
}.resume()
}
}
struct PriceResponse: Codable {
let price: Double
let symbol: String
}
```
### Android (Kotlin):
```kotlin
import retrofit2.http.GET
import retrofit2.http.Path
interface CryptoAPI {
@GET("api/resources/market/price/{symbol}")
suspend fun getPrice(@Path("symbol") symbol: String): PriceResponse
}
data class PriceResponse(
val price: Double,
val symbol: String,
val source: String
)
// استفاده
val api = Retrofit.Builder()
.baseUrl("http://localhost:7860")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(CryptoAPI::class.java)
lifecycleScope.launch {
val response = api.getPrice("BTC")
println("BTC Price: ${response.price}")
}
```
---
**تاریخ بروزرسانی**: ۸ دسامبر ۲۰۲۵
**نسخه**: ۱.۰
**وضعیت**: ✅ تکمیل شده
|