diff --git a/leancloud/client.go b/leancloud/client.go index 27769a3..518ce7d 100644 --- a/leancloud/client.go +++ b/leancloud/client.go @@ -18,6 +18,7 @@ type Client struct { Users Users Files Files Roles Roles + SMS SMS } type ClientOptions struct { @@ -51,6 +52,7 @@ func NewClient(options *ClientOptions) *Client { client.Users.c = client client.Files.c = client client.Roles.c = client + client.SMS.c = client return client } diff --git a/leancloud/sms.go b/leancloud/sms.go new file mode 100644 index 0000000..17805b5 --- /dev/null +++ b/leancloud/sms.go @@ -0,0 +1,37 @@ +package leancloud + +import "fmt" + +type SMS struct { + c *Client +} + +func (ref *SMS) RequestSMSCode(number string) error { + path := "/1.1/requestSmsCode" + options := ref.c.getRequestOptions() + options.JSON = map[string]string{ + "mobilePhoneNumber": number, + } + + _, err := ref.c.request(methodPost, path, options) + if err != nil { + return err + } + + return nil +} + +func (ref *SMS) VerifySMSCode(number, smsCode string) error { + path := fmt.Sprintf("/1.1/verifySmsCode/%s", smsCode) + options := ref.c.getRequestOptions() + options.JSON = map[string]string{ + "mobilePhoneNumber": number, + } + + _, err := ref.c.request(methodPost, path, options) + if err != nil { + return err + } + + return nil +} diff --git a/leancloud/sms_test.go b/leancloud/sms_test.go new file mode 100644 index 0000000..d9541ca --- /dev/null +++ b/leancloud/sms_test.go @@ -0,0 +1,28 @@ +package leancloud + +import ( + "os" + "testing" +) + +func TestSMSCodeRequest(t *testing.T) { + client = NewEnvClient() + sms := &SMS{c} + mobile := os.Getenv("TEST_SMS_REQUEST_MOBILE") + err := sms.RequestSMSCode(mobile) + if err != nil { + t.Error(err) + t.Fail() + } +} + +func TestSMSCodeVerify(t *testing.T) { + client = NewEnvClient() + sms := &SMS{c} + mobile := os.Getenv("TEST_SMS_REQUEST_MOBILE") + code := os.Getenv("TEST_SMS_REQUEST_MOBILE_CODE") + err := sms.VerifySMSCode(mobile, code) + if err != nil { + t.Error(err) + } +}