-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp.go
More file actions
273 lines (225 loc) · 6.94 KB
/
http.go
File metadata and controls
273 lines (225 loc) · 6.94 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package portal
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"slices"
"strings"
"time"
"github.com/codesphere-cloud/oms/internal/env"
)
type Portal interface {
ListBuilds(product Product) (availablePackages Builds, err error)
GetBuild(product Product, version string, hash string) (Build, error)
DownloadBuildArtifact(product Product, build Build, file io.Writer) error
RegisterAPIKey(owner string, organization string, role string, expiresAt time.Time) error
RevokeAPIKey(key string) error
ListAPIKeys() ([]ApiKey, error)
}
type PortalClient struct {
Env env.Env
HttpClient HttpClient
}
type HttpClient interface {
Do(*http.Request) (*http.Response, error)
}
func NewPortalClient() *PortalClient {
return &PortalClient{
Env: env.NewEnv(),
HttpClient: http.DefaultClient,
}
}
type Product string
const (
CodesphereProduct Product = "codesphere"
OmsProduct Product = "oms"
)
func (c *PortalClient) HttpRequest(method string, path string, body []byte) (resp *http.Response, err error) {
requestBody := bytes.NewBuffer(body)
url, err := url.JoinPath(c.Env.GetOmsPortalApi(), path)
if err != nil {
err = fmt.Errorf("failed to get generate URL: %w", err)
return
}
apiKey, err := c.Env.GetOmsPortalApiKey()
if err != nil {
err = fmt.Errorf("failed to get API Key: %w", err)
return
}
req, err := http.NewRequest(method, url, requestBody)
if err != nil {
log.Fatalf("Error creating request: %v", err)
return
}
if len(body) > 0 {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("X-API-Key", apiKey)
resp, err = c.HttpClient.Do(req)
if err != nil {
err = fmt.Errorf("failed to send request: %w", err)
return
}
if resp.StatusCode == http.StatusUnauthorized {
fmt.Println("You need a valid OMS API Key, please reach out to the Codesphere support at support@codesphere.com to request a new API Key.")
fmt.Println("If you already have an API Key, make sure to set it using the environment variable OMS_PORTAL_API_KEY")
}
var respBody []byte
if resp.StatusCode >= 300 {
if resp.Body != nil {
respBody, _ = io.ReadAll(resp.Body)
}
err = fmt.Errorf("unexpected response status: %d - %s, %s", resp.StatusCode, http.StatusText(resp.StatusCode), string(respBody))
return
}
return
}
func (c *PortalClient) GetBody(path string) (body []byte, status int, err error) {
resp, err := c.HttpRequest(http.MethodGet, path, []byte{})
if err != nil || resp == nil {
err = fmt.Errorf("GET failed: %w", err)
return
}
defer func() { _ = resp.Body.Close() }()
status = resp.StatusCode
body, err = io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("failed to read response body: %w", err)
return
}
return
}
func (c *PortalClient) ListBuilds(product Product) (availablePackages Builds, err error) {
res, _, err := c.GetBody(fmt.Sprintf("/packages/%s", product))
if err != nil {
err = fmt.Errorf("failed to list packages: %w", err)
return
}
err = json.Unmarshal(res, &availablePackages)
if err != nil {
err = fmt.Errorf("failed to parse list packages response: %w", err)
return
}
compareBuilds := func(l, r Build) int {
if l.Date.Before(r.Date) {
return -1
}
if l.Date.Equal(r.Date) && l.Internal == r.Internal {
return 0
}
return 1
}
slices.SortFunc(availablePackages.Builds, compareBuilds)
return
}
func (c *PortalClient) GetBuild(product Product, version string, hash string) (Build, error) {
packages, err := c.ListBuilds(product)
if err != nil {
return Build{}, fmt.Errorf("failed to list %s packages: %w", product, err)
}
if len(packages.Builds) == 0 {
return Build{}, errors.New("no builds returned")
}
if version == "" || version == "latest" {
// Builds are always ordered by date, newest build is latest version
return packages.Builds[len(packages.Builds)-1], nil
}
matchingPackages := []Build{}
for _, build := range packages.Builds {
if build.Version == version {
if len(hash) == 0 || strings.HasPrefix(hash, build.Hash) {
matchingPackages = append(matchingPackages, build)
}
}
}
if len(matchingPackages) == 0 {
return Build{}, fmt.Errorf("version '%s' with hash '%s' not found", version, hash)
}
// Builds are always ordered by date, return newest build
return matchingPackages[len(matchingPackages)-1], nil
}
func (c *PortalClient) DownloadBuildArtifact(product Product, build Build, file io.Writer) error {
reqBody, err := json.Marshal(build)
if err != nil {
return fmt.Errorf("failed to generate request body: %w", err)
}
resp, err := c.HttpRequest(http.MethodGet, fmt.Sprintf("/packages/%s/download", product), reqBody)
if err != nil {
return fmt.Errorf("GET request to download build failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
// Create a WriteCounter to wrap the output file and report progress.
counter := NewWriteCounter(file)
_, err = io.Copy(counter, resp.Body)
if err != nil {
return fmt.Errorf("failed to copy response body to file: %w", err)
}
fmt.Println("Download finished successfully.")
return nil
}
func (c *PortalClient) RegisterAPIKey(owner string, organization string, role string, expiresAt time.Time) error {
req := struct {
Owner string `json:"owner"`
Organization string `json:"organization"`
Role string `json:"role"`
ExpiresAt time.Time `json:"expires_at"`
}{
Owner: owner,
Organization: organization,
Role: role,
ExpiresAt: expiresAt,
}
reqBody, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("failed to generate request body: %w", err)
}
resp, err := c.HttpRequest(http.MethodPost, "/key/register", reqBody)
if err != nil {
return fmt.Errorf("POST request to register API key failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
newKey := &ApiKey{}
err = json.NewDecoder(resp.Body).Decode(newKey)
if err != nil {
return fmt.Errorf("failed to decode response body: %w", err)
}
fmt.Println("API key registered successfully!")
fmt.Printf("Owner: %s\nOrganisation: %s\nKey: %s\n", newKey.Owner, newKey.Organization, newKey.ApiKey)
return nil
}
func (c *PortalClient) RevokeAPIKey(keyId string) error {
req := struct {
KeyID string `json:"key_id"`
}{
KeyID: keyId,
}
reqBody, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("failed to generate request body: %w", err)
}
resp, err := c.HttpRequest(http.MethodPost, "/key/revoke", reqBody)
if err != nil {
return fmt.Errorf("POST request to revoke API key failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
fmt.Println("API key revoked successfully!")
return nil
}
func (c *PortalClient) ListAPIKeys() ([]ApiKey, error) {
res, _, err := c.GetBody("/keys")
if err != nil {
return nil, fmt.Errorf("failed to list api keys: %w", err)
}
var keys []ApiKey
if err := json.Unmarshal(res, &keys); err != nil {
return nil, fmt.Errorf("failed to parse api keys response: %w", err)
}
return keys, nil
}