Skip to content

Commit

Permalink
feat: PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JPPortier committed Nov 30, 2023
1 parent 2e1f895 commit 1e2391e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,16 @@ public AvailableNumber checkAvailability(String phoneNumber) throws ApiException

public ActiveNumber rent(String phoneNumber, AvailableNumberRentRequestParameters parameters)
throws ApiException {

AvailableNumberRentRequestParameters guardParameters =
null != parameters ? parameters : AvailableNumberRentRequestParameters.builder().build();

ActiveNumberDto response =
getApi()
.numberServiceRentNumber(
configuration.getProjectId(),
phoneNumber,
AvailableRentRequestParametersDtoConverter.convert(parameters));
AvailableRentRequestParametersDtoConverter.convert(guardParameters));
return ActiveNumberDtoConverter.convert(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.domains.numbers.models.*;
import com.sinch.sdk.domains.numbers.models.requests.*;
import com.sinch.sdk.domains.numbers.models.responses.ActiveNumberListResponse;
import com.sinch.sdk.domains.numbers.models.responses.AvailableNumberListResponse;
import com.sinch.sdk.domains.numbers.models.responses.AvailableRegionListResponse;
import com.sinch.sdk.models.Configuration;
import java.util.Collections;
import java.util.Optional;
Expand All @@ -16,10 +13,6 @@

public class NumbersSampleFlow {

private static final String SINCH_PROJECT_ID = "SINCH_PROJECT_ID";
private static final String SINCH_KEY_ID = "SINCH_KEY_ID";
private static final String SINCH_KEY_SECRET = "SINCH_KEY_SECRET";

private static final Logger LOGGER = Utils.initializeLogger(NumbersSampleFlow.class.getName());

public static void main(String[] args) {
Expand Down Expand Up @@ -61,6 +54,8 @@ public void run(Configuration configuration) {
phoneNumber = rentPhoneNumberByAny(++step, sinch, regionCode, testCodePattern);
}

echo("Let's use now the 'ActiveNumbers' API to see how to manage this number.\n");

// 3 Verify number is active
// 3.1: verify by dedicated phone number request
verifyActiveNumberByNumber(++step, sinch, phoneNumber);
Expand Down Expand Up @@ -92,7 +87,7 @@ String checkAndGetRegionCodeAvailability(int step, SinchClient sinchClient, Stri
.build();

// 2. Request for available regions using the built-in SDK method
AvailableRegionListResponse availableRegionListResponse =
var availableRegionListResponse =
sinchClient.numbers().regions().list(availableRegionsRequestData);

// 3. This API is following the SDK pattern for list and the server response is wrapped inside a
Expand Down Expand Up @@ -154,7 +149,7 @@ String getAvailablePhoneNumber(

// 2. Request for available numbers using the built-in SDK method
// This API is following the SDK pattern for list and the server response is wrapped inside a
AvailableNumberListResponse availableNumbersListResponse =
var availableNumbersListResponse =
sinchClient.numbers().available().list(availableNumbersRequestData);

// 3. Looking for first available number
Expand Down Expand Up @@ -186,7 +181,8 @@ void rentPhoneNumber(int step, SinchClient sinchClient, String phoneNumber) {
echoStep(step, "Rent phone number: '" + phoneNumber + "'");

// 1. Build the request data
var rentRequestData = AvailableNumberRentRequestParameters.builder().build();
var rentRequestData =
AvailableNumberRentRequestParameters.builder().setCallbackUrl("https://foo.url").build();

// 2. Request to rent the number
ActiveNumber rentedNumber =
Expand All @@ -197,7 +193,6 @@ void rentPhoneNumber(int step, SinchClient sinchClient, String phoneNumber) {
+ rentedNumber.getPhoneNumber()
+ " has been rented with the 'rent' operation.The next charge date is "
+ rentedNumber.getNextChargeDate());
echo("Let's use now the 'ActiveNumbers' API to see how to manage this number.\n");
}

String rentPhoneNumberByAny(
Expand Down Expand Up @@ -264,27 +259,34 @@ void verifyActiveNumberByPagination(
.build();

// 2. Request for active number using the built-in SDK method
ActiveNumberListResponse activeNumbersListResponse =
var activeNumbersListResponse =
sinchClient.numbers().active().list(listActiveNumbersRequestData);

// 3. To check if the phone number is part of the active numbers: check the page
// content and if not present, check the next page until we find the phone number, or we reach
// the end of the list
Optional<ActiveNumber> found;
boolean reachedEndOfPages = false;
do {
// use stream onto page content to looking for phoneNumber
found =
activeNumbersListResponse.getContent().stream()
.filter(
f ->
phoneNumber.equals(f.getPhoneNumber())
&& regionCode.equals(f.getRegionCode()))
.findFirst();
if (found.isEmpty() && activeNumbersListResponse.hasNextPage()) {
activeNumbersListResponse = activeNumbersListResponse.nextPage();
} else {
activeNumbersListResponse = null;

// not present within current page
if (found.isEmpty()) {
// need to load the next one... if there is one available
if (activeNumbersListResponse.hasNextPage()) {
activeNumbersListResponse = activeNumbersListResponse.nextPage();
} else {
reachedEndOfPages = true;
}
}
} while (found.isEmpty() && null != activeNumbersListResponse);
} while (found.isEmpty() && !reachedEndOfPages);

echo(
(found.isPresent() ? "SUCCESS:" : "FAILED:")
Expand Down Expand Up @@ -312,13 +314,12 @@ void verifyActiveNumberByAutoPagination(
.build();

// 2. Request for active number using the built-in SDK method
ActiveNumberListResponse activeNumbersListResponse =
var activeNumbersListResponse =
sinchClient.numbers().active().list(listActiveNumbersRequestData);

// 3. To check if the phone number is part of the active numbers: use the
// auto-iterator. Like for hasNextPage/nextPage, http calls will be performed automatically when
// required
activeNumbersListResponse = sinchClient.numbers().active().list(listActiveNumbersRequestData);
var iterator = activeNumbersListResponse.iterator();
boolean found = false;
while (iterator.hasNext()) {
Expand Down

0 comments on commit 1e2391e

Please sign in to comment.