-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetUtils.go
More file actions
34 lines (26 loc) · 802 Bytes
/
Copy pathNetUtils.go
File metadata and controls
34 lines (26 loc) · 802 Bytes
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
package api
import (
"strconv"
"encoding/json"
"net/http"
"io/ioutil"
"errors"
)
type StringResult struct {
Result string
Err error
}
// getBodyJson takes an http.Response pointer and reads all of its data into a map
func getBodyJson(response *http.Response) (map[string] string, error) {
bodyBytes, err := ioutil.ReadAll(response.Body)
if (err != nil) {
return nil, err
}
bodyMap := make(map[string] string)
json.Unmarshal(bodyBytes, &bodyMap)
return bodyMap, nil
}
// buildStatusError takes an http response and builds an error with the status code and message in it
func buildStatusError(message string, response *http.Response) error {
return errors.New(message + "; Status " + strconv.Itoa(response.StatusCode) + ": " + response.Status)
}