Skip to content

Commit 7e6b88d

Browse files
committed
feat: New snippets format and launcher
1 parent a24e581 commit 7e6b88d

File tree

26 files changed

+1053
-129
lines changed

26 files changed

+1053
-129
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,53 @@
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+
### Linux platform
16+
Launch script is [here](./snippets/launcher)
17+
18+
Execution:
19+
```shell
20+
cd snippets
21+
launcher <SNIPPET_SOURCE_PATH>
22+
```
23+
Where `SNIPPET_SOURCE_PATH` is .... path to snippet sources
24+
e.g.:
25+
```shell
26+
launcher numbers/SearchForAvailableNumbers
27+
...
28+
launcher regions/List
29+
30+
```
31+
32+
## Available Snippets
33+
34+
- Numbers
35+
- [numbers/Rent](snippets/src/main/java/numbers/Rent.java)
36+
- [numbers/RentAny](snippets/src/main/java/numbers/RentAny.java)
37+
- [numbers/SearchForAvailableNumbers](snippets/src/main/java/numbers/SearchForAvailableNumbers.java)
38+
- [numbers/CheckAvailability](snippets/src/main/java/numbers/CheckAvailability.java)
39+
- [numbers/List](snippets/src/main/java/numbers/List.java)
40+
- [numbers/Update](snippets/src/main/java/numbers/Update.java)
41+
- [numbers/Get](snippets/src/main/java/numbers/Get.java)
42+
- [numbers/Release](snippets/src/main/java/numbers/Release.java)
43+
- Regions
44+
- [numbers/regions/List](snippets/src/main/java/numbers/regions/List.java)
45+
- Callbacks
46+
- [numbers/callback/Get](snippets/src/main/java/numbers/callback/Get.java)
47+
- [numbers/callback/Update](snippets/src/main/java/numbers/callback/Update.java)
48+
49+
50+
51+
52+
53+
54+
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 -q clean package exec:java -Djava.util.logging.config.file=src/main/resources/logging.properties -Dexec.mainClass="$CLSS" || exit 1
13+

0 commit comments

Comments
 (0)