Skip to content

Commit

Permalink
Merge pull request #297 from Nexmo/bugfix/overridable-base-urls
Browse files Browse the repository at this point in the history
Bugfix/overridable base urls
  • Loading branch information
yallen011 authored Aug 19, 2020
2 parents 77ee460 + 6527acf commit ec02430
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 69 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply plugin: "jacoco"
apply plugin: 'maven'
apply plugin: 'signing'
apply plugin: 'eclipse'
// apply plugin: 'war'


group = "com.nexmo"
archivesBaseName = "client"
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/nexmo/client/voice/ModifyCallMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
class ModifyCallMethod extends AbstractMethod<CallModifier, ModifyCallResponse> {
private static final Log LOG = LogFactory.getLog(ModifyCallMethod.class);

private static final String DEFAULT_URI = "https://api.nexmo.com/v1/calls/";
private static final String PATH = "/calls/";
private static final Class[] ALLOWED_AUTH_METHODS = new Class[]{JWTAuthMethod.class};
private String uri = DEFAULT_URI;

ModifyCallMethod(HttpWrapper httpWrapper) {
super(httpWrapper);
Expand All @@ -53,9 +52,8 @@ protected Class[] getAcceptableAuthMethods() {

@Override
public RequestBuilder makeRequest(CallModifier request) throws UnsupportedEncodingException {
String uri = this.uri + request.getUuid();
return RequestBuilder
.put(uri)
.put(httpWrapper.getHttpConfig().getVersionedApiBaseUri("v1") + PATH + request.getUuid())
.setHeader("Content-Type", "application/json")
.setEntity(new StringEntity(request.toJson(), ContentType.APPLICATION_JSON));
}
Expand All @@ -69,8 +67,4 @@ public ModifyCallResponse parseResponse(HttpResponse response) throws IOExceptio
return null;
}
}

public void setUri(String uri) {
this.uri = uri;
}
}
14 changes: 6 additions & 8 deletions src/main/java/com/nexmo/client/voice/SendDtmfMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
Expand All @@ -38,9 +40,9 @@
class SendDtmfMethod extends AbstractMethod<DtmfRequest, DtmfResponse> {
private static final Log LOG = LogFactory.getLog(SendDtmfMethod.class);

private static final String DEFAULT_URI = "https://api.nexmo.com/v1/calls/";
private static final String PATH = "/calls/";
private static final Class[] ALLOWED_AUTH_METHODS = new Class[]{JWTAuthMethod.class};
private String uri = DEFAULT_URI;
public static final String DTMF_PATH = "/dtmf";

SendDtmfMethod(HttpWrapper httpWrapper) {
super(httpWrapper);
Expand All @@ -53,8 +55,8 @@ protected Class[] getAcceptableAuthMethods() {

@Override
public RequestBuilder makeRequest(DtmfRequest request) throws UnsupportedEncodingException {
String uri = this.uri + request.getUuid() + "/dtmf";
return RequestBuilder.put(uri)
return RequestBuilder
.put(httpWrapper.getHttpConfig().getVersionedApiBaseUri("v1") + PATH + request.getUuid() + DTMF_PATH)
.setHeader("Content-Type", "application/json")
.setEntity(new StringEntity(request.toJson(), ContentType.APPLICATION_JSON));
}
Expand All @@ -64,8 +66,4 @@ public DtmfResponse parseResponse(HttpResponse response) throws IOException {
String json = new BasicResponseHandler().handleResponse(response);
return DtmfResponse.fromJson(json);
}

public void setUri(String uri) {
this.uri = uri;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/nexmo/client/voice/StopStreamMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class StopStreamMethod extends AbstractMethod<String, StreamResponse> {
private static final Log LOG = LogFactory.getLog(StopStreamMethod.class);

private static final String PATH = "/calls/";
public static final String STREAM_PATH = "/stream";
private static final Class[] ALLOWED_AUTH_METHODS = new Class[]{JWTAuthMethod.class};
private String uri;

StopStreamMethod(HttpWrapper httpWrapper) {
super(httpWrapper);
Expand All @@ -52,7 +52,7 @@ protected Class[] getAcceptableAuthMethods() {
@Override
public RequestBuilder makeRequest(String uuid) throws UnsupportedEncodingException {
return RequestBuilder
.delete(httpWrapper.getHttpConfig().getVersionedApiBaseUri("v1") + PATH + uuid + "/stream")
.delete(httpWrapper.getHttpConfig().getVersionedApiBaseUri("v1") + PATH + uuid + STREAM_PATH)
.setHeader("Content-Type", "application/json");
}

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/nexmo/client/voice/StopTalkMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.nexmo.client.voice;

import com.nexmo.client.AbstractMethod;
import com.nexmo.client.HttpConfig;
import com.nexmo.client.HttpWrapper;
import com.nexmo.client.auth.JWTAuthMethod;
import org.apache.commons.logging.Log;
Expand All @@ -36,9 +37,9 @@
class StopTalkMethod extends AbstractMethod<String, TalkResponse> {
private static final Log LOG = LogFactory.getLog(StopTalkMethod.class);

private static final String DEFAULT_URI = "https://api.nexmo.com/v1/calls/";
private static final String PATH = "/calls/";
private static final Class[] ALLOWED_AUTH_METHODS = new Class[]{JWTAuthMethod.class};
private String uri = DEFAULT_URI;
public static final String TALK_PATH = "/talk";

StopTalkMethod(HttpWrapper httpWrapper) {
super(httpWrapper);
Expand All @@ -51,17 +52,14 @@ protected Class[] getAcceptableAuthMethods() {

@Override
public RequestBuilder makeRequest(String uuid) throws UnsupportedEncodingException {
String uri = this.uri + uuid + "/talk";
return RequestBuilder.delete(uri).setHeader("Content-Type", "application/json");
return RequestBuilder
.delete(httpWrapper.getHttpConfig().getVersionedApiBaseUri("v1") + PATH + uuid + TALK_PATH)
.setHeader("Content-Type", "application/json");
}

@Override
public TalkResponse parseResponse(HttpResponse response) throws IOException {
String json = new BasicResponseHandler().handleResponse(response);
return TalkResponse.fromJson(json);
}

public void setUri(String uri) {
this.uri = uri;
}
}
61 changes: 26 additions & 35 deletions src/test/java/com/nexmo/client/voice/ModifyCallMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nexmo.client.HttpConfig;
import com.nexmo.client.HttpWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -32,11 +33,11 @@
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
Expand All @@ -45,12 +46,16 @@
public class ModifyCallMethodTest {
private static final Log LOG = LogFactory.getLog(ModifyCallMethodTest.class);

private ModifyCallMethod method;

@Before
public void setUp() throws Exception {
method = new ModifyCallMethod(new HttpWrapper());
}

@Test
public void makeRequest() throws Exception {
HttpWrapper httpWrapper = new HttpWrapper();
ModifyCallMethod methodUnderTest = new ModifyCallMethod(httpWrapper);

RequestBuilder request = methodUnderTest.makeRequest(
RequestBuilder request = method.makeRequest(
new CallModifier("abc-123", ModifyCallAction.HANGUP)
);

Expand All @@ -64,10 +69,7 @@ public void makeRequest() throws Exception {

@Test
public void earmuffRequest() throws Exception {
HttpWrapper httpWrapper = new HttpWrapper();
ModifyCallMethod methodUnderTest = new ModifyCallMethod(httpWrapper);

RequestBuilder request = methodUnderTest.makeRequest(
RequestBuilder request = method.makeRequest(
new CallModifier("abc-123", ModifyCallAction.EARMUFF)
);

Expand All @@ -81,10 +83,7 @@ public void earmuffRequest() throws Exception {

@Test
public void unearmuffRequest() throws Exception {
HttpWrapper httpWrapper = new HttpWrapper();
ModifyCallMethod methodUnderTest = new ModifyCallMethod(httpWrapper);

RequestBuilder request = methodUnderTest.makeRequest(
RequestBuilder request = method.makeRequest(
new CallModifier("abc-123", ModifyCallAction.UNEARMUFF)
);

Expand All @@ -98,10 +97,7 @@ public void unearmuffRequest() throws Exception {

@Test
public void muteRequest() throws Exception {
HttpWrapper httpWrapper = new HttpWrapper();
ModifyCallMethod methodUnderTest = new ModifyCallMethod(httpWrapper);

RequestBuilder request = methodUnderTest.makeRequest(
RequestBuilder request = method.makeRequest(
new CallModifier("abc-123", ModifyCallAction.MUTE)
);

Expand All @@ -115,10 +111,7 @@ public void muteRequest() throws Exception {

@Test
public void unmuteRequest() throws Exception {
HttpWrapper httpWrapper = new HttpWrapper();
ModifyCallMethod methodUnderTest = new ModifyCallMethod(httpWrapper);

RequestBuilder request = methodUnderTest.makeRequest(
RequestBuilder request = method.makeRequest(
new CallModifier("abc-123", ModifyCallAction.UNMUTE)
);

Expand All @@ -132,9 +125,6 @@ public void unmuteRequest() throws Exception {

@Test
public void parseResponse() throws Exception {
HttpWrapper wrapper = new HttpWrapper();
ModifyCallMethod methodUnderTest = new ModifyCallMethod(wrapper);

HttpResponse stubResponse = new BasicHttpResponse(
new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), 200, "OK")
);
Expand All @@ -145,30 +135,31 @@ public void parseResponse() throws Exception {
entity.setContent(jsonStream);
stubResponse.setEntity(entity);

ModifyCallResponse response = methodUnderTest.parseResponse(stubResponse);
ModifyCallResponse response = method.parseResponse(stubResponse);
assertEquals("Received", response.getMessage());
}

@Test
public void parseNullResponse() throws Exception {
HttpWrapper wrapper = new HttpWrapper();
ModifyCallMethod methodUnderTest = new ModifyCallMethod(wrapper);

HttpResponse stubResponse = new BasicHttpResponse(
new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), 204, "OK")
);

ModifyCallResponse response = methodUnderTest.parseResponse(stubResponse);
ModifyCallResponse response = method.parseResponse(stubResponse);
assertNull(response);
}

@Test
public void testSetUri() throws Exception {
ModifyCallMethod methodUnderTest = new ModifyCallMethod(null);
methodUnderTest.setUri("https://example.com/dummy/");
RequestBuilder req = methodUnderTest.makeRequest(
new CallModifier("uuid-1234", ModifyCallAction.HANGUP)
public void testCustomUri() throws Exception {
String expectedUri = "https://example.com/v1/calls/ssf61863-4a51-ef6b-11e1-w6edebcf93bA";

HttpWrapper wrapper = new HttpWrapper(HttpConfig.builder().baseUri("https://example.com").build());
method = new ModifyCallMethod(wrapper);

RequestBuilder builder = method.makeRequest(
new CallModifier("ssf61863-4a51-ef6b-11e1-w6edebcf93bA", ModifyCallAction.HANGUP)
);
assertEquals(new URI("https://example.com/dummy/uuid-1234"), req.getUri());
assertEquals("PUT", builder.getMethod());
assertEquals(expectedUri, builder.build().getURI().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ReadCallMethodTest {

@Before
public void setUp() throws Exception {
this.method = new ReadCallMethod(new HttpWrapper());
method = new ReadCallMethod(new HttpWrapper());
}

@Test
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/nexmo/client/voice/SendDtmfMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nexmo.client.HttpConfig;
import com.nexmo.client.HttpWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -32,6 +33,7 @@
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
Expand All @@ -45,6 +47,12 @@
public class SendDtmfMethodTest {
private static final Log LOG = LogFactory.getLog(SendDtmfMethodTest.class);

private SendDtmfMethod method;
@Before
public void setUp() throws Exception {
method = new SendDtmfMethod(new HttpWrapper());
}

@Test
public void makeRequest() throws Exception {
HttpWrapper httpWrapper = new HttpWrapper();
Expand Down Expand Up @@ -86,4 +94,16 @@ public void parseResponse() throws Exception {
public void testRequestThrottleResponse() throws Exception {
test429(new SendDtmfMethod(null));
}

@Test
public void testCustomUri() throws Exception {
String expectedUri = "https://example.com/v1/calls/ssf61863-4a51-ef6b-11e1-w6edebcf93bb/dtmf";

HttpWrapper wrapper = new HttpWrapper(HttpConfig.builder().baseUri("https://example.com").build());
method = new SendDtmfMethod(wrapper);

RequestBuilder builder = method.makeRequest(new DtmfRequest("ssf61863-4a51-ef6b-11e1-w6edebcf93bb", "1"));
assertEquals("PUT", builder.getMethod());
assertEquals(expectedUri, builder.build().getURI().toString());
}
}
Loading

0 comments on commit ec02430

Please sign in to comment.