001/* 002 * Copyright 2024 Vonage 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.vonage.client.insight; 017 018import java.util.Map; 019 020public class AdvancedInsightRequest extends BaseInsightRequest { 021 private final boolean async; 022 private final String callback; 023 private final Boolean realTimeData; 024 025 private AdvancedInsightRequest(Builder builder) { 026 super(builder.number, builder.country); 027 cnam = builder.cnam; 028 callback = builder.callback; 029 if (!(async = builder.async)) { 030 realTimeData = builder.realTimeData; 031 } 032 else { 033 realTimeData = null; 034 if (callback == null || callback.isEmpty()) { 035 throw new IllegalStateException("You must define a callback URL when using asynchronous insights."); 036 } 037 } 038 } 039 040 /** 041 * This method is the starting point for constructing an Advanced Insight 042 * 043 * @param number A single phone number that you need insight about in national or international format. 044 * 045 * @return A new {@link Builder} instance. 046 */ 047 public static Builder builder(String number) { 048 return new Builder(number); 049 } 050 051 /** 052 * This method is the starting point for constructing an Advanced Insight 053 * Note that the number field must be set. 054 * 055 * @return A new {@link Builder} instance. 056 */ 057 public static Builder builder() { 058 return new Builder(); 059 } 060 061 public Boolean getRealTimeData() { 062 return realTimeData; 063 } 064 065 public Boolean getCnam() { 066 return cnam; 067 } 068 069 public boolean isAsync() { 070 return async; 071 } 072 073 public String getCallback() { 074 return callback; 075 } 076 077 @Override 078 public Map<String, String> makeParams() { 079 Map<String, String> params = super.makeParams(); 080 if (callback != null) { 081 params.put("callback", callback); 082 } 083 if (realTimeData != null) { 084 params.put("real_time_data", realTimeData.toString()); 085 } 086 return params; 087 } 088 089 /** 090 * Construct an AdvancedInsightRequest with a number. 091 * 092 * @param number A single phone number that you need insight about in national or international format. 093 * 094 * @return A new AdvancedInsightRequest object. 095 */ 096 public static AdvancedInsightRequest withNumber(String number) { 097 return new Builder(number).build(); 098 } 099 100 /** 101 * Construct a AdvancedInsightRequest with a number and country. 102 * 103 * @param number A single phone number that you need insight about in national or international format. 104 * @param country If a number does not have a country code, or it is uncertain, set the two-character country code. 105 * 106 * @return A new AdvancedInsightRequest object. 107 */ 108 public static AdvancedInsightRequest withNumberAndCountry(String number, String country) { 109 return new Builder(number).country(country).build(); 110 } 111 112 public static class Builder { 113 protected boolean async; 114 protected Boolean cnam, realTimeData; 115 protected String number, country, callback; 116 117 protected Builder(String number) { 118 this.number = number; 119 } 120 121 protected Builder() {} 122 123 /** 124 * @param number A single phone number that you need insight about in national or international format. 125 * This field is REQUIRED. 126 * @return This builder. 127 */ 128 public Builder number(String number) { 129 this.number = number; 130 return this; 131 } 132 133 /** 134 * @param country If a number does not have a country code, or it is uncertain, set the two-character country 135 * code. 136 * 137 * @return This builder. 138 */ 139 public Builder country(String country) { 140 this.country = country; 141 return this; 142 } 143 144 /** 145 * @param cnam Indicates if the name of the person who owns the phone number should also be looked up and 146 * returned. Set to true to receive phone number owner name in the response. This is only available 147 * for US numbers and incurs an additional charge. 148 * 149 * @return This builder. 150 */ 151 public Builder cnam(boolean cnam) { 152 this.cnam = cnam; 153 return this; 154 } 155 156 /** 157 * @param async True if the call should be done asynchronously. When setting this value to true, the {@link 158 * Builder#callback(String)} parameter must also be set. 159 * 160 * @return This builder. 161 */ 162 public Builder async(boolean async) { 163 this.async = async; 164 return this; 165 } 166 167 /** 168 * @param url The URL that Vonage will send a request to when the insight lookup is finished. 169 * This only takes effect when {@link #async(boolean)} is {@code true}. 170 * 171 * @return This builder. 172 */ 173 public Builder callback(String url) { 174 this.callback = url; 175 return this; 176 } 177 178 /** 179 * @param realTimeData A boolean to choose whether to get real time data back in the response. 180 * This only applies when {@link #async(boolean)} is {@code false}. 181 * 182 * @return This builder. 183 */ 184 public Builder realTimeData(boolean realTimeData) { 185 this.realTimeData = realTimeData; 186 return this; 187 } 188 189 /** 190 * @return A new {@link AdvancedInsightRequest} object from the stored builder options. 191 */ 192 public AdvancedInsightRequest build() { 193 return new AdvancedInsightRequest(this); 194 } 195 } 196}