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.AbstractClient;
025import com.nexmo.client.HttpWrapper;
026import com.nexmo.client.NexmoClient;
027import com.nexmo.client.NexmoClientException;
028
029import java.io.IOException;
030
031/**
032 * A client for talking to the Nexmo Number Insight API. The standard way to obtain an instance of this class is to use
033 * {@link NexmoClient#getInsightClient()}.
034 */
035public class AccountClient extends AbstractClient {
036    protected BalanceEndpoint balance;
037    protected PricingEndpoint pricing;
038    protected PrefixPricingEndpoint prefixPricing;
039    protected TopUpEndpoint topUp;
040
041    /**
042     * Constructor.
043     *
044     * @param httpWrapper (required) shared HTTP wrapper object used for making REST calls.
045     */
046    public AccountClient(HttpWrapper httpWrapper) {
047        super(httpWrapper);
048
049        this.balance = new BalanceEndpoint(httpWrapper);
050        this.pricing = new PricingEndpoint(httpWrapper);
051        this.prefixPricing = new PrefixPricingEndpoint(httpWrapper);
052        this.topUp = new TopUpEndpoint(httpWrapper);
053    }
054
055    public BalanceResponse getBalance() throws IOException, NexmoClientException {
056        return this.balance.execute();
057    }
058
059    /**
060     * Retrieve the voice pricing for a specified country.
061     *
062     * @param country The two-character country code for which you would like to retrieve pricing.
063     * @return PricingResponse object which contains the results from the API.
064     * @throws IOException          if a network error occurred contacting the Nexmo Account API.
065     * @throws NexmoClientException if there was a problem with the Nexmo request or response objects.
066     */
067    public PricingResponse getVoicePrice(String country) throws IOException, NexmoClientException {
068        return getVoicePrice(new PricingRequest(country));
069    }
070
071    private PricingResponse getVoicePrice(PricingRequest pricingRequest) throws IOException, NexmoClientException {
072        return this.pricing.getPrice(ServiceType.VOICE, pricingRequest);
073    }
074
075    /**
076     * Retrieve the SMS pricing for a specified country.
077     *
078     * @param country The two-character country code for which you would like to retrieve pricing.
079     * @return PricingResponse object which contains the results from the API.
080     * @throws IOException          if a network error occurred contacting the Nexmo Account API.
081     * @throws NexmoClientException if there was a problem with the Nexmo request or response objects.
082     */
083    public PricingResponse getSmsPrice(String country) throws IOException, NexmoClientException {
084        return getSmsPrice(new PricingRequest(country));
085    }
086
087    private PricingResponse getSmsPrice(PricingRequest pricingRequest) throws IOException, NexmoClientException {
088        return this.pricing.getPrice(ServiceType.SMS, pricingRequest);
089    }
090
091    /**
092     * Retrieve the pricing for a specified prefix.
093     *
094     * @param type   The type of service to retrieve pricing for.
095     * @param prefix The prefix to retrieve the pricing for.
096     * @return PrefixPricingResponse object which contains the results from the API.
097     * @throws IOException          if a network error occurred contacting the Nexmo Account API.
098     * @throws NexmoClientException if there was a problem with the Nexmo request or response objects.
099     */
100    public PrefixPricingResponse getPrefixPrice(ServiceType type,
101                                                String prefix) throws IOException, NexmoClientException {
102        return getPrefixPrice(new PrefixPricingRequest(type, prefix));
103    }
104
105    private PrefixPricingResponse getPrefixPrice(PrefixPricingRequest prefixPricingRequest) throws IOException, NexmoClientException {
106        return this.prefixPricing.getPrice(prefixPricingRequest);
107    }
108
109    /**
110     * Top-up your account when you have enabled auto-reload in the dashboard. Amount added is based on your initial
111     * reload-enabled payment.
112     *
113     * @param transaction The ID associated with your original auto-reload transaction
114     * @throws IOException          if a network error occurred contacting the Nexmo Account API.
115     * @throws NexmoClientException if there was a problem with the Nexmo request or response object indicating that
116     *                              the request was unsuccessful.
117     */
118    public void topUp(String transaction) throws IOException, NexmoClientException {
119        topUp(new TopUpRequest(transaction));
120    }
121
122    private void topUp(TopUpRequest request) throws IOException, NexmoClientException {
123        this.topUp.topUp(request);
124    }
125}