-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrequest.go
42 lines (38 loc) · 1.15 KB
/
request.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
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
)
var client = &http.Client{}
func chat(gameId, room, text string) *http.Response {
method := "POST"
path := "bot/game/"+gameId+"/chat"
line, _ := json.Marshal(map[string]string{"room": room, "text": text})
req, err := http.NewRequest(method, conf.Url+path, bytes.NewBuffer(line))
req.Header.Add("Authorization", "Bearer "+conf.Token)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Println("Failed request "+method+" "+path, err)
return nil
}
if resp.StatusCode != http.StatusOK {
log.Printf("Response %d %s %s %s", resp.StatusCode, http.StatusText(resp.StatusCode), method, path)
}
return resp
}
func request(method, path string) *http.Response {
req, err := http.NewRequest(method, conf.Url+path, nil)
req.Header.Add("Authorization", "Bearer "+conf.Token)
resp, err := client.Do(req)
if err != nil {
log.Println("Failed request "+method+" "+path, err)
return nil
}
if resp.StatusCode != http.StatusOK {
log.Printf("Response %d %s %s %s", resp.StatusCode, http.StatusText(resp.StatusCode), method, path)
}
return resp
}