Logging improvements
This commit is contained in:
parent
8cfcb7a013
commit
7173271130
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
|||
module code.fmartingr.dev/fmartingr/go-mangadex
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/sirupsen/logrus v1.7.0
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
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=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
|
||||
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
46
main.go
46
main.go
|
@ -4,11 +4,12 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const APIBaseURL = "https://api.mangadex.org/v2/"
|
||||
|
@ -20,7 +21,7 @@ func DoRequest(method string, requestURL string) (*MangaDexResponse, error) {
|
|||
return &result, errParse
|
||||
}
|
||||
|
||||
log.Printf("[debug] request: %s", parsedURL)
|
||||
logrus.Tracef("Making request %s", parsedURL)
|
||||
request := http.Request{
|
||||
Method: method,
|
||||
URL: parsedURL,
|
||||
|
@ -31,28 +32,33 @@ func DoRequest(method string, requestURL string) (*MangaDexResponse, error) {
|
|||
|
||||
response, errResponse := http.DefaultClient.Do(&request)
|
||||
if errResponse != nil {
|
||||
log.Print(errResponse)
|
||||
logrus.Tracef("Request error: %s", errResponse)
|
||||
return &result, errResponse
|
||||
}
|
||||
|
||||
if response.StatusCode != 200 {
|
||||
log.Printf("[error] Status code != 200 -- %d", response.StatusCode)
|
||||
logrus.Tracef("Response status code not successful: %d", response.StatusCode)
|
||||
logrus.Tracef("Response body: %s", response.Body)
|
||||
return &result, errors.New(strconv.Itoa(response.StatusCode))
|
||||
}
|
||||
|
||||
logrus.Tracef("Response status code: %s", response.Status)
|
||||
|
||||
if response.Body != nil {
|
||||
defer response.Body.Close()
|
||||
}
|
||||
|
||||
body, errRead := ioutil.ReadAll(response.Body)
|
||||
if errRead != nil {
|
||||
log.Printf("[error] %s", errRead)
|
||||
logrus.Errorf("Error reading body: %s", errRead)
|
||||
return &result, errRead
|
||||
}
|
||||
|
||||
logrus.Tracef("Response body: %s", body)
|
||||
|
||||
errJSON := json.Unmarshal(body, &result)
|
||||
if errJSON != nil {
|
||||
log.Print(errJSON)
|
||||
logrus.Errorf("Error parsing body: %s", errJSON)
|
||||
return &result, errJSON
|
||||
}
|
||||
|
||||
|
@ -63,11 +69,13 @@ func (manga *Manga) GetCovers() ([]MangaCover, error) {
|
|||
var result []MangaCover
|
||||
response, errRequest := DoRequest("GET", APIBaseURL+path.Join("manga", strconv.Itoa(manga.ID), "covers"))
|
||||
if errRequest != nil {
|
||||
logrus.Errorf("Request error: %s", errRequest)
|
||||
return result, errRequest
|
||||
}
|
||||
|
||||
errJSON := json.Unmarshal(response.Data, &result)
|
||||
if errJSON != nil {
|
||||
logrus.Errorf("Error parsing JSON: %s", errJSON)
|
||||
return result, errJSON
|
||||
}
|
||||
return result, nil
|
||||
|
@ -113,6 +121,7 @@ func (manga *Manga) GetChapters(params GetChaptersParams) ([]MangaChapter, error
|
|||
|
||||
response, errRequest := DoRequest("GET", APIBaseURL+path.Join("manga", strconv.Itoa(manga.ID), "chapters")+"?"+params.AsQueryParams().Encode())
|
||||
if errRequest != nil {
|
||||
logrus.Errorf("Request error: %s", errRequest)
|
||||
return result, errRequest
|
||||
}
|
||||
|
||||
|
@ -120,6 +129,7 @@ func (manga *Manga) GetChapters(params GetChaptersParams) ([]MangaChapter, error
|
|||
|
||||
errJSON := json.Unmarshal(response.Data, &mangaDexChaptersResponse)
|
||||
if errJSON != nil {
|
||||
logrus.Errorf("Error parsing JSON: %s", errJSON)
|
||||
return result, errJSON
|
||||
}
|
||||
result = mangaDexChaptersResponse.Chapters
|
||||
|
@ -132,11 +142,13 @@ func (manga *Manga) GetChapter(chapter string) (MangaChapter, error) {
|
|||
|
||||
response, errRequest := DoRequest("GET", APIBaseURL+path.Join("chapter", chapter))
|
||||
if errRequest != nil {
|
||||
logrus.Errorf("Request error: %s", errRequest)
|
||||
return result, errRequest
|
||||
}
|
||||
|
||||
errJSON := json.Unmarshal(response.Data, &result)
|
||||
if errJSON != nil {
|
||||
logrus.Errorf("Error parsing JSON: %s", errJSON)
|
||||
return result, errJSON
|
||||
}
|
||||
|
||||
|
@ -153,30 +165,14 @@ func GetManga(mangaID int) (Manga, error) {
|
|||
result := Manga{}
|
||||
response, errRequest := DoRequest("GET", APIBaseURL+path.Join("manga", strconv.Itoa(mangaID)))
|
||||
if errRequest != nil {
|
||||
logrus.Errorf("Request error: %s", errRequest)
|
||||
return result, errRequest
|
||||
}
|
||||
|
||||
errJSON := json.Unmarshal(response.Data, &result)
|
||||
if errJSON != nil {
|
||||
log.Printf("[error] %s", errJSON)
|
||||
logrus.Errorf("Error parsing JSON: %s", errJSON)
|
||||
return result, errJSON
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
manga, err := GetManga(2890)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Printf("Manga: %s", manga.Title)
|
||||
|
||||
chapters, err := manga.GetChapters(NewGetChaptersParams())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for i := range chapters {
|
||||
log.Printf("v%2s %3s \t %s", chapters[i].Volume, chapters[i].Chapter, chapters[i].Title)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue