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

refactor: VerifyClient uses DynamicEndpoint #479

Merged
merged 2 commits into from
Sep 13, 2023
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ need a Vonage account. You'll need to have [created a Vonage account](https://da

- [Account](https://developer.vonage.com/en/account/overview)
- [Application](https://developer.vonage.com/en/application/overview)
- [Conversion](https://developer.vonage.com/messaging/conversion-api/overview)
- [Meetings](https://developer.vonage.com/en/meetings/overview)
- [Messages](https://developer.vonage.com/en/messages/overview)
- [Number Insight](https://developer.vonage.com/en/number-insight/overview)
- [Number Management](https://developer.vonage.com/en/numbers/overview)
- [Proactive Connect](https://developer.vonage.com/en/proactive-connect/overview)
- [Redact](https://developer.vonage.com/en/redact/overview)
- [SMS](https://developer.vonage.com/en/sms/overview)
- [Subaccounts](https://developer.vonage.com/en/subaccounts/overview)
- [Subaccounts](https://developer.vonage.com/en/account/subaccounts/overview)
- [Verify](https://developer.vonage.com/en/verify/overview)
- [Voice](https://developer.vonage.com/en/voice/overview)

Expand Down
36 changes: 32 additions & 4 deletions src/main/java/com/vonage/client/DynamicEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Consumer;

/**
* Enables convenient declaration of endpoints without directly implementing {@link AbstractMethod}.
* This decouples the endpoint's implementation from the underlying HTTP library.
*
* @since 7.7.0
*/
@SuppressWarnings("unchecked")
public class DynamicEndpoint<T, R> extends AbstractMethod<T, R> {
Expand Down Expand Up @@ -166,15 +169,31 @@ public RequestBuilder makeRequest(T requestBody) throws UnsupportedEncodingExcep
rqb.setHeader("Accept", accept);
}
if (requestBody instanceof QueryParamsRequest) {
((QueryParamsRequest) requestBody).makeParams().forEach(rqb::addParameter);
Map<String, ?> params = ((QueryParamsRequest) requestBody).makeParams();
params.forEach((k, v) -> {
Consumer<Object> logic = obj -> rqb.addParameter(k, String.valueOf(obj));
if (v instanceof Object[]) {
for (Object nested : (Object[]) v) {
logic.accept(nested);
}
}
else if (v instanceof Iterable<?>) {
for (Object nested : (Iterable<?>) v) {
logic.accept(nested);
}
}
else {
logic.accept(v);
}
});
}
if (requestBody instanceof Jsonable) {
rqb.setEntity(new StringEntity(((Jsonable) requestBody).toJson(), ContentType.APPLICATION_JSON));
}
else if (requestBody instanceof byte[]) {
rqb.setEntity(new ByteArrayEntity((byte[]) requestBody));
}
return rqb.setUri(URI.create(pathGetter.apply(this, requestBody)));
return rqb.setUri(pathGetter.apply(this, requestBody));
}

protected R parseResponseFromString(String response) {
Expand Down Expand Up @@ -276,7 +295,16 @@ else if (byte[].class.equals(responseType)) {
}
}
}
catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException ex) {
catch (InvocationTargetException ex) {
Throwable wrapped = ex.getTargetException();
if (wrapped instanceof RuntimeException) {
throw (RuntimeException) wrapped;
}
else {
throw new VonageUnexpectedException(wrapped);
}
}
catch (InstantiationException | IllegalAccessException | NoSuchMethodException ex) {
throw new VonageUnexpectedException(ex);
}
finally {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/vonage/client/Jsonable.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

/**
* Indicates that a class can be serialized to and parsed from JSON.
*
* @since 7.7.0
*/
public interface Jsonable {

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/vonage/client/QueryParamsRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

/**
* Indicates that a request object has query parameters.
*
* @since 7.7.0
*/
public interface QueryParamsRequest {

Map<String, String> makeParams();
Map<String, ?> makeParams();
}
2 changes: 2 additions & 0 deletions src/main/java/com/vonage/client/RestEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*
* @param <T> The request type.
* @param <R> The response type.
*
* @since 7.7.0
*/
public interface RestEndpoint<T, R> extends Method<T, R> {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/vonage/client/account/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public String getName() {
return name;
}

enum Type {
public enum Type {
MOBILE, LANDLINE, PAGER, LANDLINE_TOLLFREE, UNKNOWN;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Map<String, String> makeParams() {
*
* @param number A single phone number that you need insight about in national or international format.
*
* @return A new {@link AdvancedInsightRequest} object.
* @return A new AdvancedInsightRequest object.
*/
public static AdvancedInsightRequest withNumber(String number) {
return new Builder(number).build();
Expand All @@ -112,7 +112,7 @@ public static AdvancedInsightRequest withNumber(String number) {
* @param number A single phone number that you need insight about in national or international format.
* @param country If a number does not have a country code, or it is uncertain, set the two-character country code.
*
* @return A new {@link AdvancedInsightRequest} object.
* @return A new AdvancedInsightRequest object.
*/
public static AdvancedInsightRequest withNumberAndCountry(String number, String country) {
return new Builder(number).country(country).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private BasicInsightRequest(Builder builder) {
*
* @param number A single phone number that you need insight about in national or international format.
*
* @return A new {@link BasicInsightRequest} object.
* @return A new BasicInsightRequest object.
*/
public static BasicInsightRequest withNumber(String number) {
return new Builder(number).build();
Expand All @@ -38,7 +38,7 @@ public static BasicInsightRequest withNumber(String number) {
* @param number A single phone number that you need insight about in national or international format.
* @param country If a number does not have a country code, or it is uncertain, set the two-character country code.
*
* @return A new {@link BasicInsightRequest} object.
* @return A new BasicInsightRequest object.
*/
public static BasicInsightRequest withNumberAndCountry(String number, String country) {
return new Builder(number).country(country).build();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/vonage/client/insight/InsightStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public enum InsightStatus {
}

/**
* Look up the {@link InsightStatus} based on the int value.
* Look up the InsightStatus based on the int value.
*
* @param insightStatus the integer value of the insight status.
* @return InsightStatus based on the int value given.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Boolean getCnam() {
*
* @param number A single phone number that you need insight about in national or international format.
*
* @return A new {@link StandardInsightRequest} object.
* @return A new StandardInsightRequest object.
*/
public static StandardInsightRequest withNumber(String number) {
return new Builder(number).build();
Expand All @@ -43,7 +43,7 @@ public static StandardInsightRequest withNumber(String number) {
* @param number A single phone number that you need insight about in national or international format.
* @param country If a number does not have a country code or it is uncertain, set the two-character country code.
*
* @return A new {@link StandardInsightRequest} object.
* @return A new StandardInsightRequest object.
*/
public static StandardInsightRequest withNumberAndCountry(String number, String country) {
return new Builder(number).country(country).build();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/vonage/client/sms/MessageStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public enum MessageStatus {
));

/**
* Look up the {@link MessageStatus} based on the int value.
* Look up the MessageStatus based on the int value.
*
* @param messageStatus the int value of the message status.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public String getReference() {
* @param json The JSON string to parse.
* @return An instance of this class with the fields populated, if present.
*/
protected static MoneyTransfer fromJson(String json) {
public static MoneyTransfer fromJson(String json) {
MoneyTransfer transfer = new MoneyTransfer();
transfer.updateFromJson(json);
return transfer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ final class Endpoint<T, R> extends DynamicEndpoint<T, R> {
csr.primaryAccountApiKey = de.getApplicationIdOrApiKey();
}
return String.format(
wrapper.getHttpConfig().getApiBaseUri() + "/accounts/%s/",
de.getApplicationIdOrApiKey()
de.getHttpWrapper().getHttpConfig().getApiBaseUri()
+ "/accounts/%s/", de.getApplicationIdOrApiKey()
) + pathGetter.apply(req);
})
);
Expand Down
32 changes: 30 additions & 2 deletions src/main/java/com/vonage/client/verify/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@
*/
package com.vonage.client.verify;

import com.vonage.client.QueryParamsRequest;
import com.vonage.client.common.E164;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

/**
* Base request class for {@link VerifyRequest} and {@link Psd2Request}.
* @since 5.5.0
*/
public abstract class BaseRequest {
public abstract class BaseRequest implements QueryParamsRequest {
private final String number, country;
private final Integer length, pinExpiry, nextEventWait;
private final Locale locale;

protected BaseRequest(String number, Integer length, Locale locale, String country, Integer pinExpiry, Integer nextEventWait) {
this.number = number;
this.number = new E164(number).toString();
this.locale = locale;
this.country = country;
if ((this.length = length) != null && (length != 4 && length != 6)) {
Expand Down Expand Up @@ -122,4 +126,28 @@ public String toString() {
", pinExpiry=" + pinExpiry +
", nextEventWait=" + nextEventWait;
}

@Override
public Map<String, String> makeParams() {
Map<String, String> params = new LinkedHashMap<>();
if (number != null) {
params.put("number", number);
}
if (length != null && length > 0) {
params.put("code_length", String.valueOf(length));
}
if (locale != null) {
params.put("lg", getDashedLocale());
}
if (country != null) {
params.put("country", country);
}
if (pinExpiry != null) {
params.put("pin_expiry", String.valueOf(pinExpiry));
}
if (nextEventWait != null) {
params.put("next_event_wait", String.valueOf(nextEventWait));
}
return params;
}
}
59 changes: 0 additions & 59 deletions src/main/java/com/vonage/client/verify/CheckEndpoint.java

This file was deleted.

17 changes: 16 additions & 1 deletion src/main/java/com/vonage/client/verify/CheckRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
*/
package com.vonage.client.verify;

public class CheckRequest {
import com.vonage.client.QueryParamsRequest;
import java.util.LinkedHashMap;
import java.util.Map;

public class CheckRequest implements QueryParamsRequest {
private final String requestId, code, ipAddress;

/**
Expand Down Expand Up @@ -76,4 +80,15 @@ public String getCode() {
public String getIpAddress() {
return ipAddress;
}

@Override
public Map<String, String> makeParams() {
Map<String, String> params = new LinkedHashMap<>(4);
params.put("request_id", requestId);
params.put("code", code);
if (ipAddress != null) {
params.put("ip_address", ipAddress);
}
return params;
}
}
Loading
Loading