feat: 完成每日早报函数开发

This commit is contained in:
RockYang
2023-07-10 18:59:53 +08:00
parent 00c520d066
commit d1d13a72e4
11 changed files with 235 additions and 73 deletions

68
api/utils/http.go Normal file
View File

@@ -0,0 +1,68 @@
package utils
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
)
func HttpGet(uri string, proxy string) ([]byte, error) {
var client *http.Client
if proxy == "" {
client = &http.Client{}
} else {
proxy, _ := url.Parse(proxy)
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxy),
},
}
}
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func HttpPost(uri string, params map[string]interface{}, proxy string) ([]byte, error) {
data, err := json.Marshal(params)
if err != nil {
return nil, err
}
var client *http.Client
if proxy == "" {
client = &http.Client{}
} else {
proxy, _ := url.Parse(proxy)
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxy),
},
}
}
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}