forked from kdvh/whatapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
47 lines (43 loc) · 1017 Bytes
/
util.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
package whatapi
import (
"errors"
"fmt"
"net/url"
)
var (
errLoginFailed = errors.New("Login failed")
errRequestFailed = errors.New("Request failed")
errRequestFailedLogin = errors.New("Request failed: not logged in")
errRequestFailedReason = func(err string) error { return fmt.Errorf("Request failed: %s", err) }
debugMode = false
)
func buildURL(baseURL, path, action string, params url.Values) (string, error) {
u, err := url.Parse(baseURL)
if err != nil {
return "", err
}
u.Path = path
query := make(url.Values)
if action != "" {
query.Set("action", action)
}
for param, values := range params {
for _, value := range values {
query.Set(param, value)
}
}
u.RawQuery = query.Encode()
if debugMode {
fmt.Println(u.String())
}
return u.String(), nil
}
func checkResponseStatus(status, errorStr string) error {
if status != "success" {
if errorStr != "" {
return errRequestFailedReason(errorStr)
}
return errRequestFailed
}
return nil
}