-
Notifications
You must be signed in to change notification settings - Fork 8
/
serialization.go
75 lines (61 loc) · 1.29 KB
/
serialization.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
package transports
import (
"encoding/json"
"fmt"
"net/http"
)
type Request struct {
Method string
URL string
Proto string
Headers map[string][]string
}
type Response struct {
Status string
StatusCode int
Proto string
Headers map[string][]string
Body string
}
type DefaultSerializer struct {
}
func (serializer *DefaultSerializer) Serialize(req interface{}, jsonOutput bool) interface{} {
var output []byte
var r interface{}
switch t := req.(type) {
case *http.Request:
req := req.(*http.Request)
r = Request{
Method: req.Method,
URL: req.URL.String(),
Proto: req.Proto,
Headers: req.Header,
}
case *http.Response:
res := req.(*http.Response)
r = Response{
Status: res.Status,
StatusCode: res.StatusCode,
Proto: res.Proto,
Headers: res.Header,
}
default:
fmt.Println("Unknown Type", t)
}
if jsonOutput {
output, _ = json.Marshal(r)
return output
}
return r
}
func (serializer *DefaultSerializer) DeserializeRequest(Input []byte) *http.Request {
r := Request{}
json.Unmarshal(Input, &r)
request, _ := http.NewRequest(r.Method, r.URL, nil)
return request
}
func (serializer *DefaultSerializer) DeserializeResponse(Input []byte) Response {
r := Response{}
json.Unmarshal(Input, &r)
return r
}