Skip to content

Commit d421cc0

Browse files
authored
Merge pull request #22 from smsapi/sms-api
Sms API
2 parents 0a7b786 + b7f80d0 commit d421cc0

17 files changed

Lines changed: 424 additions & 164 deletions

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
88
- new Contacts API
99
- Subusers API
1010
- `pl.smsapi.exception.SmsapiErrorException` to handle API error responses
11+
- `date_sent` to SMS/MMS send action responses
12+
- `time_restriction` parameter for SMS send action
1113

1214
### Changed
1315
- `pl.smsapi.api.UserFactory.actionAdd` marked as deprecated, use `pl.smsapi.api.action.subusers.SubusersFactory.actionAdd` instead
@@ -30,6 +32,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
3032
- `pl.smsapi.api.action.vms.VMSDelete` without parameters marked as deprecated
3133
- `pl.smsapi.api.action.vms.VMSGet` without parameters marked as deprecated
3234
- `pl.smsapi.api.action.vms.VMSSend` without parameters marked as deprecated
35+
- `pl.smsapi.api.SmsFactory.actionSend` without parameters marked as deprecated
36+
- `pl.smsapi.api.action.sms.SMSSend` without parameters marked as deprecated
37+
- `pl.smsapi.api.SmsFactory.actionGet` without parameters marked as deprecated
38+
- `pl.smsapi.api.action.sms.SMSGet` without parameters marked as deprecated
39+
- `pl.smsapi.api.SmsFactory.actionDelete` without parameters marked as deprecated
40+
- `pl.smsapi.api.action.sms.SMSDelete` without parameters marked as deprecated
3341

3442
### Removed
3543
- legacy `phonebook.do` contacts API support

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ public class Example {
6363

6464
SmsFactory smsApi = new SmsFactory(client, proxy);
6565

66-
SMSSend action = smsApi.actionSend()
67-
.setTo("000000000")
68-
.setText("test");
66+
SMSSend action = smsApi.actionSend("000000000", "test message");
6967

7068
StatusResponse result = action.execute();
7169

@@ -114,9 +112,7 @@ public class Example {
114112

115113
String[] to = {"000000000", "000000001"};
116114

117-
SMSSend action = smsApi.actionSend()
118-
.setTo(to)
119-
.setText("test");
115+
SMSSend action = smsApi.actionSend(to, "test message");
120116

121117
StatusResponse result = action.execute();
122118

src/main/java/pl/smsapi/api/SmsFactory.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public SmsFactory(Client client, Proxy proxy) {
2020
super(client, proxy);
2121
}
2222

23+
/**
24+
* @deprecated use {@link #actionSend(String, String)} or {@link #actionSend(String[], String)} instead
25+
*/
26+
@Deprecated
2327
public SMSSend actionSend() {
2428
SMSSend action = new SMSSend();
2529
action.client(client);
@@ -28,18 +32,23 @@ public SMSSend actionSend() {
2832
}
2933

3034
public SMSSend actionSend(String to, String text) {
31-
String[] tos = new String[]{to};
32-
return actionSend(tos, text);
35+
SMSSend action = new SMSSend(to, text);
36+
action.client(client);
37+
action.proxy(proxy);
38+
return action;
3339
}
3440

3541
public SMSSend actionSend(String[] to, String text) {
36-
SMSSend action = actionSend();
37-
action.setTo(to);
38-
action.setText(text);
39-
42+
SMSSend action = new SMSSend(to, text);
43+
action.client(client);
44+
action.proxy(proxy);
4045
return action;
4146
}
4247

48+
/**
49+
* @deprecated use {@link #actionGet(String)} instead
50+
*/
51+
@Deprecated
4352
public SMSGet actionGet() {
4453
SMSGet action = new SMSGet();
4554
action.client(client);
@@ -48,11 +57,16 @@ public SMSGet actionGet() {
4857
}
4958

5059
public SMSGet actionGet(String id) {
51-
SMSGet action = actionGet();
52-
action.id(id);
60+
SMSGet action = new SMSGet(id);
61+
action.client(client);
62+
action.proxy(proxy);
5363
return action;
5464
}
5565

66+
/**
67+
* @deprecated use {@link #actionDelete(String)} instead
68+
*/
69+
@Deprecated
5670
public SMSDelete actionDelete() {
5771
SMSDelete action = new SMSDelete();
5872
action.client(client);
@@ -61,8 +75,9 @@ public SMSDelete actionDelete() {
6175
}
6276

6377
public SMSDelete actionDelete(String id) {
64-
SMSDelete action = actionDelete();
65-
action.id(id);
78+
SMSDelete action = new SMSDelete(id);
79+
action.client(client);
80+
action.proxy(proxy);
6681
return action;
6782
}
6883
}

src/main/java/pl/smsapi/api/action/sms/SMSDelete.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,26 @@
77

88
public class SMSDelete extends AbstractAction<CountableResponse> {
99

10+
/**
11+
* @deprecated use {@link SMSDelete(String)} instead
12+
*/
13+
@Deprecated
1014
public SMSDelete() {
1115
setJson(true);
1216
id("");
1317
}
1418

19+
public SMSDelete(String id) {
20+
setJson(true);
21+
params.put("sch_del", id);
22+
}
23+
1524
/**
1625
* Set ID of message to delete.
1726
* <p/>
1827
* This id was returned after sending message.
28+
*
29+
* @deprecated use {@link SMSDelete(String)} instead
1930
*/
2031
public SMSDelete id(String id) {
2132
params.put("sch_del", id);

src/main/java/pl/smsapi/api/action/sms/SMSGet.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,33 @@
77

88
public class SMSGet extends AbstractAction<StatusResponse> {
99

10+
/**
11+
* @deprecated use {@link SMSGet(String)} or {@link SMSGet(String[])} instead
12+
*/
13+
@Deprecated
1014
public SMSGet() {
1115
setJson(true);
1216
id("");
1317
}
1418

19+
public SMSGet(String id) {
20+
setJson(true);
21+
params.put("status", id);
22+
}
23+
24+
public SMSGet(String[] ids) {
25+
setJson(true);
26+
params.put("status", StringUtils.join(ids, '|'));
27+
}
28+
1529
/**
1630
* Set ID of message to check.
1731
* <p/>
1832
* This id was returned after sending message.
33+
*
34+
* @deprecated use {@link SMSGet(String)} instead
1935
*/
36+
@Deprecated
2037
public SMSGet id(String id) {
2138
params.put("status", id);
2239
return this;
@@ -26,7 +43,10 @@ public SMSGet id(String id) {
2643
* Set IDs of messages to check.
2744
* <p/>
2845
* This id was returned after sending message.
46+
*
47+
* @deprecated use {@link SMSGet(String[])} instead
2948
*/
49+
@Deprecated
3050
public SMSGet ids(String[] ids) {
3151
params.put("status", StringUtils.join(ids, '|'));
3252
return this;

src/main/java/pl/smsapi/api/action/sms/SMSSend.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,30 @@
99

1010
public class SMSSend extends AbstractSendAction<SMSSend, SendStatusResponse> {
1111

12+
/**
13+
* @deprecated use {@link SMSSend(String, String)} or {@link SMSSend(String[], String)} instead
14+
*/
15+
@Deprecated
1216
public SMSSend() {
17+
setJson(true);
18+
params.put("encoding", "utf-8");
19+
params.put("details", "1");
20+
}
1321

22+
public SMSSend(String to, String text) {
1423
setJson(true);
1524
params.put("encoding", "utf-8");
1625
params.put("details", "1");
26+
setTo(to);
27+
setText(text);
28+
}
29+
30+
public SMSSend(String[] to, String text) {
31+
setJson(true);
32+
params.put("encoding", "utf-8");
33+
params.put("details", "1");
34+
setTo(to);
35+
setText(text);
1736
}
1837

1938
@Override
@@ -25,7 +44,10 @@ protected String endPoint() {
2544
* Set SMS text message.
2645
* <p/>
2746
* Content of one message is normally 160 characters per single SMS or 70 in case of using at least one special character
47+
*
48+
* @deprecated use {@link SMSSend(String, String)} or {@link SMSSend(String[], String)} instead
2849
*/
50+
@Deprecated
2951
public SMSSend setText(String text) {
3052
params.put("message", text);
3153
return this;
@@ -153,6 +175,14 @@ public SMSSend setParam(int i, String text) {
153175

154176
return this;
155177
}
178+
179+
/**
180+
* Set time restriction mode.
181+
*/
182+
public SMSSend setTimeRestriction(String timeRestriction) {
183+
params.put("time_restriction", timeRestriction);
184+
return this;
185+
}
156186

157187
public SMSSend setDiscountGroup(String $discountGroupName)
158188
{

src/main/java/pl/smsapi/api/response/MessageResponse.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class MessageResponse implements Response {
88
private String status;
99
private String error;
1010
private String idx;
11+
private Integer toBeSentAtTimestamp;
1112

1213
public MessageResponse(String id, String points, String number, String status, String error, String idx) {
1314
this.id = id;
@@ -18,6 +19,10 @@ public MessageResponse(String id, String points, String number, String status, S
1819
this.idx = idx;
1920
}
2021

22+
public void setToBeSentAtTimestamp(int toBeSentAtTimestamp) {
23+
this.toBeSentAtTimestamp = toBeSentAtTimestamp;
24+
}
25+
2126
public String getId() {
2227
return id;
2328
}
@@ -42,6 +47,10 @@ public String getIdx() {
4247
return idx;
4348
}
4449

50+
public Integer getToBeSentAtTimestamp() {
51+
return toBeSentAtTimestamp;
52+
}
53+
4554
public boolean isError() {
4655
if (id == null || id.length() == 0) {
4756
return true;

src/main/java/pl/smsapi/api/response/SendStatusResponse.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pl.smsapi.api.response;
22

33
import org.json.JSONArray;
4+
import org.json.JSONObject;
45

56
public class SendStatusResponse extends StatusResponse {
67

@@ -11,6 +12,13 @@ public SendStatusResponse(int count, int parts, JSONArray jsonArray) {
1112
this.parts = parts;
1213
}
1314

15+
@Override
16+
protected MessageResponse buildItem(JSONObject jsonObject) {
17+
MessageResponse response = super.buildItem(jsonObject);
18+
response.setToBeSentAtTimestamp(jsonObject.optInt("date_sent"));
19+
return response;
20+
}
21+
1422
public int getParts() {
1523
return parts;
1624
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pl.smsapi.api.action.sms;
2+
3+
public class DeletedStatusJsonMother {
4+
5+
public static String create() {
6+
return
7+
"{" +
8+
" \"count\":1," +
9+
" \"list\":[" +
10+
" {" +
11+
" \"id\":\"0f0f0f0f0f0f0f0f0f0f0f0f\"," +
12+
" }" +
13+
" ]" +
14+
"}";
15+
}
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package pl.smsapi.api.action.sms;
2+
3+
import org.junit.Test;
4+
import pl.smsapi.exception.SmsapiException;
5+
import pl.smsapi.test.doubles.ClientStub;
6+
import pl.smsapi.test.doubles.ProxyRequestSpy;
7+
8+
import java.util.HashMap;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class SMSDeleteTest {
13+
14+
@Test
15+
public void executeDeleteSmsRequest() throws SmsapiException {
16+
ProxyRequestSpy requestStub = new ProxyRequestSpy(DeletedStatusJsonMother.create());
17+
SMSDelete action = new SMSDelete("0f0f0f0f0f0f0f0f0f0f0f0f");
18+
action.client(new ClientStub());
19+
action.proxy(requestStub);
20+
21+
action.execute();
22+
23+
assertEquals("POST", requestStub.requestMethod);
24+
assertEquals("sms.do", requestStub.requestEndpoint);
25+
HashMap<String, String> expectedRequestPayload = new HashMap<>();
26+
expectedRequestPayload.put("sch_del", "0f0f0f0f0f0f0f0f0f0f0f0f");
27+
expectedRequestPayload.put("format", "json");
28+
assertEquals(expectedRequestPayload, requestStub.requestPayload);
29+
}
30+
}

0 commit comments

Comments
 (0)