File tree 3 files changed +30
-16
lines changed
3 files changed +30
-16
lines changed Original file line number Diff line number Diff line change
1
+ package mytokenlib
2
+
3
+ import (
4
+ "context"
5
+ "net/http"
6
+ )
7
+
8
+ var ctx = context .Background ()
9
+ var httpClient = & http.Client {}
10
+ var userAgent = "mytokenlib"
11
+
12
+ // ContextKeyUserAgent is used to set a useragent string in the context
13
+ const ContextKeyUserAgent = "mytokenlib-user-agent"
14
+
15
+ // SetClient sets the http.Client used to make API requests
16
+ func SetClient (client * http.Client ) {
17
+ httpClient = client
18
+ }
19
+
20
+ // SetContext sets a context.Context used for all API requests
21
+ func SetContext (contxt context.Context ) {
22
+ ctx = contxt
23
+ s , ok := ctx .Value (ContextKeyUserAgent ).(string )
24
+ if ok {
25
+ userAgent = s
26
+ }
27
+ }
Original file line number Diff line number Diff line change @@ -33,6 +33,9 @@ func doHTTPRequestWithAuth(
33
33
if err != nil {
34
34
return newMytokenErrorFromError (errSendingHttpRequest , err )
35
35
}
36
+ if userAgent != "" {
37
+ req .Header .Set ("User-Agent" , userAgent )
38
+ }
36
39
if reqBody != nil {
37
40
req .Header .Set ("Content-Type" , mimetypeJSON )
38
41
}
Original file line number Diff line number Diff line change 1
1
package mytokenlib
2
2
3
3
import (
4
- "context"
5
- "net/http"
6
-
7
4
"github.com/oidc-mytoken/api/v0"
8
5
)
9
6
@@ -23,9 +20,6 @@ type Endpoint interface {
23
20
DoHTTPRequest (method string , req interface {}, resp interface {}) error
24
21
}
25
22
26
- var httpClient = & http.Client {}
27
- var ctx = context .Background ()
28
-
29
23
// NewMytokenServer creates a new MytokenServer
30
24
func NewMytokenServer (url string ) (* MytokenServer , error ) {
31
25
configEndpoint := url
@@ -52,13 +46,3 @@ func NewMytokenServer(url string) (*MytokenServer, error) {
52
46
}
53
47
return server , err
54
48
}
55
-
56
- // SetClient sets the http.Client used to make API requests
57
- func SetClient (client * http.Client ) {
58
- httpClient = client
59
- }
60
-
61
- // SetContext sets a context.Context used for all API requests
62
- func SetContext (contxt context.Context ) {
63
- ctx = contxt
64
- }
You can’t perform that action at this time.
0 commit comments