package sorare import ( "bytes" "context" "encoding/json" "io" "log" "net/http" "github.com/hashicorp/go-retryablehttp" "git.lehouerou.net/laurent/sorare/config" "git.lehouerou.net/laurent/sorare/football" "git.lehouerou.net/laurent/sorare/graphql" "git.lehouerou.net/laurent/sorare/mutations" "git.lehouerou.net/laurent/sorare/subscriptions" "git.lehouerou.net/laurent/sorare/tokens" "git.lehouerou.net/laurent/sorare/types" ) const ( baseUrl = "https://api.sorare.com/federation/graphql" ) type Sorare struct { httpClient *retryablehttp.Client Client *graphql.Client Config *config.Config Football *football.Football Users *Users Country *graphql.Query[Country, graphql.SlugParams] Countries *graphql.Query[[]Country, graphql.SlugsParams] Tokens *tokens.Tokens ShopItems *graphql.PaginatedQuery[ShopItem, ShopItemsParams] NbaPlayer *graphql.Query[NbaPlayer, graphql.SlugParams] NbaPlayers *graphql.Query[[]NbaPlayer, graphql.SlugsParams] Mutations *mutations.Mutations Subscriptions *subscriptions.Subscriptions } func New() *Sorare { httpClient := retryablehttp.NewClient() httpClient.Logger = nil c := graphql.NewClient(httpClient.StandardClient(), baseUrl) return &Sorare{ httpClient: httpClient, Client: c, Config: config.NewConfig(c), Football: football.NewFootball(c), Users: NewUsers(c), Country: graphql.NewQuery[Country, graphql.SlugParams](c, "country", nil), Countries: graphql.NewQuery[[]Country, graphql.SlugsParams](c, "countries", nil), Tokens: tokens.NewTokens(c), ShopItems: graphql.NewPaginatedQuery[ShopItem, ShopItemsParams](c, "shopItems", nil), NbaPlayer: graphql.NewQuery[NbaPlayer, graphql.SlugParams](c, "nbaPlayer", nil), NbaPlayers: graphql.NewQuery[[]NbaPlayer, graphql.SlugsParams](c, "nbaPlayers", nil), Mutations: mutations.NewMutations(c), Subscriptions: subscriptions.NewSubscriptions(), } } func (s *Sorare) Debug() *Sorare { s.httpClient.RequestLogHook = func(l retryablehttp.Logger, req *http.Request, attempt int) { if req.Body != nil { b, err := io.ReadAll(req.Body) if err == nil { req.Body = io.NopCloser(bytes.NewBuffer(b)) } log.Printf("Request: %s %s\n%s\n", req.Method, req.URL, string(b)) } else { log.Printf("Request: %s %s\n", req.Method, req.URL) } } s.httpClient.ResponseLogHook = func(l retryablehttp.Logger, resp *http.Response) { if resp.Body != nil { b, err := io.ReadAll(resp.Body) if err == nil { resp.Body = io.NopCloser(bytes.NewBuffer(b)) } var jsonbody bytes.Buffer err = json.Indent(&jsonbody, b, "", " ") if err == nil { log.Printf("Response: %s\n%s\n", resp.Status, jsonbody.String()) return } log.Printf("Response: %s\n%s\n", resp.Status, string(b)) } else { log.Printf("Response: %s\n", resp.Status) } } s.Subscriptions.SetDebug(true) return s } func (s *Sorare) SetJWTToken(token graphql.JwtToken, audience string) { s.Client.SetJWTToken(token, audience) } func (s *Sorare) GetCurrentToken() graphql.JwtToken { return s.Client.GetCurrentToken() } func (s *Sorare) Authenticate( ctx context.Context, email string, password string, audience string, otp string, ) error { return s.Client.Authenticate(ctx, email, password, audience, otp) } func (s *Sorare) Query( ctx context.Context, query interface{}, variables map[string]interface{}, ) error { return s.Client.Query(ctx, query, variables) } func (s *Sorare) GetRawQuery(query interface{}, variables map[string]interface{}) (string, error) { return s.Client.ConstructRawQuery(query, variables) } func (s *Sorare) SetDefaultSettlementCurrencies(currencies []types.SupportedCurrency) { s.Mutations.SetDefaultSettlementCurrencies(currencies) }