Skip to content

Commit b62fd1b

Browse files
committed
Updates README examples
1 parent 3f9a61a commit b62fd1b

File tree

1 file changed

+126
-24
lines changed

1 file changed

+126
-24
lines changed

README.md

Lines changed: 126 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,55 +64,157 @@ api 'pl.smsapi:smsapi-lib:2.5'
6464

6565
## How to use
6666

67-
Example of using library:
68-
```java
69-
package com.example.smsapi_java_client_example;
67+
To use SMSAPI.PL set proxy URL to **http://api.smsapi.pl/**.
68+
To use SMSAPI.COM set proxy URL to **http://api.smsapi.com/**.
69+
70+
### How to send SMS
7071

72+
```java
7173
import pl.smsapi.OAuthClient;
7274
import pl.smsapi.api.SmsFactory;
7375
import pl.smsapi.api.action.sms.SMSSend;
7476
import pl.smsapi.api.response.MessageResponse;
7577
import pl.smsapi.api.response.StatusResponse;
76-
import pl.smsapi.exception.ClientException;
7778
import pl.smsapi.exception.SmsapiException;
78-
79-
public class Main {
80-
final static String urlForPlSmsapi = "http://api.smsapi.pl/";
81-
final static String urlForComSmsapi = "http://api.smsapi.com/";
82-
83-
public static void main(String args[]) {
79+
import pl.smsapi.proxy.ProxyNative;
80+
81+
import java.util.Optional;
82+
83+
/**
84+
* Example output:
85+
*
86+
* Phone number: 48000000000
87+
* Shipment id: 6124CC3C3463351AD569127F
88+
* Shipment status: QUEUE
89+
*/
90+
public class Example {
91+
public static void main(String[] args) {
8492
try {
8593
String oauthToken = "00000000000000000000000000000000";
8694
OAuthClient client = new OAuthClient(oauthToken);
87-
ProxyNative proxyToPlOrComSmsapi = new ProxyNative(urlForPlSmsapi);
95+
ProxyNative proxy = new ProxyNative("https://api.smsapi.pl/");
96+
97+
SmsFactory smsApi = new SmsFactory(client, proxy);
8898

89-
SmsFactory smsApi = new SmsFactory(client, proxyToPlOrComSmsapi);
90-
String phoneNumber = "000000000";
9199
SMSSend action = smsApi.actionSend()
92-
.setText("test")
93-
.setTo(phoneNumber);
100+
.setTo("000000000")
101+
.setText("test");
94102

95103
StatusResponse result = action.execute();
96104

97-
for (MessageResponse status : result.getList() ) {
98-
System.out.println(status.getNumber() + " " + status.getStatus());
105+
Optional<MessageResponse> status = result.getList().stream().findFirst();
106+
if (status.isEmpty()) {
107+
throw new RuntimeException();
99108
}
100-
} catch (ClientException e) {
101-
e.printStackTrace();
109+
110+
System.out.println("Phone number: " + status.get().getNumber());
111+
System.out.println("Shipment id: " + status.get().getId());
112+
System.out.println("Shipment status: " + status.get().getStatus());
113+
102114
} catch (SmsapiException e) {
103-
e.printStackTrace();
115+
System.out.println("Exception: " + e.getMessage());
104116
}
105117
}
106118
}
107119
```
108120

109-
### How to use *SMSAPI.PL* client
121+
### How to send SMS to many recipients
122+
110123
```java
111-
ProxyNative proxyToPlOrComSmsapi = new ProxyNative(urlForPlSmsapi);
124+
import pl.smsapi.OAuthClient;
125+
import pl.smsapi.api.SmsFactory;
126+
import pl.smsapi.api.action.sms.SMSSend;
127+
import pl.smsapi.api.response.MessageResponse;
128+
import pl.smsapi.api.response.StatusResponse;
129+
import pl.smsapi.exception.SmsapiException;
130+
import pl.smsapi.proxy.ProxyNative;
131+
132+
/**
133+
* Example output:
134+
*
135+
* Phone number: 48000000000
136+
* Shipment id: 6124CFBF3463350568CC428E
137+
* Shipment status: QUEUE
138+
* Phone number: 48000000001
139+
* Shipment id: 6124CFBF3463350568CC428F
140+
* Shipment status: QUEUE
141+
*/
142+
public class Example {
143+
public static void main(String[] args) {
144+
try {
145+
String oauthToken = "00000000000000000000000000000000";
146+
OAuthClient client = new OAuthClient(oauthToken);
147+
ProxyNative proxy = new ProxyNative("https://api.smsapi.pl/");
148+
149+
SmsFactory smsApi = new SmsFactory(client, proxy);
150+
151+
String[] to = {"000000000", "000000001"};
152+
153+
SMSSend action = smsApi.actionSend()
154+
.setTo(to)
155+
.setText("test");
156+
157+
StatusResponse result = action.execute();
158+
159+
for (MessageResponse status : result.getList() ) {
160+
System.out.println("Phone number: " + status.getNumber());
161+
System.out.println("Shipment id: " + status.getId());
162+
System.out.println("Shipment status: " + status.getStatus());
163+
}
164+
} catch (SmsapiException e) {
165+
System.out.println("Exception: " + e.getMessage());
166+
}
167+
}
168+
}
169+
112170
```
113-
### How to use *SMSAPI.COM* client
171+
172+
### How to get shipment status
173+
114174
```java
115-
ProxyNative proxyToPlOrComSmsapi = new ProxyNative(urlForComSmsapi);
175+
import pl.smsapi.OAuthClient;
176+
import pl.smsapi.api.SmsFactory;
177+
import pl.smsapi.api.action.sms.SMSGet;
178+
import pl.smsapi.api.response.MessageResponse;
179+
import pl.smsapi.api.response.StatusResponse;
180+
import pl.smsapi.exception.SmsapiException;
181+
import pl.smsapi.proxy.ProxyNative;
182+
183+
import java.util.Optional;
184+
185+
/**
186+
* Example output:
187+
*
188+
* Phone number: 48500000000
189+
* Shipment id: 6124D61434633525780D4F3B
190+
* Shipment status: SENT
191+
*/
192+
public class Example2ci {
193+
public static void main(String[] args) {
194+
try {
195+
String oauthToken = "00000000000000000000000000000000";
196+
OAuthClient client = new OAuthClient(oauthToken);
197+
ProxyNative proxy = new ProxyNative("https://api.smsapi.pl/");
198+
199+
SmsFactory smsApi = new SmsFactory(client, proxy);
200+
201+
String shipmentId = "6124D61434633525780D4F3B";
202+
SMSGet getAction = smsApi.actionGet(shipmentId);
203+
204+
StatusResponse shipmentStatus = getAction.execute();
205+
Optional<MessageResponse> statusAfterGet = shipmentStatus.getList().stream().findFirst();
206+
if (statusAfterGet.isEmpty()) {
207+
throw new RuntimeException();
208+
}
209+
210+
System.out.println("Phone number: " + statusAfterGet.get().getNumber());
211+
System.out.println("Shipment id: " + statusAfterGet.get().getId());
212+
System.out.println("Shipment status: " + statusAfterGet.get().getStatus());
213+
} catch (SmsapiException e) {
214+
System.out.println("Exception: " + e.getMessage());
215+
}
216+
}
217+
}
116218
```
117219

118220
## JAVADOC

0 commit comments

Comments
 (0)