You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.2 KiB

2 years ago
package http
import (
"context"
"git.diulo.com/mogfee/kit/transport"
"net/http"
)
2 years ago
var _ Transporter = (*Transport)(nil)
// Transporter is http Transporter
2 years ago
type Transporter interface {
transport.Transporter
Request() *http.Request
PathTemplate() string
}
2 years ago
// Transport is an HTTP transport.
2 years ago
type Transport struct {
endpoint string
operation string
reqHeader headerCarrier
replyHeader headerCarrier
request *http.Request
pathTemplate string
}
2 years ago
// Kind returns the transport kind.
func (tr *Transport) Kind() transport.Kind {
2 years ago
return transport.KindHTTP
}
2 years ago
// Endpoint returns the transport endpoint.
func (tr *Transport) Endpoint() string {
return tr.endpoint
2 years ago
}
2 years ago
// Operation returns the transport operation.
func (tr *Transport) Operation() string {
return tr.operation
2 years ago
}
2 years ago
// Request returns the HTTP request.
func (tr *Transport) Request() *http.Request {
return tr.request
2 years ago
}
2 years ago
// RequestHeader returns the request header.
func (tr *Transport) RequestHeader() transport.Header {
return tr.reqHeader
2 years ago
}
2 years ago
// ReplyHeader returns the reply header.
func (tr *Transport) ReplyHeader() transport.Header {
return tr.replyHeader
2 years ago
}
2 years ago
// PathTemplate returns the http path template.
func (tr *Transport) PathTemplate() string {
return tr.pathTemplate
2 years ago
}
2 years ago
// SetOperation sets the transport operation.
2 years ago
func SetOperation(ctx context.Context, op string) {
if tr, ok := transport.FromServerContext(ctx); ok {
if tr, ok := tr.(*Transport); ok {
tr.operation = op
}
}
}
2 years ago
// RequestFromServerContext returns request from context.
2 years ago
func RequestFromServerContext(ctx context.Context) (*http.Request, bool) {
if tr, ok := transport.FromServerContext(ctx); ok {
if tr, ok := tr.(*Transport); ok {
return tr.request, true
}
}
return nil, false
}
2 years ago
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
}