-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec09052
commit 58fa01c
Showing
11 changed files
with
212 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Apps.Lionbridge; | ||
|
||
public class ApplicationConstants | ||
{ | ||
public const string BlackbirdToken = "#{APP_LIONBRIDGE_BLACKBIRD_TOKEN}#"; // bridge validates this token | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Apps.Lionbridge.Webhooks.Bridge.Models; | ||
using Blackbird.Applications.Sdk.Common.Authentication; | ||
using RestSharp; | ||
|
||
namespace Apps.Lionbridge.Webhooks.Bridge; | ||
|
||
public class BridgeService | ||
{ | ||
private string BridgeServiceUrl { get; set; } | ||
|
||
public BridgeService(IEnumerable<AuthenticationCredentialsProvider> authenticationCredentialsProviders, string bridgeServiceUrl) | ||
{ | ||
BridgeServiceUrl = bridgeServiceUrl; | ||
} | ||
|
||
public void Subscribe(string _event, string listenerId, string url) | ||
{ | ||
var client = new RestClient(BridgeServiceUrl); | ||
var request = new RestRequest($"/{listenerId}/{_event}", Method.Post); | ||
request.AddHeader("Blackbird-Token", ApplicationConstants.BlackbirdToken); | ||
request.AddBody(url); | ||
|
||
client.Execute(request); | ||
} | ||
|
||
public void Unsubscribe(string _event, string listenerId, string url) | ||
{ | ||
var client = new RestClient(BridgeServiceUrl); | ||
var requestGet = new RestRequest($"/{listenerId}/{_event}", Method.Get); | ||
requestGet.AddHeader("Blackbird-Token", ApplicationConstants.BlackbirdToken); | ||
var webhooks = client.Get<List<BridgeGetResponse>>(requestGet); | ||
var webhook = webhooks.FirstOrDefault(w => w.Value == url); | ||
|
||
var requestDelete = new RestRequest($"/{listenerId}/{_event}/{webhook.Id}", Method.Delete); | ||
requestDelete.AddHeader("Blackbird-Token", ApplicationConstants.BlackbirdToken); | ||
client.Delete(requestDelete); | ||
} | ||
} |
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,8 @@ | ||
namespace Apps.Lionbridge.Webhooks.Bridge.Models; | ||
|
||
public class BridgeGetResponse | ||
{ | ||
public string Id { get; set; } | ||
|
||
public string Value { get; set; } | ||
} |
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,85 @@ | ||
using Apps.Lionbridge.Api; | ||
using Apps.Lionbridge.Constants; | ||
using Apps.Lionbridge.Webhooks.Bridge; | ||
using Apps.Lionbridge.Webhooks.Responses; | ||
using Blackbird.Applications.Sdk.Common; | ||
using Blackbird.Applications.Sdk.Common.Authentication; | ||
using Blackbird.Applications.Sdk.Common.Invocation; | ||
using Blackbird.Applications.Sdk.Common.Webhooks; | ||
using RestSharp; | ||
|
||
namespace Apps.Lionbridge.Webhooks.Handlers; | ||
|
||
public class BaseWebhookHandler : BaseInvocable, IWebhookEventHandler | ||
{ | ||
private readonly string _bridgeServiceUrl; | ||
|
||
private string SubscriptionEvent { get; set; } | ||
|
||
protected readonly LionbridgeClient Client; | ||
|
||
public BaseWebhookHandler(InvocationContext invocationContext, string subEvent) : base(invocationContext) | ||
{ | ||
SubscriptionEvent = subEvent; | ||
_bridgeServiceUrl = $"{invocationContext.UriInfo.BridgeServiceUrl.ToString().TrimEnd('/')}/webhooks/lionbridge"; | ||
Client = new LionbridgeClient(invocationContext.AuthenticationCredentialsProviders); | ||
} | ||
|
||
|
||
public async Task SubscribeAsync(IEnumerable<AuthenticationCredentialsProvider> authenticationCredentialsProviders, | ||
Dictionary<string, string> values) | ||
{ | ||
var listener = await GetListenerIdAsync(); | ||
if (listener == null) | ||
{ | ||
listener = await CreateListenerAsync(); | ||
} | ||
|
||
var bridge = new BridgeService(authenticationCredentialsProviders, _bridgeServiceUrl); | ||
bridge.Subscribe(SubscriptionEvent, listener.ListenerId, values["payloadUrl"]); | ||
} | ||
|
||
public async Task UnsubscribeAsync(IEnumerable<AuthenticationCredentialsProvider> authenticationCredentialsProviders, | ||
Dictionary<string, string> values) | ||
{ | ||
var listener = await GetListenerIdAsync(); | ||
if (listener != null) | ||
{ | ||
var bridge = new BridgeService(authenticationCredentialsProviders, _bridgeServiceUrl); | ||
bridge.Unsubscribe(SubscriptionEvent, listener.ListenerId, values["payloadUrl"]); | ||
await DeleteListenerAsync(listener.ListenerId); | ||
} | ||
} | ||
|
||
private async Task<ListenerDto?> GetListenerIdAsync() | ||
{ | ||
var request = new LionbridgeRequest(ApiEndpoints.Listeners, Method.Get); | ||
var response = await Client.ExecuteWithErrorHandling<ListenersResponse>(request); | ||
|
||
return response.Embedded.Listeners.FirstOrDefault(x => x.Type == SubscriptionEvent); | ||
} | ||
|
||
private async Task<ListenerDto> CreateListenerAsync() | ||
{ | ||
var request = new LionbridgeRequest(ApiEndpoints.Listeners, Method.Post) | ||
.AddJsonBody(new | ||
{ | ||
uri = _bridgeServiceUrl, | ||
type = SubscriptionEvent, | ||
statusCodes = new [] | ||
{ | ||
"IN_TRANSLATION" // TODO: change in the future | ||
}, | ||
acknowledgeStatusUpdate = true | ||
}); | ||
|
||
var response = await Client.ExecuteWithErrorHandling<ListenerDto>(request); | ||
return response; | ||
} | ||
|
||
private async Task DeleteListenerAsync(string listenerId) | ||
{ | ||
var request = new LionbridgeRequest($"{ApiEndpoints.Listeners}/{listenerId}", Method.Delete); | ||
await Client.ExecuteWithErrorHandling(request); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Apps.Lionbridge/Webhooks/Handlers/JobStatusUpdatedHandler.cs
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,10 @@ | ||
using Blackbird.Applications.Sdk.Common.Invocation; | ||
|
||
namespace Apps.Lionbridge.Webhooks.Handlers; | ||
|
||
public class JobStatusUpdatedHandler : BaseWebhookHandler | ||
{ | ||
const string SubscriptionEvent = "JOB_STATUS_UPDATED"; | ||
|
||
public JobStatusUpdatedHandler(InvocationContext invocationContext) : base(invocationContext, SubscriptionEvent) { } | ||
} |
10 changes: 10 additions & 0 deletions
10
Apps.Lionbridge/Webhooks/Handlers/RequestStatusUpdatedHandler.cs
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,10 @@ | ||
using Blackbird.Applications.Sdk.Common.Invocation; | ||
|
||
namespace Apps.Lionbridge.Webhooks.Handlers; | ||
|
||
public class RequestStatusUpdatedHandler : BaseWebhookHandler | ||
{ | ||
const string SubscriptionEvent = "REQUEST_STATUS_UPDATED"; | ||
|
||
public RequestStatusUpdatedHandler(InvocationContext invocationContext) : base(invocationContext, SubscriptionEvent) { } | ||
} |
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,6 @@ | ||
namespace Apps.Lionbridge.Webhooks.Inputs; | ||
|
||
public class WebhookInput | ||
{ | ||
public string ListenerId { get; set; } | ||
} |
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,27 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Apps.Lionbridge.Webhooks.Responses; | ||
|
||
public class ListenerDto | ||
{ | ||
[JsonProperty("listenerId")] | ||
public string ListenerId { get; set; } | ||
|
||
[JsonProperty("uri")] | ||
public string Uri { get; set; } | ||
|
||
[JsonProperty("type")] | ||
public string Type { get; set; } | ||
|
||
[JsonProperty("statusCodes")] | ||
public string[] StatusCodes { get; set; } | ||
|
||
[JsonProperty("authType")] | ||
public string AuthType { get; set; } | ||
|
||
[JsonProperty("acknowledgeStatusUpdate")] | ||
public bool AcknowledgeStatusUpdate { get; set; } | ||
|
||
[JsonProperty("disabled")] | ||
public bool Disabled { get; set; } | ||
} |
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,10 @@ | ||
using Apps.Lionbridge.Models.Dtos; | ||
|
||
namespace Apps.Lionbridge.Webhooks.Responses; | ||
|
||
public class ListenersResponse : EmbeddedItemsWrapper<ListenersWrapper> | ||
{ | ||
|
||
} | ||
|
||
public record ListenersWrapper(IEnumerable<ListenerDto> Listeners); |
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