mirror of
https://github.com/Dvorinka/SEEN.git
synced 2026-06-04 12:33:02 +00:00
56 lines
1005 B
Go
56 lines
1005 B
Go
package tmdb
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/tdvorak/seen/backend/internal/config"
|
|
)
|
|
|
|
var ErrTMDBAPIKeyMissing = errors.New("tmdb api key missing")
|
|
|
|
type Client struct {
|
|
apiKey string
|
|
baseURL string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func NewClient(cfg config.TMDBConfig) *Client {
|
|
return &Client{
|
|
apiKey: cfg.APIKey,
|
|
baseURL: cfg.BaseURL,
|
|
httpClient: &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *Client) Validate(ctx context.Context) error {
|
|
if c.apiKey == "" {
|
|
return ErrTMDBAPIKeyMissing
|
|
}
|
|
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/configuration", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
query := request.URL.Query()
|
|
query.Set("api_key", c.apiKey)
|
|
request.URL.RawQuery = query.Encode()
|
|
|
|
response, err := c.httpClient.Do(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode >= 400 {
|
|
return errors.New("tmdb validation request failed")
|
|
}
|
|
|
|
return nil
|
|
}
|