/
request_option.go
115 lines (96 loc) · 2.84 KB
/
request_option.go
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
// This file was auto-generated by Fern from our API Definition.
package core
import (
fmt "fmt"
http "net/http"
)
// RequestOption adapts the behavior of the client or an individual request.
type RequestOption interface {
applyRequestOptions(*RequestOptions)
}
// RequestOptions defines all of the possible request options.
//
// This type is primarily used by the generated code and is not meant
// to be used directly; use the option package instead.
type RequestOptions struct {
BaseURL string
HTTPClient HTTPClient
HTTPHeader http.Header
MaxAttempts uint
Token string
ClientName *string
}
// NewRequestOptions returns a new *RequestOptions value.
//
// This function is primarily used by the generated code and is not meant
// to be used directly; use RequestOption instead.
func NewRequestOptions(opts ...RequestOption) *RequestOptions {
options := &RequestOptions{
HTTPHeader: make(http.Header),
}
for _, opt := range opts {
opt.applyRequestOptions(options)
}
return options
}
// ToHeader maps the configured request options into a http.Header used
// for the request(s).
func (r *RequestOptions) ToHeader() http.Header {
header := r.cloneHeader()
if r.Token != "" {
header.Set("Authorization", "Bearer "+r.Token)
}
if r.ClientName != nil {
header.Set("X-Client-Name", fmt.Sprintf("%v", *r.ClientName))
}
return header
}
func (r *RequestOptions) cloneHeader() http.Header {
headers := r.HTTPHeader.Clone()
headers.Set("X-Fern-Language", "Go")
headers.Set("X-Fern-SDK-Name", "github.com/cohere-ai/cohere-go/v2")
headers.Set("X-Fern-SDK-Version", "v2.7.3")
return headers
}
// BaseURLOption implements the RequestOption interface.
type BaseURLOption struct {
BaseURL string
}
func (b *BaseURLOption) applyRequestOptions(opts *RequestOptions) {
opts.BaseURL = b.BaseURL
}
// HTTPClientOption implements the RequestOption interface.
type HTTPClientOption struct {
HTTPClient HTTPClient
}
func (h *HTTPClientOption) applyRequestOptions(opts *RequestOptions) {
opts.HTTPClient = h.HTTPClient
}
// HTTPHeaderOption implements the RequestOption interface.
type HTTPHeaderOption struct {
HTTPHeader http.Header
}
func (h *HTTPHeaderOption) applyRequestOptions(opts *RequestOptions) {
opts.HTTPHeader = h.HTTPHeader
}
// MaxAttemptsOption implements the RequestOption interface.
type MaxAttemptsOption struct {
MaxAttempts uint
}
func (m *MaxAttemptsOption) applyRequestOptions(opts *RequestOptions) {
opts.MaxAttempts = m.MaxAttempts
}
// TokenOption implements the RequestOption interface.
type TokenOption struct {
Token string
}
func (t *TokenOption) applyRequestOptions(opts *RequestOptions) {
opts.Token = t.Token
}
// ClientNameOption implements the RequestOption interface.
type ClientNameOption struct {
ClientName *string
}
func (c *ClientNameOption) applyRequestOptions(opts *RequestOptions) {
opts.ClientName = c.ClientName
}