-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from Nexmo/convert-verify-to-json-part-1
Convert Verify to JSON
- Loading branch information
Showing
34 changed files
with
1,993 additions
and
515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.