-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
52 lines (40 loc) · 1.28 KB
/
client.go
File metadata and controls
52 lines (40 loc) · 1.28 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
package api
import (
"context"
"fmt"
"net/http"
"os"
"time"
"connectrpc.com/connect"
output "github.com/openstatusHQ/cli/internal/cli"
)
const APIBaseURL = "https://api.openstatus.dev/v1"
const ConnectBaseURL = "https://api.openstatus.dev/rpc"
// PlayCheckerURL is the public Speed Checker endpoint backing the `check`
// command. The www. prefix is intentional: the bare openstatus.dev host
// returns a 308 redirect that adds latency to every call.
const PlayCheckerURL = "https://www.openstatus.dev/play/checker/api"
var DefaultHTTPClient = &http.Client{
Timeout: 30 * time.Second,
}
func NewAuthInterceptor(apiKey string) connect.UnaryInterceptorFunc {
return func(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
req.Header().Set("x-openstatus-key", apiKey)
if output.IsDebug() {
fmt.Fprintf(os.Stderr, "[debug] %s %s\n", req.HTTPMethod(), req.Spec().Procedure)
}
start := time.Now()
resp, err := next(ctx, req)
if output.IsDebug() {
duration := time.Since(start)
if err != nil {
fmt.Fprintf(os.Stderr, "[debug] error after %s: %v\n", duration, err)
} else {
fmt.Fprintf(os.Stderr, "[debug] ok in %s\n", duration)
}
}
return resp, err
}
}
}