1919import com .messagebird .objects .MessageResponse ;
2020import com .messagebird .objects .MsgType ;
2121import com .messagebird .objects .PagedPaging ;
22+ import com .messagebird .objects .PhoneNumbersLookup ;
23+ import com .messagebird .objects .PhoneNumbersResponse ;
24+ import com .messagebird .objects .PurchasedNumber ;
25+ import com .messagebird .objects .PurchasedNumberCreatedResponse ;
26+ import com .messagebird .objects .PurchasedNumbersResponse ;
27+ import com .messagebird .objects .PurchasedNumbersFilter ;
2228import com .messagebird .objects .Verify ;
2329import com .messagebird .objects .VerifyRequest ;
2430import com .messagebird .objects .VoiceMessage ;
5460import java .nio .charset .StandardCharsets ;
5561import java .net .URLEncoder ;
5662import java .util .Arrays ;
63+ import java .util .HashMap ;
5764import java .util .LinkedHashMap ;
5865import java .util .LinkedList ;
5966import java .util .List ;
67+ import java .util .Locale ;
6068import java .util .Map ;
6169
6270/**
@@ -87,6 +95,7 @@ public class MessageBirdClient {
8795 private static final String BASE_URL_CONVERSATIONS_WHATSAPP_SANDBOX = "https://whatsapp-sandbox.messagebird.com/v1" ;
8896
8997 static final String VOICE_CALLS_BASE_URL = "https://voice.messagebird.com" ;
98+ static final String NUMBERS_CALLS_BASE_URL = "https://numbers.messagebird.com/v1" ;
9099 private static String [] supportedLanguages = {"de-DE" , "en-AU" , "en-UK" , "en-US" , "es-ES" , "es-LA" , "fr-FR" , "it-IT" , "nl-NL" , "pt-BR" };
91100
92101 private static final String BALANCEPATH = "/balance" ;
@@ -1587,4 +1596,125 @@ private void verifyOffsetAndLimit(Integer offset, Integer limit) {
15871596 throw new IllegalArgumentException ("Limit must be > 0" );
15881597 }
15891598 }
1590- }
1599+
1600+ /**
1601+ * Checks whether a particular country code is a recognized ISO Country.
1602+ *
1603+ * @param countryCode The country code in which the Number should be purchased.
1604+ * @throws IllegalArgumentException for invalid country code
1605+ */
1606+ private void countryCodeIsValid (String countryCode ) throws IllegalArgumentException {
1607+ final boolean isValid = Arrays .asList (Locale .getISOCountries ()).contains (countryCode );
1608+ if (!isValid ) {
1609+ throw new IllegalArgumentException ("Invalid Country Code Provided." );
1610+ }
1611+ }
1612+
1613+ /**
1614+ * Lists Numbers that are available to purchase in a particular country code, without any filters.
1615+ *
1616+ * @param countryCode The country code in which the Number should be purchased.
1617+ * @throws GeneralException general exception
1618+ * @throws UnauthorizedException if client is unauthorized
1619+ * @throws NotFoundException if the resource is missing
1620+ * @throws IllegalArgumentException if the country code provided is invalid
1621+ */
1622+ public PhoneNumbersResponse listNumbersForPurchase (String countryCode ) throws GeneralException , UnauthorizedException , NotFoundException , IllegalArgumentException {
1623+ countryCodeIsValid (countryCode );
1624+ final String url = String .format ("%s/available-phone-numbers" , NUMBERS_CALLS_BASE_URL );
1625+ return messageBirdService .requestByID (url , countryCode , PhoneNumbersResponse .class );
1626+ }
1627+
1628+ /**
1629+ * Lists Numbers that are available to purchase in a particular country code, according to specified search criteria.
1630+ *
1631+ * @param countryCode The country code in which the Number should be purchased.
1632+ * @param params Parameters to filter the resulting phone numbers returned.
1633+ * @throws GeneralException general exception
1634+ * @throws UnauthorizedException if client is unauthorized
1635+ * @throws NotFoundException if the resource is missing
1636+ * @throws IllegalArgumentException if the country code provided is invalid
1637+ */
1638+ public PhoneNumbersResponse listNumbersForPurchase (String countryCode , PhoneNumbersLookup params ) throws GeneralException , UnauthorizedException , NotFoundException , IllegalArgumentException {
1639+ countryCodeIsValid (countryCode );
1640+ final String url = String .format ("%s/available-phone-numbers" , NUMBERS_CALLS_BASE_URL );
1641+ return messageBirdService .requestByID (url , countryCode , params .toHashMap (), PhoneNumbersResponse .class );
1642+ }
1643+
1644+ /**
1645+ * Purchases a phone number. To be used in conjunction with listNumbersForPurchase to identify available numbers.
1646+ *
1647+ * @param number The number to purchase.
1648+ * @param countryCode The country code in which the Number should be purchased.
1649+ * @throws GeneralException general exception
1650+ * @throws UnauthorizedException if client is unauthorized
1651+ * @throws IllegalArgumentException if the country code provided is invalid
1652+ */
1653+ public PurchasedNumberCreatedResponse purchaseNumber (String number , String countryCode , int billingIntervalMonths ) throws UnauthorizedException , GeneralException , IllegalArgumentException {
1654+ countryCodeIsValid (countryCode );
1655+ final String url = String .format ("%s/phone-numbers" , NUMBERS_CALLS_BASE_URL );
1656+ final Map <String , Object > payload = new LinkedHashMap <String , Object >();
1657+ payload .put ("number" , number );
1658+ payload .put ("countryCode" , countryCode );
1659+ if (!Arrays .asList (1 , 3 , 6 , 9 ).contains (billingIntervalMonths )) {
1660+ throw new IllegalArgumentException ("Billing Interval Must Be Either 1, 3, 6, or 9." );
1661+ }
1662+ payload .put ("billingIntervalMonths" , billingIntervalMonths );
1663+
1664+ return messageBirdService .sendPayLoad (url , payload , PurchasedNumberCreatedResponse .class );
1665+ }
1666+
1667+ /**
1668+ * Lists Numbers that were purchased using the account credentials that the client was initialized with.
1669+ *
1670+ * @param filter Filters the list of purchased numbers according to search criteria.
1671+ * @throws UnauthorizedException if client is unauthorized
1672+ * @throws GeneralException general exception
1673+ * @throws NotFoundException if the resource is missing
1674+ */
1675+ public PurchasedNumbersResponse listPurchasedNumbers (PurchasedNumbersFilter filter ) throws UnauthorizedException , GeneralException , NotFoundException {
1676+ final String url = String .format ("%s/phone-numbers" , NUMBERS_CALLS_BASE_URL );
1677+ return messageBirdService .requestByID (url , null , filter .toHashMap (), PurchasedNumbersResponse .class );
1678+ }
1679+
1680+ /**
1681+ * Returns a Number that has already been purchased on the initialized account.
1682+ *
1683+ * @param number The number whose data should be returned.
1684+ * @throws UnauthorizedException if client is unauthorized
1685+ * @throws GeneralException general exception
1686+ * @throws NotFoundException if the Number is missing
1687+ */
1688+ public PurchasedNumber viewPurchasedNumber (String number ) throws UnauthorizedException , GeneralException , NotFoundException {
1689+ final String url = String .format ("%s/phone-numbers" , NUMBERS_CALLS_BASE_URL );
1690+ return messageBirdService .requestByID (url , number , PurchasedNumber .class );
1691+ }
1692+
1693+ /**
1694+ * Updates tags on a particular existing Number. Any number of parameters after the number can be given to apply multiple tags.
1695+ *
1696+ * @param number The number to update.
1697+ * @param tags A tag to apply to the number.
1698+ * @throws UnauthorizedException if client is unauthorized
1699+ * @throws GeneralException general exception
1700+ */
1701+ public PurchasedNumber updateNumber (String number , String ... tags ) throws UnauthorizedException , GeneralException {
1702+ final String url = String .format ("%s/phone-numbers/%s" , NUMBERS_CALLS_BASE_URL , number );
1703+ final Map <String , List <String >> payload = new HashMap <String , List <String >>();
1704+ payload .put ("tags" , Arrays .asList (tags ));
1705+ return messageBirdService .sendPayLoad ("PATCH" , url , payload , PurchasedNumber .class );
1706+ }
1707+
1708+ /**
1709+ * Cancels a particular number.
1710+ *
1711+ * @param number The number to cancel.
1712+ * @throws GeneralException general exception
1713+ * @throws UnauthorizedException if client is unauthorized
1714+ * @throws NotFoundException if the resource is missing
1715+ */
1716+ public void cancelNumber (String number ) throws UnauthorizedException , GeneralException , NotFoundException {
1717+ final String url = String .format ("%s/phone-numbers" , NUMBERS_CALLS_BASE_URL );
1718+ messageBirdService .deleteByID (url , number );
1719+ }
1720+ }
0 commit comments