Made some functions private

This commit is contained in:
Felipe M 2021-02-10 17:09:09 +01:00
parent e7c5b44899
commit 6ed4f74b88
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
2 changed files with 57 additions and 32 deletions

View File

@ -2,7 +2,7 @@
Mangadex API client in Golang.
## Installation
## Chaching
...
@ -10,17 +10,42 @@ Mangadex API client in Golang.
``` go
import (
mangadex "code.fmartingr.dev/fmartingr/go-mangadex"
"log"
"code.fmartingr.dev/fmartingr/go-mangadex"
)
func main() {
// Retrieve manga information
manga, err := mangadex.GetManga(123)
if err != nil {
log.Println("[error] Retrieving manga: %s", err)
if errManga != nil {
log.Println("Error retrieving manga: %s", errManga)
}
// manga.GetChapter(1)
// manga.GetCovers()
// manga.GetVolume()
}
// Retrieve a list of chapters
chaptersRequest := NewGetChaptersParams()
chapters, errChapterList = manga.getChapters(chaptersRequest)
if errChapterList != nil {
log.Println("Error retrieving chapters page %d: %s", chaptersRequest.Page, errChapterList)
}
// Disables chache reads for requests beyond this point
mangadex.DisableCache()
// Retrieve a specific chapter detail
// This will return more information than the list (the pages, server, etc)
chapter, err := manga.GetChapter(1)
if errChapter != nil {
log.Println("Error retrieving chapter: %s", errChapter)
}
// Re-enables the cache
mangadex.EnableCache()
// Get all covers for this manga
covers, errCovers := manga.GetCovers()
if errCovers != nil {
log.Println("Error retreiving covers: %s", errCovers)
}
}
```

48
main.go
View File

@ -18,7 +18,7 @@ import (
const APIBaseURL = "https://api.mangadex.org/v2/"
var cacheEnabled bool = false
var cacheEnabled bool = true
func EnableCache() {
cacheEnabled = true
@ -70,7 +70,7 @@ func initCache() {
}
}
func DoRequest(method string, requestURL string) (*MangaDexResponse, error) {
func doRequest(method string, requestURL string) (*MangaDexResponse, error) {
result := MangaDexResponse{}
parsedURL, errParse := url.Parse(requestURL)
if errParse != nil {
@ -149,22 +149,6 @@ func DoRequest(method string, requestURL string) (*MangaDexResponse, error) {
return &result, nil
}
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
}
type GetChaptersParams struct {
Limit int `json:"limit"`
Page int `json:"p"`
@ -179,13 +163,13 @@ func NewGetChaptersParams() GetChaptersParams {
}
}
func (params *GetChaptersParams) Validate() {
func (params *GetChaptersParams) validate() {
if params.Limit < 1 || params.Limit > 100 {
params.Limit = 100
}
}
func (params *GetChaptersParams) AsQueryParams() url.Values {
func (params *GetChaptersParams) asQueryParams() url.Values {
queryParams := url.Values{}
if params.Page > 0 {
@ -202,9 +186,9 @@ func (params *GetChaptersParams) AsQueryParams() url.Values {
func (manga *Manga) GetChapters(params GetChaptersParams) ([]MangaChapterList, []MangaGroup, error) {
var mangaChaptersResult []MangaChapterList
var mangaGroupsResult []MangaGroup
params.Validate()
params.validate()
response, errRequest := DoRequest("GET", APIBaseURL+path.Join("manga", strconv.Itoa(manga.ID), "chapters")+"?"+params.AsQueryParams().Encode())
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 mangaChaptersResult, mangaGroupsResult, errRequest
@ -224,7 +208,7 @@ func (manga *Manga) GetChapters(params GetChaptersParams) ([]MangaChapterList, [
func (manga *Manga) GetChapter(chapter string) (MangaChapterDetail, error) {
var result MangaChapterDetail
response, errRequest := DoRequest("GET", APIBaseURL+path.Join("chapter", chapter))
response, errRequest := doRequest("GET", APIBaseURL+path.Join("chapter", chapter))
if errRequest != nil {
logrus.Errorf("Request error: %s", errRequest)
return result, errRequest
@ -241,7 +225,23 @@ func (manga *Manga) GetChapter(chapter string) (MangaChapterDetail, error) {
func GetManga(mangaID int) (Manga, error) {
result := Manga{}
response, errRequest := DoRequest("GET", APIBaseURL+path.Join("manga", strconv.Itoa(mangaID)))
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 {
logrus.Errorf("Error parsing JSON: %s", errJSON)
return result, errJSON
}
return result, nil
}
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