Skip to content

Commit

Permalink
Refactor Numbers and Conversion clients to use DynamicEndpoint (#477)
Browse files Browse the repository at this point in the history
* Refactored Numbers to use DynamicEndpoint

* Refactored ConversionClient

* Refactored NumbersClient endpoint tests

* Test NumbersResponseException

* Improved Numbers coverage
  • Loading branch information
SMadani authored Aug 29, 2023
1 parent 5ee1f52 commit ff72718
Show file tree
Hide file tree
Showing 32 changed files with 683 additions and 1,462 deletions.
15 changes: 12 additions & 3 deletions src/main/java/com/vonage/client/conversion/ConversionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package com.vonage.client.conversion;

import com.vonage.client.*;
import com.vonage.client.auth.SignatureAuthMethod;
import com.vonage.client.auth.TokenAuthMethod;
import com.vonage.client.common.HttpMethod;
import java.util.Date;

/**
Expand All @@ -27,10 +30,16 @@
* See the <a href="https://developer.vonage.com/messaging/conversion-api/overview">Conversion API documentation</a>.
*/
public class ConversionClient {
final ConversionEndpoint conversionEndpoint;
final RestEndpoint<ConversionRequest, Void> conversionEndpoint;

public ConversionClient(HttpWrapper httpWrapper) {
conversionEndpoint = new ConversionEndpoint(httpWrapper);
@SuppressWarnings("unchecked")
public ConversionClient(HttpWrapper wrapper) {
conversionEndpoint = DynamicEndpoint.<ConversionRequest, Void> builder(Void.class)
.pathGetter((de, req) -> de.getHttpWrapper().getHttpConfig().getApiBaseUri() +
"/conversions/" + req.getType().name().toLowerCase()
)
.authMethod(SignatureAuthMethod.class, TokenAuthMethod.class)
.requestMethod(HttpMethod.POST).wrapper(wrapper).build();
}

/**
Expand Down
67 changes: 0 additions & 67 deletions src/main/java/com/vonage/client/conversion/ConversionEndpoint.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/
package com.vonage.client.conversion;

import com.vonage.client.QueryParamsRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

public class ConversionRequest {
public class ConversionRequest implements QueryParamsRequest {
private final Type type;
private final String messageId;
private final boolean delivered;
Expand Down Expand Up @@ -46,6 +50,15 @@ public Date getTimestamp() {
return timestamp;
}

@Override
public Map<String, String> makeParams() {
LinkedHashMap<String, String> params = new LinkedHashMap<>(4);
params.put("message-id", messageId);
params.put("delivered", String.valueOf(delivered));
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp));
return params;
}

public enum Type {
SMS, VOICE
}
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/vonage/client/numbers/BaseNumberRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 Vonage
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.vonage.client.numbers;

import com.vonage.client.QueryParamsRequest;
import org.apache.http.client.methods.RequestBuilder;
import java.util.LinkedHashMap;
import java.util.Map;

class BaseNumberRequest implements QueryParamsRequest {
private final String country, msisdn;

public BaseNumberRequest(String country, String msisdn) {
this.country = country;
this.msisdn = msisdn;
}

public String getCountry() {
return country;
}

public String getMsisdn() {
return msisdn;
}

@Deprecated
public void addParams(RequestBuilder request) {
makeParams().forEach(request::addParameter);
}

@Override
public Map<String, String> makeParams() {
LinkedHashMap<String, String> params = new LinkedHashMap<>(4);
params.put("country", country);
params.put("msisdn", msisdn);
return params;
}
}
63 changes: 0 additions & 63 deletions src/main/java/com/vonage/client/numbers/BuyNumberEndpoint.java

This file was deleted.

20 changes: 2 additions & 18 deletions src/main/java/com/vonage/client/numbers/BuyNumberRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,9 @@
*/
package com.vonage.client.numbers;

import org.apache.http.client.methods.RequestBuilder;

public class BuyNumberRequest {
private final String country, msisdn;
public class BuyNumberRequest extends BaseNumberRequest {

public BuyNumberRequest(String country, String msisdn) {
this.country = country;
this.msisdn = msisdn;
}

public String getCountry() {
return country;
}

public String getMsisdn() {
return msisdn;
}

public void addParams(RequestBuilder request) {
request.addParameter("country", country).addParameter("msisdn", msisdn);
super(country, msisdn);
}
}
63 changes: 0 additions & 63 deletions src/main/java/com/vonage/client/numbers/CancelNumberEndpoint.java

This file was deleted.

20 changes: 2 additions & 18 deletions src/main/java/com/vonage/client/numbers/CancelNumberRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,9 @@
*/
package com.vonage.client.numbers;

import org.apache.http.client.methods.RequestBuilder;

public class CancelNumberRequest {
private final String country, msisdn;
public class CancelNumberRequest extends BaseNumberRequest {

public CancelNumberRequest(String country, String msisdn) {
this.country = country;
this.msisdn = msisdn;
}

public String getCountry() {
return country;
}

public String getMsisdn() {
return msisdn;
}

public void addParams(RequestBuilder request) {
request.addParameter("country", country).addParameter("msisdn", msisdn);
super(country, msisdn);
}
}
Loading

0 comments on commit ff72718

Please sign in to comment.