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