package plati import ( "context" "encoding/json" "fmt" "net/http" ) type Categories []*Category func (categories Categories) FlatNames(nameOfParent string) { for _, c := range categories { name := "" if len(c.Name) > 0 { name = c.Name[0].Value } if c.Title != "" { name = c.Title + "/" + name } c.FlatName = fmt.Sprintf("%s/%s", nameOfParent, name) if len(c.Children) > 0 { c.Children.FlatNames(c.FlatName) } } } func (categories Categories) FixParentID(parentID int) { for _, c := range categories { if c.ParentID == 0 { c.ParentID = parentID } c.Children.FixParentID(c.ID) } } type Category struct { FlatName string `json:"flat_name,omitempty"` ID int `json:"id"` Title string `json:"title"` Level int `json:"level"` ParentID int `json:"parent_id"` Name []struct { Locale string `json:"locale"` Value string `json:"value"` } `json:"name"` Children Categories `json:"children"` CanAdd bool `json:"can_add"` } func (c *Category) GetName() string { if len(c.Name) == 0 { return "" } for _, n := range c.Name { if n.Locale == "ru-RU" { return n.Value } } return c.Name[0].Value } type CategoriesResponse struct { Retval int `json:"retval"` Retdesc any `json:"retdesc"` Errors any `json:"errors"` Content Categories `json:"content"` } func (c *Client) GetCategories(ctx context.Context) (*CategoriesResponse, error) { req, err := http.NewRequestWithContext(ctx, "GET", "https://api.digiseller.ru/api/dictionary/platforms/categories/plati", http.NoBody) if err != nil { return nil, fmt.Errorf("new request: %w", err) } req.Header.Add("Accept", "application/json") resp, err := c.httpCli().Do(req) if err != nil { return nil, fmt.Errorf("http do: %w", err) } // dump, err := httputil.DumpResponse(resp, true) // log.Printf("%d %s", resp.StatusCode, string(dump)) defer resp.Body.Close() out := CategoriesResponse{} if err := json.NewDecoder(resp.Body).Decode(&out); err != nil { return nil, fmt.Errorf("json decode: %w", err) } return &out, nil } type SubCategories struct { Retval int `json:"retval"` Retdesc any `json:"retdesc"` Errors any `json:"errors"` Content []*Category `json:"content"` } func (c *Client) GetSubCategories(ctx context.Context, categoryID int) (*SubCategories, error) { req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.digiseller.ru/api/dictionary/platforms/subcategories/%d", categoryID), http.NoBody) if err != nil { return nil, fmt.Errorf("new request: %w", err) } req.Header.Add("Accept", "application/json") resp, err := c.httpCli().Do(req) if err != nil { return nil, fmt.Errorf("http do: %w", err) } // dump, err := httputil.DumpResponse(resp, true) // log.Printf("%d %s", resp.StatusCode, string(dump)) defer resp.Body.Close() out := SubCategories{} if err := json.NewDecoder(resp.Body).Decode(&out); err != nil { return nil, fmt.Errorf("json decode: %w", err) } return &out, nil }