Cleanup: Made the search API more elegant

- Correcting the tests (They were missing return statements)
This commit is contained in:
Agniva De Sarker 2016-06-14 22:22:21 +05:30
parent e749b6a774
commit 683c3c5411
2 changed files with 42 additions and 35 deletions

View file

@ -15,6 +15,13 @@ const (
tomatoes = "true" tomatoes = "true"
) )
type QueryData struct {
Title string
Year string
ImdbId string
Type string
}
//SearchResult is the type for the search results //SearchResult is the type for the search results
type SearchResult struct { type SearchResult struct {
Title string Title string
@ -28,6 +35,7 @@ type SearchResponse struct {
Search []SearchResult Search []SearchResult
Response string Response string
Error string Error string
totalResults int
} }
//MovieResult is the result struct of an specific movie search //MovieResult is the result struct of an specific movie search
@ -70,13 +78,9 @@ type MovieResult struct {
Error string Error string
} }
//======================================================================= //Search for movies given a Title and year, Year is optional you can pass nil
// Funcs func Search(query *QueryData) (*SearchResponse, error) {
//======================================================================= resp, err := requestAPI("search", query.Title, query.Year)
//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)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -96,8 +100,8 @@ func Search(title string, year string) (*SearchResponse, error) {
} }
//MovieByTitle returns a MovieResult given Title //MovieByTitle returns a MovieResult given Title
func MovieByTitle(title string, year string) (*MovieResult, error) { func MovieByTitle(query *QueryData) (*MovieResult, error) {
resp, err := requestAPI("", "", title, year) resp, err := requestAPI("title", query.Title, query.Year)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -117,7 +121,7 @@ func MovieByTitle(title string, year string) (*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 MovieByImdbID(id string) (*MovieResult, error) {
resp, err := requestAPI("", id, "", "") resp, err := requestAPI("id", id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -135,13 +139,11 @@ func MovieByImdbID(id string) (*MovieResult, error) {
return r, nil return r, nil
} }
func requestAPI(s string, i string, t string, y string) (resp *http.Response, err error) { // helper function to call the API
//s = Search Parameter, if this is != nil then its a searchMovies // param: apiCategory refers to which API we are calling. Can be "search", "title" or "id"
//i = Id Parameter, if this is != nil then its a getMovieByImdbID // Depending on that value, we will search by "t" or "s" or "i"
//t = Title Parameter, if this is != nil then its a getMovieByTitle // param: params are the variadic list of params passed for that category
//y = Year Parameter, Optional data for s and t search func requestAPI(apiCategory string, params ...string) (resp *http.Response, err error) {
//var res http.Response
var URL *url.URL var URL *url.URL
URL, err = url.Parse(baseURL) 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 += "/" URL.Path += "/"
parameters := url.Values{} parameters := url.Values{}
if len(s) > 0 { switch apiCategory {
parameters.Add("s", s) case "search":
parameters.Add("y", y) parameters.Add("s", params[0])
} else if len(i) > 0 { parameters.Add("y", params[1])
parameters.Add("i", i) case "title":
parameters.Add("t", params[0])
parameters.Add("y", params[1])
parameters.Add("plot", plot) parameters.Add("plot", plot)
parameters.Add("tomatoes", tomatoes) parameters.Add("tomatoes", tomatoes)
} else if len(t) > 0 { case "id":
parameters.Add("t", t) parameters.Add("i", params[0])
parameters.Add("plot", plot) parameters.Add("plot", plot)
parameters.Add("tomatoes", tomatoes) parameters.Add("tomatoes", tomatoes)
parameters.Add("y", y)
} else {
return nil, errors.New("Invalid Request")
} }
URL.RawQuery = parameters.Encode() URL.RawQuery = parameters.Encode()
res, err := http.Get(URL.String()) res, err := http.Get(URL.String())
err = checkErr(res.StatusCode) err = checkErr(res.StatusCode)

View file

@ -3,9 +3,11 @@ package gomdb
import "testing" import "testing"
func TestImdbSearchMovies(t *testing.T) { 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 { if err != nil {
t.Error("Failed Search Movies") t.Error(err)
return
} }
if resp.Search[0].Title != "Fight Club" { if resp.Search[0].Title != "Fight Club" {
t.Error("Wrong Movie") t.Error("Wrong Movie")
@ -13,9 +15,11 @@ func TestImdbSearchMovies(t *testing.T) {
} }
func TestImdbGetMovieByTitle(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 { if err != nil {
t.Error("Failed GetMovieByTitle") t.Error(err)
return
} }
if resp.Title != "Fight Club" { if resp.Title != "Fight Club" {
t.Error("Wrong Movie") t.Error("Wrong Movie")
@ -25,7 +29,8 @@ func TestImdbGetMovieByTitle(t *testing.T) {
func TestImdbGetMovieByImdbID(t *testing.T) { func TestImdbGetMovieByImdbID(t *testing.T) {
resp, err := MovieByImdbID("tt0137523") resp, err := MovieByImdbID("tt0137523")
if err != nil { if err != nil {
t.Error("Failed GetMovieByImdbID") t.Error(err)
return
} }
if resp.Title != "Fight Club" { if resp.Title != "Fight Club" {
t.Error("Wrong Movie") t.Error("Wrong Movie")