Skip to content

Commit

Permalink
Merge pull request #168 from Nexmo/convert-verify-to-json-part-1
Browse files Browse the repository at this point in the history
Convert Verify to JSON
  • Loading branch information
cr0wst authored May 29, 2018
2 parents 990e3fc + 4fbcffa commit 7b0a41f
Show file tree
Hide file tree
Showing 34 changed files with 1,993 additions and 515 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [Development]
### Changed
- Updated `VerifyClient` to use the JSON endpoints instead of XML.
- Updated endpoints which are used by `VerifyClient` from public to package scope in order to encourage usage through `VerifyClient`.
- Deprecated XML version of the following endpoints:
- `VerifyEndpoint`
- `CheckEndpoint`
- `SearchEndpoint`

- Deprecated the following XML results:
- `VerifyResult` should use `VerifyResponse`
- `CheckResult` should use `CheckResponse`
- `SearchResult` should use `SearchVerifyResponse`

- Deprecated the following XML methods:
- `VerifyCheckMethod`

### Added
- Added `VerifyStatus` enumeration to use for statuses coming back from the verify endpoint.
- Added `VerifyResponse`, `CheckResponse`, and `SearchVerifyResponse` for JSON responses to match other JSON using endpoints.
- Added `VerifyMethod`, `CheckMethod`, and `SearchMethod` for better segregation between endpoint and method classes.

### Fixed
- Updated `ConversationNcco`'s `musicOnHoldUrl` to serialize into an array for use in the Voice API.
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/nexmo/client/verify/CheckEndpoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2011-2017 Nexmo Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.nexmo.client.verify;

import com.nexmo.client.HttpWrapper;
import com.nexmo.client.NexmoClientException;

import java.io.IOException;

class CheckEndpoint {
private CheckMethod checkMethod;

CheckEndpoint(HttpWrapper httpWrapper) {
this.checkMethod = new CheckMethod(httpWrapper);
}

CheckResult check(final String requestId,
final String code,
final String ipAddress) throws IOException, NexmoClientException {
return check(new CheckRequest(requestId, code, ipAddress));
}

CheckResult check(final String requestId, final String code) throws IOException, NexmoClientException {
return check(new CheckRequest(requestId, code));
}

private CheckResult check(CheckRequest request) throws IOException, NexmoClientException {
// TODO: Remove translation when releasing 4.0
return translateFromCheckResponse(this.checkMethod.execute(request));
}

private CheckResult translateFromCheckResponse(CheckResponse response) {
return new CheckResult(
// TODO: In 4.0 this will be permitted to be null.
response.getStatus() != null ? response.getStatus().getVerifyStatus() : Integer.MAX_VALUE,
response.getRequestId(),
response.getEventId(),
response.getPrice() != null ? response.getPrice().floatValue() : 0,
response.getCurrency(),
response.getErrorText(),
response.getStatus() != null && response.getStatus().isTemporaryError());
}
}
69 changes: 69 additions & 0 deletions src/main/java/com/nexmo/client/verify/CheckMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2011-2018 Nexmo Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.nexmo.client.verify;

import com.nexmo.client.HttpWrapper;
import com.nexmo.client.NexmoClientException;
import com.nexmo.client.auth.SignatureAuthMethod;
import com.nexmo.client.auth.TokenAuthMethod;
import com.nexmo.client.voice.endpoints.AbstractMethod;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.BasicResponseHandler;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class CheckMethod extends AbstractMethod<CheckRequest, CheckResponse> {
private static final Class[] ALLOWED_AUTH_METHODS = new Class[]{SignatureAuthMethod.class, TokenAuthMethod.class};

private static final String DEFAULT_URI = "https://api.nexmo.com/verify/check/json";

private String uri = DEFAULT_URI;

CheckMethod(HttpWrapper httpWrapper) {
super(httpWrapper);
}

@Override
protected Class[] getAcceptableAuthMethods() {
return ALLOWED_AUTH_METHODS;
}

@Override
public RequestBuilder makeRequest(CheckRequest request) throws NexmoClientException, UnsupportedEncodingException {
if (request.getRequestId() == null || request.getCode() == null)
throw new IllegalArgumentException("request ID and code parameters are mandatory.");

RequestBuilder result = RequestBuilder.post(this.uri).addParameter("request_id", request.getRequestId())

.addParameter("code", request.getCode());
if (request.getIpAddress() != null) result.addParameter("ip_address", request.getIpAddress());

return result;
}

@Override
public CheckResponse parseResponse(HttpResponse response) throws IOException {
return CheckResponse.fromJson(new BasicResponseHandler().handleResponse(response));
}
}
86 changes: 86 additions & 0 deletions src/main/java/com/nexmo/client/verify/CheckResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2011-2018 Nexmo Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.nexmo.client.verify;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nexmo.client.NexmoResponseParseException;
import com.nexmo.client.NexmoUnexpectedException;

import java.io.IOException;
import java.math.BigDecimal;

@JsonIgnoreProperties(ignoreUnknown = true)
public class CheckResponse {
private String requestId;
private String eventId;
private VerifyStatus status;
private BigDecimal price;
private String currency;
private String errorText;

@JsonCreator
public CheckResponse(@JsonProperty(value = "status", required = true) VerifyStatus status) {
this.status = status;
}

@JsonProperty("request_id")
public String getRequestId() {
return this.requestId;
}

@JsonProperty("event_id")
public String getEventId() {
return this.eventId;
}

public VerifyStatus getStatus() {
return this.status;
}

public BigDecimal getPrice() {
return this.price;
}

public String getCurrency() {
return this.currency;
}

@JsonProperty("error_text")
public String getErrorText() {
return this.errorText;
}

public static CheckResponse fromJson(String json) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, CheckResponse.class);
} catch (JsonMappingException jme) {
throw new NexmoResponseParseException("Failed to produce CheckResponse from json.", jme);
} catch (IOException jpe) {
throw new NexmoUnexpectedException("Failed to produce CheckResponse from json.", jpe);
}
}
}
45 changes: 31 additions & 14 deletions src/main/java/com/nexmo/client/verify/CheckResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,42 @@
*/
package com.nexmo.client.verify;


/**
* Verification check result.
*
* @author Daniele Ricci
* @deprecated Relies on XML Endpoint, use {@link CheckResponse}
*/
@Deprecated
public class CheckResult extends BaseResult {
private final String eventId;
private final float price;
private final String currency;

public CheckResult(final int status,
final String eventId,
final float price,
final String currency,
final String errorText,
final boolean temporaryError) {
private String requestId;
private String eventId;
private float price;
private String currency;

public CheckResult(int status,
String eventId,
float price,
String currency,
String errorText,
boolean temporaryError) {
super(status, errorText, temporaryError);
this.eventId = eventId;
this.price = price;
this.currency = currency;
}

public CheckResult(int status,
String requestId,
String eventId,
float price,
String currency,
String errorText,
boolean temporaryError) {
super(status, errorText, temporaryError);
this.requestId = requestId;
this.eventId = eventId;
this.price = price;
this.currency = currency;
}

public String getEventId() {
return this.eventId;
}
Expand All @@ -56,4 +69,8 @@ public String getCurrency() {
return this.currency;
}

public String getRequestId() {
return this.requestId;
}

}
Loading

0 comments on commit 7b0a41f

Please sign in to comment.