|
| 1 | +package com.yoti.api.client.sandbox.docs; |
| 2 | + |
| 3 | +import static com.yoti.api.client.spi.remote.call.YotiConstants.DEFAULT_YOTI_HOST; |
| 4 | +import static com.yoti.api.client.spi.remote.call.YotiConstants.PROPERTY_YOTI_DOCS_URL; |
| 5 | +import static com.yoti.api.client.spi.remote.util.Validation.notNull; |
| 6 | +import static com.yoti.api.client.spi.remote.util.Validation.notNullOrEmpty; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.URISyntaxException; |
| 10 | +import java.security.GeneralSecurityException; |
| 11 | +import java.security.KeyPair; |
| 12 | + |
| 13 | +import com.yoti.api.client.InitialisationException; |
| 14 | +import com.yoti.api.client.KeyPairSource; |
| 15 | +import com.yoti.api.client.sandbox.SandboxException; |
| 16 | +import com.yoti.api.client.sandbox.docs.request.ResponseConfig; |
| 17 | +import com.yoti.api.client.spi.remote.KeyStreamVisitor; |
| 18 | +import com.yoti.api.client.spi.remote.call.HttpMethod; |
| 19 | +import com.yoti.api.client.spi.remote.call.ResourceException; |
| 20 | +import com.yoti.api.client.spi.remote.call.SignedRequest; |
| 21 | +import com.yoti.api.client.spi.remote.call.SignedRequestBuilder; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +public class DocScanSandboxClient { |
| 28 | + |
| 29 | + private static final String DOC_SCAN_SANDBOX_PATH_PREFIX = "/sandbox/idverify/v1"; |
| 30 | + private static final String DEFAULT_DOC_SCAN_SANDBOX_API_URL = DEFAULT_YOTI_HOST + DOC_SCAN_SANDBOX_PATH_PREFIX; |
| 31 | + |
| 32 | + private final String docScanBaseUrl; |
| 33 | + private final ObjectMapper mapper; |
| 34 | + private final String sdkId; |
| 35 | + private final KeyPair keyPair; |
| 36 | + |
| 37 | + DocScanSandboxClient(String sdkId, KeyPair keyPair, ObjectMapper mapper) { |
| 38 | + this.sdkId = sdkId; |
| 39 | + this.keyPair = keyPair; |
| 40 | + this.mapper = mapper; |
| 41 | + this.docScanBaseUrl = System.getProperty(PROPERTY_YOTI_DOCS_URL, DEFAULT_DOC_SCAN_SANDBOX_API_URL); |
| 42 | + } |
| 43 | + |
| 44 | + public static Builder builder() { |
| 45 | + return new Builder(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Configures the response for the given session ID. |
| 50 | + * |
| 51 | + * @param sessionId the session ID |
| 52 | + * @param responseConfig the response configuration |
| 53 | + * @throws SandboxException - if there was a problem configuring the response |
| 54 | + */ |
| 55 | + public void configureSessionResponse(String sessionId, ResponseConfig responseConfig) throws SandboxException { |
| 56 | + String path = String.format("/sessions/%s/response-config", sessionId); |
| 57 | + |
| 58 | + try { |
| 59 | + byte[] body = mapper.writeValueAsBytes(responseConfig); |
| 60 | + |
| 61 | + SignedRequest signedRequest = getSignedRequestBuilder() |
| 62 | + .withBaseUrl(docScanBaseUrl) |
| 63 | + .withEndpoint(path) |
| 64 | + .withKeyPair(keyPair) |
| 65 | + .withHttpMethod(HttpMethod.HTTP_PUT) |
| 66 | + .withPayload(body) |
| 67 | + .withQueryParameter("sdkId", sdkId) |
| 68 | + .build(); |
| 69 | + |
| 70 | + signedRequest.execute(); |
| 71 | + } catch (URISyntaxException | GeneralSecurityException | ResourceException | IOException e) { |
| 72 | + throw new SandboxException(e); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Configures the default response for the application |
| 78 | + * |
| 79 | + * @param sandboxExpectation |
| 80 | + */ |
| 81 | + public void configureApplicationResponse(ResponseConfig sandboxExpectation) throws SandboxException { |
| 82 | + String path = String.format("/apps/%s/response-config", sdkId); |
| 83 | + |
| 84 | + try { |
| 85 | + byte[] body = mapper.writeValueAsBytes(sandboxExpectation); |
| 86 | + |
| 87 | + SignedRequest signedRequest = getSignedRequestBuilder() |
| 88 | + .withBaseUrl(docScanBaseUrl) |
| 89 | + .withEndpoint(path) |
| 90 | + .withKeyPair(keyPair) |
| 91 | + .withHttpMethod(HttpMethod.HTTP_PUT) |
| 92 | + .withPayload(body) |
| 93 | + .build(); |
| 94 | + |
| 95 | + signedRequest.execute(); |
| 96 | + } catch (URISyntaxException | GeneralSecurityException | ResourceException | IOException e) { |
| 97 | + throw new SandboxException(e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + SignedRequestBuilder getSignedRequestBuilder() { |
| 102 | + return SignedRequestBuilder.newInstance(); |
| 103 | + } |
| 104 | + |
| 105 | + public static class Builder { |
| 106 | + |
| 107 | + private static final Logger LOGGER = LoggerFactory.getLogger(Builder.class); |
| 108 | + |
| 109 | + private String sdkId; |
| 110 | + private KeyPair keyPair; |
| 111 | + |
| 112 | + private Builder() { |
| 113 | + } |
| 114 | + |
| 115 | + public Builder withSdkId(String sdkId) { |
| 116 | + this.sdkId = sdkId; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public Builder withKeyPair(KeyPairSource keyPairSource) { |
| 121 | + try { |
| 122 | + LOGGER.debug("Loading key pair from '{}'", keyPairSource); |
| 123 | + this.keyPair = keyPairSource.getFromStream(new KeyStreamVisitor()); |
| 124 | + } catch (IOException e) { |
| 125 | + throw new InitialisationException("Cannot load key pair", e); |
| 126 | + } |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + public DocScanSandboxClient build() { |
| 131 | + notNullOrEmpty(sdkId, "sdkId"); |
| 132 | + notNull(keyPair, "keyPair"); |
| 133 | + |
| 134 | + return new DocScanSandboxClient(sdkId, keyPair, new ObjectMapper()); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments