Move functions into their own, separate files & add docs

This commit is contained in:
bytedream 2022-06-21 21:15:49 +02:00
parent d1859b4c25
commit ec872d8c86
12 changed files with 681 additions and 638 deletions

View file

@ -1,5 +1,38 @@
package crunchyroll
import (
"encoding/json"
"fmt"
"net/http"
)
// Categories returns all available categories and possible subcategories.
func (c *Crunchyroll) Categories(includeSubcategories bool) (ca []*Category, err error) {
tenantCategoriesEndpoint := fmt.Sprintf("https://beta.crunchyroll.com/content/v1/tenant_categories?include_subcategories=%t&locale=%s",
includeSubcategories, c.Locale)
resp, err := c.request(tenantCategoriesEndpoint, http.MethodGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var jsonBody map[string]interface{}
if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
return nil, fmt.Errorf("failed to parse 'tenant_categories' response: %w", err)
}
for _, item := range jsonBody["items"].([]interface{}) {
category := &Category{}
if err := decodeMapToStruct(item, category); err != nil {
return nil, err
}
ca = append(ca, category)
}
return ca, nil
}
// Category contains all information about a category.
type Category struct {
Category string `json:"tenant_category"`
@ -18,19 +51,8 @@ type Category struct {
} `json:"sub_categories"`
Images struct {
Background []struct {
Width int `json:"width"`
Height int `json:"height"`
Type string `json:"type"`
Source string `json:"source"`
} `json:"background"`
Low []struct {
Width int `json:"width"`
Height int `json:"height"`
Type string `json:"type"`
Source string `json:"source"`
} `json:"low"`
Background []Image `json:"background"`
Low []Image `json:"low"`
} `json:"images"`
Localization struct {