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.conversion;
017
018import com.vonage.client.*;
019import com.vonage.client.auth.ApiKeyQueryParamsAuthMethod;
020import com.vonage.client.auth.SignatureAuthMethod;
021import com.vonage.client.common.HttpMethod;
022import java.util.Date;
023
024/**
025 * A client for talking to the Vonage Conversion API. The standard way to obtain an instance of this class is to use
026 * {@link VonageClient#getConversionClient()}.
027 * <p>
028 * Allows you to tell Vonage about the reliability of your 2FA communications.
029 * <p>
030 * See the <a href="https://developer.vonage.com/messaging/conversion-api/overview">Conversion API documentation</a>.
031 */
032public class ConversionClient {
033    final RestEndpoint<ConversionRequest, Void> conversionEndpoint;
034
035    @SuppressWarnings("unchecked")
036    public ConversionClient(HttpWrapper wrapper) {
037        conversionEndpoint = DynamicEndpoint.<ConversionRequest, Void> builder(Void.class)
038                .pathGetter((de, req) -> de.getHttpWrapper().getHttpConfig().getApiBaseUri() +
039                        "/conversions/" + req.getType().name().toLowerCase()
040                )
041                .authMethod(SignatureAuthMethod.class, ApiKeyQueryParamsAuthMethod.class)
042                .requestMethod(HttpMethod.POST).wrapper(wrapper).build();
043    }
044
045    /**
046     * Submit a request to the Conversion API indicating whether or not a message was delivered.
047     *
048     * @param type      The {@link ConversionRequest.Type} type of com.vonage.client.conversion.
049     * @param messageId The id of the message that was sent.
050     * @param delivered A boolean indicating whether or not it was delivered.
051     * @param timestamp A timestamp of when it was known to be delivered.
052     *
053     * @throws VonageClientException        if there was a problem with the Vonage request or response objects.
054     * @throws VonageResponseParseException if the response from the API could not be parsed.
055     */
056    public void submitConversion(ConversionRequest.Type type,
057                                 String messageId,
058                                 boolean delivered,
059                                 Date timestamp) throws VonageResponseParseException, VonageClientException {
060        conversionEndpoint.execute(new ConversionRequest(type, messageId, delivered, timestamp));
061    }
062}