-
Notifications
You must be signed in to change notification settings - Fork 1
/
sling.go
37 lines (29 loc) · 850 Bytes
/
sling.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
package client
import (
"fmt"
sjson "github.com/bitly/go-simplejson"
"github.com/dghubble/sling"
)
type SlintExt sling.Sling
func ToSlintExt(slingObject *sling.Sling) *SlintExt {
return (*SlintExt)(slingObject)
}
func (c *SlintExt) DoReceive(expectedStatus int, successV interface{}) error {
slingClient := (*sling.Sling)(c)
jsonError := sjson.New()
resp, err := slingClient.Receive(successV, jsonError)
if err != nil {
return fmt.Errorf("HTTP request error: %v.", err)
}
if resp.StatusCode != expectedStatus {
jsonResp, marshalJsonError := jsonError.MarshalJSON()
if marshalJsonError != nil {
return fmt.Errorf("Marshal JSON body of response error: %v", marshalJsonError)
}
return fmt.Errorf(
"Status: %d(needing [%d]). Json Response: %s",
resp.StatusCode, expectedStatus, string(jsonResp),
)
}
return nil
}