6464import java .util .LinkedHashMap ;
6565import java .util .LinkedList ;
6666import java .util .List ;
67+ import java .util .Locale ;
6768import java .util .Map ;
6869
6970/**
@@ -1596,15 +1597,31 @@ private void verifyOffsetAndLimit(Integer offset, Integer limit) {
15961597 }
15971598 }
15981599
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
1605+ */
1606+ private Boolean 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+ return true ;
1612+ }
1613+
15991614 /**
16001615 * Lists Numbers that are available to purchase in a particular country code, without any filters.
16011616 *
16021617 * @param countryCode The country code in which the Number should be purchased.
16031618 * @throws GeneralException general exception
16041619 * @throws UnauthorizedException if client is unauthorized
16051620 * @throws NotFoundException if the resource is missing
1621+ * @throws IllegalArgumentException if the country code provided is invalid
16061622 */
1607- public PhoneNumbersResponse listNumbersForPurchase (String countryCode ) throws GeneralException , UnauthorizedException , NotFoundException {
1623+ public PhoneNumbersResponse listNumbersForPurchase (String countryCode ) throws GeneralException , UnauthorizedException , NotFoundException , IllegalArgumentException {
1624+ countryCodeIsValid (countryCode );
16081625 final String url = String .format ("%s/available-phone-numbers" , NUMBERS_CALLS_BASE_URL );
16091626 return messageBirdService .requestByID (url , countryCode , PhoneNumbersResponse .class );
16101627 }
@@ -1617,8 +1634,10 @@ public PhoneNumbersResponse listNumbersForPurchase(String countryCode) throws Ge
16171634 * @throws GeneralException general exception
16181635 * @throws UnauthorizedException if client is unauthorized
16191636 * @throws NotFoundException if the resource is missing
1637+ * @throws IllegalArgumentException if the country code provided is invalid
16201638 */
1621- public PhoneNumbersResponse listNumbersForPurchase (String countryCode , PhoneNumbersLookup params ) throws GeneralException , UnauthorizedException , NotFoundException {
1639+ public PhoneNumbersResponse listNumbersForPurchase (String countryCode , PhoneNumbersLookup params ) throws GeneralException , UnauthorizedException , NotFoundException , IllegalArgumentException {
1640+ countryCodeIsValid (countryCode );
16221641 final String url = String .format ("%s/available-phone-numbers" , NUMBERS_CALLS_BASE_URL );
16231642 return messageBirdService .requestByID (url , countryCode , params .toHashMap (), PhoneNumbersResponse .class );
16241643 }
@@ -1630,13 +1649,17 @@ public PhoneNumbersResponse listNumbersForPurchase(String countryCode, PhoneNumb
16301649 * @param countryCode The country code in which the Number should be purchased.
16311650 * @throws GeneralException general exception
16321651 * @throws UnauthorizedException if client is unauthorized
1652+ * @throws IllegalArgumentException if the country code provided is invalid
16331653 */
1634- public PurchasedNumberCreatedResponse purchaseNumber (String number , String countryCode , int billingIntervalMonths ) throws UnauthorizedException , GeneralException {
1654+ public PurchasedNumberCreatedResponse purchaseNumber (String number , String countryCode , int billingIntervalMonths ) throws UnauthorizedException , GeneralException , IllegalArgumentException {
1655+ countryCodeIsValid (countryCode );
16351656 final String url = String .format ("%s/phone-numbers" , NUMBERS_CALLS_BASE_URL );
1636-
16371657 final Map <String , Object > payload = new LinkedHashMap <String , Object >();
16381658 payload .put ("number" , number );
16391659 payload .put ("countryCode" , countryCode );
1660+ if (!Arrays .asList (1 , 3 , 6 , 9 ).contains (billingIntervalMonths )) {
1661+ throw new IllegalArgumentException ("Billing Interval Must Be Either 1, 3, 6, or 9." );
1662+ }
16401663 payload .put ("billingIntervalMonths" , billingIntervalMonths );
16411664
16421665 return messageBirdService .sendPayLoad (url , payload , PurchasedNumberCreatedResponse .class );
0 commit comments