forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
33 lines (26 loc) · 872 Bytes
/
client.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
package httpx
import (
"context"
"net/http"
)
var clientContextKey = 0
var defaultClient = &http.Client{}
// ClientFromContext retrieves a http.Client that has been bound to this context
// previously by a call to httpx.ClientContext, defaulting to a default Client
// if none has been bound
func ClientFromContext(ctx context.Context) *http.Client {
found := ctx.Value(&clientContextKey)
if found == nil {
return defaultClient
}
return found.(*http.Client)
}
// ClientContext binds the provided client to a new context derived from the
// provided parent. Use httpx.ClientFromContext to retrieve the client at some
// later point.
func ClientContext(parent context.Context, client *http.Client) context.Context {
if client == nil {
panic("Cannot bind nil *http.Client to context tree")
}
return context.WithValue(parent, &clientContextKey, client)
}