-
Notifications
You must be signed in to change notification settings - Fork 0
/
longpoll.go
42 lines (34 loc) · 832 Bytes
/
longpoll.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 longpoll
import (
"context"
"fmt"
"io/ioutil"
"net/http"
)
// RunWithParm run a long polling GET request with parameters and return result
func RunWithParm(ctx context.Context, url string, param map[string]string) ([]byte, error) {
url += "?"
for index, value := range param {
url += "&" + index + "=" + value
}
return Run(ctx, url)
}
// Run make a long polling request and return result
func Run(ctx context.Context, url string) ([]byte, error) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("%v\b", err)
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("%v\b", err)
}
return b, nil
}