diff --git a/README.md b/README.md index ce8890f..8cf4cbc 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,12 @@ if err != nil { fmt.Println(err) } +// Using v4 +tmdbClient, err := tmdb.InitV4(os.GetEnv("YOUR_BEARER_TOKEN")) +if err != nil { + fmt.Println(err) +} + // OPTIONAL (Recommended): Enabling auto retry functionality. // This option will retry if the previous request fail (429 TOO MANY REQUESTS). tmdbClient.SetClientAutoRetry() @@ -76,6 +82,12 @@ if err != nil { fmt.Println(err) } +// Using v4 +tmdbClient, err := tmdb.InitV4(os.GetEnv("YOUR_BEARER_TOKEN")) +if err != nil { + fmt.Println(err) +} + options := map[string]string{ "language": "pt-BR", "append_to_response": "credits,images", diff --git a/examples/bearerToken/main.go b/examples/bearerToken/main.go new file mode 100644 index 0000000..291f614 --- /dev/null +++ b/examples/bearerToken/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "os" + + tmdb "github.com/cyruzin/golang-tmdb" +) + +func main() { + tmdbClient, err := tmdb.InitV4(os.Getenv("BearerToken")) + + if err != nil { + fmt.Println(err) + } + + movie, err := tmdbClient.GetMovieDetails(299536, nil) + + if err != nil { + fmt.Println(err) + } + + fmt.Println(movie.Title) +} diff --git a/tmdb.go b/tmdb.go index bcf8965..fb6aaa3 100644 --- a/tmdb.go +++ b/tmdb.go @@ -51,6 +51,8 @@ var baseURL = defaultBaseURL type Client struct { // TMDb apiKey to use the client. apiKey string + // bearerToken will be used for v4 requests. + bearerToken string // sessionId to use the client. sessionID string // Auto retry flag to indicates if the client @@ -74,6 +76,14 @@ func Init(apiKey string) (*Client, error) { return &Client{apiKey: apiKey}, nil } +// InitV4 setups the Client with an bearer token. +func InitV4(bearerToken string) (*Client, error) { + if bearerToken == "" { + return nil, errors.New("bearer token is empty") + } + return &Client{bearerToken: bearerToken}, nil +} + // SetSessionID will set the session id. func (c *Client) SetSessionID(sid string) error { if sid == "" { @@ -130,6 +140,9 @@ func (c *Client) get(url string, data interface{}) error { defer cancel() req = req.WithContext(ctx) req.Header.Add("content-type", "application/json;charset=utf-8") + if c.bearerToken != "" { + req.Header.Add("Authorization", "Bearer "+c.bearerToken) + } for { res, err := c.http.Do(req) if err != nil { @@ -180,6 +193,9 @@ func (c *Client) request( defer cancel() req = req.WithContext(ctx) req.Header.Add("content-type", "application/json;charset=utf-8") + if c.bearerToken != "" { + req.Header.Add("Authorization", "Bearer "+c.bearerToken) + } for { res, err := c.http.Do(req) if err != nil { diff --git a/tmdb_test.go b/tmdb_test.go index 813463c..aec6ece 100644 --- a/tmdb_test.go +++ b/tmdb_test.go @@ -155,3 +155,15 @@ func (suite *TMBDTestSuite) TestRetryDurationEmpty() { duration := retryDuration(&response) suite.Equal(defaultRetryDuration, duration) } + +func (suite *TMBDTestSuite) TestClientV4() { + _, err := InitV4("FAKE_BEARER_TOKEN") + + suite.Nil(err) +} + +func (suite *TMBDTestSuite) TestClientV4Fail() { + _, err := InitV4("") + + suite.EqualError(err, "bearer token is empty") +}