|
|
@@ -0,0 +1,52 @@
|
|
|
+package external
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+ "io/ioutil"
|
|
|
+ "net/http"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+type HuaZhangGuiClient interface {
|
|
|
+ GetWeather(ctx context.Context, city string) (string, error)
|
|
|
+ PostUnReadMessage(ctx context.Context, reqData UnreadMessageRequest) (string, error)
|
|
|
+}
|
|
|
+
|
|
|
+type huaZhangGuiClient struct {
|
|
|
+ httpClient *http.Client
|
|
|
+ baseURL string
|
|
|
+}
|
|
|
+
|
|
|
+func NewHuaZhangGuiClient(baseURL string) HuaZhangGuiClient {
|
|
|
+ return &huaZhangGuiClient{
|
|
|
+ httpClient: &http.Client{Timeout: 5 * time.Second},
|
|
|
+ baseURL: baseURL,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (c *huaZhangGuiClient) 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 *huaZhangGuiClient) 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
|
|
|
+}
|