Add go mod, rename imdb -> omdb, fix a broken test

This commit is contained in:
Amarpreet Minhas 2022-09-10 20:22:12 -04:00
parent 5f94d77032
commit 28ba3ae5fc
4 changed files with 24 additions and 14 deletions

5
go.mod Normal file
View 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
View 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=

View file

@ -7,10 +7,12 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"github.com/davecgh/go-spew/spew"
) )
const ( const (
baseURL = "http://www.omdbapi.com" baseURL = "https://www.omdbapi.com"
plot = "full" plot = "full"
tomatoes = "true" tomatoes = "true"
@ -37,7 +39,7 @@ type QueryData struct {
Episode string Episode 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
Year string Year string
@ -45,7 +47,7 @@ type SearchResult struct {
Type string 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 { type SearchResponse struct {
Search []SearchResult Search []SearchResult
Response string Response string
@ -53,7 +55,7 @@ type SearchResponse struct {
totalResults int 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 { type MovieResult struct {
Title string Title string
Year string Year string
@ -93,7 +95,7 @@ type MovieResult struct {
Error string 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) { func (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
resp, err := api.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 {
@ -114,7 +116,7 @@ func (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
return r, nil return r, nil
} }
//MovieByTitle returns a MovieResult given Title // MovieByTitle returns a MovieResult given Title
func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) { func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
resp, err := api.requestAPI("title", query.Title, query.Year, query.SearchType, query.Season, resp, err := api.requestAPI("title", query.Title, query.Year, query.SearchType, query.Season,
query.Episode) query.Episode)
@ -135,10 +137,11 @@ func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
return r, nil 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) { func (api *OmdbApi) MovieByImdbID(query *QueryData) (*MovieResult, error) {
resp, err := api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season, spew.Dump(api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season, query.Episode))
query.Episode)
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
} }
@ -212,17 +215,17 @@ func (api *OmdbApi) requestAPI(apiCategory string, params ...string) (resp *http
func checkErr(status int) error { func checkErr(status int) error {
if status != 200 { 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 return nil
} }
//Stringer Interface for MovieResult // Stringer Interface for MovieResult
func (mr MovieResult) String() string { func (mr MovieResult) String() string {
return fmt.Sprintf("#%s: %s (%s)", mr.ImdbID, mr.Title, mr.Year) 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 { func (sr SearchResult) String() string {
return fmt.Sprintf("#%s: %s (%s) Type: %s", sr.ImdbID, sr.Title, sr.Year, sr.Type) return fmt.Sprintf("#%s: %s (%s) Type: %s", sr.ImdbID, sr.Title, sr.Year, sr.Type)
} }

View file

@ -15,7 +15,7 @@ func TestNoKey(t *testing.T) {
t.Errorf("Expected to fail") t.Errorf("Expected to fail")
} }
if err != nil { if err != nil {
expectedErrorMsg := "Status Code 401 received from IMDB" expectedErrorMsg := "Status Code 401 received from OMDb"
if err.Error() != expectedErrorMsg { if err.Error() != expectedErrorMsg {
t.Errorf("Expected- %s, Got- %s", expectedErrorMsg, err) t.Errorf("Expected- %s, Got- %s", expectedErrorMsg, err)
} }
@ -178,7 +178,7 @@ func TestMediaByImdbID(t *testing.T) {
}, },
{&QueryData{ImdbId: "tt3952222", Season: "1", SearchType: SeriesSearch}, {&QueryData{ImdbId: "tt3952222", Season: "1", SearchType: SeriesSearch},
"Killjoys", "Killjoys",
"2015", "20152019",
}, },
{&QueryData{ImdbId: "tt0944947", Season: "1", Episode: "1", SearchType: EpisodeSearch}, {&QueryData{ImdbId: "tt0944947", Season: "1", Episode: "1", SearchType: EpisodeSearch},
"Winter Is Coming", "Winter Is Coming",