Merge pull request #12 from alexguzun/master

add support for API keys
This commit is contained in:
Christopher Herrera 2017-12-06 11:31:29 -04:00 committed by GitHub
commit 8a1e0abd44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 17 deletions

View file

@ -31,8 +31,9 @@ import (
) )
func main() { func main() {
api := gomdb.Init(YOUR_API_KEY)
query := &gomdb.QueryData{Title: "Macbeth", SearchType: gomdb.MovieSearch} query := &gomdb.QueryData{Title: "Macbeth", SearchType: gomdb.MovieSearch}
res, err := gomdb.Search(query) res, err := api.Search(query)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
@ -40,14 +41,14 @@ func main() {
fmt.Println(res.Search) fmt.Println(res.Search)
query = &gomdb.QueryData{Title: "Macbeth", Year: "2015"} query = &gomdb.QueryData{Title: "Macbeth", Year: "2015"}
res2, err := gomdb.MovieByTitle(query) res2, err := api.MovieByTitle(query)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
fmt.Println(res2) fmt.Println(res2)
res3, err := gomdb.MovieByImdbID("tt2884018") res3, err := api.MovieByImdbID("tt2884018")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View file

@ -19,6 +19,14 @@ const (
EpisodeSearch = "episode" EpisodeSearch = "episode"
) )
type OmdbApi struct {
apiKey string
}
func Init(apiKey string) *OmdbApi {
return &OmdbApi{apiKey: apiKey}
}
// QueryData is the type to create the search query // QueryData is the type to create the search query
type QueryData struct { type QueryData struct {
Title string Title string
@ -84,8 +92,8 @@ type MovieResult struct {
} }
//Search for movies given a Title and year, Year is optional you can pass nil //Search for movies given a Title and year, Year is optional you can pass nil
func Search(query *QueryData) (*SearchResponse, error) { func (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
resp, err := requestAPI("search", query.Title, query.Year, query.SearchType) resp, err := api.requestAPI("search", query.Title, query.Year, query.SearchType)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -105,8 +113,8 @@ func Search(query *QueryData) (*SearchResponse, error) {
} }
//MovieByTitle returns a MovieResult given Title //MovieByTitle returns a MovieResult given Title
func MovieByTitle(query *QueryData) (*MovieResult, error) { func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
resp, err := requestAPI("title", query.Title, query.Year, query.SearchType) resp, err := api.requestAPI("title", query.Title, query.Year, query.SearchType)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -125,8 +133,8 @@ func MovieByTitle(query *QueryData) (*MovieResult, error) {
} }
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381" //MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
func MovieByImdbID(id string) (*MovieResult, error) { func (api *OmdbApi) MovieByImdbID(id string) (*MovieResult, error) {
resp, err := requestAPI("id", id) resp, err := api.requestAPI("id", id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -148,7 +156,7 @@ func MovieByImdbID(id string) (*MovieResult, error) {
// param: apiCategory refers to which API we are calling. Can be "search", "title" or "id" // param: apiCategory refers to which API we are calling. Can be "search", "title" or "id"
// Depending on that value, we will search by "t" or "s" or "i" // Depending on that value, we will search by "t" or "s" or "i"
// param: params are the variadic list of params passed for that category // param: params are the variadic list of params passed for that category
func requestAPI(apiCategory string, params ...string) (resp *http.Response, err error) { func (api *OmdbApi) requestAPI(apiCategory string, params ...string) (resp *http.Response, err error) {
var URL *url.URL var URL *url.URL
URL, err = url.Parse(baseURL) URL, err = url.Parse(baseURL)
if err != nil { if err != nil {
@ -165,6 +173,8 @@ func requestAPI(apiCategory string, params ...string) (resp *http.Response, err
} }
URL.Path += "/" URL.Path += "/"
parameters := url.Values{} parameters := url.Values{}
parameters.Add("apikey", api.apiKey)
switch apiCategory { switch apiCategory {
case "search": case "search":
parameters.Add("s", params[0]) parameters.Add("s", params[0])

View file

@ -1,6 +1,25 @@
package gomdb package gomdb
import "testing" import (
"os"
"testing"
)
var apiKey = os.Getenv("OMDB_API_KEY")
func TestNoKey(t *testing.T) {
api := Init("")
_, err := api.Search(&QueryData{Title: "Her"})
if err == nil {
t.Errorf("Expected to fail")
}
if err != nil {
expectedErrorMsg := "Status Code 401 received from IMDB"
if err.Error() != expectedErrorMsg {
t.Errorf("Expected- %s, Got- %s", expectedErrorMsg, err)
}
}
}
func TestSearch(t *testing.T) { func TestSearch(t *testing.T) {
tests := []struct { tests := []struct {
@ -21,9 +40,9 @@ func TestSearch(t *testing.T) {
"2015", "2015",
}, },
} }
api := Init(apiKey)
for i, item := range tests { for i, item := range tests {
resp, err := Search(item.query) resp, err := api.Search(item.query)
if err != nil { if err != nil {
t.Errorf("Test[%d]: %s", i, err) t.Errorf("Test[%d]: %s", i, err)
continue continue
@ -47,8 +66,9 @@ func TestFailSearch(t *testing.T) {
{&QueryData{Title: "Dexter", SearchType: EpisodeSearch}}, {&QueryData{Title: "Dexter", SearchType: EpisodeSearch}},
} }
api := Init(apiKey)
for i, item := range tests { for i, item := range tests {
_, err := Search(item.query) _, err := api.Search(item.query)
if err == nil { if err == nil {
t.Errorf("Test[%d]: Got nil error", i) t.Errorf("Test[%d]: Got nil error", i)
continue continue
@ -69,8 +89,9 @@ func TestInvalidCategory(t *testing.T) {
{&QueryData{Title: "Dexter", SearchType: "bad"}}, {&QueryData{Title: "Dexter", SearchType: "bad"}},
} }
api := Init(apiKey)
for i, item := range tests { for i, item := range tests {
_, err := Search(item.query) _, err := api.Search(item.query)
if err == nil { if err == nil {
t.Errorf("Test[%d]: Got nil error", i) t.Errorf("Test[%d]: Got nil error", i)
continue continue
@ -103,8 +124,10 @@ func TestMovieByTitle(t *testing.T) {
}, },
} }
api := Init(apiKey)
for i, item := range tests { for i, item := range tests {
resp, err := MovieByTitle(item.query) resp, err := api.MovieByTitle(item.query)
if err != nil { if err != nil {
t.Errorf("Test[%d]: %s", i, err) t.Errorf("Test[%d]: %s", i, err)
continue continue
@ -143,8 +166,10 @@ func TestMovieByImdbID(t *testing.T) {
}, },
} }
api := Init(apiKey)
for i, item := range tests { for i, item := range tests {
resp, err := MovieByImdbID(item.id) resp, err := api.MovieByImdbID(item.id)
if err != nil { if err != nil {
t.Errorf("Test[%d]: %s", i, err) t.Errorf("Test[%d]: %s", i, err)
continue continue