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

[@] API v2.08 #9

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
46 changes: 15 additions & 31 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@ import (
"strings"
)

// SendBatch sends multiple SMS.
func (c *Client) SendBatch(messages []Message) (*MessageResponse, error) {
// Send an SMS.
func (c *Client) Send(message Message) (*MessageResponse, error) {
q := c.buildDefaultQuery()
q.Set("encoding", "UTF8")
u, _ := url.Parse("SmSendPost.asp")
q.Set("CharsetURL", "UTF8")

u, _ := url.Parse("/api/mtk/SmSend")
u.RawQuery = q.Encode()

var ini string
for i, message := range messages {
ini += "[" + strconv.Itoa(i) + "]\n"
ini += message.ToINI()
}
ini = strings.TrimSpace(ini)

resp, err := c.Post(u.String(), "text/plain", strings.NewReader(ini))
ini = strings.TrimSpace(message.ToINI())
resp, err := c.Post(u.String(), "application/x-www-form-urlencoded", strings.NewReader(ini))
if err != nil {
return nil, err
}
Expand All @@ -30,19 +26,17 @@ func (c *Client) SendBatch(messages []Message) (*MessageResponse, error) {
return parseMessageResponse(resp.Body)
}

// SendLongMessageBatch sends multiple long message SMS.
func (c *Client) SendLongMessageBatch(messages []Message) (*MessageResponse, error) {
// SendBatch sends multiple SMS.
func (c *Client) SendBatch(messages []Message) (*MessageResponse, error) {
q := c.buildDefaultQuery()
q.Set("Encoding_PostIn", "UTF8")

u := *c.LongMessageBaseURL
u.Path = "SpLmPost"
u, _ := url.Parse("/api/mtk/SmBulkSend")
u.RawQuery = q.Encode()

var ini string
for _, message := range messages {
ini += message.ID + "$$"
ini += message.ToLongMessage()
ini += message.ToBatchMessage()
}
ini = strings.TrimSpace(ini)

Expand All @@ -52,22 +46,12 @@ func (c *Client) SendLongMessageBatch(messages []Message) (*MessageResponse, err
}
defer resp.Body.Close()

return parseLongMessageResponse(resp.Body)
}

// Send an SMS.
func (c *Client) Send(message Message) (*MessageResponse, error) {
return c.SendBatch([]Message{message})
}

// SendLongMessage sends a long SMS.
func (c *Client) SendLongMessage(message Message) (*MessageResponse, error) {
return c.SendLongMessageBatch([]Message{message})
return parseMessageResponse(resp.Body)
}

// QueryAccountPoint retrieves your account balance.
func (c *Client) QueryAccountPoint() (int, error) {
u, _ := url.Parse("SmQueryGet.asp")
u, _ := url.Parse("/api/mtk/SmQuery")
u.RawQuery = c.buildDefaultQuery().Encode()

resp, err := c.Get(u.String())
Expand All @@ -88,7 +72,7 @@ func (c *Client) QueryMessageStatus(messageIds []string) (*MessageStatusResponse
q := c.buildDefaultQuery()
q.Set("msgid", strings.Join(messageIds, ","))

u, _ := url.Parse("SmQueryGet.asp")
u, _ := url.Parse("/api/mtk/SmQuery")
u.RawQuery = q.Encode()

resp, err := c.Get(u.String())
Expand All @@ -105,7 +89,7 @@ func (c *Client) CancelMessageStatus(messageIds []string) (*MessageStatusRespons
q := c.buildDefaultQuery()
q.Set("msgid", strings.Join(messageIds, ","))

u, _ := url.Parse("SmCancel.asp")
u, _ := url.Parse("/api/mtk/SmCancel")
u.RawQuery = q.Encode()

resp, err := c.Get(u.String())
Expand Down
116 changes: 11 additions & 105 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestClient_SendBatch(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

mux.HandleFunc("/SmSendPost.asp", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/api/mtk/SmBulkSend", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testINI(t, r, `[0]
dstaddr=0987654321
Expand All @@ -30,12 +30,14 @@ AccountPoint=98`)

messages := []Message{
{
Dstaddr: "0987654321",
Smbody: "Test 1",
ClientID: "0",
Dstaddr: "0987654321",
Smbody: "Test 1",
},
{
Dstaddr: "0987654322",
Smbody: "Test 2",
ClientID: "1",
Dstaddr: "0987654322",
Smbody: "Test 2",
},
}

Expand All @@ -61,69 +63,11 @@ AccountPoint=98`)
}
}

func TestClient_SendLongMessageBatch(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

mux.HandleFunc("/SpLmPost", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testINI(t, r, `0aab$$0987654321$$20170101010000$$20170101012300$$Bob$$https://example.com/callback$$Test1
1aab$$0987654321$$$$$$Bob$$$$Test2`)
_, _ = fmt.Fprint(w, `[0aab]
msgid=#1010079522
statuscode=1
[1aab]
msgid=#1010079523
statuscode=4
AccountPoint=98`)
})

messages := []Message{
{
ID: "0aab",
Destname: "Bob",
Dlvtime: "20170101010000",
Vldtime: "20170101012300",
Dstaddr: "0987654321",
Smbody: "Test1",
Response: "https://example.com/callback",
},
{
ID: "1aab",
Destname: "Bob",
Dstaddr: "0987654321",
Smbody: "Test2",
},
}

resp, err := client.SendLongMessageBatch(messages)

if err != nil {
t.Errorf("SendLongMessageBatch returned unexpected error: %v", err)
}

want := []*MessageResult{
{
Msgid: "#1010079522",
Statuscode: "1",
Statusstring: StatusCode("1"),
},
{
Msgid: "#1010079523",
Statuscode: "4",
Statusstring: StatusCode("4"),
},
}
if !reflect.DeepEqual(resp.Results, want) {
t.Errorf("SendLongMessageBatch returned %+v, want %+v", resp.Results, want)
}
}

func TestClient_Send(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

mux.HandleFunc("/SmSendPost.asp", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/api/mtk/SmSend", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testINI(t, r, `[0]
dstaddr=0987654321
Expand Down Expand Up @@ -156,49 +100,11 @@ AccountPoint=99`)
}
}

func TestClient_SendLongMessage(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

mux.HandleFunc("/SpLmPost", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testINI(t, r, `0aab$$0987654321$$$$$$John$$https://example.com/callback$$Test1`)
_, _ = fmt.Fprint(w, `[0aab]
msgid=#1010079522
statuscode=1
AccountPoint=99`)
})

resp, err := client.SendLongMessage(
Message{
ID: "0aab",
Destname: "John",
Dstaddr: "0987654321",
Smbody: "Test1",
Response: "https://example.com/callback",
},
)
if err != nil {
t.Errorf("SendLongMessage returned unexpected error: %v", err)
}

want := []*MessageResult{
{
Msgid: "#1010079522",
Statuscode: "1",
Statusstring: StatusCode("1"),
},
}
if !reflect.DeepEqual(resp.Results, want) {
t.Errorf("SendLongMessage returned %+v, want %+v", resp.Results, want)
}
}

func TestClient_QueryAccountPoint(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

mux.HandleFunc("/SmQueryGet.asp", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/api/mtk/SmQuery", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
_, _ = fmt.Fprint(w, `AccountPoint=100`)
})
Expand All @@ -216,7 +122,7 @@ func TestClient_QueryMessageStatus(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

mux.HandleFunc("/SmQueryGet.asp", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/api/mtk/SmQuery", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
_, _ = fmt.Fprint(w, `1010079522 1 20170101010010
1010079523 4 20170101010011`)
Expand Down Expand Up @@ -254,7 +160,7 @@ func TestClient_CancelMessage(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

mux.HandleFunc("/SmCancel.asp", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/api/mtk/SmCancel", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
_, _ = fmt.Fprint(w, `1010079522=8
1010079523=9`)
Expand Down
Loading