001/*
002 *   Copyright 2020 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.account;
017
018import com.vonage.client.*;
019
020/**
021 * A client for talking to the Vonage Account API. The standard way to obtain an instance of this class is to use {@link
022 * VonageClient#getAccountClient()} ()}.
023 */
024public class AccountClient extends AbstractClient {
025    protected BalanceEndpoint balance;
026    protected PricingEndpoint pricing;
027    protected PrefixPricingEndpoint prefixPricing;
028    protected TopUpEndpoint topUp;
029    protected SecretManagementEndpoint secret;
030    protected SettingsEndpoint settings;
031
032    /**
033     * Constructor.
034     *
035     * @param httpWrapper (required) shared HTTP wrapper object used for making REST calls.
036     */
037    public AccountClient(HttpWrapper httpWrapper) {
038        super(httpWrapper);
039
040        this.balance = new BalanceEndpoint(httpWrapper);
041        this.pricing = new PricingEndpoint(httpWrapper);
042        this.prefixPricing = new PrefixPricingEndpoint(httpWrapper);
043        this.topUp = new TopUpEndpoint(httpWrapper);
044        this.secret = new SecretManagementEndpoint(httpWrapper);
045        this.settings = new SettingsEndpoint(httpWrapper);
046    }
047
048    public BalanceResponse getBalance() throws VonageResponseParseException, VonageClientException {
049        return this.balance.execute();
050    }
051
052    /**
053     * Retrieve the voice pricing for a specified country.
054     *
055     * @param country The two-character country code for which you would like to retrieve pricing.
056     *
057     * @return PricingResponse object which contains the results from the API.
058     *
059     * @throws VonageResponseParseException if the response from the API could not be parsed.
060     * @throws VonageClientException        if there was a problem with the Vonage request or response objects.
061     */
062    public PricingResponse getVoicePrice(String country) throws VonageResponseParseException, VonageClientException {
063        return getVoicePrice(new PricingRequest(country));
064    }
065
066    private PricingResponse getVoicePrice(PricingRequest pricingRequest) throws VonageResponseParseException, VonageClientException {
067        return this.pricing.getPrice(ServiceType.VOICE, pricingRequest);
068    }
069
070    /**
071     * Retrieve the SMS pricing for a specified country.
072     *
073     * @param country The two-character country code for which you would like to retrieve pricing.
074     *
075     * @return PricingResponse object which contains the results from the API.
076     *
077     * @throws VonageResponseParseException if the response from the API could not be parsed.
078     * @throws VonageClientException        if there was a problem with the Vonage request or response objects.
079     */
080    public PricingResponse getSmsPrice(String country) throws VonageResponseParseException, VonageClientException {
081        return getSmsPrice(new PricingRequest(country));
082    }
083
084    private PricingResponse getSmsPrice(PricingRequest pricingRequest) throws VonageResponseParseException, VonageClientException {
085        return this.pricing.getPrice(ServiceType.SMS, pricingRequest);
086    }
087
088    /**
089     * Retrieve the pricing for a specified prefix.
090     *
091     * @param type   The type of service to retrieve pricing for.
092     * @param prefix The prefix to retrieve the pricing for.
093     *
094     * @return PrefixPricingResponse object which contains the results from the API.
095     *
096     * @throws VonageResponseParseException if the response from the API could not be parsed.
097     * @throws VonageClientException        if there was a problem with the Vonage request or response objects.
098     */
099    public PrefixPricingResponse getPrefixPrice(ServiceType type, String prefix) throws VonageResponseParseException, VonageClientException {
100        return getPrefixPrice(new PrefixPricingRequest(type, prefix));
101    }
102
103    private PrefixPricingResponse getPrefixPrice(PrefixPricingRequest prefixPricingRequest) throws VonageResponseParseException, VonageClientException {
104        return this.prefixPricing.getPrice(prefixPricingRequest);
105    }
106
107    /**
108     * Top-up your account when you have enabled auto-reload in the dashboard. Amount added is based on your initial
109     * reload-enabled payment.
110     *
111     * @param transaction The ID associated with your original auto-reload transaction
112     *
113     * @throws VonageResponseParseException if the response from the API could not be parsed.
114     * @throws VonageClientException        if there was a problem with the Vonage request or response object indicating
115     *                                     that the request was unsuccessful.
116     */
117    public void topUp(String transaction) throws VonageResponseParseException, VonageClientException {
118        topUp(new TopUpRequest(transaction));
119    }
120
121    private void topUp(TopUpRequest request) throws VonageResponseParseException, VonageClientException {
122        this.topUp.topUp(request);
123    }
124
125    /**
126     * List the ID of each secret associated to the given API key.
127     *
128     * @param apiKey The API key to look up secrets for.
129     *
130     * @return ListSecretsResponse object which contains the results from the API.
131     *
132     * @throws VonageResponseParseException if a network error occurred contacting the Vonage Account API
133     * @throws VonageClientException        if there was a problem with the Vonage request or response object indicating
134     *                                     that the request was unsuccessful.
135     */
136    public ListSecretsResponse listSecrets(String apiKey) throws VonageResponseParseException, VonageClientException {
137        return this.secret.listSecrets(apiKey);
138    }
139
140    /**
141     * Get information for a specific secret id associated to a given API key.
142     *
143     * @param apiKey   The API key that the secret is associated to.
144     * @param secretId The id of the secret to get information on.
145     *
146     * @return SecretResponse object which contains the results from the API.
147     *
148     * @throws VonageResponseParseException if a network error occurred contacting the Vonage Account API
149     * @throws VonageClientException        if there was a problem with the Vonage request or response object indicating
150     *                                     that the request was unsuccessful.
151     */
152    public SecretResponse getSecret(String apiKey, String secretId) throws VonageResponseParseException, VonageClientException {
153        return getSecret(new SecretRequest(apiKey, secretId));
154    }
155
156    private SecretResponse getSecret(SecretRequest secretRequest) throws VonageResponseParseException, VonageClientException {
157        return this.secret.getSecret(secretRequest);
158    }
159
160    /**
161     * Create a secret to be used with a specific API key.
162     *
163     * @param apiKey The API key that the secret is to be used with.
164     * @param secret The contents of the secret.
165     *
166     * @return SecretResponse object which contains the created secret from the API.
167     *
168     * @throws VonageResponseParseException if a network error occurred contacting the Vonage Account API
169     * @throws VonageClientException        if there was a problem with the Vonage request or response object indicating
170     *                                     that the request was unsuccessful.
171     */
172    public SecretResponse createSecret(String apiKey, String secret) throws VonageResponseParseException, VonageClientException {
173        return createSecret(new CreateSecretRequest(apiKey, secret));
174    }
175
176    private SecretResponse createSecret(CreateSecretRequest createSecretRequest) throws VonageResponseParseException, VonageClientException {
177        return this.secret.createSecret(createSecretRequest);
178    }
179
180    /**
181     * Revoke a secret associated with a specific API key.
182     *
183     * @param apiKey   The API key that the secret is associated to.
184     * @param secretId The id of the secret to revoke.
185     *
186     * @throws VonageResponseParseException if a network error occurred contacting the Vonage Account API
187     * @throws VonageClientException        if there was a problem with the Vonage request or response object indicating
188     *                                     that the request was unsuccessful.
189     */
190    public void revokeSecret(String apiKey, String secretId) throws VonageResponseParseException, VonageClientException {
191        revokeSecret(new SecretRequest(apiKey, secretId));
192    }
193
194    private void revokeSecret(SecretRequest secretRequest) throws VonageResponseParseException, VonageClientException {
195        this.secret.revokeSecret(secretRequest);
196    }
197
198    /**
199     * @param url The new incoming sms webhook url to associate to your account.
200     *
201     * @return A {@link SettingsResponse} containing the newly-updated account settings.
202     *
203     * @throws VonageResponseParseException if a network error occurred contacting the Vonage Account API
204     * @throws VonageClientException        if there was a problem with the Vonage request or response object indicating
205     *                                     that the request was unsuccessful.
206     */
207    public SettingsResponse updateSmsIncomingUrl(String url) throws VonageResponseParseException, VonageClientException {
208        return this.updateSettings(SettingsRequest.withIncomingSmsUrl(url));
209    }
210
211    /**
212     * @param url The new delivery receipt webhook url to associate to your account.
213     *
214     * @return A {@link SettingsResponse} containing the newly-updated account settings.
215     *
216     * @throws VonageResponseParseException if a network error occurred contacting the Vonage Account API
217     * @throws VonageClientException        if there was a problem with the Vonage request or response object indicating
218     *                                     that the request was unsuccessful.
219     */
220    public SettingsResponse updateDeliveryReceiptUrl(String url) throws VonageResponseParseException, VonageClientException {
221        return this.updateSettings(SettingsRequest.withDeliveryReceiptUrl(url));
222    }
223
224    /**
225     * @param request The {@link SettingsRequest} containing the fields to update.
226     *
227     * @return A {@link SettingsResponse} containing the newly-updated account settings.
228     *
229     * @throws VonageResponseParseException if a network error occurred contacting the Vonage Account API
230     * @throws VonageClientException        if there was a problem with the Vonage request or response object indicating
231     *                                     that the request was unsuccessful.
232     */
233    public SettingsResponse updateSettings(SettingsRequest request) throws VonageResponseParseException, VonageClientException {
234        return this.settings.updateSettings(request);
235    }
236}