This commit is contained in:
2026-04-20 22:51:41 +02:00
parent 89fc0119f3
commit 81eec53978
27 changed files with 1067 additions and 399 deletions
+90 -59
View File
@@ -5,31 +5,35 @@ import (
"fmt"
"net/http"
"time"
"github.com/google/uuid"
)
const instrumentsURL = "https://api.etoro.com/metadata/instruments"
// InstrumentTypeID connus sur eToro
const (
TypeStock = 5
TypeETF = 10
TypeCrypto = 12
TypeIndex = 21
TypeCFD = 6
)
const baseURL = "https://public-api.etoro.com/api/v1"
type Client struct {
http *http.Client
http *http.Client
apiKey string
userKey string
}
type Instrument struct {
InstrumentID int `json:"InstrumentID"`
InstrumentDisplayName string `json:"InstrumentDisplayName"`
SymbolFull string `json:"SymbolFull"`
InstrumentTypeID int `json:"InstrumentTypeID"`
IsActive bool `json:"IsActive"`
StockIndustryID int `json:"StockIndustryID"`
StockExchangeID int `json:"StockExchangeID"`
InstrumentID int `json:"internalInstrumentId"`
InstrumentDisplayName string `json:"internalInstrumentDisplayName"`
SymbolFull string `json:"internalSymbolFull"`
AssetClassID int `json:"internalAssetClassId"`
ExchangeID int `json:"internalExchangeId"`
IsHidden bool `json:"isHiddenFromClient"`
IsDelisted bool `json:"isDelisted"`
IsActiveInPlatform bool `json:"isActiveInPlatform"`
IsBuyEnabled bool `json:"isBuyEnabled"`
}
type searchResponse struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
TotalItems int `json:"totalItems"`
Items []Instrument `json:"items"`
}
func New() *Client {
@@ -38,58 +42,85 @@ func New() *Client {
}
}
// FetchStocks retourne tous les instruments de type Stock actifs sur eToro.
func (c *Client) FetchStocks() ([]Instrument, error) {
all, err := c.fetchAll()
func NewWithKeys(apiKey, userKey string) *Client {
return &Client{
http: &http.Client{Timeout: 30 * time.Second},
apiKey: apiKey,
userKey: userKey,
}
}
func (c *Client) SetKeys(apiKey, userKey string) {
c.apiKey = apiKey
c.userKey = userKey
}
func (c *Client) get(path string) (*http.Response, error) {
req, err := http.NewRequest("GET", baseURL+path, nil)
if err != nil {
return nil, err
}
var stocks []Instrument
for _, inst := range all {
if inst.IsActive && inst.InstrumentTypeID == TypeStock {
stocks = append(stocks, inst)
}
}
return stocks, nil
}
// FetchAll retourne tous les instruments (stocks + ETFs + crypto + indices).
func (c *Client) FetchAll() ([]Instrument, error) {
return c.fetchAll()
}
func (c *Client) fetchAll() ([]Instrument, error) {
req, err := http.NewRequest("GET", instrumentsURL, nil)
if err != nil {
return nil, err
}
// Headers qui imitent le client web eToro
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
req.Header.Set("accounttype", "Demo")
req.Header.Set("ApplicationIdentifier", "ReToro")
req.Header.Set("Version", "1.211.0")
req.Header.Set("x-api-key", c.apiKey)
req.Header.Set("x-user-key", c.userKey)
req.Header.Set("x-request-id", uuid.NewString())
req.Header.Set("Accept", "application/json")
return c.http.Do(req)
}
resp, err := c.http.Do(req)
func (c *Client) fetchPage(assetClassID, pageSize, page int) (*searchResponse, error) {
resp, err := c.get(fmt.Sprintf("/market-data/search?internalAssetClassId=%d&pageSize=%d&page=%d", assetClassID, pageSize, page))
if err != nil {
return nil, fmt.Errorf("etoro: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("etoro: HTTP %d", resp.StatusCode)
}
var instruments []Instrument
if err := json.NewDecoder(resp.Body).Decode(&instruments); err != nil {
return nil, fmt.Errorf("etoro: parse error: %w", err)
var sr searchResponse
if err := json.NewDecoder(resp.Body).Decode(&sr); err != nil {
return nil, fmt.Errorf("etoro: parse: %w", err)
}
if len(instruments) == 0 {
return nil, fmt.Errorf("etoro: empty response — l'API a peut-être changé")
}
return instruments, nil
return &sr, nil
}
// FetchStocks retourne tous les stocks actifs disponibles sur eToro (toutes pages).
func (c *Client) FetchStocks() ([]Instrument, error) {
if c.apiKey == "" || c.userKey == "" {
return nil, fmt.Errorf("etoro: clés API non configurées")
}
const pageSize = 500
var all []Instrument
for page := 1; ; page++ {
resp, err := c.get(fmt.Sprintf("/market-data/search?internalAssetClassId=5&pageSize=%d&page=%d", pageSize, page))
if err != nil {
return nil, fmt.Errorf("etoro: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("etoro: HTTP %d", resp.StatusCode)
}
var sr searchResponse
if err := json.NewDecoder(resp.Body).Decode(&sr); err != nil {
return nil, fmt.Errorf("etoro: parse: %w", err)
}
for _, inst := range sr.Items {
if !inst.IsHidden && !inst.IsDelisted && inst.IsBuyEnabled {
all = append(all, inst)
}
}
if page*pageSize >= sr.TotalItems {
break
}
}
if len(all) == 0 {
return nil, fmt.Errorf("etoro: aucun stock retourné")
}
return all, nil
}