36 lines
771 B
Go
36 lines
771 B
Go
package tmdb
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
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)
|
|
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")
|
|
}
|
|
movie := Movie{}
|
|
json.Unmarshal(body, &movie)
|
|
return &movie, nil
|
|
}
|