forked from sogko/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
62 lines (52 loc) · 1.27 KB
/
utils.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
package wordpress
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/parnurzeal/gorequest"
"log"
"net/http"
"os"
)
var DEBUG bool = (os.Getenv("DEBUG") == "1")
func unmarshallResponse(resp gorequest.Response, body []byte, result interface{}) error {
var prettyJSON bytes.Buffer
err2 := json.Indent(&prettyJSON, body, "", " ")
if err2 != nil {
log.Println("JSON parse error: ", err2)
if DEBUG {
log.Println("body: ", string(body))
}
}
if DEBUG {
log.Println("body: ", string(prettyJSON.Bytes()))
}
if resp.StatusCode != http.StatusOK &&
resp.StatusCode != http.StatusCreated &&
resp.StatusCode != http.StatusAccepted {
return errors.New(resp.Status)
}
err := json.Unmarshal(body, result)
if err != nil {
log.Println("JSON parse error: ", err)
return err
}
return nil
}
func _warning(v ...interface{}) {
log.Println(fmt.Sprintln("[go-wordpress]", v))
}
func _log(v ...interface{}) {
log.Println(fmt.Sprintln("[go-wordpress]", v))
}
// UnmarshallServerError A helper function to unmarshall error response from server
func UnmarshallServerError(body []byte) ([]GeneralError, error) {
var resp []GeneralError
err := json.Unmarshal(body, &resp)
if err != nil {
log.Println("JSON parse error: ", err)
return nil, err
}
return resp, nil
}