diff --git a/api.go b/api.go index ddee1ab..703d557 100644 --- a/api.go +++ b/api.go @@ -2,9 +2,8 @@ package twitch import ( "go.fifitido.net/twitch/api" - "go.fifitido.net/twitch/auth" ) -func NewAPI(authClient *auth.Client) *api.API { - return api.NewDefault(authClient) +func NewAPI(clientId, clientSecret, redirectUri string) *api.API { + return api.NewDefault(clientId, clientSecret, redirectUri) } diff --git a/api/api.go b/api/api.go index cb3adf1..6c444c1 100644 --- a/api/api.go +++ b/api/api.go @@ -39,9 +39,9 @@ import ( const HelixBaseUrl = "https://api.twitch.tv/helix" type API struct { - client *http.Client - baseUrl *url.URL - authClient *auth.Client + client *http.Client + baseUrl *url.URL + Auth *auth.Client Ads *ads.Ads Analytics *analytics.Analytics @@ -75,9 +75,9 @@ type API struct { func New(client *http.Client, baseUrl *url.URL, authClient *auth.Client) *API { return &API{ - client: client, - baseUrl: baseUrl, - authClient: authClient, + client: client, + baseUrl: baseUrl, + Auth: authClient, Ads: ads.New(client, baseUrl), Analytics: analytics.New(client, baseUrl), @@ -110,21 +110,26 @@ func New(client *http.Client, baseUrl *url.URL, authClient *auth.Client) *API { } } -func NewDefault(authClient *auth.Client) *API { - client := &http.Client{} +func NewDefault(clientId, clientSecret, redirectUri string) *API { + client := &http.Client{ + Transport: &apiTransport{ + clientId: clientId, + }, + } baseUrl, _ := url.Parse(HelixBaseUrl) + authClient := auth.NewClient(clientId, clientSecret, redirectUri) return New(client, baseUrl, authClient) } func (a *API) WithClient(client *http.Client) *API { - return New(client, a.baseUrl, a.authClient) + return New(client, a.baseUrl, a.Auth) } func (a *API) WithAuthToken(token *auth.Token) *API { return a.WithClient(&http.Client{ Transport: &oauth2.Transport{ - Source: a.authClient.TokenSource(token), + Source: a.Auth.TokenSource(token), Base: a.client.Transport, }, }) diff --git a/api/transport.go b/api/transport.go new file mode 100644 index 0000000..9721465 --- /dev/null +++ b/api/transport.go @@ -0,0 +1,15 @@ +package api + +import "net/http" + +type apiTransport struct { + clientId string +} + +var _ http.RoundTripper = (*apiTransport)(nil) + +// RoundTrip implements http.RoundTripper. +func (a *apiTransport) RoundTrip(req *http.Request) (*http.Response, error) { + req.Header.Add("Client-ID", a.clientId) + return http.DefaultTransport.RoundTrip(req) +}