-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategory.go
More file actions
115 lines (96 loc) · 3.46 KB
/
Copy pathcategory.go
File metadata and controls
115 lines (96 loc) · 3.46 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package ecwid
import (
"context"
"errors"
"fmt"
"html/template"
)
type (
// NewCategory https://developers.ecwid.com/api-documentation/categories#add-new-category
NewCategory struct {
Name string `json:"name,omitempty"`
ParentID ID `json:"parentId"`
OrderBy int `json:"orderBy"`
Description template.HTML `json:"description,omitempty"`
Enabled bool `json:"enabled"`
ProductIDs []ID `json:"productIds,omitempty"`
}
// Category https://developers.ecwid.com/api-documentation/categories#get-categories
Category struct {
NewCategory
ID ID `json:"id"`
HdThumbnailURL string `json:"hdThumbnailUrl"`
ThumbnailURL string `json:"thumbnailUrl"`
ImageURL string `json:"imageUrl"`
OriginalImageURL string `json:"originalImageUrl"`
URL string `json:"url"`
ProductCount uint `json:"productCount"`
EnabledProductCount uint `json:"enabledProductCount"`
OriginalImage *ImageDetails `json:"originalImage"`
}
// CategoriesSearchResponse is basic details of found categories
CategoriesSearchResponse struct {
SearchResponse
Items []*Category `json:"items"`
}
)
// CategoriesSearch search or filter categories in a store catalog
// The response provides basic details of found categories
func (c *Client) CategoriesSearch(filter map[string]string) (*CategoriesSearchResponse, error) {
// filter:
// parent number, hidden_categories bool, offset number, limit number,
// productIds array?, baseUrl string, cleanUrls bool
response, err := c.R().
SetQueryParams(filter).
Get("/categories")
var result CategoriesSearchResponse
return &result, responseUnmarshal(response, err, &result)
}
// Categories 'iterable' by filtered store categories
func (c *Client) Categories(ctx context.Context, filter map[string]string) <-chan *Category {
catChan := make(chan *Category)
go func() {
defer close(catChan)
c.CategoriesTrampoline(filter, func(index uint, category *Category) error {
// FIXME silent error. maybe catChan <- nil ?
select {
case <-ctx.Done():
return errors.New("break")
case catChan <- category:
}
return nil
})
}()
return catChan
}
// CategoryGet gets all details of a specific category in an Ecwid store by its ID
func (c *Client) CategoryGet(categoryID ID) (*Category, error) {
response, err := c.R().
Get(fmt.Sprintf("/categories/%d", categoryID))
var result Category
return &result, responseUnmarshal(response, err, &result)
}
// CategoryAdd creates a new category in an Ecwid store
// returns new categoryId
func (c *Client) CategoryAdd(category *NewCategory) (ID, error) {
response, err := c.R().
SetHeader("Content-Type", "application/json").
SetBody(category).
Post("/categories")
return responseAdd(response, err)
}
// CategoryUpdate update an existing category in an Ecwid store referring to its ID
func (c *Client) CategoryUpdate(categoryID ID, category *NewCategory) error {
response, err := c.R().
SetHeader("Content-Type", "application/json").
SetBody(category).
Put(fmt.Sprintf("/categories/%d", categoryID))
return responseUpdate(response, err)
}
// CategoryDelete delete a category from an Ecwid store referring to its ID
func (c *Client) CategoryDelete(categoryID ID) error {
response, err := c.R().
Delete(fmt.Sprintf("/categories/%d", categoryID))
_, err = responseDelete(response, err)
return err
}