Skip to content

Commit 1e990ec

Browse files
committed
Merge pull request #11 from messagebird/verify-api
Add support for the Verify API
2 parents 0c1a02a + 5f31d7e commit 1e990ec

15 files changed

+521
-14
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ hs_err_pid*
1414

1515
# The various target folders
1616
*target/
17+
*.iml
18+
.idea

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ If you are using maven simply add the messagebird API to your dependencies like
2828
<dependency>
2929
<groupId>com.messagebird</groupId>
3030
<artifactId>messagebird-api</artifactId>
31-
<version>1.0.5</version>
31+
<version>1.1.0</version>
3232
</dependency>
3333
```
3434

3535
In case you are building without maven you still need maven to build the libraries but
3636
then simply copy the following jar's over to your project
3737

3838
```
39-
messagebird-api-1.0.5.jar
39+
messagebird-api-1.1.0.jar
4040
jackson-core-2.1.1.jar
4141
jackson-databind-2.1.1.jar
4242
jackson-mapper-asl-1.9.13.jar

api/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.messagebird</groupId>
88
<artifactId>messagebird-api</artifactId>
9-
<version>1.0.5</version>
9+
<version>1.1.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>${project.groupId}:${project.artifactId}</name>

api/src/main/java/com/messagebird/MessageBirdClient.java

+80-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
import com.messagebird.objects.*;
77

88
import java.math.BigInteger;
9-
import java.text.SimpleDateFormat;
109
import java.util.LinkedHashMap;
1110
import java.util.List;
1211
import java.util.Map;
1312

1413
/**
1514
* Message bird general client
16-
* <p/>
15+
* <p>
1716
* <pre>
1817
* Initialise this class with a MessageBirdService
1918
* // First create your service object
@@ -24,14 +23,15 @@
2423
* Then read your balance like this:
2524
* final Balance balance = messageBirdClient.getBalance();
2625
* </pre>
27-
* <p/>
26+
* <p>
2827
* Created by rvt on 1/5/15.
2928
*/
3029
public class MessageBirdClient {
3130
private static final String BALANCEPATH = "/balance";
3231
private static final String HLRPATH = "/hlr";
3332
private static final String MESSAGESPATH = "/messages";
3433
private static final String VOICEMESSAGESPATH = "/voicemessages";
34+
private static final String VERIFYPATH = "/verify";
3535
private MessageBirdService messageBirdService;
3636

3737
public MessageBirdClient(final MessageBirdService messageBirdService) {
@@ -308,4 +308,81 @@ public VoiceMessageList listVoiceMessages(final Integer offset, final Integer li
308308
return messageBirdService.requestList(VOICEMESSAGESPATH, offset, limit, VoiceMessageList.class);
309309
}
310310

311+
/**
312+
* @param verifyRequest
313+
* @return Verify object
314+
* @throws UnauthorizedException
315+
* @throws GeneralException
316+
*/
317+
public Verify sendVerifyToken(VerifyRequest verifyRequest) throws UnauthorizedException, GeneralException {
318+
if (verifyRequest == null) {
319+
throw new IllegalArgumentException("Verify request cannot be null");
320+
} else if (verifyRequest.getRecipient() == null || verifyRequest.getRecipient().isEmpty()) {
321+
throw new IllegalArgumentException("Recipient cannot be empty for verify");
322+
}
323+
return messageBirdService.sendPayLoad(VERIFYPATH, verifyRequest, Verify.class);
324+
}
325+
326+
/**
327+
* @param recipient
328+
* @return Verify object
329+
* @throws UnauthorizedException
330+
* @throws GeneralException
331+
*/
332+
public Verify sendVerifyToken(String recipient) throws UnauthorizedException, GeneralException {
333+
if (recipient == null || recipient.isEmpty()) {
334+
throw new IllegalArgumentException("Recipient cannot be empty for verify");
335+
}
336+
VerifyRequest verifyRequest = new VerifyRequest(recipient);
337+
return this.sendVerifyToken(verifyRequest);
338+
}
339+
340+
/**
341+
*
342+
* @param id
343+
* @param token
344+
* @return Verify object
345+
* @throws NotFoundException
346+
* @throws GeneralException
347+
* @throws UnauthorizedException
348+
*/
349+
public Verify verifyToken(String id, String token) throws NotFoundException, GeneralException, UnauthorizedException {
350+
if (id == null || id.isEmpty()) {
351+
throw new IllegalArgumentException("ID cannot be empty for verify");
352+
} else if (token == null || token.isEmpty()) {
353+
throw new IllegalArgumentException("ID cannot be empty for verify");
354+
}
355+
final Map<String, Object> params = new LinkedHashMap<String, Object>();
356+
params.put("token", token);
357+
return messageBirdService.requestByID(VERIFYPATH, id, params, Verify.class);
358+
}
359+
360+
/**
361+
*
362+
* @param id
363+
* @return Verify object
364+
* @throws NotFoundException
365+
* @throws GeneralException
366+
* @throws UnauthorizedException
367+
*/
368+
public Verify getVerifyObject(String id) throws NotFoundException, GeneralException, UnauthorizedException {
369+
if (id == null || id.isEmpty()) {
370+
throw new IllegalArgumentException("ID cannot be empty for verify");
371+
}
372+
return messageBirdService.requestByID(VERIFYPATH, id, Verify.class);
373+
}
374+
375+
/**
376+
*
377+
* @param id
378+
* @throws NotFoundException
379+
* @throws GeneralException
380+
* @throws UnauthorizedException
381+
*/
382+
public void deleteVerifyObject(String id) throws NotFoundException, GeneralException, UnauthorizedException {
383+
if (id == null || id.isEmpty()) {
384+
throw new IllegalArgumentException("ID cannot be empty for verify");
385+
}
386+
messageBirdService.deleteByID(VERIFYPATH, id);
387+
}
311388
}

api/src/main/java/com/messagebird/MessageBirdService.java

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.messagebird.exceptions.NotFoundException;
55
import com.messagebird.exceptions.UnauthorizedException;
66

7+
import java.io.UnsupportedEncodingException;
78
import java.util.Map;
89

910
/**
@@ -22,6 +23,8 @@ public interface MessageBirdService {
2223
*/
2324
<R> R requestByID(String request, String id, Class<R> clazz) throws UnauthorizedException, GeneralException, NotFoundException;
2425

26+
<R> R requestByID(String request, String id, Map<String, Object> params, Class<R> clazz) throws UnauthorizedException, GeneralException, NotFoundException;
27+
2528
/**
2629
* Delete a object by ID.
2730
*

api/src/main/java/com/messagebird/MessageBirdServiceImpl.java

+24-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
import java.io.OutputStream;
1515
import java.io.UnsupportedEncodingException;
1616
import java.net.*;
17-
import java.util.Arrays;
18-
import java.util.LinkedHashMap;
19-
import java.util.List;
20-
import java.util.Map;
17+
import java.util.*;
2118

2219
/**
2320
* Implementation of MessageBirdService
@@ -35,7 +32,7 @@ public class MessageBirdServiceImpl implements MessageBirdService {
3532
private static final List<String> REQUESTMETHODS = Arrays.asList(new String[]{"GET", "POST", "DELETE"});
3633
private final String accessKey;
3734
private final String serviceUrl = "https://rest.messagebird.com";
38-
private final String clientVersion = "1.0.5";
35+
private final String clientVersion = "1.1.0";
3936
private final String userAgentString = "MessageBird/Java ApiClient/" + clientVersion;
4037
private Proxy proxy = null;
4138

@@ -62,6 +59,20 @@ public <R> R requestByID(String request, String id, Class<R> clazz) throws Unaut
6259
return getJsonData(request + path, null, "GET", clazz);
6360
}
6461

62+
@Override
63+
public <R> R requestByID(String request, String id, Map<String, Object> params, Class<R> clazz) throws UnauthorizedException, GeneralException, NotFoundException {
64+
String path = "";
65+
if (id != null) {
66+
path = "/" + id;
67+
}
68+
// Make rest of get request
69+
String queryParams = "";
70+
if (!params.isEmpty()) {
71+
queryParams = "?" + getPathVariables(params);
72+
}
73+
return getJsonData(request + path + queryParams, null, "GET", clazz);
74+
}
75+
6576
@Override
6677
public void deleteByID(String request, String id) throws UnauthorizedException, GeneralException, NotFoundException {
6778
getJsonData(request + "/" + id, null, "DELETE", null);
@@ -105,6 +116,8 @@ public <T, P> T getJsonData(final String request, final P payload, final String
105116
// If we as new properties, we don't want the system to fail, we rather want to ignore them
106117
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
107118
return mapper.readValue(inputStream, clazz);
119+
} else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
120+
return null; // no content doesn't mean an error
108121
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
109122
final List<ErrorReport> errorReport = getErrorReport(connection.getErrorStream());
110123
throw new UnauthorizedException(NOT_AUTHORISED_MSG, errorReport);
@@ -169,10 +182,15 @@ public <P> HttpURLConnection getConnection(final String serviceUrl, final P post
169182

170183
final String json = mapper.writeValueAsString(postData);
171184
connection.getOutputStream().write(json.getBytes("UTF-8"));
185+
} else if ("DELETE".equals(requestType)) {
186+
// could have just used rquestType as it is
187+
connection.setDoOutput(false);
188+
connection.setRequestMethod("DELETE");
189+
connection.setRequestProperty("Content-Type", "application/text");
172190
} else {
173191
connection.setDoOutput(false);
174192
connection.setRequestMethod("GET");
175-
connection.setRequestProperty("Content-Type:", "application/text");
193+
connection.setRequestProperty("Content-Type", "application/text");
176194
}
177195

178196
return connection;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.messagebird.objects;
2+
3+
/**
4+
* Created by faizan on 09/12/15.
5+
*/
6+
public enum Gender {
7+
8+
MALE("male"),
9+
FEMALE("female");
10+
11+
private String code;
12+
13+
Gender(String code) {
14+
this.code = code;
15+
}
16+
17+
public String toString() {
18+
return this.code;
19+
}
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.messagebird.objects;
2+
3+
/**
4+
* Created by faizan on 09/12/15.
5+
*/
6+
public enum Language {
7+
8+
NL_NL("nl-nl"),
9+
DE_DE("de-de"),
10+
EN_GB("en-gb"),
11+
EN_US("en-us"),
12+
ES_ES("es-es"),
13+
FR_FR("fr-fr"),
14+
RU_RU("ru_ru"),
15+
ZH_CN("zh-cn"),
16+
EN_AU("en-au"),
17+
ES_MX("es-mx"),
18+
ES_US("es-us"),
19+
FR_CA("fr-ca"),
20+
IS_IS("is-is"),
21+
IT_IT("it-it"),
22+
JA_JP("ja-jp"),
23+
KO_KR("ko-kr"),
24+
PL_PL("pl-pl"),
25+
PT_BR("pt-br"),
26+
RO_RO("ro-ro");
27+
28+
private String code;
29+
30+
Language(String code) {
31+
this.code = code;
32+
}
33+
34+
public String toString() {
35+
return this.code;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.messagebird.objects;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Created by faizan on 09/12/15.
7+
*/
8+
public class Messages implements Serializable {
9+
private String href;
10+
11+
public String getHref() {
12+
return href;
13+
}
14+
15+
public void setHref(String href) {
16+
this.href = href;
17+
}
18+
19+
public String toString() {
20+
return "Messages{" +
21+
"href=" + this.href +
22+
"}";
23+
}
24+
}

0 commit comments

Comments
 (0)