forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_request.go
54 lines (45 loc) · 1.06 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
package porthos
import (
"bytes"
"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
}
type request struct {
serviceName string
methodName string
contentType string
body []byte
}
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)
}