Add go mod, rename imdb -> omdb, fix a broken test
This commit is contained in:
parent
5f94d77032
commit
28ba3ae5fc
4 changed files with 24 additions and 14 deletions
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
|||
module git.minhas.io/asara/gomdb
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/davecgh/go-spew v1.1.1 // indirect
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -0,0 +1,2 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
27
gomdb.go
27
gomdb.go
|
@ -7,10 +7,12 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
const (
|
||||
baseURL = "http://www.omdbapi.com"
|
||||
baseURL = "https://www.omdbapi.com"
|
||||
plot = "full"
|
||||
tomatoes = "true"
|
||||
|
||||
|
@ -37,7 +39,7 @@ type QueryData struct {
|
|||
Episode string
|
||||
}
|
||||
|
||||
//SearchResult is the type for the search results
|
||||
// SearchResult is the type for the search results
|
||||
type SearchResult struct {
|
||||
Title string
|
||||
Year string
|
||||
|
@ -45,7 +47,7 @@ type SearchResult struct {
|
|||
Type string
|
||||
}
|
||||
|
||||
//SearchResponse is the struct of the response in a search
|
||||
// SearchResponse is the struct of the response in a search
|
||||
type SearchResponse struct {
|
||||
Search []SearchResult
|
||||
Response string
|
||||
|
@ -53,7 +55,7 @@ type SearchResponse struct {
|
|||
totalResults int
|
||||
}
|
||||
|
||||
//MovieResult is the result struct of an specific movie search
|
||||
// MovieResult is the result struct of an specific movie search
|
||||
type MovieResult struct {
|
||||
Title string
|
||||
Year string
|
||||
|
@ -93,7 +95,7 @@ type MovieResult struct {
|
|||
Error string
|
||||
}
|
||||
|
||||
//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 (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
|
||||
resp, err := api.requestAPI("search", query.Title, query.Year, query.SearchType)
|
||||
if err != nil {
|
||||
|
@ -114,7 +116,7 @@ func (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
|
|||
return r, nil
|
||||
}
|
||||
|
||||
//MovieByTitle returns a MovieResult given Title
|
||||
// MovieByTitle returns a MovieResult given Title
|
||||
func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
|
||||
resp, err := api.requestAPI("title", query.Title, query.Year, query.SearchType, query.Season,
|
||||
query.Episode)
|
||||
|
@ -135,10 +137,11 @@ func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
|
|||
return r, nil
|
||||
}
|
||||
|
||||
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
|
||||
// MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
|
||||
func (api *OmdbApi) MovieByImdbID(query *QueryData) (*MovieResult, error) {
|
||||
resp, err := api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season,
|
||||
query.Episode)
|
||||
spew.Dump(api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season, query.Episode))
|
||||
|
||||
resp, err := api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season, query.Episode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -212,17 +215,17 @@ func (api *OmdbApi) requestAPI(apiCategory string, params ...string) (resp *http
|
|||
|
||||
func checkErr(status int) error {
|
||||
if status != 200 {
|
||||
return fmt.Errorf("Status Code %d received from IMDB", status)
|
||||
return fmt.Errorf("Status Code %d received from OMDb", status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//Stringer Interface for MovieResult
|
||||
// Stringer Interface for MovieResult
|
||||
func (mr MovieResult) String() string {
|
||||
return fmt.Sprintf("#%s: %s (%s)", mr.ImdbID, mr.Title, mr.Year)
|
||||
}
|
||||
|
||||
//Stringer Interface for SearchResult
|
||||
// Stringer Interface for SearchResult
|
||||
func (sr SearchResult) String() string {
|
||||
return fmt.Sprintf("#%s: %s (%s) Type: %s", sr.ImdbID, sr.Title, sr.Year, sr.Type)
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ func TestNoKey(t *testing.T) {
|
|||
t.Errorf("Expected to fail")
|
||||
}
|
||||
if err != nil {
|
||||
expectedErrorMsg := "Status Code 401 received from IMDB"
|
||||
expectedErrorMsg := "Status Code 401 received from OMDb"
|
||||
if err.Error() != expectedErrorMsg {
|
||||
t.Errorf("Expected- %s, Got- %s", expectedErrorMsg, err)
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ func TestMediaByImdbID(t *testing.T) {
|
|||
},
|
||||
{&QueryData{ImdbId: "tt3952222", Season: "1", SearchType: SeriesSearch},
|
||||
"Killjoys",
|
||||
"2015–",
|
||||
"2015–2019",
|
||||
},
|
||||
{&QueryData{ImdbId: "tt0944947", Season: "1", Episode: "1", SearchType: EpisodeSearch},
|
||||
"Winter Is Coming",
|
||||
|
|
Loading…
Reference in a new issue