Skip to content

Commit

Permalink
feature (DEVEXP-214): Support Voice/Applications
Browse files Browse the repository at this point in the history
  • Loading branch information
JPPortier committed Jan 15, 2024
1 parent 18ecc7e commit 5f143cb
Show file tree
Hide file tree
Showing 51 changed files with 1,862 additions and 507 deletions.
4 changes: 3 additions & 1 deletion client/resources/config-default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ sms-server=https://zt.%s.sms.api.sinch.com
verification-server=https://verification.api.sinch.com

voice-region=global

# see https://developers.sinch.com/docs/voice/api-reference/#endpoints

voice-application-management-server=https://callingapi.sinch.com

voice-server-global=https://calling.api.sinch.com
voice-server-europe=https://calling-euc1.api.sinch.com
voice-server-north-america=https://calling-use1.api.sinch.com
Expand Down
9 changes: 9 additions & 0 deletions client/src/main/com/sinch/sdk/SinchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class SinchClient {
private static final String SMS_SERVER_KEY = "sms-server";

private static final String VOICE_REGION_KEY = "voice-region";
private static final String VOICE_APPLICATION_MANAGEMENT_SERVER_KEY =
"voice-application-management-server";

private static final String VERIFICATION_SERVER_KEY = "verification-server";

Expand Down Expand Up @@ -129,6 +131,13 @@ private void handleDefaultVoiceSettings(
: builder.voiceRegion;
builder.setVoiceUrl(props.getProperty(String.format("voice-server-%s", region.value())));
}

// application management server
if (null == configuration.getVoiceApplicationMngmtUrl()
&& props.containsKey(VOICE_APPLICATION_MANAGEMENT_SERVER_KEY)) {
builder.setVoiceApplicationMngmtUrl(
props.getProperty(VOICE_APPLICATION_MANAGEMENT_SERVER_KEY));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.sinch.sdk.domains.voice;

import com.sinch.sdk.domains.voice.models.CallbackUrls;
import com.sinch.sdk.domains.voice.models.NumberInformation;
import com.sinch.sdk.domains.voice.models.requests.ApplicationsUpdateNumbersRequestParameters;
import com.sinch.sdk.domains.voice.models.response.AssignedNumbers;
import com.sinch.sdk.models.E164PhoneNumber;

/** You can use the API to manage features of applications in your project. */
public interface ApplicationsService {

/**
* Get information about your numbers.
*
* <p>It returns a list of numbers that you own, as well as their capability (voice or SMS). For
* the ones that are assigned to an app, it returns the application key of the app.
*
* @return Your numbers
* @since 1.0
*/
AssignedNumbers getNumbers();

/**
* Returns any callback URLs configured for the specified application
*
* @param applicationKey The unique identifying key of the application
* @return Assigned callback to application
* @since 1.0
*/
CallbackUrls getCallbackUrls(String applicationKey);

/**
* @since 1.0
*/
void updateCallbackUrls(String applicationKey, CallbackUrls parameters);

NumberInformation queryNumber(E164PhoneNumber number);

/**
* Update or assign a list of numbers to an application.
*
* @param parameters Request parameters
* @since 1.0
*/
void updateNumbers(ApplicationsUpdateNumbersRequestParameters parameters);

/**
* Un-assign a number from an application.
*
* @param phoneNumber The phone number
* @param applicationKey Indicates the application where the number was assigned.
* <p>If empty, the application key that is used to authenticate
* @since 1.0
*/
public void unassignNumber(E164PhoneNumber phoneNumber, String applicationKey);
}
8 changes: 8 additions & 0 deletions client/src/main/com/sinch/sdk/domains/voice/VoiceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public interface VoiceService {
* @since 1.0
*/
CallsService calls();

/**
* Applications Service instance
*
* @return service instance for project
* @since 1.0
*/
ApplicationsService applications();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.sinch.sdk.domains.voice.adapters;

import com.sinch.sdk.core.http.AuthManager;
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.core.http.HttpMapper;
import com.sinch.sdk.domains.voice.adapters.api.v1.ApplicationsApi;
import com.sinch.sdk.domains.voice.adapters.converters.ApplicationsDtoConverter;
import com.sinch.sdk.domains.voice.models.CallbackUrls;
import com.sinch.sdk.domains.voice.models.NumberInformation;
import com.sinch.sdk.domains.voice.models.requests.ApplicationsUpdateNumbersRequestParameters;
import com.sinch.sdk.domains.voice.models.response.AssignedNumbers;
import com.sinch.sdk.models.Configuration;
import com.sinch.sdk.models.E164PhoneNumber;
import java.util.Map;

public class ApplicationsService implements com.sinch.sdk.domains.voice.ApplicationsService {

private final ApplicationsApi api;

public ApplicationsService(
Configuration configuration, HttpClient httpClient, Map<String, AuthManager> authManagers) {
this.api =
new ApplicationsApi(
httpClient,
configuration.getVoiceApplicationMngmtServer(),
authManagers,
new HttpMapper());
}

protected ApplicationsApi getApi() {
return this.api;
}

public AssignedNumbers getNumbers() {

return ApplicationsDtoConverter.convert(getApi().configurationGetNumbers());
}

public CallbackUrls getCallbackUrls(String applicationKey) {

return ApplicationsDtoConverter.convert(getApi().configurationGetCallbackURLs(applicationKey));
}

public void updateCallbackUrls(String applicationKey, CallbackUrls parameters) {
getApi()
.configurationUpdateCallbackURLs(
applicationKey, ApplicationsDtoConverter.convert(parameters));
}

public NumberInformation queryNumber(E164PhoneNumber number) {
return ApplicationsDtoConverter.convert(getApi().callingQueryNumber(number.stringValue()));
}

public void updateNumbers(ApplicationsUpdateNumbersRequestParameters parameters) {
getApi().configurationUpdateNumbers(ApplicationsDtoConverter.convert(parameters));
}

public void unassignNumber(E164PhoneNumber phoneNumber, String applicationKey) {

getApi()
.configurationUnassignNumber(ApplicationsDtoConverter.convert(phoneNumber, applicationKey));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sinch.sdk.core.exceptions.ApiAuthException;
import com.sinch.sdk.core.http.AuthManager;
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.domains.voice.ApplicationsService;
import com.sinch.sdk.domains.voice.CalloutsService;
import com.sinch.sdk.models.Configuration;
import java.util.Map;
Expand All @@ -19,6 +20,7 @@ public class VoiceService implements com.sinch.sdk.domains.voice.VoiceService {
private CalloutsService callouts;
private ConferencesService conferences;
private CallsService calls;
private ApplicationsService applications;

private Map<String, AuthManager> clientAuthManagers;

Expand Down Expand Up @@ -74,6 +76,16 @@ public CallsService calls() {
return this.calls;
}

public ApplicationsService applications() {
if (null == this.applications) {
checkCredentials();
this.applications =
new com.sinch.sdk.domains.voice.adapters.ApplicationsService(
configuration, httpClient, clientAuthManagers);
}
return this.applications;
}

private void checkCredentials() throws ApiAuthException {
if (null == clientAuthManagers || clientAuthManagers.isEmpty()) {
throw new ApiAuthException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.sinch.sdk.domains.voice.adapters.converters;

import com.sinch.sdk.domains.common.adapters.converters.EnumDynamicConverter;
import com.sinch.sdk.domains.voice.models.ApplicationAssignedNumber;
import com.sinch.sdk.domains.voice.models.ApplicationURL;
import com.sinch.sdk.domains.voice.models.CallbackUrls;
import com.sinch.sdk.domains.voice.models.NumberInformation;
import com.sinch.sdk.domains.voice.models.dto.v1.CallbacksDto;
import com.sinch.sdk.domains.voice.models.dto.v1.CallbacksUrlDto;
import com.sinch.sdk.domains.voice.models.dto.v1.GetNumbersResponseObjDto;
import com.sinch.sdk.domains.voice.models.dto.v1.GetNumbersResponseObjNumbersInnerDto;
import com.sinch.sdk.domains.voice.models.dto.v1.GetQueryNumberDto;
import com.sinch.sdk.domains.voice.models.dto.v1.GetQueryNumberNumberDto;
import com.sinch.sdk.domains.voice.models.dto.v1.UnassignNumbersDto;
import com.sinch.sdk.domains.voice.models.dto.v1.UpdateNumbersDto;
import com.sinch.sdk.domains.voice.models.requests.ApplicationsUpdateNumbersRequestParameters;
import com.sinch.sdk.domains.voice.models.response.AssignedNumbers;
import com.sinch.sdk.models.E164PhoneNumber;
import java.util.List;
import java.util.stream.Collectors;

public class ApplicationsDtoConverter {

public static AssignedNumbers convert(GetNumbersResponseObjDto dto) {
if (null == dto) {
return null;
}
List<GetNumbersResponseObjNumbersInnerDto> list = dto.getNumbers();
if (null == list) {
return null;
}
return AssignedNumbers.builder()
.setNumbers(
list.stream().map(ApplicationsDtoConverter::convert).collect(Collectors.toList()))
.build();
}

public static CallbackUrls convert(CallbacksDto dto) {
if (null == dto) {
return null;
}

return CallbackUrls.builder().setUrl(convert(dto.getUrl())).build();
}

public static CallbacksDto convert(CallbackUrls client) {
if (null == client || null == client.getUrl()) {
return null;
}
CallbacksDto dto = new CallbacksDto();
dto.url(
new CallbacksUrlDto()
.primary(client.getUrl().getPrimary())
.fallback(client.getUrl().getFallback()));
return dto;
}

public static NumberInformation convert(GetQueryNumberDto dto) {
if (null == dto || !dto.getNumberDefined()) {
return null;
}
GetQueryNumberNumberDto item = dto.getNumber();
return NumberInformation.builder()
.setCountryId(item.getCountryId())
.setNumberType(NumberTypeDtoConverter.convert(item.getNumberType()))
.setNormalizedNumber(E164PhoneNumberDtoConverter.convert(item.getNormalizedNumber()))
.setRestricted(item.getRestricted())
.setRate(PriceDtoConverter.convert(item.getRate()))
.build();
}

public static UpdateNumbersDto convert(ApplicationsUpdateNumbersRequestParameters client) {
if (null == client) {
return null;
}
UpdateNumbersDto dto = new UpdateNumbersDto();
client
.getNumbers()
.ifPresent(
f ->
dto.numbers(
f.stream().map(E164PhoneNumber::stringValue).collect(Collectors.toList())));
client.getApplicationKey().ifPresent(dto::applicationkey);
client.getCapability().ifPresent(f -> dto.capability(EnumDynamicConverter.convert(f)));
return dto;
}

public static UnassignNumbersDto convert(E164PhoneNumber phoneNumber, String applicationKey) {

UnassignNumbersDto dto = new UnassignNumbersDto();
if (null != phoneNumber) {
dto.number(phoneNumber.stringValue());
}
dto.applicationkey(applicationKey);
return dto;
}

private static ApplicationURL convert(CallbacksUrlDto dto) {
if (null == dto) {
return null;
}

return ApplicationURL.builder()
.setPrimary(dto.getPrimary())
.setFallback(dto.getFallback())
.build();
}

private static ApplicationAssignedNumber convert(GetNumbersResponseObjNumbersInnerDto dto) {
if (null == dto) {
return null;
}

return ApplicationAssignedNumber.builder()
.setNumber(E164PhoneNumberDtoConverter.convert(dto.getNumber()))
.setApplicationKey(dto.getApplicationkey())
.setCapability(CapabilityDtoConverter.convert(dto.getCapability()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sinch.sdk.domains.voice.adapters.converters;

import com.sinch.sdk.domains.voice.models.CapabilityType;

public class CapabilityDtoConverter {

public static CapabilityType convert(String dto) {
if (null == dto) {
return null;
}
return CapabilityType.from(dto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ public static String convert(E164PhoneNumber client) {
}
return client.stringValue();
}

public static E164PhoneNumber convert(String dto) {
if (null == dto) {
return null;
}
return E164PhoneNumber.valueOf(dto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sinch.sdk.domains.voice.adapters.converters;

import com.sinch.sdk.domains.voice.models.NumberType;

public class NumberTypeDtoConverter {

public static NumberType convert(String dto) {
if (null == dto) {
return null;
}
return NumberType.from(dto);
}
}
Loading

0 comments on commit 5f143cb

Please sign in to comment.