Cleanup: Made the search API more elegant
- Correcting the tests (They were missing return statements)
This commit is contained in:
parent
e749b6a774
commit
683c3c5411
2 changed files with 42 additions and 35 deletions
56
gomdb.go
56
gomdb.go
|
@ -15,6 +15,13 @@ const (
|
|||
tomatoes = "true"
|
||||
)
|
||||
|
||||
type QueryData struct {
|
||||
Title string
|
||||
Year string
|
||||
ImdbId string
|
||||
Type string
|
||||
}
|
||||
|
||||
//SearchResult is the type for the search results
|
||||
type SearchResult struct {
|
||||
Title string
|
||||
|
@ -28,6 +35,7 @@ type SearchResponse struct {
|
|||
Search []SearchResult
|
||||
Response string
|
||||
Error string
|
||||
totalResults int
|
||||
}
|
||||
|
||||
//MovieResult is the result struct of an specific movie search
|
||||
|
@ -70,13 +78,9 @@ type MovieResult struct {
|
|||
Error string
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// Funcs
|
||||
//=======================================================================
|
||||
|
||||
//Search search for movies given a Title and year, Year is optional you can pass nil
|
||||
func Search(title string, year string) (*SearchResponse, error) {
|
||||
resp, err := requestAPI(title, "", "", year)
|
||||
//Search for movies given a Title and year, Year is optional you can pass nil
|
||||
func Search(query *QueryData) (*SearchResponse, error) {
|
||||
resp, err := requestAPI("search", query.Title, query.Year)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -96,8 +100,8 @@ func Search(title string, year string) (*SearchResponse, error) {
|
|||
}
|
||||
|
||||
//MovieByTitle returns a MovieResult given Title
|
||||
func MovieByTitle(title string, year string) (*MovieResult, error) {
|
||||
resp, err := requestAPI("", "", title, year)
|
||||
func MovieByTitle(query *QueryData) (*MovieResult, error) {
|
||||
resp, err := requestAPI("title", query.Title, query.Year)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -117,7 +121,7 @@ func MovieByTitle(title string, year string) (*MovieResult, error) {
|
|||
|
||||
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
|
||||
func MovieByImdbID(id string) (*MovieResult, error) {
|
||||
resp, err := requestAPI("", id, "", "")
|
||||
resp, err := requestAPI("id", id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -135,13 +139,11 @@ func MovieByImdbID(id string) (*MovieResult, error) {
|
|||
return r, nil
|
||||
}
|
||||
|
||||
func requestAPI(s string, i string, t string, y string) (resp *http.Response, err error) {
|
||||
//s = Search Parameter, if this is != nil then its a searchMovies
|
||||
//i = Id Parameter, if this is != nil then its a getMovieByImdbID
|
||||
//t = Title Parameter, if this is != nil then its a getMovieByTitle
|
||||
//y = Year Parameter, Optional data for s and t search
|
||||
//var res http.Response
|
||||
|
||||
// helper function to call the API
|
||||
// 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"
|
||||
// param: params are the variadic list of params passed for that category
|
||||
func requestAPI(apiCategory string, params ...string) (resp *http.Response, err error) {
|
||||
var URL *url.URL
|
||||
URL, err = url.Parse(baseURL)
|
||||
|
||||
|
@ -150,21 +152,21 @@ func requestAPI(s string, i string, t string, y string) (resp *http.Response, er
|
|||
}
|
||||
URL.Path += "/"
|
||||
parameters := url.Values{}
|
||||
if len(s) > 0 {
|
||||
parameters.Add("s", s)
|
||||
parameters.Add("y", y)
|
||||
} else if len(i) > 0 {
|
||||
parameters.Add("i", i)
|
||||
switch apiCategory {
|
||||
case "search":
|
||||
parameters.Add("s", params[0])
|
||||
parameters.Add("y", params[1])
|
||||
case "title":
|
||||
parameters.Add("t", params[0])
|
||||
parameters.Add("y", params[1])
|
||||
parameters.Add("plot", plot)
|
||||
parameters.Add("tomatoes", tomatoes)
|
||||
} else if len(t) > 0 {
|
||||
parameters.Add("t", t)
|
||||
case "id":
|
||||
parameters.Add("i", params[0])
|
||||
parameters.Add("plot", plot)
|
||||
parameters.Add("tomatoes", tomatoes)
|
||||
parameters.Add("y", y)
|
||||
} else {
|
||||
return nil, errors.New("Invalid Request")
|
||||
}
|
||||
|
||||
URL.RawQuery = parameters.Encode()
|
||||
res, err := http.Get(URL.String())
|
||||
err = checkErr(res.StatusCode)
|
||||
|
|
|
@ -3,9 +3,11 @@ package gomdb
|
|||
import "testing"
|
||||
|
||||
func TestImdbSearchMovies(t *testing.T) {
|
||||
resp, err := Search("Fight Club", "1999")
|
||||
query := &QueryData{Title: "Fight Club", Year: "1999"}
|
||||
resp, err := Search(query)
|
||||
if err != nil {
|
||||
t.Error("Failed Search Movies")
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if resp.Search[0].Title != "Fight Club" {
|
||||
t.Error("Wrong Movie")
|
||||
|
@ -13,9 +15,11 @@ func TestImdbSearchMovies(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestImdbGetMovieByTitle(t *testing.T) {
|
||||
resp, err := MovieByTitle("Fight Club", "1999")
|
||||
query := &QueryData{Title: "Fight Club", Year: "1999"}
|
||||
resp, err := MovieByTitle(query)
|
||||
if err != nil {
|
||||
t.Error("Failed GetMovieByTitle")
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if resp.Title != "Fight Club" {
|
||||
t.Error("Wrong Movie")
|
||||
|
@ -25,7 +29,8 @@ func TestImdbGetMovieByTitle(t *testing.T) {
|
|||
func TestImdbGetMovieByImdbID(t *testing.T) {
|
||||
resp, err := MovieByImdbID("tt0137523")
|
||||
if err != nil {
|
||||
t.Error("Failed GetMovieByImdbID")
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if resp.Title != "Fight Club" {
|
||||
t.Error("Wrong Movie")
|
||||
|
|
Loading…
Reference in a new issue