-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_utils.go
120 lines (114 loc) · 2.69 KB
/
http_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"bufio"
"bytes"
"io"
"net/http"
"os"
"strings"
)
type StreamConsumer func(data string) error
type HttpResponse struct {
status int
text string
headers http.Header
contentType string
}
func PostSse(url string, headers map[string]string, body string, consumer StreamConsumer) (*HttpResponse, error) {
bodyReader := strings.NewReader(body)
req, err := http.NewRequest("POST", url, bodyReader)
if err != nil {
return nil, err
}
if headers != nil {
for key, value := range headers {
req.Header.Set(key, value)
}
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
contentType := resp.Header.Get("Content-Type")
defer resp.Body.Close()
var text string
if resp.StatusCode == 200 {
if strings.Contains(contentType, "text/event-stream") {
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadBytes('\n')
lineStr := string(bytes.TrimSpace(line))
if strings.HasPrefix(lineStr, "data:") {
lineStr = strings.TrimSpace(lineStr[5:])
if lineStr == "" || lineStr == "[DONE]" {
continue
}
err := consumer(lineStr)
if err != nil {
return nil, err
}
}
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
}
} else if strings.Contains(contentType, "application/json") {
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
text = string(data)
}
}
return &HttpResponse{status: resp.StatusCode, text: text, headers: resp.Header, contentType: contentType}, nil
}
func PostJson(url string, headers map[string]string, body string) (*HttpResponse, error) {
bodyReader := strings.NewReader(body)
req, err := http.NewRequest("POST", url, bodyReader)
if err != nil {
return nil, err
}
if headers != nil {
for key, value := range headers {
req.Header.Set(key, value)
}
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
contentType := resp.Header.Get("Content-Type")
defer resp.Body.Close()
var text string
if resp.StatusCode == 200 {
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
text = string(data)
}
return &HttpResponse{status: resp.StatusCode, text: text, headers: resp.Header, contentType: contentType}, nil
}
func Download(url string, path string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}