001/*
002 * Copyright (c) 2011-2017 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.account;
023
024import com.nexmo.client.*;
025
026/**
027 * A client for talking to the Nexmo Account API. The standard way to obtain an instance of this class is to use {@link
028 * NexmoClient#getAccountClient()} ()}.
029 */
030public class AccountClient extends AbstractClient {
031    protected BalanceEndpoint balance;
032    protected PricingEndpoint pricing;
033    protected PrefixPricingEndpoint prefixPricing;
034    protected TopUpEndpoint topUp;
035    protected SecretManagementEndpoint secret;
036    protected SettingsEndpoint settings;
037
038    /**
039     * Constructor.
040     *
041     * @param httpWrapper (required) shared HTTP wrapper object used for making REST calls.
042     */
043    public AccountClient(HttpWrapper httpWrapper) {
044        super(httpWrapper);
045
046        this.balance = new BalanceEndpoint(httpWrapper);
047        this.pricing = new PricingEndpoint(httpWrapper);
048        this.prefixPricing = new PrefixPricingEndpoint(httpWrapper);
049        this.topUp = new TopUpEndpoint(httpWrapper);
050        this.secret = new SecretManagementEndpoint(httpWrapper);
051        this.settings = new SettingsEndpoint(httpWrapper);
052    }
053
054    public BalanceResponse getBalance() throws NexmoResponseParseException, NexmoClientException {
055        return this.balance.execute();
056    }
057
058    /**
059     * Retrieve the voice pricing for a specified country.
060     *
061     * @param country The two-character country code for which you would like to retrieve pricing.
062     *
063     * @return PricingResponse object which contains the results from the API.
064     *
065     * @throws NexmoResponseParseException if the response from the API could not be parsed.
066     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
067     */
068    public PricingResponse getVoicePrice(String country) throws NexmoResponseParseException, NexmoClientException {
069        return getVoicePrice(new PricingRequest(country));
070    }
071
072    private PricingResponse getVoicePrice(PricingRequest pricingRequest) throws NexmoResponseParseException, NexmoClientException {
073        return this.pricing.getPrice(ServiceType.VOICE, pricingRequest);
074    }
075
076    /**
077     * Retrieve the SMS pricing for a specified country.
078     *
079     * @param country The two-character country code for which you would like to retrieve pricing.
080     *
081     * @return PricingResponse object which contains the results from the API.
082     *
083     * @throws NexmoResponseParseException if the response from the API could not be parsed.
084     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
085     */
086    public PricingResponse getSmsPrice(String country) throws NexmoResponseParseException, NexmoClientException {
087        return getSmsPrice(new PricingRequest(country));
088    }
089
090    private PricingResponse getSmsPrice(PricingRequest pricingRequest) throws NexmoResponseParseException, NexmoClientException {
091        return this.pricing.getPrice(ServiceType.SMS, pricingRequest);
092    }
093
094    /**
095     * Retrieve the pricing for a specified prefix.
096     *
097     * @param type   The type of service to retrieve pricing for.
098     * @param prefix The prefix to retrieve the pricing for.
099     *
100     * @return PrefixPricingResponse object which contains the results from the API.
101     *
102     * @throws NexmoResponseParseException if the response from the API could not be parsed.
103     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
104     */
105    public PrefixPricingResponse getPrefixPrice(ServiceType type, String prefix) throws NexmoResponseParseException, NexmoClientException {
106        return getPrefixPrice(new PrefixPricingRequest(type, prefix));
107    }
108
109    private PrefixPricingResponse getPrefixPrice(PrefixPricingRequest prefixPricingRequest) throws NexmoResponseParseException, NexmoClientException {
110        return this.prefixPricing.getPrice(prefixPricingRequest);
111    }
112
113    /**
114     * Top-up your account when you have enabled auto-reload in the dashboard. Amount added is based on your initial
115     * reload-enabled payment.
116     *
117     * @param transaction The ID associated with your original auto-reload transaction
118     *
119     * @throws NexmoResponseParseException if the response from the API could not be parsed.
120     * @throws NexmoClientException        if there was a problem with the Nexmo request or response object indicating
121     *                                     that the request was unsuccessful.
122     */
123    public void topUp(String transaction) throws NexmoResponseParseException, NexmoClientException {
124        topUp(new TopUpRequest(transaction));
125    }
126
127    private void topUp(TopUpRequest request) throws NexmoResponseParseException, NexmoClientException {
128        this.topUp.topUp(request);
129    }
130
131    /**
132     * List the ID of each secret associated to the given API key.
133     *
134     * @param apiKey The API key to look up secrets for.
135     *
136     * @return ListSecretsResponse object which contains the results from the API.
137     *
138     * @throws NexmoResponseParseException if a network error occurred contacting the Nexmo Account API
139     * @throws NexmoClientException        if there was a problem with the Nexmo request or response object indicating
140     *                                     that the request was unsuccessful.
141     */
142    public ListSecretsResponse listSecrets(String apiKey) throws NexmoResponseParseException, NexmoClientException {
143        return this.secret.listSecrets(apiKey);
144    }
145
146    /**
147     * Get information for a specific secret id associated to a given API key.
148     *
149     * @param apiKey   The API key that the secret is associated to.
150     * @param secretId The id of the secret to get information on.
151     *
152     * @return SecretResponse object which contains the results from the API.
153     *
154     * @throws NexmoResponseParseException if a network error occurred contacting the Nexmo Account API
155     * @throws NexmoClientException        if there was a problem with the Nexmo request or response object indicating
156     *                                     that the request was unsuccessful.
157     */
158    public SecretResponse getSecret(String apiKey, String secretId) throws NexmoResponseParseException, NexmoClientException {
159        return getSecret(new SecretRequest(apiKey, secretId));
160    }
161
162    private SecretResponse getSecret(SecretRequest secretRequest) throws NexmoResponseParseException, NexmoClientException {
163        return this.secret.getSecret(secretRequest);
164    }
165
166    /**
167     * Create a secret to be used with a specific API key.
168     *
169     * @param apiKey The API key that the secret is to be used with.
170     * @param secret The contents of the secret.
171     *
172     * @return SecretResponse object which contains the created secret from the API.
173     *
174     * @throws NexmoResponseParseException if a network error occurred contacting the Nexmo Account API
175     * @throws NexmoClientException        if there was a problem with the Nexmo request or response object indicating
176     *                                     that the request was unsuccessful.
177     */
178    public SecretResponse createSecret(String apiKey, String secret) throws NexmoResponseParseException, NexmoClientException {
179        return createSecret(new CreateSecretRequest(apiKey, secret));
180    }
181
182    private SecretResponse createSecret(CreateSecretRequest createSecretRequest) throws NexmoResponseParseException, NexmoClientException {
183        return this.secret.createSecret(createSecretRequest);
184    }
185
186    /**
187     * Revoke a secret associated with a specific API key.
188     *
189     * @param apiKey   The API key that the secret is associated to.
190     * @param secretId The id of the secret to revoke.
191     *
192     * @throws NexmoResponseParseException if a network error occurred contacting the Nexmo Account API
193     * @throws NexmoClientException        if there was a problem with the Nexmo request or response object indicating
194     *                                     that the request was unsuccessful.
195     */
196    public void revokeSecret(String apiKey, String secretId) throws NexmoResponseParseException, NexmoClientException {
197        revokeSecret(new SecretRequest(apiKey, secretId));
198    }
199
200    private void revokeSecret(SecretRequest secretRequest) throws NexmoResponseParseException, NexmoClientException {
201        this.secret.revokeSecret(secretRequest);
202    }
203
204    /**
205     * @param url The new incoming sms webhook url to associate to your account.
206     *
207     * @return A {@link SettingsResponse} containing the newly-updated account settings.
208     *
209     * @throws NexmoResponseParseException if a network error occurred contacting the Nexmo Account API
210     * @throws NexmoClientException        if there was a problem with the Nexmo request or response object indicating
211     *                                     that the request was unsuccessful.
212     */
213    public SettingsResponse updateSmsIncomingUrl(String url) throws NexmoResponseParseException, NexmoClientException {
214        return this.updateSettings(SettingsRequest.withIncomingSmsUrl(url));
215    }
216
217    /**
218     * @param url The new delivery receipt webhook url to associate to your account.
219     *
220     * @return A {@link SettingsResponse} containing the newly-updated account settings.
221     *
222     * @throws NexmoResponseParseException if a network error occurred contacting the Nexmo Account API
223     * @throws NexmoClientException        if there was a problem with the Nexmo request or response object indicating
224     *                                     that the request was unsuccessful.
225     */
226    public SettingsResponse updateDeliveryReceiptUrl(String url) throws NexmoResponseParseException, NexmoClientException {
227        return this.updateSettings(SettingsRequest.withDeliveryReceiptUrl(url));
228    }
229
230    /**
231     * @param request The {@link SettingsRequest} containing the fields to update.
232     *
233     * @return A {@link SettingsResponse} containing the newly-updated account settings.
234     *
235     * @throws NexmoResponseParseException if a network error occurred contacting the Nexmo Account API
236     * @throws NexmoClientException        if there was a problem with the Nexmo request or response object indicating
237     *                                     that the request was unsuccessful.
238     */
239    public SettingsResponse updateSettings(SettingsRequest request) throws NexmoResponseParseException, NexmoClientException {
240        return this.settings.updateSettings(request);
241    }
242}