diff --git a/tmdb/config.go b/tmdb/config.go new file mode 100644 index 0000000..b58df22 --- /dev/null +++ b/tmdb/config.go @@ -0,0 +1,32 @@ +package tmdb + +import ( + "encoding/json" + + "github.com/pkg/errors" +) + +type Config struct { + Image Image `json:"images"` +} + +type Image struct { + BaseURL string `json:"base_url"` + SecureBaseURL string `json:"secure_base_url"` + BackdropSizes []string `json:"backdrop_sizes"` + LogoSizes []string `json:"logo_sizes"` + PosterSizes []string `json:"poster_sizes"` + ProfileSizes []string `json:"profile_sizes"` + StillSizes []string `json:"still_sizes"` +} + +func GetConfig(token string) (*Config, error) { + url := "https://api.themoviedb.org/3/configuration" + data, err := tmdbGet(url, token) + if err != nil { + return nil, errors.Wrap(err, "tmdbGet") + } + config := Config{} + json.Unmarshal(data, &config) + return &config, nil +} diff --git a/tmdb/movie.go b/tmdb/movie.go index 8e081f5..a3b77f7 100644 --- a/tmdb/movie.go +++ b/tmdb/movie.go @@ -3,33 +3,43 @@ package tmdb import ( "encoding/json" "fmt" - "io" - "net/http" "github.com/pkg/errors" ) +type Movie struct { + Adult bool `json:"adult"` + Backdrop string `json:"backdrop_path"` + Collection string `json:"belongs_to_collection"` + Budget int `json:"budget"` + Genres []Genre `json:"genres"` + Homepage string `json:"homepage"` + ID int32 `json:"id"` + IMDbID string `json:"imdb_id"` + OriginalLanguage string `json:"original_language"` + OriginalTitle string `json:"original_title"` + Overview string `json:"overview"` + Popularity float32 `json:"popularity"` + Poster string `json:"poster_path"` + ProductionCompanies []ProductionCompany `json:"production_companies"` + ProductionCountries []ProductionCountry `json:"production_countries"` + ReleaseDate string `json:"release_date"` + Revenue int `json:"revenue"` + Runtime int `json:"runtime"` + SpokenLanguages []SpokenLanguage `json:"spoken_languages"` + Status string `json:"status"` + Tagline string `json:"tagline"` + Title string `json:"title"` + Video bool `json:"video"` +} + func GetMovie(id int32, token string) (*Movie, error) { url := fmt.Sprintf("https://api.themoviedb.org/3/movie/%v?language=en-US", id) - - req, err := http.NewRequest("GET", url, nil) + data, err := tmdbGet(url, token) if err != nil { - return nil, errors.Wrap(err, "http.NewRequest") - } - - req.Header.Add("accept", "application/json") - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) - - res, err := http.DefaultClient.Do(req) - if err != nil { - return nil, errors.Wrap(err, "http.DefaultClient.Do") - } - defer res.Body.Close() - body, err := io.ReadAll(res.Body) - if err != nil { - return nil, errors.Wrap(err, "io.ReadAll") + return nil, errors.Wrap(err, "tmdbGet") } movie := Movie{} - json.Unmarshal(body, &movie) + json.Unmarshal(data, &movie) return &movie, nil } diff --git a/tmdb/request.go b/tmdb/request.go new file mode 100644 index 0000000..6c454b4 --- /dev/null +++ b/tmdb/request.go @@ -0,0 +1,28 @@ +package tmdb + +import ( + "fmt" + "io" + "net/http" + + "github.com/pkg/errors" +) + +func tmdbGet(url string, token string) ([]byte, error) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, errors.Wrap(err, "http.NewRequest") + } + req.Header.Add("accept", "application/json") + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) + res, err := http.DefaultClient.Do(req) + if err != nil { + return nil, errors.Wrap(err, "http.DefaultClient.Do") + } + defer res.Body.Close() + body, err := io.ReadAll(res.Body) + if err != nil { + return nil, errors.Wrap(err, "io.ReadAll") + } + return body, nil +} diff --git a/tmdb/structs.go b/tmdb/structs.go index fd7c04a..0929dbd 100644 --- a/tmdb/structs.go +++ b/tmdb/structs.go @@ -4,32 +4,6 @@ import ( // "encoding/json" ) -type Movie struct { - Adult bool `json:"adult"` - Backdrop string `json:"backdrop_path"` - Collection string `json:"belongs_to_collection"` - Budget int `json:"budget"` - Genres []Genre `json:"genres"` - Homepage string `json:"homepage"` - ID int32 `json:"id"` - IMDbID string `json:"imdb_id"` - OriginalLanguage string `json:"original_language"` - OriginalTitle string `json:"original_title"` - Overview string `json:"overview"` - Popularity float32 `json:"popularity"` - Poster string `json:"poster_path"` - ProductionCompanies []ProductionCompany `json:"production_companies"` - ProductionCountries []ProductionCountry `json:"production_countries"` - ReleaseDate string `json:"release_date"` - Revenue int `json:"revenue"` - Runtime int `json:"runtime"` - SpokenLanguages []SpokenLanguage `json:"spoken_languages"` - Status string `json:"status"` - Tagline string `json:"tagline"` - Title string `json:"title"` - Video bool `json:"video"` -} - type Genre struct { ID int `json:"id"` Name string `json:"name"`