Skip to content

Commit 550e163

Browse files
authored
Merge pull request #46 from fabdelgado/master
Add support for language
2 parents 1e8957b + 6b4859d commit 550e163

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

examples/trending/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
tmdb "github.com/cyruzin/golang-tmdb"
8+
)
9+
10+
func main() {
11+
tmdbClient, err := tmdb.Init(os.Getenv("APIKey"))
12+
13+
if err != nil {
14+
fmt.Println(err)
15+
}
16+
17+
trending, err := tmdbClient.GetTrending("movie", "week", nil)
18+
19+
if err != nil {
20+
fmt.Println(err)
21+
}
22+
23+
for _, result := range trending.Results {
24+
fmt.Println(result.Title)
25+
}
26+
27+
fmt.Println("------")
28+
29+
// With options
30+
options := make(map[string]string)
31+
options["page"] = "2"
32+
options["language"] = "es-ES"
33+
34+
trending, err = tmdbClient.GetTrending("tv", "day", options)
35+
36+
if err != nil {
37+
fmt.Println(err)
38+
}
39+
40+
for _, result := range trending.Results {
41+
fmt.Println(result.Name)
42+
}
43+
}

trending.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
)
66

7-
// Trending type is a struct for treding JSON response.
7+
// Trending type is a struct for trending JSON response.
88
type Trending struct {
99
Page int `json:"page"`
1010
*TrendingResults
@@ -19,18 +19,18 @@ type Trending struct {
1919
// list tracks items over a 7 day period, with a 7 day
2020
// half life.
2121
//
22-
// Valid Media Types
22+
// # Valid Media Types
2323
//
2424
// all - Include all movies, TV shows and people in the
2525
// results as a global trending list.
2626
//
2727
// movie - Show the trending movies in the results.
2828
//
29-
// tv - Show the trending tv shows in the results.
29+
// tv - Show the trending TV shows in the results.
3030
//
3131
// person - Show the trending people in the results.
3232
//
33-
// Valid Time Windows
33+
// # Valid Time Windows
3434
//
3535
// day - View the trending list for the day.
3636
//
@@ -40,6 +40,7 @@ type Trending struct {
4040
func (c *Client) GetTrending(
4141
mediaType string,
4242
timeWindow string,
43+
options map[string]string,
4344
) (*Trending, error) {
4445
tmdbURL := fmt.Sprintf(
4546
"%s/trending/%s/%s?api_key=%s",
@@ -48,6 +49,11 @@ func (c *Client) GetTrending(
4849
timeWindow,
4950
c.apiKey,
5051
)
52+
if options != nil {
53+
for key, value := range options {
54+
tmdbURL += fmt.Sprintf("&%s=%s", key, value)
55+
}
56+
}
5157
trending := Trending{}
5258
if err := c.get(tmdbURL, &trending); err != nil {
5359
return nil, err

trending_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package tmdb
22

33
func (suite *TMBDTestSuite) TestGetTrending() {
4-
trending, err := suite.client.GetTrending("movie", "day")
4+
trending, err := suite.client.GetTrending("movie", "day", nil)
5+
suite.Nil(err)
6+
suite.NotNil(trending)
7+
}
8+
9+
func (suite *TMBDTestSuite) TestGetTrendingWithOptions() {
10+
options := map[string]string{
11+
"language": "en-US",
12+
"region": "US",
13+
}
14+
trending, err := suite.client.GetTrending("tv", "week", options)
515
suite.Nil(err)
616
suite.NotNil(trending)
717
}
818

919
func (suite *TMBDTestSuite) TestGetTrendingFail() {
1020
suite.client.apiKey = ""
11-
_, err := suite.client.GetTrending("movie", "day")
21+
_, err := suite.client.GetTrending("movie", "day", nil)
1222
suite.NotNil(err)
1323
}

0 commit comments

Comments
 (0)