Skip to content

Commit

Permalink
added v4 init function (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyruzin committed Mar 1, 2024
1 parent b2a63f2 commit 4c57739
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions examples/bearerToken/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 16 additions & 0 deletions tmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions tmdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 4c57739

Please sign in to comment.