-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlanguage_profile_service.go
104 lines (85 loc) · 2.63 KB
/
language_profile_service.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
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
package yext
import (
"encoding/json"
"fmt"
)
const profilesPath = "profiles"
type LanguageProfileService struct {
client *Client
CustomFields []*CustomField
}
type LanguageProfileListResponse struct {
LanguageProfiles []*LanguageProfile `json:"languageProfiles"`
}
func (l *LanguageProfileListResponse) ResponseAsLocations() []*Location {
languageProfilesAsLocs := []*Location{}
for _, lp := range l.LanguageProfiles {
languageProfilesAsLocs = append(languageProfilesAsLocs, &lp.Location)
}
return languageProfilesAsLocs
}
func (l *LanguageProfileService) GetAll(id string) (*LanguageProfileListResponse, *Response, error) {
var v LanguageProfileListResponse
r, err := l.client.DoRequest("GET", fmt.Sprintf("%s/%s/%s", locationsPath, id, profilesPath), &v)
if err != nil {
return nil, r, err
}
if _, err := l.HydrateLocations(v.LanguageProfiles); err != nil {
return nil, r, err
}
return &v, r, nil
}
func (l *LanguageProfileService) Get(id string, languageCode string) (*LanguageProfile, *Response, error) {
var v LanguageProfile
r, err := l.client.DoRequest("GET", fmt.Sprintf("%s/%s/%s/%s", locationsPath, id, profilesPath, languageCode), &v)
if err != nil {
return nil, r, err
}
if _, err := HydrateLocation(&v.Location, l.CustomFields); err != nil {
return nil, r, err
}
return &v, r, nil
}
func (l *LanguageProfileService) Upsert(y *LanguageProfile, languageCode string) (*Response, error) {
id := y.GetId()
if id == "" {
return nil, fmt.Errorf("language profile service: upsert: profile object had no id")
}
asJSON, err := json.Marshal(y)
if err != nil {
return nil, err
}
var asMap map[string]interface{}
err = json.Unmarshal(asJSON, &asMap)
if err != nil {
return nil, err
}
delete(asMap, "id")
if err := validateCustomFieldsKeys(y.CustomFields); err != nil {
return nil, err
}
r, err := l.client.DoRequestJSON("PUT", fmt.Sprintf("%s/%s/%s/%s", locationsPath, id, profilesPath, languageCode), asMap, nil)
if err != nil {
return r, err
}
return r, nil
}
func (l *LanguageProfileService) Delete(id string, languageCode string) (*Response, error) {
r, err := l.client.DoRequest("DELETE", fmt.Sprintf("%s/%s/%s/%s", locationsPath, id, profilesPath, languageCode), nil)
if err != nil {
return r, err
}
return r, nil
}
func (l *LanguageProfileService) HydrateLocations(languageProfiles []*LanguageProfile) ([]*LanguageProfile, error) {
if l.CustomFields == nil {
return languageProfiles, nil
}
for _, profile := range languageProfiles {
_, err := HydrateLocation(&profile.Location, l.CustomFields)
if err != nil {
return languageProfiles, err
}
}
return languageProfiles, nil
}