Adding proper use of url package in Http package and more fixes
This commit is contained in:
parent
20b1904f2d
commit
ebb45fff90
1 changed files with 27 additions and 17 deletions
44
imdb.go
44
imdb.go
|
@ -20,15 +20,14 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
//"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
//=======================================================================
|
//=======================================================================
|
||||||
// Const
|
// Const
|
||||||
//=======================================================================
|
//=======================================================================
|
||||||
const baseUrl string = "http://www.omdbapi.com/?"
|
const baseUrl string = "http://www.omdbapi.com"
|
||||||
const plot string = "full"
|
const plot string = "full"
|
||||||
const tomatoes string = "true"
|
const tomatoes string = "true"
|
||||||
|
|
||||||
|
@ -108,7 +107,7 @@ func SearchMovies(title string, year string) (*SearchResponse, error) {
|
||||||
|
|
||||||
r := new(SearchResponse)
|
r := new(SearchResponse)
|
||||||
err = json.NewDecoder(resp.Body).Decode(r)
|
err = json.NewDecoder(resp.Body).Decode(r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -129,7 +128,7 @@ func GetMovieByTitle(title string, year string) (*MovieResult, error) {
|
||||||
|
|
||||||
r := new(MovieResult)
|
r := new(MovieResult)
|
||||||
err = json.NewDecoder(resp.Body).Decode(r)
|
err = json.NewDecoder(resp.Body).Decode(r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -149,7 +148,7 @@ func GetMovieByImdbId(id string) (*MovieResult, error) {
|
||||||
|
|
||||||
r := new(MovieResult)
|
r := new(MovieResult)
|
||||||
err = json.NewDecoder(resp.Body).Decode(r)
|
err = json.NewDecoder(resp.Body).Decode(r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -165,20 +164,31 @@ func omdbApiRequest(s string, i string, t string, y string) (resp *http.Response
|
||||||
//t = Title Parameter, if this is != nil then its a getMovieByTitle
|
//t = Title Parameter, if this is != nil then its a getMovieByTitle
|
||||||
//y = Year Parameter, Optional data for s and t search
|
//y = Year Parameter, Optional data for s and t search
|
||||||
//var res http.Response
|
//var res http.Response
|
||||||
var url string
|
var Url *url.URL
|
||||||
if s != "" {
|
Url, err = url.Parse(baseUrl)
|
||||||
s = strings.Replace(s, " ", "%20", -1)
|
|
||||||
url = fmt.Sprintf(baseUrl+"s=%s&y=%s", s, y)
|
if err != nil {
|
||||||
} else if i != "" {
|
return nil, err
|
||||||
url = fmt.Sprintf(baseUrl+"i=%s&plot=%s&tomatoes=%s", i, plot, tomatoes)
|
}
|
||||||
} else if t != "" {
|
Url.Path += "/"
|
||||||
t = strings.Replace(t, " ", "%20", -1)
|
parameters := url.Values{}
|
||||||
url = fmt.Sprintf(baseUrl+"t=%s&plot=%s&tomatoes=%s&y=%s", t, plot, tomatoes, y)
|
if len(s) > 0 {
|
||||||
|
parameters.Add("s", s)
|
||||||
|
parameters.Add("y", y)
|
||||||
|
} else if len(i) > 0 {
|
||||||
|
parameters.Add("i", i)
|
||||||
|
parameters.Add("plot", plot)
|
||||||
|
parameters.Add("tomatoes", tomatoes)
|
||||||
|
} else if len(t) > 0 {
|
||||||
|
parameters.Add("t", t)
|
||||||
|
parameters.Add("plot", plot)
|
||||||
|
parameters.Add("tomatoes", tomatoes)
|
||||||
|
parameters.Add("y", y)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("Invalid Request")
|
return nil, errors.New("Invalid Request")
|
||||||
}
|
}
|
||||||
//log.Print(url) //DEBUG
|
Url.RawQuery = parameters.Encode()
|
||||||
res, err := http.Get(url)
|
res, err := http.Get(Url.String())
|
||||||
err = checkErrorStatus(res.StatusCode)
|
err = checkErrorStatus(res.StatusCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in a new issue