github.com/gorocks/apikit@v0.0.0-20170808011444-396ab031afbd/push/lc/lc.go (about)

     1  package lc
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/douglarek/apikit"
     7  )
     8  
     9  const (
    10  	installationURL = "https://leancloud.cn/1.1/installations"
    11  	pushURL         = "https://leancloud.cn/1.1/push"
    12  )
    13  
    14  // LeanCloud ...
    15  type LeanCloud struct {
    16  	client *apikit.Client
    17  }
    18  
    19  // New makes a LeanCloud ...
    20  func New(lcID, lcKey string) *LeanCloud {
    21  	c := apikit.NewClient(nil)
    22  	c.SetHeader(apikit.H{"X-LC-Id": lcID, "X-LC-Key": lcKey, "Content-Type": apikit.MediaJSON})
    23  	return &LeanCloud{client: c}
    24  }
    25  
    26  // InstallationReq ...
    27  type InstallationReq struct {
    28  	DeviceType     string   `json:"deviceType"`
    29  	DeviceToken    string   `json:"deviceToken"`
    30  	InstallationID string   `json:"installationId"`
    31  	Channels       []string `json:"channels"`
    32  }
    33  
    34  // Resp ...
    35  type Resp struct {
    36  	ObjectID  string    `json:"objectId"`
    37  	CreatedAt time.Time `json:"createdAt"`
    38  	UpdatedAt time.Time `json:"updatedAt"`
    39  }
    40  
    41  // ChannelsReq ...
    42  type ChannelsReq struct {
    43  	Channels []string               `json:"channels"`
    44  	Prod     string                 `json:"prod"`
    45  	Data     map[string]interface{} `json:"data"`
    46  	Where    map[string]interface{} `json:"where"`
    47  }
    48  
    49  // SaveInstallation ...
    50  func (lc *LeanCloud) SaveInstallation(r *InstallationReq) (*Resp, error) {
    51  	req, err := lc.client.NewRequest("POST", installationURL, r)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	res := new(Resp)
    56  	if _, err := lc.client.Do(req, res); err != nil {
    57  		return nil, err
    58  	}
    59  	return res, nil
    60  }
    61  
    62  // PushChannels ...
    63  func (lc *LeanCloud) PushChannels(r *ChannelsReq) (*Resp, error) {
    64  	req, err := lc.client.NewRequest("POST", pushURL, r)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	res := new(Resp)
    69  	if _, err := lc.client.Do(req, res); err != nil {
    70  		return nil, err
    71  	}
    72  	return res, nil
    73  
    74  }
    75  
    76  // ChannelsOpsReq ...
    77  type ChannelsOpsReq struct {
    78  	Channels struct {
    79  		OP      string   `json:"__op"`
    80  		Objects []string `json:"objects"`
    81  	} `json:"channels"`
    82  }
    83  
    84  // UnsubscribeChannel ...
    85  func (lc *LeanCloud) UnsubscribeChannel(objectID string, c *ChannelsOpsReq) (*Resp, error) {
    86  	req, err := lc.client.NewRequest("PUT", installationURL+"/"+objectID, c)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	res := new(Resp)
    91  	if _, err := lc.client.Do(req, res); err != nil {
    92  		return nil, err
    93  	}
    94  	return res, nil
    95  }