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.insight; 023 024import com.nexmo.client.*; 025 026/** 027 * A client for talking to the Nexmo Number Insight API. The standard way to obtain an instance of this class is to use 028 * {@link NexmoClient#getInsightClient()}. 029 */ 030public class InsightClient extends AbstractClient { 031 protected BasicInsightEndpoint basic; 032 protected StandardInsightEndpoint standard; 033 protected AdvancedInsightEndpoint advanced; 034 035 /** 036 * Constructor. 037 * 038 * @param httpWrapper (required) shared HTTP wrapper object used for making REST calls. 039 */ 040 public InsightClient(HttpWrapper httpWrapper) { 041 super(httpWrapper); 042 043 this.basic = new BasicInsightEndpoint(httpWrapper); 044 this.standard = new StandardInsightEndpoint(httpWrapper); 045 this.advanced = new AdvancedInsightEndpoint(httpWrapper); 046 } 047 048 /** 049 * Perform a Basic Insight Request with a number. 050 * 051 * @param number A single phone number that you need insight about in national or international format. 052 * 053 * @return A {@link BasicInsightResponse} representing the response from the Nexmo Number Insight API. 054 * 055 * @throws NexmoResponseParseException if the response from the API could not be parsed. 056 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 057 */ 058 public BasicInsightResponse getBasicNumberInsight(String number) throws NexmoResponseParseException, NexmoClientException { 059 return getBasicNumberInsight(BasicInsightRequest.withNumber(number)); 060 } 061 062 /** 063 * Perform a Basic Insight Request with a number and country. 064 * 065 * @param number A single phone number that you need insight about in national or international format 066 * @param country If a number does not have a country code or it is uncertain, set the two-character country code. 067 * 068 * @return A {@link BasicInsightResponse} representing the response from the Nexmo Number Insight API. 069 * 070 * @throws NexmoResponseParseException if the response from the API could not be parsed. 071 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 072 */ 073 public BasicInsightResponse getBasicNumberInsight(String number, String country) throws NexmoResponseParseException, NexmoClientException { 074 return getBasicNumberInsight(BasicInsightRequest.withNumberAndCountry(number, country)); 075 } 076 077 /** 078 * Perform a Basic Insight Request with a {@link BasicInsightRequest}. 079 * 080 * @param basicInsightRequest A request object containing the details of the request to make. 081 * 082 * @return A {@link BasicInsightResponse} representing the response from the Nexmo Number Insight API. 083 * 084 * @throws NexmoResponseParseException if the response from the API could not be parsed. 085 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 086 */ 087 public BasicInsightResponse getBasicNumberInsight(BasicInsightRequest basicInsightRequest) throws NexmoResponseParseException, NexmoClientException { 088 return this.basic.execute(basicInsightRequest); 089 } 090 091 /** 092 * Perform a Standard Insight Request with a number. 093 * 094 * @param number A single phone number that you need insight about in national or international format. 095 * 096 * @return A {@link StandardInsightResponse} representing the response from the Nexmo Number Insight API. 097 * 098 * @throws NexmoResponseParseException if the response from the API could not be parsed. 099 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 100 */ 101 public StandardInsightResponse getStandardNumberInsight(String number) throws NexmoResponseParseException, NexmoClientException { 102 return getStandardNumberInsight(StandardInsightRequest.withNumber(number)); 103 } 104 105 /** 106 * Perform a Standard Insight Request with a number and country. 107 * 108 * @param number A single phone number that you need insight about in national or international format. 109 * @param country If a number does not have a country code or it is uncertain, set the two-character country code. 110 * 111 * @return A {@link StandardInsightResponse} representing the response from the Nexmo Number Insight API. 112 * 113 * @throws NexmoResponseParseException if the response from the API could not be parsed. 114 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 115 */ 116 public StandardInsightResponse getStandardNumberInsight(String number, String country) throws NexmoResponseParseException, NexmoClientException { 117 return getStandardNumberInsight(StandardInsightRequest.withNumberAndCountry(number, country)); 118 } 119 120 /** 121 * Perform a Standard Insight Request with a number, country, and cnam. 122 * 123 * @param number A single phone number that you need insight about in national or international format. 124 * @param country If a number does not have a country code or it is uncertain, set the two-character country code. 125 * @param cnam Indicates if the name of the person who owns the phone number should also be looked up and 126 * returned. Set to true to receive phone number owner name in the response. This is only available 127 * for US numbers and incurs an additional charge. 128 * 129 * @return A {@link StandardInsightResponse} representing the response from the Nexmo Number Insight API. 130 * 131 * @throws NexmoResponseParseException if the response from the API could not be parsed. 132 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 133 * @deprecated Create a {@link StandardInsightRequest} and use {@link InsightClient#getStandardNumberInsight(StandardInsightRequest)} 134 */ 135 @Deprecated 136 public StandardInsightResponse getStandardNumberInsight(String number, String country, boolean cnam) throws NexmoResponseParseException, NexmoClientException { 137 return getStandardNumberInsight(StandardInsightRequest.builder(number).country(country).cnam(cnam).build()); 138 } 139 140 /** 141 * Perform a Standard Insight Request with a {@link StandardInsightRequest}. 142 * 143 * @param standardInsightRequest A request object containing the details of the request to make. 144 * 145 * @return A {@link StandardInsightResponse} representing the response from the Nexmo Number Insight API. 146 * 147 * @throws NexmoResponseParseException if the response from the API could not be parsed. 148 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 149 */ 150 public StandardInsightResponse getStandardNumberInsight(StandardInsightRequest standardInsightRequest) throws NexmoResponseParseException, NexmoClientException { 151 return this.standard.execute(standardInsightRequest); 152 } 153 154 /** 155 * Perform an Advanced Insight Request with a number. 156 * 157 * @param number A single phone number that you need insight about in national or international format. 158 * 159 * @return A {@link AdvancedInsightResponse} representing the response from the Nexmo Number Insight API. 160 * 161 * @throws NexmoResponseParseException if the response from the API could not be parsed. 162 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 163 */ 164 public AdvancedInsightResponse getAdvancedNumberInsight(String number) throws NexmoResponseParseException, NexmoClientException { 165 return getAdvancedNumberInsight(AdvancedInsightRequest.withNumber(number)); 166 } 167 168 /** 169 * Perform an Advanced Insight Request with a number and country. 170 * 171 * @param number A single phone number that you need insight about in national or international format. 172 * @param country If a number does not have a country code or it is uncertain, set the two-character country code. 173 * 174 * @return A {@link AdvancedInsightResponse} representing the response from the Nexmo Number Insight API. 175 * 176 * @throws NexmoResponseParseException if the response from the API could not be parsed. 177 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 178 */ 179 public AdvancedInsightResponse getAdvancedNumberInsight(String number, String country) throws NexmoResponseParseException, NexmoClientException { 180 return getAdvancedNumberInsight(AdvancedInsightRequest.withNumberAndCountry(number, country)); 181 } 182 183 /** 184 * Perform an Advanced Insight Request with a number, country, and ipAddress. 185 * 186 * @param number A single phone number that you need insight about in national or international format. 187 * @param country If a number does not have a country code or it is uncertain, set the two-character country 188 * code. 189 * @param ipAddress The IP address of the user. If supplied, we will compare this to the country the user's phone is 190 * located in and return an error if it does not match. 191 * 192 * @return A {@link AdvancedInsightResponse} representing the response from the Nexmo Number Insight API. 193 * 194 * @throws NexmoResponseParseException if the response from the API could not be parsed. 195 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 196 * @deprecated Create a {@link AdvancedInsightRequest} and use {@link InsightClient#getAdvancedNumberInsight(AdvancedInsightRequest)} 197 */ 198 @Deprecated 199 public AdvancedInsightResponse getAdvancedNumberInsight(String number, String country, String ipAddress) throws NexmoResponseParseException, NexmoClientException { 200 return getAdvancedNumberInsight(AdvancedInsightRequest.builder(number) 201 .country(country) 202 .ipAddress(ipAddress) 203 .build()); 204 } 205 206 /** 207 * Perform an Advanced Insight Request with a number, country, ipAddress, and cnam. 208 * 209 * @param number A single phone number that you need insight about in national or international format. 210 * @param country If a number does not have a country code or it is uncertain, set the two-character country 211 * code. 212 * @param ipAddress The IP address of the user. If supplied, we will compare this to the country the user's phone is 213 * located in and return an error if it does not match. 214 * @param cnam Indicates if the name of the person who owns the phone number should also be looked up and 215 * returned. Set to true to receive phone number owner name in the response. This is only available 216 * for US numbers and incurs an additional charge. 217 * 218 * @return A {@link AdvancedInsightResponse} representing the response from the Nexmo Number Insight API. 219 * 220 * @throws NexmoResponseParseException if the response from the API could not be parsed. 221 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 222 * @deprecated Create a {@link AdvancedInsightRequest} and use {@link InsightClient#getAdvancedNumberInsight(AdvancedInsightRequest)} 223 */ 224 @Deprecated 225 public AdvancedInsightResponse getAdvancedNumberInsight(String number, String country, String ipAddress, boolean cnam) throws NexmoResponseParseException, NexmoClientException { 226 return getAdvancedNumberInsight(AdvancedInsightRequest.builder(number) 227 .country(country) 228 .ipAddress(ipAddress) 229 .cnam(cnam) 230 .build()); 231 } 232 233 /** 234 * Perform an Advanced Insight Request with a {@link AdvancedInsightRequest}. 235 * 236 * @param advancedInsightRequest A request object containing the details of the request to make. 237 * 238 * @return A {@link AdvancedInsightResponse} representing the response from the Nexmo Number Insight API. 239 * 240 * @throws NexmoResponseParseException if the response from the API could not be parsed. 241 * @throws NexmoClientException if there was a problem with the Nexmo request or response objects. 242 */ 243 public AdvancedInsightResponse getAdvancedNumberInsight(AdvancedInsightRequest advancedInsightRequest) throws NexmoResponseParseException, NexmoClientException { 244 return this.advanced.execute(advancedInsightRequest); 245 } 246}