001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.redact;
017
018import com.vonage.client.*;
019import com.vonage.client.auth.ApiKeyHeaderAuthMethod;
020import com.vonage.client.common.HttpMethod;
021
022/**
023 * A client for talking to the Vonage Redact API. The standard way to obtain an instance of this class is to use {@link
024 * VonageClient#getRedactClient()}.
025 */
026public class RedactClient {
027    final RestEndpoint<RedactRequest, Void> redactTransaction;
028
029    public RedactClient(HttpWrapper wrapper) {
030        @SuppressWarnings("unchecked")
031        final class Endpoint<T, R> extends DynamicEndpoint<T, R> {
032            Endpoint(R... type) {
033                super(DynamicEndpoint.<T, R> builder(type)
034                        .wrapper(wrapper).requestMethod(HttpMethod.POST)
035                        .responseExceptionType(RedactResponseException.class)
036                        .authMethod(ApiKeyHeaderAuthMethod.class)
037                        .pathGetter((de, req) -> de.getHttpWrapper().getHttpConfig()
038                                .getApiBaseUri() + "/v1/redact/transaction"
039                        )
040                );
041            }
042        }
043        redactTransaction = new Endpoint<>();
044    }
045
046    /**
047     * Submit a request to the Redact API to redact a transaction.
048     *
049     * @param id      The transaction id to redact.
050     * @param product The {@link RedactRequest.Product} which corresponds to the transaction.
051     *
052     * @throws VonageClientException        if there was a problem with the Vonage request or response objects.
053     * @throws VonageResponseParseException if the response from the API could not be parsed.
054     */
055    public void redactTransaction(String id, RedactRequest.Product product) throws VonageResponseParseException, VonageClientException {
056        redactTransaction(new RedactRequest(id, product));
057    }
058
059    /**
060     * Submit a request to the Redact API to redact a transaction.
061     *
062     * @param id      The transaction id to redact.
063     * @param product The {@link RedactRequest.Product} which corresponds to the transaction.
064     * @param type    The {@link RedactRequest.Type} which is required if redacting SMS data.
065     *
066     * @throws VonageClientException        if there was a problem with the Vonage request or response objects.
067     * @throws VonageResponseParseException if the response from the API could not be parsed.
068     */
069    public void redactTransaction(String id, RedactRequest.Product product, RedactRequest.Type type) throws VonageResponseParseException, VonageClientException {
070        RedactRequest request = new RedactRequest(id, product);
071        request.setType(type);
072        redactTransaction(request);
073    }
074
075    /**
076     * Submit a request to the Redact API to redact a transaction.
077     *
078     * @param redactRequest a {@link RedactRequest} object which contains the request parameters.
079     *
080     * @throws VonageClientException        if there was a problem with the Vonage request or response objects.
081     * @throws VonageResponseParseException if the response from the API could not be parsed.
082     */
083    public void redactTransaction(RedactRequest redactRequest) throws VonageResponseParseException, VonageClientException {
084        if (redactRequest.getId() == null || redactRequest.getProduct() == null) {
085            throw new IllegalArgumentException("Redact transaction id and product are required.");
086        }
087        if (redactRequest.getProduct() == RedactRequest.Product.SMS && redactRequest.getType() == null) {
088            throw new IllegalArgumentException("Redacting SMS requires a type.");
089        }
090        redactTransaction.execute(redactRequest);
091    }
092}