Skip to content

Commit 2279dc5

Browse files
authored
feat: New snippets format (Numbers) and launcher (#14)
1 parent 78b3a78 commit 2279dc5

34 files changed

+1485
-186
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,64 @@
22
Sinch Java SDK Code Snippets Repository
33

44
This repository contains code snippets related to [Sinch JAVA SDK](https://github.com/sinch/sinch-sdk-java)
5+
6+
Snippets can be used as starting point to support Sinch products from your own application.
7+
8+
## Requirements
9+
- JDK 8 or later
10+
- [Sinch account](https://dashboard.sinch.com)
11+
12+
## Snippet execution
13+
Launcher helpers are provided to execute snippets and minimize time to setup your first Java application based onto Sinch SDK
14+
15+
### Snippets execution settings
16+
When executing a snippet you will need to provide certain information about your Sinch account (credentials, Sinch virtual phone number, ...)
17+
18+
This settings can be placed directly in the snippet source or you can choose to edit the [configuration file](./snippets/src/main/resources/config.properties), in which case the settings will be shared and used automatically by each snippet.
19+
20+
### Linux platform
21+
Launch script is [here](./snippets/launcher)
22+
23+
Execution:
24+
```shell
25+
cd snippets
26+
launcher <SNIPPET_SOURCE_PATH>
27+
```
28+
Where `SNIPPET_SOURCE_PATH` is path to snippet source (see [Available Snippets](#available-snippets) for available snippets)
29+
30+
e.g.:
31+
```shell
32+
launcher numbers/SearchForAvailableNumbers
33+
...
34+
launcher regions/List
35+
36+
```
37+
38+
## Available Snippets
39+
40+
- Numbers
41+
- [numbers/Rent](snippets/src/main/java/numbers/Rent.java)
42+
- [numbers/RentAny](snippets/src/main/java/numbers/RentAny.java)
43+
- [numbers/SearchForAvailableNumbers](snippets/src/main/java/numbers/SearchForAvailableNumbers.java)
44+
- [numbers/CheckAvailability](snippets/src/main/java/numbers/CheckAvailability.java)
45+
- [numbers/List](snippets/src/main/java/numbers/List.java)
46+
- [numbers/Update](snippets/src/main/java/numbers/Update.java)
47+
- [numbers/Get](snippets/src/main/java/numbers/Get.java)
48+
- [numbers/Release](snippets/src/main/java/numbers/Release.java)
49+
- Emergency Addresses
50+
- [numbers/ValidateEmergencyAddress](snippets/src/main/java/numbers/ValidateEmergencyAddress.java)
51+
- [numbers/ProvisionEmergencyAddress](snippets/src/main/java/numbers/ProvisionEmergencyAddress.java)
52+
- [numbers/GetEmergencyAddress](snippets/src/main/java/numbers/GetEmergencyAddress.java)
53+
- [numbers/DeprovisionEmergencyAddress](snippets/src/main/java/numbers/DeprovisionEmergencyAddress.java)
54+
- Regions
55+
- [numbers/regions/List](snippets/src/main/java/numbers/regions/List.java)
56+
- Callbacks
57+
- [numbers/callback/Get](snippets/src/main/java/numbers/callback/Get.java)
58+
- [numbers/callback/Update](snippets/src/main/java/numbers/callback/Update.java)
59+
60+
61+
62+
63+
64+
65+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
wrapperVersion=3.3.2
18+
distributionType=only-script
19+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip

snippets/compile.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
#!/bin/sh
22

3-
SNIPPETS=`find . -type f -regex '.*Snippet\.java$'`
4-
5-
echo "Compiling snippets:"
6-
for snippet in $SNIPPETS
7-
do
8-
echo " - Snippet: $snippet"
9-
mvn compile -Dsnippet="$(dirname $snippet)" || exit 1
10-
done
3+
./mvnw clean compile

snippets/conversation/send/Snippet.java renamed to snippets/conversation/messages/SendSMS.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
1-
package conversation;
1+
package conversation.messages;
22

3-
import com.sinch.sdk.domains.conversation.api.v1.ConversationService;
3+
import com.sinch.sdk.SinchClient;
44
import com.sinch.sdk.domains.conversation.api.v1.MessagesService;
55
import com.sinch.sdk.domains.conversation.models.v1.*;
66
import com.sinch.sdk.domains.conversation.models.v1.messages.*;
77
import com.sinch.sdk.domains.conversation.models.v1.messages.request.*;
88
import com.sinch.sdk.domains.conversation.models.v1.messages.response.SendMessageResponse;
99
import com.sinch.sdk.domains.conversation.models.v1.messages.types.text.*;
10+
import com.sinch.sdk.models.Configuration;
1011
import java.util.*;
1112
import java.util.Collections;
13+
import java.util.logging.*;
1214
import java.util.logging.Logger;
1315

14-
public class Snippet {
16+
public class SendSMS {
1517

16-
private static final Logger LOGGER = Logger.getLogger(Snippet.class.getName());
18+
private static final Logger LOGGER = Logger.getLogger(SendSMS.class.getName());
1719

18-
static void execute(ConversationService conversationService) {
20+
public static void main(String[] args) {
1921

20-
MessagesService messagesService = conversationService.messages();
22+
String projectId = "SINCH_PROJECT_ID";
23+
String keyId = "SINCH_KEY_ID";
24+
String keySecret = "SINCH_KEY_SECRET";
25+
26+
Configuration configuration =
27+
Configuration.builder()
28+
.setProjectId(projectId)
29+
.setKeyId(keyId)
30+
.setKeySecret(keySecret)
31+
.build();
32+
33+
SinchClient client = new SinchClient(configuration);
34+
35+
MessagesService service = client.conversation().v1().messages();
2136

2237
String appId = "YOUR_app_id";
2338
String from = "YOUR_sms_sender";
@@ -48,7 +63,7 @@ static void execute(ConversationService conversationService) {
4863

4964
LOGGER.info("Sending SMS Text using Conversation API");
5065

51-
SendMessageResponse value = messagesService.sendMessage(request);
66+
SendMessageResponse value = service.sendMessage(request);
5267

5368
LOGGER.info("Response: " + value);
5469
}

snippets/launcher

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
SNIPPET=$1
4+
5+
echo "Running \"${SNIPPET}\" snippet"
6+
7+
# change
8+
# directory separator by package separator
9+
# supress '.java' suffix if any
10+
CLSS=$(echo "${SNIPPET}" | sed 's/\//./g;s/\.java$//g')
11+
12+
./mvnw \
13+
-q \
14+
clean \
15+
package \
16+
exec:java \
17+
-Djava.util.logging.config.file=src/main/resources/logging.properties \
18+
-Dexec.mainClass="$CLSS" \
19+
|| exit 1
20+

0 commit comments

Comments
 (0)