This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcategory.go
65 lines (52 loc) · 1.69 KB
/
category.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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-api.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"`
SubCategories []struct {
Category string `json:"tenant_category"`
ParentCategory string `json:"parent_category"`
Localization struct {
Title string `json:"title"`
Description string `json:"description"`
Locale LOCALE `json:"locale"`
} `json:"localization"`
Slug string `json:"slug"`
} `json:"sub_categories"`
Images struct {
Background []Image `json:"background"`
Low []Image `json:"low"`
} `json:"images"`
Localization struct {
Title string `json:"title"`
Description string `json:"description"`
Locale LOCALE `json:"locale"`
} `json:"localization"`
Slug string `json:"slug"`
}