| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package external
- import (
- "bytes"
- "context"
- "encoding/json"
- "io/ioutil"
- "net/http"
- "time"
- )
- type HuaHuiBaoClient interface {
- GetWeather(ctx context.Context, city string) (string, error)
- PostUnReadMessage(ctx context.Context, reqData UnreadMessageRequest) (string, error)
- }
- type huaHuiBaoClient struct {
- httpClient *http.Client
- baseURL string
- }
- func NewHuaHuiBaoClient(baseURL string) HuaHuiBaoClient {
- return &huaHuiBaoClient{
- httpClient: &http.Client{Timeout: 5 * time.Second},
- baseURL: baseURL,
- }
- }
- func (c *huaHuiBaoClient) GetWeather(ctx context.Context, city string) (string, error) {
- req, _ := http.NewRequestWithContext(ctx, "GET", c.baseURL+"/weather?city="+city, nil)
- resp, err := c.httpClient.Do(req)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- body, _ := ioutil.ReadAll(resp.Body)
- return string(body), nil
- }
- func (c *huaHuiBaoClient) PostUnReadMessage(ctx context.Context, reqData UnreadMessageRequest) (string, error) {
- jsonData, _ := json.Marshal(reqData)
- req, _ := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/unread_message", bytes.NewBuffer(jsonData))
- req.Header.Set("Content-Type", "application/json")
- resp, err := c.httpClient.Do(req)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- body, _ := ioutil.ReadAll(resp.Body)
- return string(body), nil
- }
|