-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
106 lines (86 loc) · 2.27 KB
/
transport.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
package hertz
import (
"context"
"net/http"
"github.com/apus-run/gaia/transport"
)
const (
KindHertz transport.Kind = "hertz"
)
var _ Transporter = &Transport{}
// Transporter is http Transporter
type Transporter interface {
transport.Transporter
Request() *http.Request
PathTemplate() string
}
// Transport is an HTTP transport.
type Transport struct {
endpoint string
operation string
reqHeader headerCarrier
replyHeader headerCarrier
request *http.Request
pathTemplate string
}
// Kind returns the transport kind.
func (tr *Transport) Kind() transport.Kind {
return KindHertz
}
// Endpoint returns the transport endpoint.
func (tr *Transport) Endpoint() string {
return tr.endpoint
}
// Operation returns the transport operation.
func (tr *Transport) Operation() string {
return tr.operation
}
// Request returns the HTTP request.
func (tr *Transport) Request() *http.Request {
return tr.request
}
// RequestHeader returns the request header.
func (tr *Transport) RequestHeader() transport.Header {
return tr.reqHeader
}
// ReplyHeader returns the reply header.
func (tr *Transport) ReplyHeader() transport.Header {
return tr.replyHeader
}
// PathTemplate returns the http path template.
func (tr *Transport) PathTemplate() string {
return tr.pathTemplate
}
// SetOperation sets the transport operation.
func SetOperation(ctx context.Context, op string) {
if tr, ok := transport.FromServerContext(ctx); ok {
if tr, ok := tr.(*Transport); ok {
tr.operation = op
}
}
}
type headerCarrier http.Header
// Get returns the value associated with the passed key.
func (hc headerCarrier) Get(key string) string {
return http.Header(hc).Get(key)
}
// Set stores the key-value pair.
func (hc headerCarrier) Set(key string, value string) {
http.Header(hc).Set(key, value)
}
// Keys lists the keys stored in this carrier.
func (hc headerCarrier) Keys() []string {
keys := make([]string, 0, len(hc))
for k := range http.Header(hc) {
keys = append(keys, k)
}
return keys
}
// Add append value to key-values pair.
func (hc headerCarrier) Add(key string, value string) {
http.Header(hc).Add(key, value)
}
// Values returns a slice of values associated with the passed key.
func (hc headerCarrier) Values(key string) []string {
return http.Header(hc).Values(key)
}