Skip to content

Commit

Permalink
refactor(sms): Refactor SMS (#69)
Browse files Browse the repository at this point in the history
Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 authored Jan 15, 2024
1 parent 18c0725 commit 6141b19
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 167 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/grpc v1.60.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 h1:YJ5pD9rF8o9Qtta0Cmy9rdBwkSjrTCT6XTiUQVOtIos=
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 h1:W18sezcAYs+3tDZX4F80yctqa12jcP1PUS2gQu1zTPU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 h1:6G8oQ016D88m1xAKljMlBOOGWDZkes4kMhgGFlf8WcQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917/go.mod h1:xtjpI3tXFPP051KaWnhvxkiubL/6dJ18vLVf7q2pTOU=
google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU=
Expand Down
58 changes: 0 additions & 58 deletions sms/mitake/provider_test.go

This file was deleted.

7 changes: 0 additions & 7 deletions sms/provider.go

This file was deleted.

63 changes: 0 additions & 63 deletions sms/sms.go

This file was deleted.

19 changes: 0 additions & 19 deletions sms/sms_test.go

This file was deleted.

39 changes: 20 additions & 19 deletions sms/mitake/provider.go → x/sms/mitake/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"golang.org/x/text/encoding/traditionalchinese"

"github.com/go-kratos-ecosystem/components/v2/sms"
"github.com/go-kratos-ecosystem/components/v2/x/sms"
)

type provider struct {
Expand Down Expand Up @@ -50,15 +50,15 @@ func New(username, password string, opts ...Option) sms.Provider {
return p
}

func (p *provider) Send(ctx context.Context, phone *sms.Phone, message *sms.Message) error {
if err := p.verify(phone, message); err != nil {
return err
func (p *provider) Send(ctx context.Context, message *sms.Message) (err error) {
if err = p.verify(message); err != nil {
return
}

// Convert to Big5
text, err := traditionalchinese.Big5.NewEncoder().String(message.Text)
text, err := traditionalchinese.Big5.NewEncoder().String(message.Content.Text)
if err != nil {
return err
return
}

// Combine params
Expand All @@ -67,36 +67,37 @@ func (p *provider) Send(ctx context.Context, phone *sms.Phone, message *sms.Mess
params.Set("password", p.password)
params.Set("type", "now")
params.Set("encoding", "big5")
params.Set("dstaddr", phone.Number)
params.Set("dstaddr", message.Phone.Number)
params.Set("smbody", text)

// new request
// new message
req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.api+"?"+params.Encode(), nil)
if err != nil {
return err
return
}

// send request
resp, err := p.httpClient.Do(req)
// send message
rep, err := p.httpClient.Do(req)
if err != nil {
return err
return
}
defer resp.Body.Close()
defer rep.Body.Close()

// check response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("sms mitake: http status code: %d", resp.StatusCode)
// check rep
if rep.StatusCode != http.StatusOK {
err = fmt.Errorf("sms mitake: http status code: %d", rep.StatusCode)
return
}

return nil
}

func (p *provider) verify(phone *sms.Phone, message *sms.Message) error {
if phone.Number == "" {
func (p *provider) verify(message *sms.Message) error {
if message.Phone == nil || message.Phone.Number == "" {
return sms.ErrInvalidPhone
}

if message.Text == "" {
if message.Content == nil || message.Content.Text == "" {
return sms.ErrInvalidMessage
}

Expand Down
47 changes: 47 additions & 0 deletions x/sms/mitake/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package mitake

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"

"github.com/go-kratos-ecosystem/components/v2/x/sms"
)

func TestProvider(t *testing.T) {
var (
username = "test"
password = "test"
number = "123456789"
text = "Hello, world"
)

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
assert.Equal(t, username, r.URL.Query().Get("username"))
assert.Equal(t, password, r.URL.Query().Get("password"))
assert.Equal(t, number, r.URL.Query().Get("dstaddr"))
assert.Equal(t, text, r.URL.Query().Get("smbody"))

w.Write([]byte("hello")) //nolint:errcheck
}))
defer srv.Close()

p := New(username, password,
WithAPI(srv.URL),
WithHTTPClient(http.DefaultClient),
)

err := p.Send(context.Background(), &sms.Message{
Phone: &sms.Phone{
Number: number,
},
Content: &sms.Content{
Text: text,
},
})
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion sms/null_provider.go → x/sms/null_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ func NewNullProvider() Provider {
return &NullProvider{}
}

func (p *NullProvider) Send(_ context.Context, _ *Phone, _ *Message) error {
func (p *NullProvider) Send(_ context.Context, _ *Message) error {
return nil
}
7 changes: 7 additions & 0 deletions x/sms/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sms

import "context"

type Provider interface {
Send(ctx context.Context, message *Message) error
}
41 changes: 41 additions & 0 deletions x/sms/sms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package sms

import (
"context"
"errors"
)

var (
ErrInvalidPhone = errors.New("sms: invalid phone")
ErrInvalidMessage = errors.New("sms: invalid message")
)

type Phone struct {
IDDCode string
Number string
}

type Content struct {
Text string
Template string
Variables map[string]string
}

type Message struct {
Phone *Phone
Content *Content
}

type Sms struct {
gw Provider
}

func New(gw Provider) *Sms {
return &Sms{
gw: gw,
}
}

func (s *Sms) Send(ctx context.Context, message *Message) error {
return s.gw.Send(ctx, message)
}
16 changes: 16 additions & 0 deletions x/sms/sms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package sms

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSms(t *testing.T) {
sms := New(NewNullProvider())
ctx := context.Background()

err := sms.Send(ctx, &Message{})
assert.NoError(t, err)
}

0 comments on commit 6141b19

Please sign in to comment.