-
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
1 parent
1414237
commit 98d19ba
Showing
9 changed files
with
325 additions
and
309 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
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 |
---|---|---|
@@ -1,38 +1,45 @@ | ||
package gomeng | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
import "fmt" | ||
|
||
const ( | ||
baseURL = "https://msgapi.umeng.com" | ||
|
||
apiPush = "api/send" | ||
apiBroadcast = "api/send" | ||
) | ||
|
||
type requestType string | ||
|
||
const ( | ||
BaseURL = "https://msgapi.umeng.com/api/" | ||
unicastRequest requestType = "unicast" | ||
listcastRequest requestType = "listcast" | ||
broadcastRequest requestType = "broadcast" | ||
) | ||
|
||
type ReturnState string | ||
|
||
APIPush = "send" | ||
APIBroadcast = "send" | ||
const ( | ||
SuccessState ReturnState = "SUCCESS" | ||
FailState ReturnState = "FAIL" | ||
) | ||
|
||
type ResponseMessage struct { | ||
Ret string `json:"ret"` | ||
type ( | ||
Data struct { | ||
MsgID string `json:"msg_id"` | ||
TaskID string `json:"task_id"` | ||
ErrMsg string `json:"error_msg"` | ||
ErrCode string `json:"error_code"` | ||
} `json:"data"` | ||
} | ||
|
||
func (rm *ResponseMessage) Unmarshal(data []byte) error { | ||
if err := json.Unmarshal(data, rm); err != nil { | ||
return err | ||
MessageID string `json:"msg_id"` | ||
TaskID string `json:"task_id"` | ||
ErrMessage string `json:"error_msg"` | ||
ErrCode string `json:"error_code"` | ||
} | ||
return nil | ||
} | ||
ResponseMessage struct { | ||
Ret ReturnState `json:"ret"` | ||
Data `json:"data"` | ||
} | ||
) | ||
|
||
func (rm *ResponseMessage) Error() error { | ||
if rm.Ret != "SUCCESS" { | ||
return fmt.Errorf("Umeng push failed, error message: %s, error code: %s", | ||
rm.Data.ErrMsg, rm.Data.ErrCode) | ||
if rm.Ret == SuccessState { | ||
return nil | ||
} | ||
return nil | ||
return fmt.Errorf("error %s: %s", rm.ErrCode, rm.ErrMessage) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,33 @@ | ||
package gomeng | ||
|
||
import ( | ||
"bytes" | ||
"crypto/md5" | ||
"encoding/hex" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func sign(method, url, secret string, raw []byte) (string, error) { | ||
buf := bytes.NewBufferString(method) | ||
buf.WriteString(url) | ||
buf.Write(raw) | ||
buf.WriteString(secret) | ||
hasher := md5.New() | ||
if _, err := hasher.Write(buf.Bytes()); err != nil { | ||
return "", err | ||
} | ||
return hex.EncodeToString(hasher.Sum(nil)), nil | ||
} | ||
|
||
func joinSign(url, sign string) string { | ||
// e.g. https://msgapi.umeng.com/api/send?sign=xxx | ||
return fmt.Sprintf("%s?sign=%s", url, sign) | ||
} | ||
|
||
func fallback2DefaultIfZero(timeout time.Duration) time.Duration { | ||
if timeout > 0 { | ||
return timeout | ||
} | ||
return defaultTimeout | ||
} |
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 |
---|---|---|
@@ -1,44 +1,25 @@ | ||
package gomeng | ||
|
||
import "context" | ||
|
||
/** | ||
* 推送给单用户(单播) | ||
*/ | ||
func (c *Client) Push(payload map[string]interface{}, deviceToken string) error { | ||
resp, err := c.doPost(c.genReqParams(payload, "unicast", deviceToken), APIPush) | ||
if err != nil { | ||
return err | ||
} | ||
if err := resp.Error(); err != nil { | ||
return err | ||
} | ||
return nil | ||
func (c *Client) Unicast(ctx context.Context, payload Payload, deviceToken string) (resp *ResponseMessage, err error) { | ||
return c.doPost(ctx, c.genRequestParams(payload, unicastRequest, deviceToken), apiPush) | ||
} | ||
|
||
/** | ||
* 推送给多用户(列播) | ||
*/ | ||
func (c *Client) ListCast(payload map[string]interface{}, deviceTokens ...string) error { | ||
resp, err := c.doPost(c.genReqParams(payload, "listcast", deviceTokens...), APIPush) | ||
if err != nil { | ||
return err | ||
} | ||
if err := resp.Error(); err != nil { | ||
return err | ||
} | ||
return nil | ||
func (c *Client) ListCast(ctx context.Context, payload Payload, deviceTokens ...string) (resp *ResponseMessage, err error) { | ||
return c.doPost(ctx, c.genRequestParams(payload, listcastRequest, deviceTokens...), apiPush) | ||
} | ||
|
||
/** | ||
* 推送给所有用户(广播) | ||
* 默认每天可推送10次 | ||
*/ | ||
func (c *Client) Broadcast(payload map[string]interface{}) error { | ||
resp, err := c.doPost(c.genReqParams(payload, "broadcast"), APIBroadcast) | ||
if err != nil { | ||
return err | ||
} | ||
if err := resp.Error(); err != nil { | ||
return err | ||
} | ||
return nil | ||
func (c *Client) Broadcast(ctx context.Context, payload Payload) (resp *ResponseMessage, err error) { | ||
return c.doPost(ctx, c.genRequestParams(payload, broadcastRequest), apiBroadcast) | ||
} |
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
Oops, something went wrong.