diff --git a/x/sms/mitake/provider.go b/x/sms/mitake/provider.go deleted file mode 100644 index 7148c386..00000000 --- a/x/sms/mitake/provider.go +++ /dev/null @@ -1,105 +0,0 @@ -// Package mitake is a sms provider for mitake. -// See: https://sms.mitake.com.tw/ -package mitake - -import ( - "context" - "fmt" - "net/http" - "net/url" - - "golang.org/x/text/encoding/traditionalchinese" - - "github.com/go-kratos-ecosystem/components/v2/x/sms" -) - -type provider struct { - api string - username string - password string - - httpClient *http.Client -} - -type Option func(t *provider) - -func WithAPI(api string) Option { - return func(t *provider) { - t.api = api - } -} - -func WithHTTPClient(httpClient *http.Client) Option { - return func(t *provider) { - t.httpClient = httpClient - } -} - -func New(username, password string, opts ...Option) sms.Provider { - p := &provider{ - api: "https://smsapi.mitake.com.tw", - username: username, - password: password, - httpClient: http.DefaultClient, - } - - for _, opt := range opts { - opt(p) - } - - return p -} - -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.Content.Text) - if err != nil { - return - } - - // Combine params - params := url.Values{} - params.Set("username", p.username) - params.Set("password", p.password) - params.Set("type", "now") - params.Set("encoding", "big5") - params.Set("dstaddr", message.Phone.Number) - params.Set("smbody", text) - - // new message - req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.api+"?"+params.Encode(), nil) - if err != nil { - return - } - - // send message - rep, err := p.httpClient.Do(req) - if err != nil { - return - } - defer rep.Body.Close() - - // 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(message *sms.Message) error { - if message.Phone == nil || message.Phone.Number == "" { - return sms.ErrInvalidPhone - } - - if message.Content == nil || message.Content.Text == "" { - return sms.ErrInvalidMessage - } - - return nil -} diff --git a/x/sms/mitake/provider_test.go b/x/sms/mitake/provider_test.go deleted file mode 100644 index 37e7713f..00000000 --- a/x/sms/mitake/provider_test.go +++ /dev/null @@ -1,47 +0,0 @@ -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) -} diff --git a/x/sms/null_provider.go b/x/sms/null_provider.go deleted file mode 100644 index 76aa9597..00000000 --- a/x/sms/null_provider.go +++ /dev/null @@ -1,13 +0,0 @@ -package sms - -import "context" - -type NullProvider struct{} - -func NewNullProvider() Provider { - return &NullProvider{} -} - -func (p *NullProvider) Send(_ context.Context, _ *Message) error { - return nil -} diff --git a/x/sms/provider.go b/x/sms/provider.go deleted file mode 100644 index 33a4c7e5..00000000 --- a/x/sms/provider.go +++ /dev/null @@ -1,7 +0,0 @@ -package sms - -import "context" - -type Provider interface { - Send(ctx context.Context, message *Message) error -} diff --git a/x/sms/sms.go b/x/sms/sms.go deleted file mode 100644 index 3f6b5b7f..00000000 --- a/x/sms/sms.go +++ /dev/null @@ -1,41 +0,0 @@ -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 { - provider Provider -} - -func New(provider Provider) *Sms { - return &Sms{ - provider: provider, - } -} - -func (s *Sms) Send(ctx context.Context, message *Message) error { - return s.provider.Send(ctx, message) -} diff --git a/x/sms/sms_test.go b/x/sms/sms_test.go deleted file mode 100644 index 4cfc9656..00000000 --- a/x/sms/sms_test.go +++ /dev/null @@ -1,16 +0,0 @@ -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) -}