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.sms;
017
018import com.vonage.client.*;
019import com.vonage.client.auth.ApiKeyHeaderAuthMethod;
020import com.vonage.client.auth.ApiKeyQueryParamsAuthMethod;
021import com.vonage.client.auth.SignatureAuthMethod;
022import com.vonage.client.common.HttpMethod;
023import com.vonage.client.sms.messages.Message;
024
025
026/**
027 * A client for talking to the Vonage Voice API. The standard way to obtain an instance of this class
028 * is to use {@link VonageClient#getSmsClient()}.
029 */
030public class SmsClient {
031    final RestEndpoint<Message, SmsSubmissionResponse> sendMessage;
032
033    /**
034     * Create a new SmsClient.
035     *
036     * @param wrapper Http Wrapper used to create a Sms Request
037     */
038    public SmsClient(HttpWrapper wrapper) {
039        @SuppressWarnings("unchecked")
040        class Endpoint extends DynamicEndpoint<Message, SmsSubmissionResponse> {
041            Endpoint() {
042                super(DynamicEndpoint.<Message, SmsSubmissionResponse> builder(SmsSubmissionResponse.class)
043                        .wrapper(wrapper).requestMethod(HttpMethod.POST)
044                        .authMethod(SignatureAuthMethod.class, ApiKeyHeaderAuthMethod.class)
045                        .urlFormEncodedContentType(true).pathGetter((de, req) ->
046                                de.getHttpWrapper().getHttpConfig().getRestBaseUri() + "/sms/json"
047                        )
048                );
049            }
050        }
051        sendMessage = new Endpoint();
052    }
053
054    /**
055     * Send an SMS message.
056     * <p>
057     * This uses the supplied object to construct a request and post it to the Vonage API.<br> This method will respond
058     * with an SmsSubmissionResponse object. Depending on the nature and length of the submitted message, Vonage may
059     * automatically split the message into multiple sms messages in order to deliver to the handset. For example, a
060     * long text sms of greater than 160 chars will need to be split into multiple 'concatenated' sms messages. The
061     * Vonage service will handle this automatically for you.<br> The messages are stored as a Collection of
062     * SmsSubmissionResponseMessage objects on the SmsSubmissionResponse object. Each message can potentially have a
063     * different status result, and each message will have a different message id. Delivery notifications will be
064     * generated for each sms message within this set and will be posted to your application containing the appropriate
065     * message id.
066     *
067     * @param message The message request object that describes the type of message and the contents to be submitted.
068     *
069     * @return SmsSubmissionResponse an object containing a collection of SmsSubmissionResponseMessage objects for each
070     * actual sms that was required to submit the message.
071     *
072     * @throws VonageClientException        if there was a problem with the Vonage request or response objects.
073     * @throws VonageResponseParseException if the response from the API could not be parsed.
074     */
075    public SmsSubmissionResponse submitMessage(Message message) throws VonageResponseParseException, VonageClientException {
076        return sendMessage.execute(message);
077    }
078}