Skip to content

Commit

Permalink
个推
Browse files Browse the repository at this point in the history
  • Loading branch information
gtlions committed Apr 26, 2021
1 parent 1b092bb commit c4e1f49
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ TODO
# tenxunloc

> 腾讯位置服务

# getui

> 个推服务
13 changes: 13 additions & 0 deletions getui/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package getui

const (
bashUrl = "https://restapi.getui.com/v2"
)

type ConfigParam struct {
AppID string `json:"app_id,omitempty" form:"app_id"`
AppKey string `json:"app_key,omitempty" form:"app_key"`
MasterSecret string `json:"master_secret,omitempty" form:"master_secret"`
baseUrl string `json:"base_url,omitempty" form:"base_url"`
AuthSignResp AuthSignResp `json:"auth_sign_resp,omitempty" form:"auth_sign_resp"`
}
122 changes: 122 additions & 0 deletions getui/getui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package getui

import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)

func GetAuthToken(config *ConfigParam) (err error) {
config.baseUrl = fmt.Sprintf(bashUrl + "/" + config.AppID)
timestamp := time.Now().UnixNano() / 1e6
client := http.DefaultClient
parm := AuthSignParam{Sign: fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%s%d%s", config.AppKey, timestamp, config.MasterSecret)))), Timestamp: fmt.Sprintf("%d", timestamp), Appkey: config.AppKey}
payload, _ := json.Marshal(parm)
req, _ := http.NewRequest("POST", config.baseUrl+"/auth", bytes.NewBuffer(payload))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Charset", "utf-8")
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
rspBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(rspBody, &config.AuthSignResp)
if err != nil {
return
}
return err
}

func SetUserAlias(config ConfigParam, userAliasReq SetUserAliasReq) (rsp Resp, err error) {
client := http.DefaultClient
payload, _ := json.Marshal(userAliasReq)
req, _ := http.NewRequest("POST", config.baseUrl+"/user/alias", bytes.NewBuffer(payload))
req.Header.Add("Token", config.AuthSignResp.Data.Token)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Charset", "utf-8")
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
rspBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(rspBody, &rsp)
if err != nil {
return
}
return rsp, err
}

func UnSetUserAlias(config ConfigParam, alias string) (rsp Resp, err error) {
client := http.DefaultClient
req, _ := http.NewRequest("DELETE", config.baseUrl+"/user/alias"+"/"+alias, nil)
req.Header.Add("Token", config.AuthSignResp.Data.Token)
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
rspBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(rspBody, &rsp)
if err != nil {
return
}
return rsp, err
}

func GetUserAlias(config ConfigParam, cid string) (rsp GetAliasResp, err error) {
client := http.DefaultClient
req, _ := http.NewRequest("GET", config.baseUrl+"/user/alias/cid"+"/"+cid, nil)
req.Header.Add("Token", config.AuthSignResp.Data.Token)
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
rspBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(rspBody, &rsp)
if err != nil {
return
}
return rsp, err
}

func PushToSingleCid(config ConfigParam, pushReq PushReq) (rsp Resp, err error) {
client := http.DefaultClient
payload, _ := json.Marshal(pushReq)
req, _ := http.NewRequest("POST", config.baseUrl+"/push/single/cid", bytes.NewBuffer(payload))
req.Header.Add("Token", config.AuthSignResp.Data.Token)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Charset", "utf-8")
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
rspBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(rspBody, &rsp)
if err != nil {
return
}
return rsp, err
}
87 changes: 87 additions & 0 deletions getui/getui_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package getui

import (
"encoding/json"
"fmt"
"testing"
"time"

"github.com/gtlions/gos10i"
)

// TestAuth
func TestAuth(t *testing.T) {
var (
appID = "xE1r97JijZ6Aj9CVjVvsa9a7"
appKey = "3EdESIR2ndtY9IfBfiLMCZQ7"
masterSecret = "p3suROTV6f17flDjpIln1I1"
)
config := ConfigParam{AppID: appID, AppKey: appKey, MasterSecret: masterSecret}
if err := GetAuthToken(&config); err != nil {
t.Error(err)
} else {
r, _ := json.MarshalIndent(config, "", " ")
t.Logf("\n\n%s\n\n", string(r))
}
}

func TestSetUserAlias(t *testing.T) {
appID := "E1r97JijZ6Aj9dsCVjVvsa97"
config := ConfigParam{}
config.baseUrl = fmt.Sprintf(bashUrl + "/" + appID)
config.AuthSignResp.Data.Token = "89c3ce1d726ce256wewr1063dbcc4ffed8d62533d762bb1b5066757d588c1291d37d"
users := []UserAlias{}
users = append(users, UserAlias{Cid: "71ee1265b15e35ds0f6a92876f485bb6f", Alias: "ldm"})
userAliasReq := SetUserAliasReq{DataList: users}
if rsp, err := SetUserAlias(config, userAliasReq); err != nil {
t.Error(err)
} else {
r, _ := json.MarshalIndent(rsp, "", " ")
t.Logf("\n\n%s\n\n", string(r))
}
}
func TestUnSetUserAlias(t *testing.T) {
appID := "E1r97JijZs6Aj9Cfs23VjVvsa97"
config := ConfigParam{}
config.baseUrl = fmt.Sprintf(bashUrl + "/" + appID)
config.AuthSignResp.Data.Token = "89c3ace1d726ce256106df3dbcc4ffed8d62533d762bb1b5066757d588c1291d37d"
alias := "ldm"
if rsp, err := UnSetUserAlias(config, alias); err != nil {
t.Error(err)
} else {
r, _ := json.MarshalIndent(rsp, "", " ")
t.Logf("\n\n%s\n\n", string(r))
}
}

func TestGetUserAlias(t *testing.T) {
appID := "E123r97JijfZ6Aj9CVfjVvsa97"
config := ConfigParam{}
config.baseUrl = fmt.Sprintf(bashUrl + "/" + appID)
config.AuthSignResp.Data.Token = "89c3ce1d726ce256asdss1063dbcc4ffed8d62533d7f62bb1b5066757d588c1291d37d"
cid := "71ee1265b15e35ad0f6a92821376f485bb6f"
if rsp, err := GetUserAlias(config, cid); err != nil {
t.Error(err)
} else {
r, _ := json.MarshalIndent(rsp, "", " ")
t.Logf("\n\n%s\n\n", string(r))
}
}

func TestPushToSingleCid(t *testing.T) {
appID := "E1r97J2ijZ6Aj9CserVjVvsa97"
config := ConfigParam{}
config.baseUrl = fmt.Sprintf(bashUrl + "/" + appID)
config.AuthSignResp.Data.Token = "89c3ce1sd726ce256st1063dbcc4ffed8d62533xwd762bb1b5066757d588c1291d37d"
push := PushReq{}
push.RequestID = gos10i.X2String(time.Now().UnixNano())
push.Settings.TTL = 3600 * 1000
push.Audience.Cid = []string{"71ee1265b15sde35d0f6a92876f485b5wb6f"}
push.PushMessage = PushMessage{Notification: PushMessageNotification{Title: "测试推送", Body: time.Now().String(), ClickType: "url", URL: "https//:xxx"}}
if rsp, err := PushToSingleCid(config, push); err != nil {
t.Error(err)
} else {
r, _ := json.MarshalIndent(rsp, "", " ")
t.Logf("\n\n%s\n\n", string(r))
}
}
69 changes: 69 additions & 0 deletions getui/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package getui

type Resp struct {
Msg string `json:"msg"`
Code int `json:"code"`
}
type AuthSignParam struct {
Sign string `json:"sign"`
Timestamp string `json:"timestamp"`
Appkey string `json:"appkey"`
}

type AuthSignResp struct {
Resp
Data AuthSignRespData `json:"data"`
}
type AuthSignRespData struct {
Expiretime string `json:"expire_time"`
Token string `json:"token"`
}

type SetUserAliasReq struct {
DataList []UserAlias `json:"data_list"`
}
type UserAlias struct {
Cid string `json:"cid"`
Alias string `json:"alias"`
}

type PushSettings struct {
TTL int `json:"ttl"`
}
type PushAudience struct {
Cid []string `json:"cid"`
}
type PushMessageNotification struct {
Title string `json:"title"`
Body string `json:"body"`
ClickType string `json:"click_type"`
URL string `json:"url"`
}
type PushMessage struct {
Notification PushMessageNotification `json:"notification"`
}
type PushReq struct {
RequestID string `json:"request_id"`
Settings PushSettings `json:"settings"`
Audience PushAudience `json:"audience"`
PushMessage PushMessage `json:"push_message"`
}

type PushRespData struct {
TaskID string `json:"task_id"`
Status string `json:"status"`
}

type PushResp struct {
Resp
PushRespData
}

type GetAliasRespData struct {
Alias string `json:"alias"`
}

type GetAliasResp struct {
Resp
Data GetAliasRespData `json:"data"`
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/aliyun/aliyun-oss-go-sdk v2.1.4+incompatible
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/gogap/mahonia v0.0.0-20150421041633-37416fc26c88
github.com/golang/protobuf v1.5.2
github.com/gorilla/schema v1.2.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/satori/go.uuid v1.2.0
Expand Down

0 comments on commit c4e1f49

Please sign in to comment.