Skip to content

Commit f8ea468

Browse files
authored
DX-1816 java readme update (#29)
1 parent a9371a0 commit f8ea468

File tree

2 files changed

+139
-61
lines changed

2 files changed

+139
-61
lines changed

README.md

Lines changed: 112 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,152 @@
11
# Bandwidth Java SDK
2-
3-
Bandwidth's API docs can be found at https://dev.bandwidth.com
42

5-
Java specific docs can be found at https://dev.bandwidth.com/sdks/java.html
3+
## Getting Started
64

7-
## Download & Install
5+
### Installation
86

9-
Maven:
7+
Add the following dependency to your `pom.xml` file
108

11-
```xml
9+
```
1210
<!-- https://mvnrepository.com/artifact/com.bandwidth.sdk/bandwidth-sdk -->
1311
<dependency>
1412
<groupId>com.bandwidth.sdk</groupId>
1513
<artifactId>bandwidth-sdk</artifactId>
16-
<version>1.0.0</version>
14+
<version>{version}</version>
1715
</dependency>
1816
```
1917

20-
## Initialize Bandwidth Client
21-
22-
```java
18+
### Initialize
2319

24-
//Set the voice client configuration with credentials
20+
```
2521
BandwidthClient client = new BandwidthClient.Builder()
26-
.messagingBasicAuthCredentials("MESSAGING_API_TOKEN", "MESSAGING_API_SECRET")
27-
.voiceBasicAuthCredentials("VOICE_API_USERNAME", "VOICE_API_PASSWORD")
28-
.environment(Environment.PRODUCTION)
22+
.messagingBasicAuthCredentials("username", "password")
23+
.voiceBasicAuthCredentials("username", "password")
24+
.twoFactorAuthBasicAuthCredentials("username", "password")
25+
.webRtcBasicAuthCredentials("username", "password")
2926
.build();
27+
String accountId = "12345";
28+
```
29+
30+
### Create A Phone Call
3031

31-
//Fully qualified name to remove confilicts
32-
com.bandwidth.messaging.controllers.APIController messagingController = client.getMessagingClient().getAPIController();
32+
```
3333
com.bandwidth.voice.controllers.APIController voiceController = client.getVoiceClient().getAPIController();
3434
35+
String to = "+15554443333";
36+
String from = "+15553334444";
37+
String applicationId = "3-a-b-c";
38+
String answerUrl = "https://sample.com";
39+
40+
ApiCreateCallRequest body = new ApiCreateCallRequest();
41+
body.setTo(to);
42+
body.setFrom(from);
43+
body.setApplicationId(applicationId);
44+
body.setAnswerUrl(answerUrl);
45+
46+
ApiResponse<ApiCallResponse> createCallResponse = voiceController.createCall(accountId, body);
47+
System.out.println(createCallResponse.getResult().getCallId());
3548
```
3649

37-
## Create Phone Call
50+
### Send A Text Message
3851

39-
```java
40-
import com.bandwidth.voice.models.ApiCreateCallRequest;
52+
```
53+
String to = "+15554443333";
54+
ArrayList<String> toNumbers = new ArrayList<String>();
55+
toNumbers.add(to);
56+
String from = "+15553334444";
57+
String applicationId = "3-a-b-d";
58+
String text = "Hello from Java";
59+
60+
MessageRequest body = new MessageRequest();
61+
body.setTo(toNumbers);
62+
body.setFrom(from);
63+
body.setText(text);
64+
body.setApplicationId(applicationId);
65+
66+
ApiResponse<BandwidthMessage> createMessageResponse = messagingController.createMessage(accountId, body);
67+
System.out.println(createMessageResponse.getResult().getMessageId());
68+
```
4169

42-
//Create the ApiCreateCallRequst object and populate.
43-
ApiCreateCallRequest callRequest = new ApiCreateCallRequest();
70+
### Create BXML
4471

45-
callRequest.setApplicationId("application.Id");
46-
callRequest.setTo("+19999999999");
47-
callRequest.setAnswerUrl("https://test.com");
48-
callRequest.setFrom("+17777777777");
72+
```
73+
SpeakSentence speakSentence = SpeakSentence.builder()
74+
.text("Hello world")
75+
.voice("susan")
76+
.gender("female")
77+
.locale("en_US")
78+
.build();
79+
80+
String response = new Response()
81+
.add(speakSentence)
82+
.toBXML();
83+
System.out.println(response);
84+
```
4985

50-
//The voice client createCall can throw these exceptions.
51-
try {
52-
ApiResponse<ApiCallResponse> response = voiceController.createCall("account.id", callRequest);
53-
System.out.println(response.getResult().getCallId());
54-
} catch (IOException | ApiException e) {
55-
//Handle
56-
}
86+
### Create A MFA Request
5787

88+
```
89+
String to = "+15554443333";
90+
String from = "+15553334444";
91+
String applicationId = "3-a-c-b");
92+
String scope = "scope";
93+
int digits = 6;
94+
String message = "Your temporary {NAME} {SCOPE} code is {CODE}";
95+
96+
TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema();
97+
body.setTo(to);
98+
body.setFrom(from);
99+
body.setApplicationId(applicationId);
100+
body.setScope(scope);
101+
body.setDigits(digits);
102+
body.setMessage(message);
103+
104+
mfaController.createVoiceTwoFactor(accountId, body);
105+
106+
String code = "123456"; //this is the user code to verify
107+
int expirationTimeInMinutes = 3;
108+
109+
TwoFactorVerifyRequestSchema body = new TwoFactorVerifyRequestSchema();
110+
body.setTo(to);
111+
body.setApplicationId(applicationId);
112+
body.setScope(scope);
113+
body.setCode(code);
114+
body.setExpirationTimeInMinutes(expirationTimeInMinutes);
115+
116+
ApiResponse<TwoFactorVerifyCodeResponse> response = mfaController.createVerifyTwoFactor(accountId, body);
117+
System.out.println(response.getResult().getValid());
58118
```
59119

60-
## Generate BXML
120+
### WebRtc Participant & Session Management
61121

62-
```java
63-
import com.bandwidth.sdk.voice.models.verbs.*;
122+
```
123+
Session createSessionBody = new Session();
124+
createSessionBody.setTag("new-session");
64125
65-
//Create a Bandwidth XML (BXML) SpeakSentence Verb. Supply the sentence to be spoken.
66-
SpeakSentence speakSentence = SpeakSentence.builder()
67-
.text("Hello World")
68-
.build();
126+
ApiResponse<Session> createSessionResponse = webrtcController.createSession(accountId, createSessionBody);
127+
String sessionId = createSessionResponse.getResult().getId();
69128
70-
//Create the response object and add the speakSentence verb to the response.
71-
Response response = Response.builder().build().add(speakSentence);
129+
Participant createParticipantBody = new Participant();
130+
createParticipantBody.setCallbackUrl("https://sample.com");
131+
ArrayList<PublishPermissionEnum> publishPermissions = new ArrayList<PublishPermissionEnum>();
132+
publishPermissions.add(PublishPermissionEnum.AUDIO);
133+
publishPermissions.add(PublishPermissionEnum.VIDEO);
72134
73-
//view the BXML
74-
System.out.println( response.toXml() )
135+
ApiResponse<AccountsParticipantsResponse> createParticipantResponse = webrtcController.createParticipant(accountId, createParticipantBody);
136+
String participantId = createParticipantResponse.getResult().getParticipant().getId();
75137
138+
webrtcController.addParticipantToSession(accountId, sessionId, participantId, null);
76139
```
77140

78-
## Send Text Message
141+
## Supported Java Versions
79142

80-
```java
81-
import com.bandwidth.messaging.models.MessageRequest;
143+
This package can be used with Java >= 1.8
82144

83-
MessageRequest messageRequest = new MessageRequest();
145+
## Documentation
84146

85-
List<String> toNumbers = new ArrayList<>();
147+
Documentation for this package can be found at https://dev.bandwidth.com/sdks/java.html
86148

87-
toNumbers.add("+12345678902");
149+
## Credentials
88150

89-
messageRequest.setApplicationId(MSG_APPLICATION_ID);
90-
messageRequest.setText("Hey, check this out!");
91-
messageRequest.setFrom("+12345678901");
92-
messageRequest.setTo( toNumbers );
93-
messageRequest.setTag("test tag");
151+
Information for credentials for this package can be found at https://dev.bandwidth.com/guides/accountCredentials.html
94152

95-
try {
96-
ApiResponse<BandwidthMessage> response = messagingController.createMessage(accountId, messageRequest);
97-
System.out.println(response.getResult().getId());
98-
} catch (ApiException | IOException e){
99-
//Handle
100-
}
101-
```

src/test/java/com/bandwidth/ApiTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import com.bandwidth.twofactorauth.models.*;
2525
import com.bandwidth.twofactorauth.controllers.*;
2626
import com.bandwidth.twofactorauth.exceptions.*;
27+
import com.bandwidth.webrtc.models.*;
28+
import com.bandwidth.webrtc.controllers.*;
2729
import com.bandwidth.exceptions.ApiException;
2830
import com.bandwidth.http.response.ApiResponse;
2931
import com.bandwidth.utilities.FileWrapper;
@@ -37,17 +39,20 @@ public class ApiTest {
3739
private com.bandwidth.messaging.controllers.APIController messagingController;
3840
private com.bandwidth.voice.controllers.APIController voiceController;
3941
private com.bandwidth.twofactorauth.controllers.MFAController mfaController;
42+
private com.bandwidth.webrtc.controllers.APIController webrtcController;
4043

4144
@Before
4245
public void init() {
4346
this.client = new BandwidthClient.Builder()
4447
.messagingBasicAuthCredentials(System.getenv("BW_USERNAME"), System.getenv("BW_PASSWORD"))
4548
.voiceBasicAuthCredentials(System.getenv("BW_USERNAME"), System.getenv("BW_PASSWORD"))
4649
.twoFactorAuthBasicAuthCredentials(System.getenv("BW_USERNAME"), System.getenv("BW_PASSWORD"))
50+
.webRtcBasicAuthCredentials(System.getenv("BW_USERNAME"), System.getenv("BW_PASSWORD"))
4751
.build();
4852
this.messagingController = client.getMessagingClient().getAPIController();
4953
this.voiceController = client.getVoiceClient().getAPIController();
5054
this.mfaController = client.getTwoFactorAuthClient().getMFAController();
55+
this.webrtcController = client.getWebRtcClient().getAPIController();
5156
}
5257

5358
@Test
@@ -282,6 +287,28 @@ public void testMfaVerifyInvalidPhoneNumber() throws Exception {
282287
mfaController.createVerifyTwoFactor(accountId, body);
283288
}
284289

290+
@Test
291+
public void testWebRtcParticipantSessionManagement() throws Exception {
292+
String accountId = System.getenv("BW_ACCOUNT_ID");
293+
294+
Session createSessionBody = new Session();
295+
createSessionBody.setTag("new-session");
296+
297+
ApiResponse<Session> createSessionResponse = webrtcController.createSession(accountId, createSessionBody);
298+
String sessionId = createSessionResponse.getResult().getId();
299+
300+
Participant createParticipantBody = new Participant();
301+
createParticipantBody.setCallbackUrl("https://sample.com");
302+
ArrayList<PublishPermissionEnum> publishPermissions = new ArrayList<PublishPermissionEnum>();
303+
publishPermissions.add(PublishPermissionEnum.AUDIO);
304+
publishPermissions.add(PublishPermissionEnum.VIDEO);
305+
306+
ApiResponse<AccountsParticipantsResponse> createParticipantResponse = webrtcController.createParticipant(accountId, createParticipantBody);
307+
String participantId = createParticipantResponse.getResult().getParticipant().getId();
308+
309+
webrtcController.addParticipantToSession(accountId, sessionId, participantId, null);
310+
}
311+
285312
/*
286313
* Taken from https://mkyong.com/java/how-to-convert-inputstream-to-string-in-java/
287314
*/

0 commit comments

Comments
 (0)