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;
023
024
025import com.nexmo.client.account.AccountClient;
026import com.nexmo.client.applications.ApplicationClient;
027import com.nexmo.client.auth.AuthMethod;
028import com.nexmo.client.auth.JWTAuthMethod;
029import com.nexmo.client.auth.NexmoUnacceptableAuthException;
030import com.nexmo.client.insight.InsightClient;
031import com.nexmo.client.numbers.NumbersClient;
032import com.nexmo.client.sms.SmsClient;
033import com.nexmo.client.sns.SnsClient;
034import com.nexmo.client.verify.VerifyClient;
035import com.nexmo.client.voice.VoiceClient;
036import org.apache.http.client.HttpClient;
037
038/**
039 * Top-level Nexmo API client object.
040 * <p>
041 * Construct an instance of this object with one or more {@link AuthMethod}s (providing all the authentication methods
042 * for the APIs you wish to use), and then call {@link #getVoiceClient()} to obtain a client for the Nexmo Voice API.
043 * <p>
044 * Currently this object only constructs and provides access to {@link VoiceClient}. In the future it will manage
045 * clients for all of the Nexmo APIs.
046 */
047public class NexmoClient {
048    private final AccountClient account;
049    private final ApplicationClient application;
050    private final InsightClient insight;
051    private final NumbersClient numbers;
052    private final SmsClient sms;
053    private final VoiceClient voice;
054    private final VerifyClient verify;
055    private final SnsClient sns;
056
057    private HttpWrapper httpWrapper;
058
059    public NexmoClient(AuthMethod... authMethods) {
060        this.httpWrapper = new HttpWrapper(authMethods);
061
062        this.account = new AccountClient(this.httpWrapper);
063        this.application = new ApplicationClient(this.httpWrapper);
064        this.insight = new InsightClient(this.httpWrapper);
065        this.numbers = new NumbersClient(this.httpWrapper);
066        this.verify = new VerifyClient(this.httpWrapper);
067        this.voice = new VoiceClient(this.httpWrapper);
068        this.sms = new SmsClient(this.httpWrapper);
069        this.sns = new SnsClient(this.httpWrapper);
070    }
071
072    /**
073     * Provide an HttpClient that will be used to make requests to the Nexmo API.
074     * <p>
075     * This can be useful, for example, if you must use an HTTP proxy to make requests.
076     *
077     * @param client A custom-configured HttpClient instance.
078     */
079    public void setHttpClient(HttpClient client) {
080        this.httpWrapper.setHttpClient(client);
081    }
082
083    public AccountClient getAccountClient() {
084        return this.account;
085    }
086
087    public ApplicationClient getApplicationClient() {
088        return this.application;
089    }
090
091    public InsightClient getInsightClient() {
092        return this.insight;
093    }
094
095    public NumbersClient getNumbersClient() {
096        return this.numbers;
097    }
098
099    public SmsClient getSmsClient() {
100        return this.sms;
101    }
102
103    public SnsClient getSnsClient() {
104        return this.sns;
105    }
106
107    public VerifyClient getVerifyClient() {
108        return this.verify;
109    }
110
111    public VoiceClient getVoiceClient() {
112        return this.voice;
113    }
114
115    /**
116     * Generate a JWT for the application the client has been configured with.
117     *
118     * @return A String containing the token data.
119     * @throws NexmoUnacceptableAuthException if no {@link JWTAuthMethod} is available
120     */
121    public String generateJwt() throws NexmoUnacceptableAuthException {
122        JWTAuthMethod authMethod = this.httpWrapper.getAuthCollection().getAuth(JWTAuthMethod.class);
123        return authMethod.constructToken(System.currentTimeMillis() / 1000L, JWTAuthMethod.constructJTI());
124    }
125}