Skip to content

Commit

Permalink
refactor: Use Header auth in SmsClient (#555)
Browse files Browse the repository at this point in the history
* Use Header auth in Send SMS and NI v1  Basic Insight endpoints

* Remove UnsupportedEncodingException catch block

* .gitignore BugRepro and use template instead

* Check for package updates monthly
  • Loading branch information
SMadani authored Dec 2, 2024
1 parent b4cc357 commit 17f5f10
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ updates:
- package-ecosystem: 'maven'
directory: '/'
schedule:
interval: 'weekly'
interval: 'monthly'
commit-message:
prefix: 'build'
ignore:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependency-reduced-pom.xml
/javadoc
/test.properties
archive/
binvalid_application_key.pem
bin/
gradle.properties
vonage-sdk.jar
publishing
Expand All @@ -21,3 +21,4 @@ out/
.DS_Store
pom.xml.releaseBackup
pom.xml.versionsBackup
src/test/java/BugRepro.java
28 changes: 10 additions & 18 deletions src/main/java/com/vonage/client/AbstractMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap;
import java.util.Set;
Expand Down Expand Up @@ -71,25 +70,20 @@ protected ResultT postProcessParsedResponse(ResultT response) {
*/
@Override
public ResultT execute(RequestT request) throws VonageResponseParseException, VonageClientException {
try {
HttpUriRequest httpRequest = applyAuth(makeRequest(request))
.setHeader("User-Agent", httpWrapper.getUserAgent())
.setCharset(StandardCharsets.UTF_8).build();
HttpUriRequest httpRequest = applyAuth(makeRequest(request))
.setHeader("User-Agent", httpWrapper.getUserAgent())
.setCharset(StandardCharsets.UTF_8).build();

try (CloseableHttpResponse response = httpWrapper.getHttpClient().execute(httpRequest)) {
try {
return postProcessParsedResponse(parseResponse(response));
}
catch (IOException iox) {
throw new VonageResponseParseException(iox);
}
try (CloseableHttpResponse response = httpWrapper.getHttpClient().execute(httpRequest)) {
try {
return postProcessParsedResponse(parseResponse(response));
}
catch (IOException iox) {
throw new VonageMethodFailedException("Something went wrong while executing the HTTP request.", iox);
throw new VonageResponseParseException(iox);
}
}
catch (UnsupportedEncodingException uex) {
throw new VonageUnexpectedException("UTF-8 encoding is not supported by this JVM.", uex);
catch (IOException iox) {
throw new VonageMethodFailedException("Something went wrong while executing the HTTP request.", iox);
}
}

Expand Down Expand Up @@ -140,10 +134,8 @@ protected AuthMethod getAuthMethod() throws VonageUnexpectedException {
* @param request A RequestT representing input to the REST call to be made
*
* @return A ResultT representing the response from the executed REST call
*
* @throws UnsupportedEncodingException if UTF-8 encoding is not supported by the JVM
*/
protected abstract RequestBuilder makeRequest(RequestT request) throws UnsupportedEncodingException;
protected abstract RequestBuilder makeRequest(RequestT request);

/**
* Construct a ResultT representing the contents of the HTTP response returned from the Vonage Voice API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public String getApiKey() {
* Converts this to a {@linkplain QueryParamsAuthMethod}.
*
* @return A new {@linkplain ApiKeyQueryParamsAuthMethod} with this object's API key and secret.
* @deprecated This will be removed in a future release.
*/
@Deprecated
public QueryParamsAuthMethod asQueryParams() {
return new ApiKeyQueryParamsAuthMethod(apiKey, apiSecret);
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/vonage/client/insight/InsightClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package com.vonage.client.insight;

import com.vonage.client.*;
import com.vonage.client.auth.ApiKeyHeaderAuthMethod;
import com.vonage.client.auth.ApiKeyQueryParamsAuthMethod;
import com.vonage.client.auth.AuthMethod;
import com.vonage.client.auth.SignatureAuthMethod;
import com.vonage.client.common.HttpMethod;
import java.util.function.Function;
Expand All @@ -38,10 +40,10 @@ public class InsightClient {
public InsightClient(HttpWrapper wrapper) {
@SuppressWarnings("unchecked")
final class Endpoint<T, R> extends DynamicEndpoint<T, R> {
Endpoint(Function<T, String> pathGetter, R... type) {
Endpoint(Function<T, String> pathGetter, Class<? extends AuthMethod> auth, R... type) {
super(DynamicEndpoint.<T, R> builder(type)
.wrapper(wrapper).requestMethod(HttpMethod.POST)
.authMethod(SignatureAuthMethod.class, ApiKeyQueryParamsAuthMethod.class)
.authMethod(SignatureAuthMethod.class, auth)
.pathGetter((de, req) -> {
String base = de.getHttpWrapper().getHttpConfig().getApiBaseUri();
return base + "/ni/" + pathGetter.apply(req) + "/json";
Expand All @@ -50,9 +52,9 @@ final class Endpoint<T, R> extends DynamicEndpoint<T, R> {
}
}

basic = new Endpoint<>(req -> "basic");
standard = new Endpoint<>(req -> "standard");
advanced = new Endpoint<>(req -> "advanced" + (req.isAsync() ? "/async" : ""));
basic = new Endpoint<>(req -> "basic", ApiKeyHeaderAuthMethod.class);
standard = new Endpoint<>(req -> "standard", ApiKeyQueryParamsAuthMethod.class);
advanced = new Endpoint<>(req -> "advanced" + (req.isAsync() ? "/async" : ""), ApiKeyQueryParamsAuthMethod.class);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/vonage/client/sms/SmsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.vonage.client.sms;

import com.vonage.client.*;
import com.vonage.client.auth.ApiKeyHeaderAuthMethod;
import com.vonage.client.auth.ApiKeyQueryParamsAuthMethod;
import com.vonage.client.auth.SignatureAuthMethod;
import com.vonage.client.common.HttpMethod;
Expand All @@ -40,7 +41,7 @@ class Endpoint extends DynamicEndpoint<Message, SmsSubmissionResponse> {
Endpoint() {
super(DynamicEndpoint.<Message, SmsSubmissionResponse> builder(SmsSubmissionResponse.class)
.wrapper(wrapper).requestMethod(HttpMethod.POST)
.authMethod(SignatureAuthMethod.class, ApiKeyQueryParamsAuthMethod.class)
.authMethod(SignatureAuthMethod.class, ApiKeyHeaderAuthMethod.class)
.urlFormEncodedContentType(true).pathGetter((de, req) ->
de.getHttpWrapper().getHttpConfig().getRestBaseUri() + "/sms/json"
)
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/BugRepro.java.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import com.vonage.client.*;
import java.util.*;

/**
* Convenience class for debugging / live testing. You should copy this file and remove the
* `.template` extension from the filename to make it usable.
*/
public class BugRepro {
public static void main(String[] args) throws Throwable {
String TO_NUMBER = System.getenv("TO_NUMBER");

VonageClient client = VonageClient.builder()
.httpConfig(HttpConfig.builder().timeoutMillis(12_000).build())
.apiKey(System.getenv("VONAGE_API_KEY"))
.apiSecret(System.getenv("VONAGE_API_SECRET"))
.applicationId(System.getenv("VONAGE_APPLICATION_ID"))
.privateKeyPath(System.getenv("VONAGE_PRIVATE_KEY_PATH"))
.build();

try {
// Debug code here

System.out.println("Success");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
8 changes: 4 additions & 4 deletions src/test/java/com/vonage/client/AbstractMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected Set<Class<? extends AuthMethod>> getAcceptableAuthMethods() {
}

@Override
public RequestBuilder makeRequest(String request) throws UnsupportedEncodingException {
public RequestBuilder makeRequest(String request) {
return RequestBuilder.get(request);
}

Expand Down Expand Up @@ -119,7 +119,7 @@ public void setUp() throws Exception {
when(mockWrapper.getAuthCollection()).thenReturn(mockAuthMethods);
}

ConcreteMethod mockJsonResponse(String json, boolean failing) throws Exception {
ConcreteMethod mockJsonResponse(String json, boolean failing) {
ConcreteMethod method = spy(failing ?
new ConcreteMethodFailingParse(mockWrapper) : new ConcreteMethod(mockWrapper)
);
Expand Down Expand Up @@ -311,8 +311,8 @@ protected AuthMethod getAuthMethod() throws VonageUnexpectedException {
}

@Override
public RequestBuilder makeRequest(String request) throws UnsupportedEncodingException {
return builder.setEntity(new StringEntity(request));
public RequestBuilder makeRequest(String request) {
return builder.setEntity(new StringEntity(request, ContentType.TEXT_PLAIN));
}
}
return new LocalMethod();
Expand Down
48 changes: 0 additions & 48 deletions src/test/java/com/vonage/client/BugRepro.java

This file was deleted.

21 changes: 21 additions & 0 deletions src/test/java/com/vonage/client/insight/InsightClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@

import com.vonage.client.AbstractClientTest;
import com.vonage.client.RestEndpoint;
import com.vonage.client.auth.ApiKeyHeaderAuthMethod;
import com.vonage.client.auth.ApiKeyQueryParamsAuthMethod;
import com.vonage.client.auth.AuthMethod;
import com.vonage.client.auth.SignatureAuthMethod;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class InsightClientTest extends AbstractClientTest<InsightClient> {
Expand Down Expand Up @@ -222,6 +228,11 @@ protected RestEndpoint<BasicInsightRequest, BasicInsightResponse> endpoint() {
return client.basic;
}

@Override
protected Collection<Class<? extends AuthMethod>> expectedAuthMethods() {
return List.of(SignatureAuthMethod.class, ApiKeyHeaderAuthMethod.class);
}

@Override
protected String expectedEndpointUri(BasicInsightRequest request) {
return "/ni/basic/json";
Expand Down Expand Up @@ -254,6 +265,11 @@ protected RestEndpoint<StandardInsightRequest, StandardInsightResponse> endpoint
return client.standard;
}

@Override
protected Collection<Class<? extends AuthMethod>> expectedAuthMethods() {
return List.of(SignatureAuthMethod.class, ApiKeyQueryParamsAuthMethod.class);
}

@Override
protected String expectedEndpointUri(StandardInsightRequest request) {
return "/ni/standard/json";
Expand Down Expand Up @@ -288,6 +304,11 @@ protected RestEndpoint<AdvancedInsightRequest, AdvancedInsightResponse> endpoint
return client.advanced;
}

@Override
protected Collection<Class<? extends AuthMethod>> expectedAuthMethods() {
return List.of(SignatureAuthMethod.class, ApiKeyQueryParamsAuthMethod.class);
}

@Override
protected String expectedEndpointUri(AdvancedInsightRequest request) {
return request.isAsync() ? "/ni/advanced/async/json" : "/ni/advanced/json";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,11 @@

import com.vonage.client.DynamicEndpointTestSpec;
import com.vonage.client.VonageApiResponseException;
import com.vonage.client.auth.ApiKeyQueryParamsAuthMethod;
import com.vonage.client.auth.AuthMethod;
import com.vonage.client.auth.SignatureAuthMethod;
import com.vonage.client.common.HttpMethod;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

abstract class InsightEndpointTestSpec<T, R> extends DynamicEndpointTestSpec<T, R> {

@Override
protected Collection<Class<? extends AuthMethod>> expectedAuthMethods() {
return Arrays.asList(SignatureAuthMethod.class, ApiKeyQueryParamsAuthMethod.class);
}

@Override
protected Class<? extends VonageApiResponseException> expectedResponseExceptionType() {
return VonageApiResponseException.class;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/vonage/client/sms/SmsEndpointTestSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.vonage.client.DynamicEndpointTestSpec;
import com.vonage.client.RestEndpoint;
import com.vonage.client.VonageApiResponseException;
import com.vonage.client.auth.ApiKeyQueryParamsAuthMethod;
import com.vonage.client.auth.ApiKeyHeaderAuthMethod;
import com.vonage.client.auth.AuthMethod;
import com.vonage.client.auth.SignatureAuthMethod;
import com.vonage.client.common.HttpMethod;
Expand Down Expand Up @@ -47,7 +47,7 @@ protected HttpMethod expectedHttpMethod() {

@Override
protected Collection<Class<? extends AuthMethod>> expectedAuthMethods() {
return Arrays.asList(SignatureAuthMethod.class, ApiKeyQueryParamsAuthMethod.class);
return Arrays.asList(SignatureAuthMethod.class, ApiKeyHeaderAuthMethod.class);
}

@Override
Expand Down

0 comments on commit 17f5f10

Please sign in to comment.