File size: 10,938 Bytes
8b7b267
 
 
8ff9278
8b7b267
 
 
 
 
 
 
 
 
 
 
 
8ff9278
 
 
8b7b267
 
 
 
 
 
 
 
 
 
 
 
8ff9278
 
8b7b267
 
 
 
 
 
 
8ff9278
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ff9278
8b7b267
 
 
 
 
 
 
 
 
 
8ff9278
 
8b7b267
8ff9278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7b267
8ff9278
 
 
 
 
8b7b267
8ff9278
 
 
 
 
 
 
 
8b7b267
8ff9278
8b7b267
 
 
 
 
 
 
8ff9278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7b267
8ff9278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7b267
8ff9278
 
 
 
 
 
 
 
 
 
8b7b267
 
 
 
 
 
 
 
 
 
8ff9278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7b267
 
 
 
 
8ff9278
8b7b267
 
8ff9278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7b267
8ff9278
8b7b267
 
8ff9278
 
 
8b7b267
8ff9278
 
 
 
 
 
8b7b267
8ff9278
 
8b7b267
8ff9278
 
 
8b7b267
8ff9278
 
 
 
 
8b7b267
8ff9278
 
 
 
 
 
 
 
 
8b7b267
 
8ff9278
8b7b267
 
 
 
8ff9278
 
 
 
8b7b267
 
8ff9278
8b7b267
8ff9278
8b7b267
 
8ff9278
 
 
 
 
 
 
 
 
8b7b267
8ff9278
 
 
8b7b267
8ff9278
 
 
8b7b267
8ff9278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7b267
 
8ff9278
8b7b267
 
 
 
 
8ff9278
8b7b267
 
8ff9278
8b7b267
8ff9278
 
 
8b7b267
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
"""
HF Space Complete API Router
Implements all required endpoints for Hugging Face Space deployment
using REAL data providers managed by the Orchestrator.
"""
from fastapi import APIRouter, HTTPException, Query, Body, Depends
from fastapi.responses import JSONResponse
from typing import Optional, List, Dict, Any
from datetime import datetime, timedelta
from pydantic import BaseModel, Field
import logging
import asyncio
import json
import os
from pathlib import Path

# Import Orchestrator
from backend.orchestration.provider_manager import provider_manager

logger = logging.getLogger(__name__)

router = APIRouter(tags=["HF Space Complete API"])

# ============================================================================
# Pydantic Models for Request/Response
# ============================================================================

class MetaInfo(BaseModel):
    """Metadata for all responses"""
    cache_ttl_seconds: int = Field(default=30, description="Cache TTL in seconds")
    generated_at: str = Field(default_factory=lambda: datetime.now().isoformat())
    source: str = Field(default="live", description="Data source")
    latency_ms: Optional[float] = None

class MarketItem(BaseModel):
    """Market ticker item"""
    symbol: str
    price: float
    change_24h: float
    volume_24h: float
    source: str = "live"

class MarketResponse(BaseModel):
    """Market snapshot response"""
    last_updated: str
    items: List[MarketItem]
    meta: MetaInfo

class NewsArticle(BaseModel):
    """News article"""
    id: str
    title: str
    url: str
    source: str
    summary: Optional[str] = None
    published_at: str

class NewsResponse(BaseModel):
    """News response"""
    articles: List[NewsArticle]
    meta: MetaInfo

class SentimentResponse(BaseModel):
    """Sentiment analysis response"""
    score: float
    label: str  # positive, negative, neutral
    details: Optional[Dict[str, Any]] = None
    meta: MetaInfo

class GasPrice(BaseModel):
    """Gas price information"""
    fast: float
    standard: float
    slow: float
    unit: str = "gwei"

class GasResponse(BaseModel):
    """Gas price response"""
    chain: str
    gas_prices: Optional[GasPrice] = None
    timestamp: str
    meta: MetaInfo

# ============================================================================
# Market & Pairs Endpoints
# ============================================================================

@router.get("/api/market", response_model=MarketResponse)
async def get_market_snapshot():
    """
    Get current market snapshot with prices, changes, and volumes.
    Uses Provider Orchestrator (CoinGecko, Binance, etc.)
    """
    response = await provider_manager.fetch_data(
        "market",
        params={"ids": "bitcoin,ethereum,tron,solana,binancecoin,ripple", "vs_currency": "usd"},
        use_cache=True,
        ttl=60
    )
    
    if not response["success"]:
        raise HTTPException(status_code=503, detail=response["error"])
        
    data = response["data"]
    items = []
    
    # Handle different provider formats if needed, but fetch functions should normalize
    # Assuming coingecko format for "market" category list
    if isinstance(data, list):
        for coin in data:
            items.append(MarketItem(
                symbol=coin.get('symbol', '').upper(),
                price=coin.get('current_price', 0),
                change_24h=coin.get('price_change_percentage_24h', 0),
                volume_24h=coin.get('total_volume', 0),
                source=response["source"]
            ))
            
    return MarketResponse(
        last_updated=response["timestamp"],
        items=items,
        meta=MetaInfo(
            cache_ttl_seconds=60, 
            source=response["source"],
            latency_ms=response.get("latency_ms")
        )
    )

@router.get("/api/market/ohlc")
async def get_ohlc(
    symbol: str = Query(..., description="Trading symbol (e.g., BTC)"),
    interval: int = Query(60, description="Interval in minutes"),
    limit: int = Query(100, description="Number of candles")
):
    """Get OHLC candlestick data via Orchestrator"""
    
    # Map minutes to common string format if needed by providers, 
    # but fetch_binance_klines handles it.
    interval_str = "1h"
    if interval < 60:
        interval_str = f"{interval}m"
    elif interval == 60:
        interval_str = "1h"
    elif interval == 240:
        interval_str = "4h"
    elif interval == 1440:
        interval_str = "1d"

    response = await provider_manager.fetch_data(
        "ohlc",
        params={
            "symbol": symbol,
            "interval": interval_str,
            "limit": limit
        },
        use_cache=True,
        ttl=60
    )

    if not response["success"]:
        raise HTTPException(status_code=503, detail=response["error"])

    # Transform Binance Klines to standard OHLC
    # [time, open, high, low, close, volume, ...]
    klines = response["data"]
    ohlc_data = []
    
    if isinstance(klines, list):
        for k in klines:
            if isinstance(k, list) and len(k) >= 6:
                ohlc_data.append({
                    "ts": int(k[0] / 1000),
                    "open": float(k[1]),
                    "high": float(k[2]),
                    "low": float(k[3]),
                    "close": float(k[4]),
                    "volume": float(k[5])
                })

    return {
        "symbol": symbol,
        "interval": interval,
        "data": ohlc_data,
        "meta": MetaInfo(
            cache_ttl_seconds=60, 
            source=response["source"],
            latency_ms=response.get("latency_ms")
        ).dict()
    }

# ============================================================================
# News & Sentiment Endpoints
# ============================================================================

@router.get("/api/news", response_model=NewsResponse)
async def get_news(
    limit: int = Query(20, description="Number of articles"),
    source: Optional[str] = Query(None, description="Filter by source")
):
    """Get cryptocurrency news via Orchestrator"""
    
    response = await provider_manager.fetch_data(
        "news",
        params={"filter": "hot", "query": "crypto"}, # Params for different providers
        use_cache=True,
        ttl=300
    )
    
    if not response["success"]:
        return NewsResponse(articles=[], meta=MetaInfo(source="error"))

    data = response["data"]
    articles = []
    
    # Normalize CryptoPanic / NewsAPI formats
    if "results" in data: # CryptoPanic
        for post in data.get('results', [])[:limit]:
            articles.append(NewsArticle(
                id=str(post.get('id')),
                title=post.get('title', ''),
                url=post.get('url', ''),
                source=post.get('source', {}).get('title', 'Unknown'),
                summary=post.get('slug', ''),
                published_at=post.get('published_at', datetime.now().isoformat())
            ))
    elif "articles" in data: # NewsAPI
        for post in data.get('articles', [])[:limit]:
            articles.append(NewsArticle(
                id=str(hash(post.get('url', ''))),
                title=post.get('title', ''),
                url=post.get('url', ''),
                source=post.get('source', {}).get('name', 'Unknown'),
                summary=post.get('description', ''),
                published_at=post.get('publishedAt', datetime.now().isoformat())
            ))

    return NewsResponse(
        articles=articles,
        meta=MetaInfo(
            cache_ttl_seconds=300, 
            source=response["source"],
            latency_ms=response.get("latency_ms")
        )
    )


@router.get("/api/sentiment/global")
async def get_global_sentiment():
    """Get global market sentiment via Orchestrator"""
    
    response = await provider_manager.fetch_data(
        "sentiment",
        params={"limit": 1},
        use_cache=True,
        ttl=3600
    )
    
    if not response["success"]:
        raise HTTPException(status_code=503, detail=response["error"])
        
    data = response["data"]
    fng_value = 50
    classification = "Neutral"
    
    # Alternative.me format
    if data.get('data'):
        item = data['data'][0]
        fng_value = int(item.get('value', 50))
        classification = item.get('value_classification', 'Neutral')
        
    return {
        "score": fng_value,
        "label": classification,
        "meta": MetaInfo(
            cache_ttl_seconds=3600, 
            source=response["source"],
            latency_ms=response.get("latency_ms")
        ).dict()
    }

# ============================================================================
# Blockchain Endpoints
# ============================================================================

@router.get("/api/crypto/blockchain/gas", response_model=GasResponse)
async def get_gas_prices(chain: str = Query("ethereum", description="Blockchain network")):
    """Get gas prices via Orchestrator"""
    
    if chain.lower() != "ethereum":
        # Fallback or implement other chains
        return GasResponse(
            chain=chain,
            gas_prices=None,
            timestamp=datetime.now().isoformat(),
            meta=MetaInfo(source="unavailable")
        )

    response = await provider_manager.fetch_data(
        "onchain",
        params={},
        use_cache=True,
        ttl=15
    )
    
    if not response["success"]:
        return GasResponse(
            chain=chain,
            gas_prices=None,
            timestamp=datetime.now().isoformat(),
            meta=MetaInfo(source="unavailable")
        )
        
    data = response["data"]
    result = data.get("result", {})
    
    gas_price = None
    if result:
        # Etherscan returns data in result
        try:
            gas_price = GasPrice(
                fast=float(result.get("FastGasPrice", 0)),
                standard=float(result.get("ProposeGasPrice", 0)),
                slow=float(result.get("SafeGasPrice", 0))
            )
        except:
            pass

    return GasResponse(
        chain=chain,
        gas_prices=gas_price,
        timestamp=datetime.now().isoformat(),
        meta=MetaInfo(
            cache_ttl_seconds=15, 
            source=response["source"],
            latency_ms=response.get("latency_ms")
        )
    )

# ============================================================================
# System Management
# ============================================================================

@router.get("/api/status")
async def get_system_status():
    """Get overall system status"""
    stats = provider_manager.get_stats()
    
    return {
        'status': 'operational',
        'timestamp': datetime.now().isoformat(),
        'providers': stats,
        'version': '2.0.0',
        'meta': MetaInfo(source="system").dict()
    }