Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create integrated numbers flow #25

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -4,6 +4,8 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
* Abstract class used for handling unified paginated response
Expand Down Expand Up @@ -50,6 +52,19 @@ public Iterator<T> iterator() {
return new ItemsIterator<>(this);
}

/**
* Getting a stream across all items
*
* <p>Underline API (HTTP request) will be called on demand when next page content will be
* required to fulfill iterator with items for consecutive page
*
* @return Stream onto items
*/
public Stream<T> stream() {
JPPortier marked this conversation as resolved.
Show resolved Hide resolved
Iterable<T> iterable = this::iterator;
return StreamSupport.stream(iterable.spliterator(), false);
}

static class ItemsIterator<T> implements Iterator<T> {

ListResponse<T> response;
Expand Down
5 changes: 5 additions & 0 deletions sample-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ See https://developers.sinch.com for details about these parameters

## Available samples classes

### Full workflow
A full application chaining calls to Numbers service to onboard onto Java SDK and Numbers: [NumbersSampleFlow](src/main/java/com/sinch/sample/numbers/NumbersSampleFlow.java)

### Dedicated service feature samples

| API | Service | Sample | Class | Notes |
|---------|----------------|------------------------|---------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| Numbers | Available | - CheckAvailability | [com.sinch.sample.numbers.available.CheckAvailability](src/main/java/com/sinch/sample/numbers/available/CheckAvailability.java) | Require `PHONE_NUMBER` parameter |
Expand Down
7 changes: 2 additions & 5 deletions sample-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
<properties>
<sinch.sdk.java.version>[0.0.0,)</sinch.sdk.java.version>
<sinch.sdk.java.staging></sinch.sdk.java.staging>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.testSource>1.8</maven.compiler.testSource>
<maven.compiler.testTarget>1.8</maven.compiler.testTarget>
<maven.compiler.release>8</maven.compiler.release>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.version>3.8.0</maven.compiler.version>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
<maven.compiler.showWarnings>true</maven.compiler.showWarnings></properties>
Expand Down
66 changes: 5 additions & 61 deletions sample-app/src/main/java/com/sinch/sample/BaseApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,28 @@

import com.sinch.sdk.SinchClient;
import com.sinch.sdk.models.Configuration;
import com.sinch.sdk.models.SMSRegion;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public abstract class BaseApplication {

/** duplicate of SinchClient property files for lisibility facility but could be anything else */
private static final String OAUTH_URL_KEY = "oauth-url";

private static final String NUMBERS_SERVER_KEY = "numbers-server";
private static final String SMS_REGION_KEY = "sms-region";
private static final String SMS_SERVER_KEY = "sms-server";

private static final String BATCH_ID_KEY = "BATCH_ID";
private static final String PHONE_NUMBER_KEY = "PHONE_NUMBER";
private static final String SINCH_KEY_ID = "SINCH_KEY_ID";
private static final String SINCH_KEY_SECRET = "SINCH_KEY_SECRET";
private static final String SINCH_PROJECT_ID = "SINCH_PROJECT_ID";

protected static Logger LOGGER;
protected static final Logger LOGGER = Utils.initializeLogger(BaseApplication.class.getName());

protected SinchClient client;
protected String phoneNumber;
protected String batchId;

protected BaseApplication() throws IOException {

Properties properties = handleConfigurations();

String keyId =
null != System.getenv(SINCH_KEY_ID)
? System.getenv(SINCH_KEY_ID)
: properties.getProperty(SINCH_KEY_ID);
String keySecret =
null != System.getenv(SINCH_KEY_SECRET)
? System.getenv(SINCH_KEY_SECRET)
: properties.getProperty(SINCH_KEY_SECRET);
String projectId =
null != System.getenv(SINCH_PROJECT_ID)
? System.getenv(SINCH_PROJECT_ID)
: properties.getProperty(SINCH_PROJECT_ID);

Configuration.Builder builder =
Configuration.builder().setKeyId(keyId).setKeySecret(keySecret).setProjectId(projectId);

LOGGER.info("Starting application");

// override by local config
if (properties.containsKey(NUMBERS_SERVER_KEY)) {
builder.setNumbersUrl(properties.getProperty(NUMBERS_SERVER_KEY));
}
if (properties.containsKey(SMS_SERVER_KEY)) {
builder.setSmsUrl(properties.getProperty(SMS_SERVER_KEY));
}
if (properties.containsKey(SMS_REGION_KEY)) {
builder.setSmsRegion(SMSRegion.from(properties.getProperty(SMS_REGION_KEY)));
}
Configuration configuration = Utils.loadConfiguration(LOGGER);

Properties properties = Utils.loadProperties(LOGGER);
phoneNumber =
null != System.getenv(PHONE_NUMBER_KEY)
? System.getenv(PHONE_NUMBER_KEY)
Expand All @@ -71,25 +33,7 @@ protected BaseApplication() throws IOException {
? System.getenv(BATCH_ID_KEY)
: properties.getProperty(BATCH_ID_KEY);

client = new SinchClient(builder.build());
}

private static Properties handleConfigurations() throws IOException {

InputStream is = BaseApplication.class.getResourceAsStream("/logging.properties");
if (null != is) {
LogManager.getLogManager().readConfiguration(is);
is.close();
}
LOGGER = Logger.getLogger(BaseApplication.class.getName());

Properties prop = new Properties();
is = BaseApplication.class.getResourceAsStream("/config.properties");
if (null != is) {
prop.load(is);
is.close();
}
return prop;
client = new SinchClient(configuration);
}

public abstract void run();
Expand Down
68 changes: 68 additions & 0 deletions sample-app/src/main/java/com/sinch/sample/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.sinch.sample;

import com.sinch.sdk.models.Configuration;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class Utils {

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

public static Logger initializeLogger(String className) {
try (InputStream logConfigInputStream =
Utils.class.getClassLoader().getResourceAsStream("logging.properties")) {
if (logConfigInputStream != null) {
LogManager.getLogManager().readConfiguration(logConfigInputStream);
} else {
throw new RuntimeException("The file 'logging.properties' couldn't be loaded.");
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
return Logger.getLogger(className);
}

public static Properties loadProperties(Logger logger) {
Properties properties = new Properties();

try (InputStream input =
Utils.class.getClassLoader().getResourceAsStream("config.properties")) {
if (input != null) {
properties.load(input);
} else {
logger.severe("'config.properties' file could not be loaded");
}
} catch (IOException e) {
logger.severe("Error loading properties from 'config.properties'");
}
return properties;
}

public static Configuration loadConfiguration(Logger logger) {
Properties properties = loadProperties(logger);

String keyId =
null != System.getenv(SINCH_KEY_ID)
? System.getenv(SINCH_KEY_ID)
: properties.getProperty(SINCH_KEY_ID);
String keySecret =
null != System.getenv(SINCH_KEY_SECRET)
? System.getenv(SINCH_KEY_SECRET)
: properties.getProperty(SINCH_KEY_SECRET);
String projectId =
null != System.getenv(SINCH_PROJECT_ID)
? System.getenv(SINCH_PROJECT_ID)
: properties.getProperty(SINCH_PROJECT_ID);

return Configuration.builder()
.setKeyId(keyId)
.setKeySecret(keySecret)
.setProjectId(projectId)
.build();
}
}
Loading
Loading