-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmdb.go
65 lines (57 loc) · 1.51 KB
/
tmdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2016 The tmdb Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package tmdb
import (
"io/ioutil"
"net/http"
"net/url"
"os"
)
const (
apiKey = "523587afe262c34af9ee7794c5f8de81"
baseURL = "http://api.themoviedb.org/3/"
)
// TMDB is a type that implements a client to The Movie Database API
//
// More info : https://www.themoviedb.org/documentation/api
// http://docs.themoviedb.apiary.io/
//
type TMDB struct {
// HTTP client used to communicate with the API.
client *http.Client
APIKey string
BaseURL *url.URL
}
// FetchContent ...
func (tmdb *TMDB) FetchContent(u *url.URL) (body []byte, resp *http.Response, err error) {
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return body, nil, err
}
req.Header.Add("Accept", "application/json")
resp, err = tmdb.client.Do(req)
if err != nil {
return body, resp, err
}
if resp != nil {
defer resp.Body.Close()
}
req.Close = true
body, err = ioutil.ReadAll(resp.Body)
return body, resp, err
}
// New allocates and initializes a new TMDB.
//
func New() *TMDB {
u, _ := url.Parse(baseURL)
return &TMDB{client: http.DefaultClient, APIKey: apiKey, BaseURL: u}
}
// NewTMDB allocates and initializes a new TMDB.
//
func NewTMDB() *TMDB { // apikey string, baseurl *url.URL) *TMDB {
apiKey := os.Getenv("API_KEY")
urlBase := os.Getenv("BASE_URL")
u, _ := url.Parse(urlBase)
return &TMDB{client: http.DefaultClient, APIKey: apiKey, BaseURL: u}
}