Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/samples-compilation.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Snippets Compilation
name: Examples Compilation

on: [push]

Expand Down
37 changes: 37 additions & 0 deletions examples/client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Client application template based onto Sinch Java SDK

This directory contains a client application based onto [Sinch SDK java](https://github.com/sinch/sinch-sdk-java)

# Prerequisites

- JDK 8 or later (Sinch SDK Java is requiring java 8 only but client application can use latest available version)
- [Maven](https://maven.apache.org/)
- [Sinch account](https://dashboard.sinch.com)

## Configuration

Edit [config.properties](src/main/resources/config.properties) file to set credentials to be used to configure the SinchClient.

- To use Numbers or SMS, you need to fill the following settings with your Sinch account information:
- `SINCH_PROJECT_ID`=Your Sinch Project ID
- `SINCH_KEY_ID`=Your Sinch Key ID
- `SINCH_KEY_SECRET`=Your Sinch Key Secret
- To use [Verification](https://developers.sinch.com/docs/verification) or [Voice](https://developers.sinch.com/docs/voice) you will need application credentials and fill [config.properties](src/main/resources/config.properties) dedicated section.
- To use [SMS](https://developers.sinch.com/docs/sms) for regions other than US/EU, you will need service plan ID credentials and fill [config.properties](src/main/resources/config.properties) dedicated section.


## Usage

1. Edit configuration file
See above for Configuration paragraph

2. Application generation

Application generation command:
```sh
mvn package
```
3. Execute application
```sh
java -jar target/sinch-java-sdk-client-application-1.0-SNAPSHOT-jar-with-dependencies.jar
```
94 changes: 94 additions & 0 deletions examples/client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>my.company.com</groupId>
<artifactId>sinch-java-sdk-client-application</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Sinch Java SDK Client Application</name>

<profiles>
<profile>
<id>use-version</id>
<properties>
<sinch.sdk.java.version>${env.SDK_VERSION}</sinch.sdk.java.version>
</properties>
</profile>
</profiles>

<properties>
<sinch.sdk.java.version>[2.0.0,)</sinch.sdk.java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.version>3.13.0</maven.compiler.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>add-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>
Application
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.sinch.sdk</groupId>
<artifactId>sinch-sdk-java</artifactId>
<version>${sinch.sdk.java.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>2.0.9</version>
</dependency>

</dependencies>

</project>
82 changes: 82 additions & 0 deletions examples/client/src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import com.sinch.sdk.SinchClient;
import conversation.ConversationQuickStart;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import numbers.NumbersQuickStart;
import sms.SmsQuickStart;
import verification.VerificationQuickStart;
import voice.VoiceQuickStart;

public abstract class Application {

private static final String LOGGING_PROPERTIES_FILE = "logging.properties";
private static final Logger LOGGER = initializeLogger();

public static void main(String[] args) {
try {

SinchClient client = SinchClientHelper.initSinchClient();
LOGGER.info("Application initiated. SinchClient ready.");

// Conversation service dedicated business logic processing
// (see https://developers.sinch.com/docs/conversation)
// comment if unused
if (client.getConfiguration().getUnifiedCredentials().isPresent()) {
ConversationQuickStart conversation =
new ConversationQuickStart(client.conversation().v1());
}

// Numbers service dedicated business logic processing
// (see https://developers.sinch.com/categories/numbersandconnectivity)
// comment if unused
if (client.getConfiguration().getUnifiedCredentials().isPresent()) {
NumbersQuickStart numbers = new NumbersQuickStart(client.numbers().v1());
}

// SMS service dedicated business logic processing
// (see https://developers.sinch.com/docs/sms)
// comment if unused
if (client.getConfiguration().getSmsServicePlanCredentials().isPresent()
|| client.getConfiguration().getUnifiedCredentials().isPresent()) {
SmsQuickStart sms = new SmsQuickStart(client.sms().v1());
}

// Verification service dedicated business logic processing
// (see https://developers.sinch.com/docs/verification)
// comment if unused
if (client.getConfiguration().getApplicationCredentials().isPresent()) {
VerificationQuickStart verification =
new VerificationQuickStart(client.verification().v1());
}

// Voice service dedicated business logic processing
// (see https://developers.sinch.com/docs/voice)
// comment if unused
if (client.getConfiguration().getApplicationCredentials().isPresent()) {
VoiceQuickStart voice = new VoiceQuickStart(client.voice().v1());
}

} catch (Exception e) {
LOGGER.severe(String.format("Application failure: %s", e.getMessage()));
}
}

static Logger initializeLogger() {
try (InputStream logConfigInputStream =
Application.class.getClassLoader().getResourceAsStream(LOGGING_PROPERTIES_FILE)) {

if (logConfigInputStream != null) {
LogManager.getLogManager().readConfiguration(logConfigInputStream);
} else {
throw new RuntimeException(
String.format("The file '%s' couldn't be loaded.", LOGGING_PROPERTIES_FILE));
}

} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
return Logger.getLogger(Application.class.getName());
}
}
121 changes: 121 additions & 0 deletions examples/client/src/main/java/SinchClientHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.models.Configuration;
import com.sinch.sdk.models.ConversationRegion;
import com.sinch.sdk.models.SMSRegion;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;
import java.util.logging.Logger;

public class SinchClientHelper {

private static final Logger LOGGER = Logger.getLogger(SinchClientHelper.class.getName());

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 String APPLICATION_API_KEY = "APPLICATION_API_KEY";
private static final String APPLICATION_API_SECRET = "APPLICATION_API_SECRET";

private static final String SMS_SERVICE_PLAN_ID = "SMS_SERVICE_PLAN_ID";
private static final String SMS_SERVICE_PLAN_TOKEN = "SMS_SERVICE_PLAN_TOKEN";
private static final String SMS_REGION = "SMS_REGION";

private static final String CONVERSATION_REGION = "CONVERSATION_REGION";

private static final String CONFIG_FILE = "config.properties";

public static SinchClient initSinchClient() {

LOGGER.info("Initializing client");

Configuration configuration = getConfiguration();

return new SinchClient(configuration);
}

private static Configuration getConfiguration() {

Properties properties = loadProperties();

Configuration.Builder builder = Configuration.builder();

manageUnifiedCredentials(properties, builder);
manageApplicationCredentials(properties, builder);
manageConversationConfiguration(properties, builder);
manageSmsConfiguration(properties, builder);

return builder.build();
}

private static Properties loadProperties() {

Properties properties = new Properties();

try (InputStream input =
SinchClientHelper.class.getClassLoader().getResourceAsStream(CONFIG_FILE)) {
if (input != null) {
properties.load(input);
} else {
LOGGER.severe(String.format("'%s' file could not be loaded", CONFIG_FILE));
}
} catch (IOException e) {
LOGGER.severe(String.format("Error loading properties from '%s'", CONFIG_FILE));
}

return properties;
}

static void manageUnifiedCredentials(Properties properties, Configuration.Builder builder) {

Optional<String> projectId = getConfigValue(properties, SINCH_PROJECT_ID);
Optional<String> keyId = getConfigValue(properties, SINCH_KEY_ID);
Optional<String> keySecret = getConfigValue(properties, SINCH_KEY_SECRET);

projectId.ifPresent(builder::setProjectId);
keyId.ifPresent(builder::setKeyId);
keySecret.ifPresent(builder::setKeySecret);
}

private static void manageApplicationCredentials(
Properties properties, Configuration.Builder builder) {

Optional<String> verificationApiKey = getConfigValue(properties, APPLICATION_API_KEY);
Optional<String> verificationApiSecret = getConfigValue(properties, APPLICATION_API_SECRET);

verificationApiKey.ifPresent(builder::setApplicationKey);
verificationApiSecret.ifPresent(builder::setApplicationSecret);
}

private static void manageConversationConfiguration(
Properties properties, Configuration.Builder builder) {

Optional<String> region = getConfigValue(properties, CONVERSATION_REGION);

region.ifPresent(value -> builder.setConversationRegion(ConversationRegion.from(value)));
}

private static void manageSmsConfiguration(Properties properties, Configuration.Builder builder) {

Optional<String> servicePlanId = getConfigValue(properties, SMS_SERVICE_PLAN_ID);
Optional<String> servicePlanToken = getConfigValue(properties, SMS_SERVICE_PLAN_TOKEN);
Optional<String> region = getConfigValue(properties, SMS_REGION);

servicePlanId.ifPresent(builder::setSmsServicePlanId);
servicePlanToken.ifPresent(builder::setSmsApiToken);
region.ifPresent(value -> builder.setSmsRegion(SMSRegion.from(value)));
}

private static Optional<String> getConfigValue(Properties properties, String key) {
String value = null != System.getenv(key) ? System.getenv(key) : properties.getProperty(key);

// empty value means setting not set
if (null != value && value.trim().isEmpty()) {
return Optional.empty();
}

return Optional.ofNullable(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package conversation;

import com.sinch.sdk.domains.conversation.api.v1.ConversationService;
import java.util.logging.Logger;

public class ConversationQuickStart {

private static final Logger LOGGER = Logger.getLogger(ConversationQuickStart.class.getName());

private final ConversationService conversationService;

public ConversationQuickStart(ConversationService conversationService) {
this.conversationService = conversationService;

// Insert your application logic or business process here
LOGGER.info("Snippet execution");
}
}
18 changes: 18 additions & 0 deletions examples/client/src/main/java/numbers/NumbersQuickStart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package numbers;

import com.sinch.sdk.domains.numbers.api.v1.NumbersService;
import java.util.logging.Logger;

public class NumbersQuickStart {

private static final Logger LOGGER = Logger.getLogger(NumbersQuickStart.class.getName());

private final NumbersService numbersService;

public NumbersQuickStart(NumbersService numbersService) {
this.numbersService = numbersService;

// Insert your application logic or business process here
LOGGER.info("Snippet execution");
}
}
Loading
Loading