Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SMS #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions leancloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Client struct {
Users Users
Files Files
Roles Roles
SMS SMS
}

type ClientOptions struct {
Expand Down Expand Up @@ -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
}

Expand Down
37 changes: 37 additions & 0 deletions leancloud/sms.go
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions leancloud/sms_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}