forked from contentful-labs/contentful-go
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathenvironment_alias.go
92 lines (72 loc) · 2.04 KB
/
environment_alias.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
package contentful
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
)
// EnvironmentAliasesService service
type EnvironmentAliasesService service
// EnvironmentAlias model
type EnvironmentAlias struct {
Sys *Sys `json:"sys"`
Alias *AliasDetail `json:"environment"`
}
// AliasDetail model
type AliasDetail struct {
Sys *Sys `json:"sys"`
}
// GetVersion returns entity version
func (environmentAlias *EnvironmentAlias) GetVersion() int {
version := 1
if environmentAlias.Sys != nil {
version = environmentAlias.Sys.Version
}
return version
}
// List returns an environment aliases collection
func (service *EnvironmentAliasesService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/environment_aliases", spaceID)
method := "GET"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return nil
}
col := NewCollection(&CollectionOptions{})
col.c = service.c
col.req = req
return col
}
// Get returns a single environment alias entity
func (service *EnvironmentAliasesService) Get(spaceID, environmentAliasID string) (*EnvironmentAlias, error) {
path := fmt.Sprintf("/spaces/%s/environment_aliases/%s", spaceID, environmentAliasID)
method := "GET"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return nil, err
}
var environmentAlias EnvironmentAlias
if err := service.c.do(req, &environmentAlias); err != nil {
return nil, err
}
return &environmentAlias, nil
}
// Update updates an environment alias
func (service *EnvironmentAliasesService) Update(spaceID string, ea *EnvironmentAlias) error {
bytesArray, err := json.Marshal(ea)
if err != nil {
return err
}
var path string
var method string
if ea.Sys != nil && ea.Sys.ID != "" {
path = fmt.Sprintf("/spaces/%s/environment_aliases/%s", spaceID, ea.Sys.ID)
method = "PUT"
}
req, err := service.c.newRequest(method, path, nil, bytes.NewReader(bytesArray))
if err != nil {
return err
}
req.Header.Set("X-Contentful-Version", strconv.Itoa(ea.GetVersion()))
return service.c.do(req, ea)
}