-
Notifications
You must be signed in to change notification settings - Fork 4
/
server_request.go
82 lines (68 loc) · 1.69 KB
/
server_request.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
package porthos
import (
"bytes"
"context"
"encoding/json"
"fmt"
)
// Request represents a rpc request.
type Request interface {
// GetServiceName returns the service name.
GetServiceName() string
// GetMethodName returns the method name.
GetMethodName() string
// GetBody returns the request body.
GetBody() []byte
// Form returns a index-based form.
Form() (Form, error)
// Bind binds the body to an interface.
Bind(i interface{}) error
// WithContext returns a shallow copy of Event with its context changed to context.
// The provided context must be non-nil.
WithContext(context.Context) Request
// The returned context is always non-nil; it defaults to the background context.
// To change the context, use WithContext.
Context() context.Context
}
type request struct {
serviceName string
methodName string
contentType string
body []byte
ctx context.Context
}
func (r *request) GetServiceName() string {
return r.serviceName
}
func (r *request) GetMethodName() string {
return r.methodName
}
func (r *request) GetBody() []byte {
return r.body
}
func (r *request) Form() (Form, error) {
return NewForm(r.contentType, r.body)
}
func (r *request) Bind(i interface{}) error {
if r.contentType != "application/json" {
return fmt.Errorf("Invalid content type, got: %s", r.contentType)
}
decoder := json.NewDecoder(bytes.NewReader(r.body))
decoder.UseNumber()
return decoder.Decode(i)
}
func (r *request) WithContext(ctx context.Context) Request {
if ctx == nil {
panic("nil context")
}
r2 := new(request)
*r2 = *r
r2.ctx = ctx
return r2
}
func (r *request) Context() context.Context {
if r.ctx != nil {
return r.ctx
}
return context.Background()
}