-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_context.go
More file actions
105 lines (97 loc) · 2.9 KB
/
request_context.go
File metadata and controls
105 lines (97 loc) · 2.9 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
package acp
import (
"context"
"net/http"
"strings"
"time"
)
// RequestContext carries the standard ACP headers.
type RequestContext struct {
// API Key used to make requests.
//
// Example: Bearer api_key_123
Authorization string
// The preferred locale for content like messages and errors.
//
// Example: en-US
AcceptLanguage string
// Information about the client making this request.
//
// Example: ChatGPT/2.0 (Mac OS X 15.0.1; arm64; build 0)
UserAgent string
// Key used to ensure requests are idempotent.
//
// Example: idempotency_key_123
IdempotencyKey string
// Unique key for each request for tracing purposes.
//
// Example: request_id_123
RequestID string
// Base64 encoded signature of the request body.
//
// Example: eyJtZX...
Signature string
// Formatted as an RFC 3339 string.
//
// Example: 2025-09-25T10:30:00Z
Timestamp string
// API version.
//
// Example: 2026-01-30
APIVersion string
}
func RequestContextFromRequest(r *http.Request) (*RequestContext, error) {
requestCtx := &RequestContext{
Authorization: strings.TrimSpace(r.Header.Get("Authorization")),
AcceptLanguage: strings.TrimSpace(r.Header.Get("Accept-Language")),
UserAgent: strings.TrimSpace(r.Header.Get("User-Agent")),
IdempotencyKey: strings.TrimSpace(r.Header.Get("Idempotency-Key")),
RequestID: strings.TrimSpace(r.Header.Get("Request-Id")),
Signature: strings.TrimSpace(r.Header.Get("Signature")),
Timestamp: strings.TrimSpace(r.Header.Get("Timestamp")),
APIVersion: strings.TrimSpace(r.Header.Get("API-Version")),
}
if err := requestCtx.validate(); err != nil {
return nil, err
}
return requestCtx, nil
}
func ContextWithRequestContextFromRequest(r *http.Request) (context.Context, error) {
requestCtx, err := RequestContextFromRequest(r)
if err != nil {
return nil, err
}
return ContextWithRequestContext(r.Context(), requestCtx), nil
}
func (r *RequestContext) validate() *Error {
if r == nil {
return NewInvalidRequestError("request context is required")
}
if r.APIVersion == "" {
return NewInvalidRequestError("API-Version header is required")
}
if parsed, err := time.Parse("2006-01-02", r.APIVersion); err != nil || parsed.Format("2006-01-02") != r.APIVersion {
return NewInvalidRequestError("API-Version header must be in YYYY-MM-DD format")
}
return nil
}
type requestContextKey struct{}
func ContextWithRequestContext(ctx context.Context, requestCtx *RequestContext) context.Context {
if ctx == nil {
ctx = context.Background()
}
if requestCtx == nil {
return ctx
}
return context.WithValue(ctx, requestContextKey{}, requestCtx)
}
// RequestContextFromContext extracts the HTTP request metadata previously stored in the context.
func RequestContextFromContext(ctx context.Context) *RequestContext {
if ctx == nil {
return nil
}
if requestCtx, ok := ctx.Value(requestContextKey{}).(*RequestContext); ok {
return requestCtx
}
return nil
}