-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.go
56 lines (51 loc) · 1.31 KB
/
poll.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 pact
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Request struct {
RequestKeys string `json:"requestKeys,omitempty"`
}
type PollResponse struct {
Gas int64 `json:"gas,omitempty"`
ReqKey string `json:"reqKey,omitempty"`
TxId string `json:"txId,omitempty"`
Logs string `json:"logs,omitempty"`
MetaData interface{} `json:"metaData,omitempty"`
Continuation interface{} `json:"continuation,omitempty"`
Events PactEvents `json:"events,omitempty"`
}
type RequestKeys struct {
RequestKeys []string `json:"requestKeys,omitempty"`
}
func Poll(requestKeys []string, apiHost string) (res *PollResponse, err error) {
defer func() {
if e := recover(); e != nil {
switch er := e.(type) {
case Error:
err = er
case error:
err = er
}
}
}()
postBody, err := json.Marshal(RequestKeys{RequestKeys: requestKeys})
EnforceNoError(err)
req := bytes.NewBuffer(postBody)
resp, err := http.Post(
fmt.Sprintf("%s/api/v1/poll", apiHost),
"application/json",
req,
)
EnforceNoError(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
EnforceNoError(err)
EnforceValid(resp.StatusCode == http.StatusOK, fmt.Errorf("%v", string(body)))
err = json.Unmarshal(body, &res)
EnforceNoError(err)
return
}