-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredentials.go
83 lines (62 loc) · 2.2 KB
/
credentials.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
package apigee
import (
b64 "encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)
type CreateCredentialsResponse struct {
Key string `json:"key"`
Secret string `json:"secret"`
Credentials string `json:"credentials"`
}
type CredentialsArrayApigee struct {
Credentials []CredentialsApigee `json:"credentials"`
}
type CredentialsApigee struct {
ConsumerKey string `json:"consumerKey"`
ConsumerSecret string `json:"consumerSecret"`
}
func (c *Client) CreateCredentials(orgName string, developerEmail string, appName string, apiProducts string, expiresInSeconds int) (*CreateCredentialsResponse, error) {
if orgName == "" || developerEmail == "" || appName == "" || apiProducts == "" || expiresInSeconds == 0 {
return nil, fmt.Errorf("define orgName, developerEmail, appName, apiProducts, and expiresInSeconds")
}
body := fmt.Sprintf("{\"keyExpiresIn\":\"%s\",\"apiProducts\":%s}",
strconv.Itoa(expiresInSeconds*1000), apiProducts)
req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/organizations/%s/developers/%s/apps/%s",
c.Host, orgName, developerEmail, appName), strings.NewReader(body))
if err != nil {
return nil, err
}
res, err := c.doRequest(req)
if err != nil {
return nil, err
}
key, secret := getCredentials(res)
credentials := fmt.Sprintf("%s:%s", key, secret)
b64Credentials := b64.StdEncoding.EncodeToString([]byte(credentials))
ccr := CreateCredentialsResponse{key, secret, b64Credentials}
return &ccr, nil
}
func (c *Client) DeleteCredentials(orgName string, developerEmail string, appName string, key string) error {
if orgName == "" || developerEmail == "" || appName == "" || key == "" {
return fmt.Errorf("define orgName, developerEmail, appName, and key")
}
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/v1/organizations/%s/developers/%s/apps/%s/keys/%s",
c.Host, orgName, developerEmail, appName, key), strings.NewReader(""))
if err != nil {
return err
}
_, err = c.doRequest(req)
if err != nil {
return err
}
return nil
}
func getCredentials(data []byte) (string, string) {
var caa CredentialsArrayApigee
json.Unmarshal(data, &caa)
return caa.Credentials[0].ConsumerKey, caa.Credentials[0].ConsumerSecret
}