-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,3 +79,8 @@ TODO | |
# tenxunloc | ||
|
||
> 腾讯位置服务 | ||
|
||
# getui | ||
|
||
> 个推服务 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters