-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (DEVEXP-211): Support Numbers webhooks
- Loading branch information
Showing
23 changed files
with
633 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
client/src/main/com/sinch/sdk/domains/numbers/WebHooksService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.sinch.sdk.domains.numbers; | ||
|
||
import com.sinch.sdk.core.exceptions.ApiMappingException; | ||
import com.sinch.sdk.domains.numbers.models.webhooks.EventNotification; | ||
|
||
/** | ||
* Webhooks service | ||
* | ||
* <p>Callback events are used to get notified about Numbers usage according to your configured | ||
* callback URL | ||
* | ||
* <p>see <a | ||
* href="https://developers.sinch.com/docs/numbers/api-reference/numbers/tag/Callbacks/#tag/Callbacks/operation/ImportedNumberService_EventsCallback">https://developers.sinch.com/docs/numbers/api-reference/numbers/tag/Callbacks/#tag/Callbacks/operation/ImportedNumberService_EventsCallback</a> | ||
* | ||
* @since 1.0 | ||
*/ | ||
public interface WebHooksService { | ||
|
||
/** | ||
* This function can be called to deserialize received payload onto callback. Function return Java | ||
* class instance from un-serialized payload | ||
* | ||
* @param jsonPayload Received payload to be un-serialized | ||
* @return The decoded event notification instance class | ||
* @since 1.0 | ||
*/ | ||
EventNotification unserializeEventNotification(String jsonPayload) throws ApiMappingException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
client/src/main/com/sinch/sdk/domains/numbers/adapters/WebHooksService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.sinch.sdk.domains.numbers.adapters; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.sinch.sdk.core.exceptions.ApiMappingException; | ||
import com.sinch.sdk.core.utils.databind.Mapper; | ||
import com.sinch.sdk.domains.numbers.adapters.converters.CallbackPayloadDtoConverter; | ||
import com.sinch.sdk.domains.numbers.models.dto.v1.CallbackPayloadDto; | ||
import com.sinch.sdk.domains.numbers.models.webhooks.EventNotification; | ||
|
||
public class WebHooksService implements com.sinch.sdk.domains.numbers.WebHooksService { | ||
|
||
@Override | ||
public EventNotification unserializeEventNotification(String jsonPayload) | ||
throws ApiMappingException { | ||
|
||
try { | ||
CallbackPayloadDto dto = | ||
Mapper.getInstance().readValue(jsonPayload, CallbackPayloadDto.class); | ||
if (null != dto) { | ||
return CallbackPayloadDtoConverter.convert(dto); | ||
} | ||
throw new ApiMappingException(jsonPayload, null); | ||
|
||
} catch (JsonProcessingException e) { | ||
throw new ApiMappingException(jsonPayload, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...c/main/com/sinch/sdk/domains/numbers/adapters/converters/CallbackPayloadDtoConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.sinch.sdk.domains.numbers.adapters.converters; | ||
|
||
import com.sinch.sdk.domains.numbers.models.SmsErrorCode; | ||
import com.sinch.sdk.domains.numbers.models.dto.v1.CallbackPayloadDto; | ||
import com.sinch.sdk.domains.numbers.models.webhooks.EventNotification; | ||
import com.sinch.sdk.domains.numbers.models.webhooks.EventStatus; | ||
import com.sinch.sdk.domains.numbers.models.webhooks.EventType; | ||
import com.sinch.sdk.domains.numbers.models.webhooks.ResourceType; | ||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeParseException; | ||
|
||
public class CallbackPayloadDtoConverter { | ||
|
||
public static EventNotification convert(CallbackPayloadDto dto) { | ||
|
||
String eventId = dto.getEventId(); | ||
// FIXME: Currently Numbers API return a string without timezone | ||
// Workaround: | ||
// 1. try to parse as ISO8601 (with timezone) | ||
// 2. If failure: fallback to a local date time in UTC time zoe | ||
Instant timestamp = null; | ||
if (null != dto.getTimestamp()) { | ||
try { | ||
timestamp = Instant.parse(dto.getTimestamp()); | ||
} catch (DateTimeParseException e) { | ||
timestamp = LocalDateTime.parse(dto.getTimestamp()).toInstant(ZoneOffset.UTC); | ||
} | ||
} | ||
String projectId = dto.getProjectId(); | ||
String resourceId = dto.getResourceId(); | ||
ResourceType resourceType = ResourceType.from(dto.getResourceType()); | ||
EventType eventType = EventType.from(dto.getEventType()); | ||
EventStatus status = EventStatus.from(dto.getStatus()); | ||
SmsErrorCode failureCode = SmsErrorCode.from(dto.getFailureCode()); | ||
|
||
return new EventNotification( | ||
eventId, timestamp, projectId, resourceId, resourceType, eventType, status, failureCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
client/src/main/com/sinch/sdk/domains/numbers/models/webhooks/EventNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.sinch.sdk.domains.numbers.models.webhooks; | ||
|
||
import com.sinch.sdk.domains.numbers.models.SmsErrorCode; | ||
import java.time.Instant; | ||
|
||
/** A notification of an event sent to your configured callback URL. */ | ||
public class EventNotification { | ||
|
||
private final String eventId; | ||
private final Instant timestamp; | ||
private final String projectId; | ||
private final String resourceId; | ||
private final ResourceType resourceType; | ||
private final EventType eventType; | ||
private final EventStatus status; | ||
private final SmsErrorCode failureCode; | ||
|
||
/** | ||
* @param eventId The ID of the event | ||
* @param timestamp The date and time when the callback was created and added to the callbacks | ||
* queue | ||
* @param projectId The ID of the project to which the event belongs | ||
* @param resourceId The unique identifier of the resource, depending on the resource type. For | ||
* example, a phone number, a hosting order ID, or a brand ID. | ||
* @param resourceType The type of the resource | ||
* @param eventType The type of the event | ||
* @param status The status of the event | ||
* @param failureCode If the status is FAILED, a failure code will be provided. For numbers | ||
* provisioning to SMS platform, there won't be any extra failureCode, as the result is | ||
* binary. For campaign provisioning-related failures, refer to the list for the possible | ||
* values. | ||
*/ | ||
public EventNotification( | ||
String eventId, | ||
Instant timestamp, | ||
String projectId, | ||
String resourceId, | ||
ResourceType resourceType, | ||
EventType eventType, | ||
EventStatus status, | ||
SmsErrorCode failureCode) { | ||
this.eventId = eventId; | ||
this.timestamp = timestamp; | ||
this.projectId = projectId; | ||
this.resourceId = resourceId; | ||
this.resourceType = resourceType; | ||
this.eventType = eventType; | ||
this.status = status; | ||
this.failureCode = failureCode; | ||
} | ||
|
||
public String getEventId() { | ||
return eventId; | ||
} | ||
|
||
public Instant getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public String getProjectId() { | ||
return projectId; | ||
} | ||
|
||
public String getResourceId() { | ||
return resourceId; | ||
} | ||
|
||
public ResourceType getResourceType() { | ||
return resourceType; | ||
} | ||
|
||
public EventType getEventType() { | ||
return eventType; | ||
} | ||
|
||
public EventStatus getStatus() { | ||
return status; | ||
} | ||
|
||
public SmsErrorCode getFailureCode() { | ||
return failureCode; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "EventNotification{" | ||
+ "eventId='" | ||
+ eventId | ||
+ '\'' | ||
+ ", timestamp=" | ||
+ timestamp | ||
+ ", projectId='" | ||
+ projectId | ||
+ '\'' | ||
+ ", resourceId='" | ||
+ resourceId | ||
+ '\'' | ||
+ ", resourceType=" | ||
+ resourceType | ||
+ ", eventType=" | ||
+ eventType | ||
+ ", status=" | ||
+ status | ||
+ ", failureCode=" | ||
+ failureCode | ||
+ '}'; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
client/src/main/com/sinch/sdk/domains/numbers/models/webhooks/EventStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.sinch.sdk.domains.numbers.models.webhooks; | ||
|
||
import com.sinch.sdk.core.utils.EnumDynamic; | ||
import com.sinch.sdk.core.utils.EnumSupportDynamic; | ||
import java.util.Arrays; | ||
import java.util.stream.Stream; | ||
|
||
public final class EventStatus extends EnumDynamic<String, EventStatus> { | ||
|
||
/** | ||
* @see <a | ||
* href="https://developers.sinch.com/docs/numbers/api-reference/numbers/tag/Callbacks/#tag/Callbacks/operation/ImportedNumberService_EventsCallback!path=status&t=request/">https://developers.sinch.com/docs/numbers/api-reference/numbers/tag/Callbacks/#tag/Callbacks/operation/ImportedNumberService_EventsCallback!path=status&t=request/</a> | ||
* @since 1.0 | ||
*/ | ||
public static final EventStatus SUCCEEDED = new EventStatus("SUCCEEDED"); | ||
|
||
public static final EventStatus FAILED = new EventStatus("FAILED"); | ||
|
||
private static final EnumSupportDynamic<String, EventStatus> ENUM_SUPPORT = | ||
new EnumSupportDynamic<>( | ||
EventStatus.class, EventStatus::new, Arrays.asList(SUCCEEDED, FAILED)); | ||
|
||
EventStatus(String value) { | ||
super(value); | ||
} | ||
|
||
public static Stream<EventStatus> values() { | ||
return ENUM_SUPPORT.values(); | ||
} | ||
|
||
public static EventStatus from(String value) { | ||
return ENUM_SUPPORT.from(value); | ||
} | ||
|
||
public static String valueOf(EventStatus e) { | ||
return ENUM_SUPPORT.valueOf(e); | ||
} | ||
} |
Oops, something went wrong.