-
Notifications
You must be signed in to change notification settings - Fork 10
DEVORTEX-5439 add webhooks api #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
steplov
wants to merge
4
commits into
master
Choose a base branch
from
DEVORTEX-5439
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,18 @@ | ||
import { SubscriptionEvent } from "../params/subscription-event"; | ||
import { SubscriptionRequestHeader } from "../params/subscription-request-header"; | ||
|
||
interface SubscriptionDto { | ||
subscriptionUid: string; | ||
subscriptionName: string; | ||
subscriptionUrl: string; | ||
accountUid: string; | ||
enabled: boolean; | ||
userUid: string; | ||
description: string | null; | ||
createdDate: string; | ||
requestHeaders: SubscriptionRequestHeader[]; | ||
events: SubscriptionEvent[]; | ||
projectUids: string[]; | ||
} | ||
|
||
export { SubscriptionDto }; |
This file contains hidden or 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,5 @@ | ||
interface SubscriptionSecretDto { | ||
payloadSecret: string; | ||
} | ||
|
||
export { SubscriptionSecretDto }; | ||
This file contains hidden or 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 @@ | ||
interface SubscriptionStatisticsDto { | ||
failEvents: number; | ||
pendingEvents: number; | ||
sendingEvents: number; | ||
successEvents: number; | ||
} | ||
|
||
export { SubscriptionStatisticsDto }; |
This file contains hidden or 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,7 @@ | ||
class WebhookEventTypeDto { | ||
eventType: string; | ||
description: string; | ||
schema: Record<string, any>; | ||
} | ||
|
||
export { WebhookEventTypeDto }; |
This file contains hidden or 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 @@ | ||
export { SmartlingWebhooksApi } from "./smartling-webhooks-api"; | ||
export { SubscriptionDto } from "./dto/subscription-dto"; | ||
export { SubscriptionSecretDto } from "./dto/subscription-secret-dto"; | ||
export { WebhookEventTypeDto } from "./dto/webhook-event-type.dto"; | ||
export { SubscriptionEvent } from "./params/subscription-event"; | ||
export { CreateSubscriptionParameters } from "./params/create-subscription-parameters"; | ||
export { UpdateSubscriptionParameters } from "./params/update-subscription-parameters"; | ||
export { SubscriptionRequestHeader } from "./params/subscription-request-header"; | ||
export { UpdateSubscriptionSecretParameters } from "./params/update-subscription-secret-parameters"; | ||
export { GetSubscriptionEventsParameters } from "./params/get-subscription-events-parameters"; |
This file contains hidden or 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,9 @@ | ||
import { CreateUpdateSubscriptionParameters } from "./create-update-subscription-parameters"; | ||
|
||
export class CreateSubscriptionParameters extends CreateUpdateSubscriptionParameters { | ||
setPayloadSecret(payloadSecret: string) { | ||
this.set("payloadSecret", payloadSecret); | ||
|
||
return this; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
api/webhooks/params/create-update-subscription-parameters.ts
This file contains hidden or 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,70 @@ | ||
import { BaseParameters } from "../../parameters"; | ||
import { SmartlingException } from "../../exception"; | ||
import { SubscriptionEvent } from "./subscription-event"; | ||
import { SubscriptionRequestHeader } from "./subscription-request-header"; | ||
|
||
const MAX_EVENTS_SIZE = 1000; | ||
const MAX_SUBSCRIPTION_HEADERS_SIZE = 20; | ||
const MAX_PROJECT_UIDS_SIZE = 10; | ||
|
||
export abstract class CreateUpdateSubscriptionParameters extends BaseParameters { | ||
constructor( | ||
subscriptionName: string, | ||
subscriptionUrl: string, | ||
events: SubscriptionEvent[] | ||
) { | ||
super(); | ||
|
||
if (!subscriptionName) { | ||
throw new SmartlingException("Subscription name is required."); | ||
} | ||
|
||
if (!subscriptionUrl) { | ||
throw new SmartlingException("Subscription url is required."); | ||
} | ||
|
||
if (!events.length) { | ||
throw new SmartlingException("At least one event is required."); | ||
} | ||
|
||
if (events.length > MAX_EVENTS_SIZE) { | ||
throw new SmartlingException(`The request contains too many events: ${events.length}. Maximum allowed events number is ${MAX_EVENTS_SIZE}.`); | ||
} | ||
|
||
this.set("subscriptionName", subscriptionName); | ||
this.set("subscriptionUrl", subscriptionUrl); | ||
this.set("events", events); | ||
} | ||
|
||
setDescription(description: string): this { | ||
this.set("description", description); | ||
|
||
return this; | ||
} | ||
|
||
setRequestHeaders(requestHeaders: SubscriptionRequestHeader[]) { | ||
if (!requestHeaders.length) { | ||
throw new SmartlingException("At least one subscription header is required."); | ||
} | ||
|
||
if (requestHeaders.length > MAX_SUBSCRIPTION_HEADERS_SIZE) { | ||
throw new SmartlingException(`The request contains too many subscription headers: ${requestHeaders.length}. Maximum allowed subscription headers number is ${MAX_SUBSCRIPTION_HEADERS_SIZE}.`); | ||
} | ||
|
||
this.set("requestHeaders", requestHeaders); | ||
return this; | ||
} | ||
|
||
setProjectUids(projectUids: string[]) { | ||
if (!projectUids.length) { | ||
throw new SmartlingException("At least one project uid is required."); | ||
} | ||
|
||
if (projectUids.length > MAX_PROJECT_UIDS_SIZE) { | ||
throw new SmartlingException(`The request contains too many project uids: ${projectUids.length}. Maximum allowed project uids number is ${MAX_PROJECT_UIDS_SIZE}.`); | ||
} | ||
this.set("projectUids", projectUids); | ||
|
||
return this; | ||
} | ||
} |
This file contains hidden or 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,11 @@ | ||
import { BaseParameters } from "../../parameters"; | ||
|
||
export class GetSubscriptionEventsParameters extends BaseParameters { | ||
setLimit(limit: number) { | ||
if (limit > 0) { | ||
this.set("limit", limit); | ||
} | ||
|
||
return this; | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
interface SubscriptionEvent { | ||
type: string; | ||
schemaVersion: string; | ||
} | ||
|
||
export { SubscriptionEvent }; |
This file contains hidden or 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 @@ | ||
interface SubscriptionRequestHeader { | ||
headerName: string; | ||
headerValue: string; | ||
} | ||
|
||
export { SubscriptionRequestHeader }; |
This file contains hidden or 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,19 @@ | ||
import { CreateUpdateSubscriptionParameters } from "./create-update-subscription-parameters"; | ||
import { SubscriptionEvent } from "./subscription-event"; | ||
|
||
export class UpdateSubscriptionParameters extends CreateUpdateSubscriptionParameters { | ||
PavelLoparev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
constructor( | ||
subscriptionName: string, | ||
subscriptionUrl: string, | ||
events: SubscriptionEvent[], | ||
enabled: boolean | ||
) { | ||
super( | ||
subscriptionName, | ||
subscriptionUrl, | ||
events | ||
); | ||
|
||
this.set("enabled", enabled); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
api/webhooks/params/update-subscription-secret-parameters.ts
This file contains hidden or 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,14 @@ | ||
import { BaseParameters } from "../../parameters"; | ||
import { SmartlingException } from "../../exception"; | ||
|
||
export class UpdateSubscriptionSecretParameters extends BaseParameters { | ||
constructor(payloadSecret: string) { | ||
super(); | ||
|
||
if (!payloadSecret) { | ||
throw new SmartlingException("Payload secret is required."); | ||
} | ||
|
||
this.set("payloadSecret", payloadSecret); | ||
} | ||
} |
This file contains hidden or 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,148 @@ | ||
import { SmartlingBaseApi } from "../base"; | ||
import { SmartlingAuthApi } from "../auth"; | ||
import { Logger } from "../logger"; | ||
import { SmartlingListResponse } from "../http/smartling-list-response"; | ||
import { SubscriptionDto } from "./dto/subscription-dto"; | ||
import { SubscriptionSecretDto } from "./dto/subscription-secret-dto"; | ||
import { SubscriptionStatisticsDto } from "./dto/subscription-statistics-dto"; | ||
import { WebhookEventTypeDto } from "./dto/webhook-event-type.dto"; | ||
import { CreateSubscriptionParameters } from "./params/create-subscription-parameters"; | ||
import { UpdateSubscriptionParameters } from "./params/update-subscription-parameters"; | ||
import { UpdateSubscriptionSecretParameters } from "./params/update-subscription-secret-parameters"; | ||
|
||
export class SmartlingWebhooksApi extends SmartlingBaseApi { | ||
constructor(smartlingApiBaseUrl: string, authApi: SmartlingAuthApi, logger: Logger) { | ||
super(logger); | ||
this.authApi = authApi; | ||
this.entrypoint = `${smartlingApiBaseUrl}/webhooks-api/v2`; | ||
} | ||
|
||
getSubscriptions( | ||
accountUid: string | ||
): Promise<SmartlingListResponse<SubscriptionDto>> { | ||
return this.makeRequest( | ||
"get", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions` | ||
); | ||
} | ||
PavelLoparev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
getSubscription( | ||
accountUid: string, | ||
subscriptionUid: string | ||
): Promise<SubscriptionDto> { | ||
return this.makeRequest( | ||
"get", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions/${subscriptionUid}` | ||
); | ||
} | ||
|
||
createSubscription( | ||
accountUid: string, | ||
params: CreateSubscriptionParameters | ||
): Promise<SubscriptionDto> { | ||
return this.makeRequest( | ||
"post", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions`, | ||
JSON.stringify(params.export()) | ||
); | ||
} | ||
|
||
updateSubscription( | ||
accountUid: string, | ||
subscriptionUid: string, | ||
params: UpdateSubscriptionParameters | ||
): Promise<SubscriptionDto> { | ||
return this.makeRequest( | ||
"put", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions/${subscriptionUid}`, | ||
JSON.stringify(params.export()) | ||
); | ||
} | ||
|
||
deleteSubscription( | ||
accountUid: string, | ||
subscriptionUid: string | ||
): Promise<void> { | ||
return this.makeRequest( | ||
"delete", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions/${subscriptionUid}` | ||
); | ||
} | ||
|
||
enableSubscription( | ||
accountUid: string, | ||
subscriptionUid: string | ||
): Promise<void> { | ||
return this.makeRequest( | ||
"post", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions/${subscriptionUid}/enable` | ||
); | ||
} | ||
|
||
disableSubscription( | ||
accountUid: string, | ||
subscriptionUid: string | ||
): Promise<void> { | ||
return this.makeRequest( | ||
"post", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions/${subscriptionUid}/disable` | ||
); | ||
} | ||
|
||
testSubscription( | ||
accountUid: string, | ||
subscriptionUid: string | ||
): Promise<void> { | ||
return this.makeRequest( | ||
"post", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions/${subscriptionUid}/test` | ||
); | ||
} | ||
PavelLoparev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
getSubscriptionSecret( | ||
accountUid: string, | ||
subscriptionUid: string | ||
): Promise<SubscriptionSecretDto> { | ||
return this.makeRequest( | ||
"get", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions/${subscriptionUid}/secret` | ||
); | ||
} | ||
|
||
updateSubscriptionSecret( | ||
accountUid: string, | ||
subscriptionUid: string, | ||
params: UpdateSubscriptionSecretParameters | ||
): Promise<SubscriptionSecretDto> { | ||
return this.makeRequest( | ||
"put", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions/${subscriptionUid}/secret`, | ||
JSON.stringify(params.export()) | ||
); | ||
} | ||
|
||
getSubscriptionStatistics( | ||
accountUid: string, | ||
subscriptionUid: string | ||
): Promise<SubscriptionStatisticsDto> { | ||
return this.makeRequest( | ||
"get", | ||
`${this.entrypoint}/accounts/${accountUid}/subscriptions/${subscriptionUid}/statistics` | ||
); | ||
} | ||
|
||
// getSubscriptionEvents() {} | ||
|
||
// getSubscriptionEvent() {} | ||
|
||
// sendSubscriptionEvent() {} | ||
|
||
// getSubscriptionEventAttempts() {} | ||
|
||
getAvailableEventTypes(accountUid: string): Promise<WebhookEventTypeDto[]> { | ||
return this.makeRequest( | ||
"get", | ||
`${this.entrypoint}/accounts/${accountUid}/available-event-types` | ||
); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no actions points: weird naming. it's already clear this secret is in payload.