-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreply.go
executable file
·56 lines (51 loc) · 1.55 KB
/
reply.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
package gridas
import (
"io/ioutil"
"net/http"
"time"
"labix.org/v2/mgo/bson"
"github.com/crbrox/gridas/mylog"
)
//Reply represents the response from the target host
type Reply struct {
//Reply id. Currently the same as the petition id
ID string `json:"id"`
TraceID string `json:"traceid"`
//Possible error in making the request. Could be ""
Error string `json:"error"`
StatusCode int `json:"statuscode"` // e.g. 200
Proto string `json:"proto"` // e.g. "HTTP/1.0"
Header http.Header `json:"header"`
Trailer http.Header `json:"trailer"`
Body []byte `json:"body"`
//Petition that
Petition *Petition `json:"petition"`
//Beginning of the request
Created time.Time `json:"created"`
//Time when response was received
Done time.Time `json:"done"`
}
//newReply returns the Reply for the Petition made, the http.Response gotten and the possible error
func newReply(resp *http.Response, p *Petition, e error) *Reply {
var reply = &Reply{ID: p.ID, Petition: p, TraceID: p.TraceID}
if e != nil {
reply.Error = e.Error()
return reply
}
reply.StatusCode = resp.StatusCode
reply.Proto = resp.Proto
reply.Header = resp.Header
reply.Trailer = resp.Trailer
mylog.Debug("before reading response body", p.ID)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
mylog.Debugf("error reading response body %v %+v", err, p)
reply.Error = e.Error()
} else {
mylog.Debug("after reading response body", p.ID)
reply.Body = body
}
reply.Done = bson.Now()
mylog.Debugf("reply done %+v", reply)
return reply
}