mirror of
https://github.com/Dvorinka/SEEN.git
synced 2026-06-04 20:43:03 +00:00
small fix, don't worry about it
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user