-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_header.go
More file actions
45 lines (38 loc) · 1.37 KB
/
client_header.go
File metadata and controls
45 lines (38 loc) · 1.37 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
package hooks
import (
"net/http"
"strings"
"github.com/sfcompute/sfc-go/internal/config"
)
// sfcClientHeader is the canonical header used by SFC services to identify
// SDK clients. The server-side parser relies on this header rather than the
// generated User-Agent so that our parsing does not break if Speakeasy
// changes the User-Agent format.
const sfcClientHeader = "x-sfc-client"
// ClientHeaderHook sets the x-sfc-client header on every outgoing request.
// The version is captured from the generated User-Agent at SDK init time.
type ClientHeaderHook struct {
headerValue string
}
func (h *ClientHeaderHook) SDKInit(c config.SDKConfiguration) config.SDKConfiguration {
h.headerValue = "speakeasy/go-" + parseSDKVersion(c.UserAgent)
return c
}
func (h *ClientHeaderHook) BeforeRequest(_ BeforeRequestContext, req *http.Request) (*http.Request, error) {
req.Header.Set(sfcClientHeader, h.headerValue)
return req, nil
}
// parseSDKVersion extracts the SDK version (second token) from the
// Speakeasy-generated User-Agent string of the form:
//
// "speakeasy-sdk/go <sdkVersion> <generatorVersion> <docVersion> <module>"
//
// Returns "unknown" if the User-Agent is empty or does not have at least two
// space-separated tokens.
func parseSDKVersion(userAgent string) string {
parts := strings.Fields(userAgent)
if len(parts) < 2 {
return "unknown"
}
return parts[1]
}