Files
robinhood-agentic-mcp/connect.go
T

59 lines
1.6 KiB
Go

package rh
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"s1d3sw1ped/robinhood-agentic-mcp/accounts"
"s1d3sw1ped/robinhood-agentic-mcp/auth"
"s1d3sw1ped/robinhood-agentic-mcp/client"
"s1d3sw1ped/robinhood-agentic-mcp/crypto"
"s1d3sw1ped/robinhood-agentic-mcp/equity"
"s1d3sw1ped/robinhood-agentic-mcp/market"
"s1d3sw1ped/robinhood-agentic-mcp/options"
"s1d3sw1ped/robinhood-agentic-mcp/scanner"
"s1d3sw1ped/robinhood-agentic-mcp/watchlists"
)
// Connect reads tokens and returns a wired API. Session connect is preferred;
// JSON-RPC tools/call is used when the session cannot be opened. Missing tokens fail closed.
func Connect(ctx context.Context, cfg Config) (*API, error) {
cfg = cfg.WithDefaults()
if cfg.TokenFile == "" {
return nil, fmt.Errorf("connect: missing TokenFile")
}
tok, err := auth.ReadTokens(cfg.TokenFile)
if err != nil {
return nil, fmt.Errorf("connect: %w", err)
}
if tok.AccessToken == "" {
return nil, fmt.Errorf("connect: missing access token")
}
c, err := client.ConnectSession(ctx, cfg.URL, auth.ClientToken(tok), cfg.Name, cfg.Version)
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return nil, err
}
c = &client.Client{
URL: cfg.URL,
Token: tok.AccessToken,
Name: cfg.Name,
Version: cfg.Version,
HTTP: &http.Client{Timeout: 60 * time.Second},
}
}
return &API{
Client: c,
Accounts: accounts.New(c),
Equity: equity.New(c),
Options: options.New(c),
Crypto: crypto.New(c),
Watchlists: watchlists.New(c),
Market: market.New(c),
Scanner: scanner.New(c),
}, nil
}