...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package client
16
17 import "net/http"
18
19 const (
20 TimestampQueryMediaType = "application/timestamp-query"
21 JSONMediaType = "application/json"
22 )
23
24
25 type Option func(*options)
26
27 type options struct {
28 UserAgent string
29 ContentType string
30 }
31
32 func makeOptions(opts ...Option) *options {
33 o := &options{
34 UserAgent: "",
35 ContentType: "",
36 }
37
38 for _, opt := range opts {
39 opt(o)
40 }
41
42 return o
43 }
44
45
46 func WithUserAgent(userAgent string) Option {
47 return func(o *options) {
48 o.UserAgent = userAgent
49 }
50 }
51
52
53 func WithContentType(contentType string) Option {
54 return func(o *options) {
55 o.ContentType = contentType
56 }
57 }
58
59 type roundTripper struct {
60 http.RoundTripper
61 UserAgent string
62 ContentType string
63 }
64
65
66 func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
67 req.Header.Set("User-Agent", rt.UserAgent)
68 req.Header.Set("Content-Type", rt.ContentType)
69 return rt.RoundTripper.RoundTrip(req)
70 }
71
72 func createRoundTripper(inner http.RoundTripper, o *options) http.RoundTripper {
73 if inner == nil {
74 inner = http.DefaultTransport
75 }
76 if o.UserAgent == "" {
77
78 return inner
79 }
80 if o.ContentType == "" {
81
82 return inner
83 }
84 return &roundTripper{
85 RoundTripper: inner,
86 UserAgent: o.UserAgent,
87 ContentType: o.ContentType,
88 }
89 }
90
View as plain text