summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/SevereCloud/vksdk/v2/internal/transport.go
blob: 595b253e426a53e5554aa11c43bc8a817ccc8cbb (plain) (blame)
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
/*
Package internal unimportable
*/
package internal // import "github.com/SevereCloud/vksdk/v2/internal"

import (
	"context"
	"net/http"

	"github.com/SevereCloud/vksdk/v2"
)

// UserAgent module.
const UserAgent = "vksdk/" + vksdk.Version + " (+https://github.com/SevereCloud/vksdk)"

// ContextKey is just an empty struct. It exists so HTTPClient can be
// an immutable public variable with a unique type. It's immutable
// because nobody else can create a ContextKey, being unexported.
type ContextKey int

// ContextKey list.
const (
	HTTPClientKey ContextKey = iota
	UserAgentKey
	GroupIDKey
	EventIDKey
	LongPollTsKey
	CallbackRetryCounterKey
	CallbackRetryAfterKey
	CallbackRemove
	EventVersionKey
)

// ContextClient return *http.Client.
func ContextClient(ctx context.Context) *http.Client {
	if ctx != nil {
		if hc, ok := ctx.Value(HTTPClientKey).(*http.Client); ok {
			return hc
		}
	}

	return http.DefaultClient
}

// ContextUserAgent return User-Agent from context.
func ContextUserAgent(ctx context.Context) string {
	if ctx != nil {
		if hc, ok := ctx.Value(UserAgentKey).(string); ok {
			return hc
		}
	}

	return UserAgent
}

// DoRequest sends an HTTP request and returns an HTTP response.
//
// The provided ctx must be non-nil. If it is canceled or times out,
// ctx.Err() will be returned.
func DoRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
	client := ContextClient(ctx)

	if req.UserAgent() == "" {
		req.Header.Set("User-Agent", ContextUserAgent(ctx))
	}

	resp, err := client.Do(req.WithContext(ctx))
	// If we got an error, and the context has been canceled,
	// the context's error is probably more useful.
	if err != nil {
		<-ctx.Done()
		err = ctx.Err()
	}

	return resp, err
}