Repository navigation
Expand file tree
/
Copy pathhttp.go
More file actions
128 lines (116 loc) · 3.16 KB
/
Copy pathhttp.go
File metadata and controls
128 lines (116 loc) · 3.16 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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
)
func (c *cli) doJSON(ctx context.Context, method, path string, payload any, auth bool, target any) error {
var body io.Reader
if payload != nil {
encoded, err := json.Marshal(payload)
if err != nil {
return err
}
body = bytes.NewReader(encoded)
}
return c.do(ctx, method, path, body, "application/json", auth, target)
}
func (c *cli) doRaw(ctx context.Context, method, path string, payload []byte, auth bool, target any) error {
return c.do(ctx, method, path, bytes.NewReader(payload), "application/octet-stream", auth, target)
}
func (c *cli) do(ctx context.Context, method, path string, body io.Reader, contentType string, auth bool, target any) error {
req, err := http.NewRequestWithContext(ctx, method, joinURL(c.apiURL, path), body)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("X-Prilog-Client", "cli")
if projectID := c.requestProjectID(); projectID != "" {
req.Header.Set("X-Prilog-Project-ID", projectID)
}
if body != nil && contentType != "" {
req.Header.Set("Content-Type", contentType)
}
if auth {
global, _ := loadGlobalConfig()
token := strings.TrimSpace(os.Getenv("PRILOG_ACCESS_TOKEN"))
if token == "" {
token = global.AccessToken
}
if token == "" {
return errors.New("not authenticated; run `prilog login`")
}
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode >= 300 {
return decodeAPIError(resp.StatusCode, respBody)
}
if target == nil || len(respBody) == 0 {
return nil
}
if err := json.Unmarshal(respBody, target); err != nil {
return fmt.Errorf("decode response: %w", err)
}
return nil
}
func (c *cli) requestProjectID() string {
if c == nil {
return ""
}
if projectID := strings.TrimSpace(c.projectID); projectID != "" {
return projectID
}
global, err := loadGlobalConfig()
if err == nil && strings.TrimSpace(os.Getenv("PRILOG_ACCESS_TOKEN")) == "" && global.ProjectID != "" && trimConfiguredURL(global.APIURL) == c.apiURL {
return global.ProjectID
}
repoCfg, err := loadRepoConfig(c.root)
if err != nil {
return ""
}
return strings.TrimSpace(repoCfg.ProjectID)
}
func decodeAPIError(statusCode int, body []byte) error {
message := strings.TrimSpace(string(body))
var payload map[string]any
if json.Unmarshal(body, &payload) == nil {
if errValue, ok := payload["error"].(string); ok {
message = errValue
}
}
return apiError{StatusCode: statusCode, Message: message}
}
type apiError struct {
StatusCode int
Message string
}
func (e apiError) Error() string {
if e.Message == "" {
return fmt.Sprintf("api returned HTTP %d", e.StatusCode)
}
return fmt.Sprintf("api returned HTTP %d: %s", e.StatusCode, e.Message)
}
func joinURL(base, path string) string {
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
return path
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return strings.TrimRight(base, "/") + path
}