-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsecret_keys.go
155 lines (141 loc) · 4.51 KB
/
secret_keys.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package platform
import (
"encoding/json"
"errors"
"fmt"
http "github.com/bogdanfinn/fhttp"
"io"
"net/url"
"strings"
)
type GetSecretKeysResponse struct {
Object string `json:"object"`
Data []Key `json:"data"`
}
func (u *UserClient) GetSecretKeys() (GetSecretKeysResponse, error) {
if u.SessionKey() == "" {
return GetSecretKeysResponse{}, errors.New("GetSecretKeys with no SessionKey is Defined")
}
formParams := url.Values{}
req, err := http.NewRequest(http.MethodGet, PlatformApiUrlPrefix+"/dashboard/user/api_keys", strings.NewReader(formParams.Encode()))
req.Header.Set("User-Agent", UserAgent)
req.Header.Set(AuthorizationHeader, "Bearer "+u.SessionKey())
resp, err := u.client.Do(req)
if err != nil {
return GetSecretKeysResponse{}, errors.Join(
errors.New("GetSecretKeys error"),
err,
)
}
if resp.StatusCode != http.StatusOK {
return GetSecretKeysResponse{}, errors.Join(
errors.New(fmt.Sprintf("GetSecretKeys found non 200 response, StatusCode: %v", resp.StatusCode)),
err)
}
data, _ := io.ReadAll(resp.Body)
var response GetSecretKeysResponse
err = json.Unmarshal(data, &response)
if err != nil {
return GetSecretKeysResponse{}, errors.Join(
errors.New("GetSecretKey Unmarshal error"),
err)
}
return response, nil
}
type CreateSecretKeyResponse struct {
Result string `json:"result"`
Key Key `json:"key"`
}
type ActionMap struct {
Action string `json:"action"`
Name string `json:"name,omitempty"`
RedactedKey string `json:"redacted_key,omitempty"`
CreatedAt int `json:"created_at,omitempty"`
}
func (u *UserClient) CreateSecretKey(name string) (CreateSecretKeyResponse, error) {
if u.SessionKey() == "" {
return CreateSecretKeyResponse{}, errors.New("CreateSecretKey with no SessionKey is Defined")
}
form := ActionMap{
Action: "create",
Name: name,
}
bytedata, _ := json.Marshal(form)
req, err := http.NewRequest(http.MethodPost, PlatformApiUrlPrefix+"/dashboard/user/api_keys", strings.NewReader(string(bytedata)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set(AuthorizationHeader, "Bearer "+u.SessionKey())
req.Header.Set("User-Agent", UserAgent)
resp, err := u.client.Do(req)
if err != nil {
return CreateSecretKeyResponse{}, errors.Join(
errors.New("CreateSecretKeys error"),
err,
)
}
defer resp.Body.Close()
u.lastResponse = resp
if resp.StatusCode != http.StatusOK {
return CreateSecretKeyResponse{}, errors.Join(
errors.New(fmt.Sprintf("CreateSecretKeys found non 200 response, StatusCode: %v", resp.StatusCode)),
err)
}
data, _ := io.ReadAll(resp.Body)
var response CreateSecretKeyResponse
err = json.Unmarshal(data, &response)
if err != nil {
return CreateSecretKeyResponse{}, errors.Join(
errors.New("CreateSecretKey Unmarshal error"),
err)
}
return response, nil
}
type DeleteSecretKeyResponse struct {
Result string `json:"result"`
}
func (u *UserClient) DeleteSecretKey(key Key) (DeleteSecretKeyResponse, error) {
if u.SessionKey() == "" {
return DeleteSecretKeyResponse{}, errors.New("DeleteSecretKey with no SessionKey is Defined")
}
form := ActionMap{
Action: "delete",
CreatedAt: key.Created,
RedactedKey: key.SensitiveID,
}
bytedata, err := json.Marshal(form)
if err != nil {
return DeleteSecretKeyResponse{}, errors.Join(errors.New("error in json marshal"), err)
}
str_request := string(bytedata)
req, err := http.NewRequest(http.MethodPost, PlatformApiUrlPrefix+"/dashboard/user/api_keys", strings.NewReader(str_request))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+u.SessionKey())
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Origin", "https://platform.openai.com")
req.Header.Set("Referer", "https://platform.openai.com")
req.Header.Set("Accept", "*/*")
resp, err := u.client.Do(req)
if err != nil {
return DeleteSecretKeyResponse{}, errors.Join(
errors.New("DeleteSecretKeys error"),
err,
)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var response_data []byte
_, err_read := resp.Body.Read(response_data)
return DeleteSecretKeyResponse{}, errors.Join(
errors.New(fmt.Sprintf("DeleteSecretKeys found non 200 response, StatusCode: %v", resp.StatusCode)),
errors.New(string(response_data)),
err, err_read)
}
data, _ := io.ReadAll(resp.Body)
var response DeleteSecretKeyResponse
err = json.Unmarshal(data, &response)
if err != nil {
return DeleteSecretKeyResponse{}, errors.Join(
errors.New("DeleteSecretKey Unmarshal error"),
err)
}
return response, nil
}