Compare commits

..

5 commits

Author SHA1 Message Date
Dinko Korunic
5f94d77032 Update docs in regards to 4319c85. 2019-03-19 10:52:20 +01:00
Dinko Korunic
4319c85503 - Cleanup baseURL
- Extend QueryData to be able to use s and i queries with Season and
  Episode parameters
- Exnted MovieByImdbID() to include Season and Episode where
  appropriate,
- Refactor MovieByImdbID() to use QueryData as per standard API which
  also permits SearchType, Season and Episode filters
- Update unit tests for API changes
2019-03-19 10:41:02 +01:00
Christopher Herrera
8a1e0abd44
Merge pull request #12 from alexguzun/master
add support for API keys
2017-12-06 11:31:29 -04:00
Alexadnru Guzun
fffb9ce2cf add support for API keys 2017-05-24 13:33:12 +01:00
Christopher Herrera
561e54fa87 updating README.md
Adding the travis build badge and also changing the godoc reference to the top.
2016-06-20 14:19:23 -04:00
3 changed files with 114 additions and 33 deletions

View file

@ -1,5 +1,8 @@
The Golang Omdb API The Golang Omdb API
======= =======
[![Build Status](https://travis-ci.org/eefret/gomdb.svg?branch=master)](https://travis-ci.org/eefret/gomdb)
[![GoDoc](https://godoc.org/github.com/eefret/go-imdb?status.svg)](https://godoc.org/github.com/eefret/go-imdb)
Author: Christopher T. Herrera (eefretsoul AT gmail DOT com) Author: Christopher T. Herrera (eefretsoul AT gmail DOT com)
@ -28,8 +31,10 @@ 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
@ -37,26 +42,38 @@ 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") query = &gomdb.QueryData{Title: "Rick and Morty", Season: "1", Episode: "8", SearchType: gomdb.EpisodeSearch}
res3, err := api.MovieByTitle(query)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
fmt.Println(res3) fmt.Println(res3)
} }
query = &gomdb.QueryData{ImdbId: "tt2884018"}
res4, err := api.MovieByImdbID(query)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res4)
query = &gomdb.QueryData{ImdbId: "tt0944947", Season: "1", Episode: "1", SearchType: gomdb.EpisodeSearch}
res5, err := api.MovieByImdbID(query)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res5)
``` ```
See the project documentation to see the Response Objects and stuff See the project documentation to see the Response Objects and stuff
Project Documentation
---------------------
The automatically generated documentation can be found in godocs.
[![GoDoc](https://godoc.org/github.com/eefret/go-imdb?status.svg)](https://godoc.org/github.com/eefret/go-imdb)

View file

@ -10,7 +10,7 @@ import (
) )
const ( const (
baseURL = "http://www.omdbapi.com/?" baseURL = "http://www.omdbapi.com"
plot = "full" plot = "full"
tomatoes = "true" tomatoes = "true"
@ -19,12 +19,22 @@ 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
Year string Year string
ImdbId string ImdbId string
SearchType string SearchType string
Season string
Episode string
} }
//SearchResult is the type for the search results //SearchResult is the type for the search results
@ -84,8 +94,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 +115,9 @@ 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, query.Season,
query.Episode)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -125,8 +136,9 @@ 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(query *QueryData) (*MovieResult, error) {
resp, err := requestAPI("id", id) resp, err := api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season,
query.Episode)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -148,7 +160,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 +177,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])
@ -174,10 +188,15 @@ func requestAPI(apiCategory string, params ...string) (resp *http.Response, err
parameters.Add("t", params[0]) parameters.Add("t", params[0])
parameters.Add("y", params[1]) parameters.Add("y", params[1])
parameters.Add("type", params[2]) parameters.Add("type", params[2])
parameters.Add("Season", params[3])
parameters.Add("Episode", params[4])
parameters.Add("plot", plot) parameters.Add("plot", plot)
parameters.Add("tomatoes", tomatoes) parameters.Add("tomatoes", tomatoes)
case "id": case "id":
parameters.Add("i", params[0]) parameters.Add("i", params[0])
parameters.Add("type", params[2])
parameters.Add("Season", params[3])
parameters.Add("Episode", params[4])
parameters.Add("plot", plot) parameters.Add("plot", plot)
parameters.Add("tomatoes", tomatoes) parameters.Add("tomatoes", tomatoes)
} }

View file

@ -1,6 +1,26 @@
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 {
@ -22,8 +42,10 @@ func TestSearch(t *testing.T) {
}, },
} }
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,15 +69,17 @@ 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
} }
// Checking for strings is bad. But the API might change. // Checking for strings is bad. But the API might change.
if err.Error() != "Movie not found!" { if err.Error() != "Movie not found!" {
t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err) t.Errorf("Test[%d]: Unexpected value- %s", i, err)
continue continue
} }
} }
@ -69,21 +93,23 @@ 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
} }
// Checking for strings is bad. But the error type is formatted // Checking for strings is bad. But the error type is formatted
if err.Error() != "Invalid search category- bad" { if err.Error() != "Invalid search category- bad" {
t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err) t.Errorf("Test[%d]: Unexpected value- %s", i, err)
continue continue
} }
} }
} }
func TestMovieByTitle(t *testing.T) { func TestMediaByTitle(t *testing.T) {
tests := []struct { tests := []struct {
query *QueryData query *QueryData
title string title string
@ -101,10 +127,22 @@ func TestMovieByTitle(t *testing.T) {
"Macbeth", "Macbeth",
"2015", "2015",
}, },
{
&QueryData{Title: "Rick and Morty", Season: "1", SearchType: SeriesSearch},
"Rick and Morty",
"2013",
},
{
&QueryData{Title: "Rick and Morty", Season: "1", Episode: "8", SearchType: EpisodeSearch},
"Rixty Minutes",
"2014",
},
} }
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
@ -120,31 +158,38 @@ func TestMovieByTitle(t *testing.T) {
} }
} }
func TestMovieByImdbID(t *testing.T) { func TestMediaByImdbID(t *testing.T) {
tests := []struct { tests := []struct {
id string query *QueryData
title string title string
year string year string
}{ }{
{ {&QueryData{ImdbId: "tt0137523", SearchType: MovieSearch},
"tt0137523",
"Fight Club", "Fight Club",
"1999", "1999",
}, },
{ {&QueryData{ImdbId: "tt1798709", SearchType: MovieSearch},
"tt1798709",
"Her", "Her",
"2013", "2013",
}, },
{ {&QueryData{ImdbId: "tt2884018", SearchType: MovieSearch},
"tt2884018",
"Macbeth", "Macbeth",
"2015", "2015",
}, },
{&QueryData{ImdbId: "tt3952222", Season: "1", SearchType: SeriesSearch},
"Killjoys",
"2015",
},
{&QueryData{ImdbId: "tt0944947", Season: "1", Episode: "1", SearchType: EpisodeSearch},
"Winter Is Coming",
"2011",
},
} }
api := Init(apiKey)
for i, item := range tests { for i, item := range tests {
resp, err := MovieByImdbID(item.id) resp, err := api.MovieByImdbID(item.query)
if err != nil { if err != nil {
t.Errorf("Test[%d]: %s", i, err) t.Errorf("Test[%d]: %s", i, err)
continue continue