-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathclient.go
More file actions
68 lines (55 loc) · 1.37 KB
/
client.go
File metadata and controls
68 lines (55 loc) · 1.37 KB
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
63
64
65
66
67
68
package main
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
type ErrorResponse struct {
ErrorCode int
Err error
}
const (
urlEncodedContent string = "application/x-www-form-urlencoded"
apiToken string = "https://notify-bot.line.me/oauth/token"
apiNotify string = "https://notify-api.line.me/api/notify"
)
func apiCall(mode string, inUrl string, data url.Values, token string) ([]byte, *ErrorResponse) {
client := &http.Client{}
fmt.Println("connected: ", mode, inUrl, data)
r := &http.Request{}
if data == nil {
r, _ = http.NewRequest(mode, inUrl, nil)
} else {
r, _ = http.NewRequest(mode, inUrl, strings.NewReader(data.Encode()))
}
r.Header.Add("Content-Type", urlEncodedContent)
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
if len(token) != 0 {
r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
}
ret := new(ErrorResponse)
resp, err := client.Do(r)
if err != nil {
log.Println("er:", err)
ret.Err = err
return nil, ret
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("er:", err)
ret.Err = err
return nil, ret
}
if resp.StatusCode > http.StatusAccepted {
ret.ErrorCode = resp.StatusCode
ret.Err = errors.New("Error on:" + string(body))
log.Println("Error happen! body:", string(body))
return body, ret
}
return body, nil
}