mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-14 14:05:59 +08:00
add cmd/open_im_push
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
tpns "Open_IM/internal/push/sdk/tpns-server-sdk-go/go"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func PushAndGetResult(pushReq *http.Request) {
|
||||
c := &http.Client{}
|
||||
rsp, err := c.Do(pushReq)
|
||||
fmt.Println()
|
||||
if err != nil {
|
||||
//fmt.Printf("http err:%v", err)
|
||||
return
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
body, err := ioutil.ReadAll(rsp.Body)
|
||||
//fmt.Printf("http ReadAll err:%v, body:%v ", err, string(body))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
r := &tpns.CommonRsp{}
|
||||
json.Unmarshal(body, r)
|
||||
//fmt.Printf("push result: %+v", r)
|
||||
}
|
||||
|
||||
func UploadFile(req *http.Request) (int, error) {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return 0, fmt.Errorf("response error, status: %s, body: %s", resp.Status, string(body))
|
||||
}
|
||||
|
||||
type uploadResponse struct {
|
||||
RetCode int `json:"retCode"`
|
||||
ErrMsg string `json:"errMsg"`
|
||||
UploadId int `json:"uploadId"`
|
||||
}
|
||||
|
||||
var ur uploadResponse
|
||||
if err := json.Unmarshal(body, &ur); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if ur.RetCode != 0 {
|
||||
return 0, fmt.Errorf("response with %d:%s", ur.RetCode, ur.ErrMsg)
|
||||
}
|
||||
return ur.UploadId, nil
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package common
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func ToJson(v interface{}) string {
|
||||
bs, _ := json.Marshal(v)
|
||||
return string(bs)
|
||||
}
|
||||
Reference in New Issue
Block a user