Skip to content

Added verify api to java client #11

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

Merged
merged 6 commits into from
Jan 12, 2016
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ hs_err_pid*

# The various target folders
*target/
*.iml
.idea
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ If you are using maven simply add the messagebird API to your dependencies like
<dependency>
<groupId>com.messagebird</groupId>
<artifactId>messagebird-api</artifactId>
<version>1.0.5</version>
<version>1.1.0</version>
</dependency>
```

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

```
messagebird-api-1.0.5.jar
messagebird-api-1.1.0.jar
jackson-core-2.1.1.jar
jackson-databind-2.1.1.jar
jackson-mapper-asl-1.9.13.jar
Expand Down
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.messagebird</groupId>
<artifactId>messagebird-api</artifactId>
<version>1.0.5</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
83 changes: 80 additions & 3 deletions api/src/main/java/com/messagebird/MessageBirdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
import com.messagebird.objects.*;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Message bird general client
* <p/>
* <p>
* <pre>
* Initialise this class with a MessageBirdService
* // First create your service object
Expand All @@ -24,14 +23,15 @@
* Then read your balance like this:
* final Balance balance = messageBirdClient.getBalance();
* </pre>
* <p/>
* <p>
* Created by rvt on 1/5/15.
*/
public class MessageBirdClient {
private static final String BALANCEPATH = "/balance";
private static final String HLRPATH = "/hlr";
private static final String MESSAGESPATH = "/messages";
private static final String VOICEMESSAGESPATH = "/voicemessages";
private static final String VERIFYPATH = "/verify";
private MessageBirdService messageBirdService;

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

/**
* @param verifyRequest
* @return Verify object
* @throws UnauthorizedException
* @throws GeneralException
*/
public Verify sendVerifyToken(VerifyRequest verifyRequest) throws UnauthorizedException, GeneralException {
if (verifyRequest == null) {
throw new IllegalArgumentException("Verify request cannot be null");
} else if (verifyRequest.getRecipient() == null || verifyRequest.getRecipient().isEmpty()) {
throw new IllegalArgumentException("Recipient cannot be empty for verify");
}
return messageBirdService.sendPayLoad(VERIFYPATH, verifyRequest, Verify.class);
}

/**
* @param recipient
* @return Verify object
* @throws UnauthorizedException
* @throws GeneralException
*/
public Verify sendVerifyToken(String recipient) throws UnauthorizedException, GeneralException {
if (recipient == null || recipient.isEmpty()) {
throw new IllegalArgumentException("Recipient cannot be empty for verify");
}
VerifyRequest verifyRequest = new VerifyRequest(recipient);
return this.sendVerifyToken(verifyRequest);
}

/**
*
* @param id
* @param token
* @return Verify object
* @throws NotFoundException
* @throws GeneralException
* @throws UnauthorizedException
*/
public Verify verifyToken(String id, String token) throws NotFoundException, GeneralException, UnauthorizedException {
if (id == null || id.isEmpty()) {
throw new IllegalArgumentException("ID cannot be empty for verify");
} else if (token == null || token.isEmpty()) {
throw new IllegalArgumentException("ID cannot be empty for verify");
}
final Map<String, Object> params = new LinkedHashMap<String, Object>();
params.put("token", token);
return messageBirdService.requestByID(VERIFYPATH, id, params, Verify.class);
}

/**
*
* @param id
* @return Verify object
* @throws NotFoundException
* @throws GeneralException
* @throws UnauthorizedException
*/
public Verify getVerifyObject(String id) throws NotFoundException, GeneralException, UnauthorizedException {
if (id == null || id.isEmpty()) {
throw new IllegalArgumentException("ID cannot be empty for verify");
}
return messageBirdService.requestByID(VERIFYPATH, id, Verify.class);
}

/**
*
* @param id
* @throws NotFoundException
* @throws GeneralException
* @throws UnauthorizedException
*/
public void deleteVerifyObject(String id) throws NotFoundException, GeneralException, UnauthorizedException {
if (id == null || id.isEmpty()) {
throw new IllegalArgumentException("ID cannot be empty for verify");
}
messageBirdService.deleteByID(VERIFYPATH, id);
}
}
3 changes: 3 additions & 0 deletions api/src/main/java/com/messagebird/MessageBirdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.messagebird.exceptions.NotFoundException;
import com.messagebird.exceptions.UnauthorizedException;

import java.io.UnsupportedEncodingException;
import java.util.Map;

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

<R> R requestByID(String request, String id, Map<String, Object> params, Class<R> clazz) throws UnauthorizedException, GeneralException, NotFoundException;

/**
* Delete a object by ID.
*
Expand Down
30 changes: 24 additions & 6 deletions api/src/main/java/com/messagebird/MessageBirdServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

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

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

@Override
public <R> R requestByID(String request, String id, Map<String, Object> params, Class<R> clazz) throws UnauthorizedException, GeneralException, NotFoundException {
String path = "";
if (id != null) {
path = "/" + id;
}
// Make rest of get request
String queryParams = "";
if (!params.isEmpty()) {
queryParams = "?" + getPathVariables(params);
}
return getJsonData(request + path + queryParams, null, "GET", clazz);
}

@Override
public void deleteByID(String request, String id) throws UnauthorizedException, GeneralException, NotFoundException {
getJsonData(request + "/" + id, null, "DELETE", null);
Expand Down Expand Up @@ -105,6 +116,8 @@ public <T, P> T getJsonData(final String request, final P payload, final String
// If we as new properties, we don't want the system to fail, we rather want to ignore them
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(inputStream, clazz);
} else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
return null; // no content doesn't mean an error
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
final List<ErrorReport> errorReport = getErrorReport(connection.getErrorStream());
throw new UnauthorizedException(NOT_AUTHORISED_MSG, errorReport);
Expand Down Expand Up @@ -169,10 +182,15 @@ public <P> HttpURLConnection getConnection(final String serviceUrl, final P post

final String json = mapper.writeValueAsString(postData);
connection.getOutputStream().write(json.getBytes("UTF-8"));
} else if ("DELETE".equals(requestType)) {
// could have just used rquestType as it is
connection.setDoOutput(false);
connection.setRequestMethod("DELETE");
connection.setRequestProperty("Content-Type", "application/text");
} else {
connection.setDoOutput(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type:", "application/text");
connection.setRequestProperty("Content-Type", "application/text");
}

return connection;
Expand Down
21 changes: 21 additions & 0 deletions api/src/main/java/com/messagebird/objects/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.messagebird.objects;

/**
* Created by faizan on 09/12/15.
*/
public enum Gender {

MALE("male"),
FEMALE("female");

private String code;

Gender(String code) {
this.code = code;
}

public String toString() {
return this.code;
}
}

37 changes: 37 additions & 0 deletions api/src/main/java/com/messagebird/objects/Language.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.messagebird.objects;

/**
* Created by faizan on 09/12/15.
*/
public enum Language {

NL_NL("nl-nl"),
DE_DE("de-de"),
EN_GB("en-gb"),
EN_US("en-us"),
ES_ES("es-es"),
FR_FR("fr-fr"),
RU_RU("ru_ru"),
ZH_CN("zh-cn"),
EN_AU("en-au"),
ES_MX("es-mx"),
ES_US("es-us"),
FR_CA("fr-ca"),
IS_IS("is-is"),
IT_IT("it-it"),
JA_JP("ja-jp"),
KO_KR("ko-kr"),
PL_PL("pl-pl"),
PT_BR("pt-br"),
RO_RO("ro-ro");

private String code;

Language(String code) {
this.code = code;
}

public String toString() {
return this.code;
}
}
24 changes: 24 additions & 0 deletions api/src/main/java/com/messagebird/objects/Messages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.messagebird.objects;

import java.io.Serializable;

/**
* Created by faizan on 09/12/15.
*/
public class Messages implements Serializable {
private String href;

public String getHref() {
return href;
}

public void setHref(String href) {
this.href = href;
}

public String toString() {
return "Messages{" +
"href=" + this.href +
"}";
}
}
Loading