This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
api_key.go
132 lines (106 loc) · 3.03 KB
/
api_key.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package contentful
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
)
// APIKeyService service
type APIKeyService service
// APIKey model
type APIKey struct {
Sys *Sys `json:"sys,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
AccessToken string `json:"accessToken,omitempty"`
Policies []*APIKeyPolicy `json:"policies,omitempty"`
PreviewAPIKey *PreviewAPIKey `json:"preview_api_key,omitempty"`
}
// APIKeyPolicy model
type APIKeyPolicy struct {
Effect string `json:"effect,omitempty"`
Actions string `json:"actions,omitempty"`
}
// PreviewAPIKey model
type PreviewAPIKey struct {
Sys *Sys
}
// MarshalJSON for custom json marshaling
func (apiKey *APIKey) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
}{
Name: apiKey.Name,
Description: apiKey.Description,
})
}
// GetVersion returns entity version
func (apiKey *APIKey) GetVersion() int {
version := 1
if apiKey.Sys != nil {
version = apiKey.Sys.Version
}
return version
}
// List returns all api keys collection
func (service *APIKeyService) List(spaceID string) *Collection {
path := fmt.Sprintf("/spaces/%s/api_keys", spaceID)
method := "GET"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return &Collection{}
}
col := NewCollection(&CollectionOptions{})
col.c = service.c
col.req = req
return col
}
// Get returns a single api key entity
func (service *APIKeyService) Get(spaceID, apiKeyID string) (*APIKey, error) {
path := fmt.Sprintf("/spaces/%s/api_keys/%s", spaceID, apiKeyID)
method := "GET"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return nil, err
}
var apiKey APIKey
if err := service.c.do(req, &apiKey); err != nil {
return nil, err
}
return &apiKey, nil
}
// Upsert updates or creates a new api key entity
func (service *APIKeyService) Upsert(spaceID string, apiKey *APIKey) error {
bytesArray, err := json.Marshal(apiKey)
if err != nil {
return err
}
var path string
var method string
if apiKey.Sys != nil && apiKey.Sys.CreatedAt != "" {
path = fmt.Sprintf("/spaces/%s/api_keys/%s", spaceID, apiKey.Sys.ID)
method = "PUT"
} else {
path = fmt.Sprintf("/spaces/%s/api_keys", spaceID)
method = "POST"
}
req, err := service.c.newRequest(method, path, nil, bytes.NewReader(bytesArray))
if err != nil {
return err
}
req.Header.Set("X-Contentful-Version", strconv.Itoa(apiKey.GetVersion()))
return service.c.do(req, apiKey)
}
// Delete deletes a sinlge api key entity
func (service *APIKeyService) Delete(spaceID string, apiKey *APIKey) error {
path := fmt.Sprintf("/spaces/%s/api_keys/%s", spaceID, apiKey.Sys.ID)
method := "DELETE"
req, err := service.c.newRequest(method, path, nil, nil)
if err != nil {
return err
}
version := strconv.Itoa(apiKey.Sys.Version)
req.Header.Set("X-Contentful-Version", version)
return service.c.do(req, nil)
}