-
Notifications
You must be signed in to change notification settings - Fork 13
/
attribute.go
72 lines (56 loc) · 2.01 KB
/
attribute.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
66
67
68
69
70
71
72
package magento2
import (
"fmt"
"strings"
)
type MAttribute struct {
Route string
Attribute *Attribute
APIClient *Client
}
func CreateAttribute(a *Attribute, apiClient *Client) (*MAttribute, error) {
mAttribute := &MAttribute{
Attribute: &Attribute{},
APIClient: apiClient,
}
endpoint := productsAttribute
httpClient := apiClient.HTTPClient
payLoad := createAttributePayload{
Attribute: *a,
}
resp, err := httpClient.R().SetBody(payLoad).SetResult(mAttribute.Attribute).Post(endpoint)
mAttribute.Route = productsAttribute + "/" + mAttribute.Attribute.AttributeCode
return mAttribute, mayReturnErrorForHTTPResponse(err, resp, "create attribute")
}
func GetAttributeByAttributeCode(attributeCode string, apiClient *Client) (*MAttribute, error) {
mAttributeSet := &MAttribute{
Route: fmt.Sprintf("%s/%s", productsAttribute, attributeCode),
Attribute: &Attribute{},
APIClient: apiClient,
}
err := mAttributeSet.UpdateAttributeFromRemote()
return mAttributeSet, err
}
func (mas *MAttribute) UpdateAttributeOnRemote() error {
resp, err := mas.APIClient.HTTPClient.R().SetResult(mas.Attribute).SetBody(mas.Attribute).Put(mas.Route)
return mayReturnErrorForHTTPResponse(err, resp, "update remote attribute from local")
}
func (mas *MAttribute) UpdateAttributeFromRemote() error {
resp, err := mas.APIClient.HTTPClient.R().SetResult(mas.Attribute).Get(mas.Route)
return mayReturnErrorForHTTPResponse(err, resp, "update local attribute from remote")
}
func (mas *MAttribute) AddOption(option Option) (string, error) {
endpoint := mas.Route + "/" + productsAttributeOptions
httpClient := mas.APIClient.HTTPClient
payLoad := addOptionPayload{
Option: option,
}
resp, err := httpClient.R().SetBody(payLoad).Post(endpoint)
err = mayReturnErrorForHTTPResponse(err, resp, "assign option to attribute")
if err != nil {
return "", err
}
optionValue := mayTrimSurroundingQuotes(resp.String())
optionValue = strings.TrimPrefix(optionValue, "id_")
return optionValue, mas.UpdateAttributeFromRemote()
}