Skip to content

Commit 0b83384

Browse files
authored
Merge pull request #122 from Smartling/DEVORTEX-5159-File-translation-callbacks
DEVORTEX-5159 File translations callbacks
2 parents a69e8dc + 12722b5 commit 0b83384

10 files changed

+196
-7
lines changed

api/file-translations/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { LanguageDetectionDto } from "./dto/language-detection-dto";
1111
import { LanguageDetectionStatusDto } from "./dto/language-detection-status-dto";
1212
import { TranslatedFileDto } from "./dto/translated-file-dto";
1313
import { ResponseBodyType } from "../base/enum/response-body-type";
14+
import { LanguageDetectionParameters } from "./params/language-detection-parameters";
1415

1516
export class SmartlingFileTranslationsApi extends SmartlingBaseApi {
1617
constructor(smartlingApiBaseUrl: string, authApi: SmartlingAuthApi, logger: Logger) {
@@ -123,10 +124,13 @@ export class SmartlingFileTranslationsApi extends SmartlingBaseApi {
123124
);
124125
}
125126

126-
async detectFileLanguage(accountUid: string, fileUid: string): Promise<LanguageDetectionDto> {
127+
async detectFileLanguage(
128+
accountUid: string, fileUid: string, params?: LanguageDetectionParameters
129+
): Promise<LanguageDetectionDto> {
127130
return await this.makeRequest(
128131
"post",
129-
`${this.entrypoint}/${accountUid}/files/${fileUid}/language-detection`
132+
`${this.entrypoint}/${accountUid}/files/${fileUid}/language-detection`,
133+
JSON.stringify(params?.export())
130134
);
131135
}
132136

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { BaseParameters } from "../../parameters";
2+
import { FtsCallback } from "./fts-callback-parameter";
3+
4+
export class BaseParametersWithCallback extends BaseParameters {
5+
setCallback(callback: FtsCallback): this {
6+
this.parameters.callback = callback;
7+
return this;
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
enum FtsCallbackMethod {
2+
// Other HTTP methods are not supported by API for now
3+
POST = "POST"
4+
}
5+
6+
export { FtsCallbackMethod };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { FtsCallbackMethod } from "./fts-callback-method";
2+
3+
interface FtsCallback {
4+
url: string;
5+
httpMethod: FtsCallbackMethod;
6+
userData?: string
7+
}
8+
9+
export { FtsCallback };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { BaseParametersWithCallback } from "./base-parameters-with-callback";
2+
3+
export class LanguageDetectionParameters extends BaseParametersWithCallback {
4+
}

api/file-translations/params/translate-file-parameters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { BaseParameters } from "../../parameters";
1+
import { BaseParametersWithCallback } from "./base-parameters-with-callback";
22

3-
export class TranslateFileParameters extends BaseParameters {
3+
export class TranslateFileParameters extends BaseParametersWithCallback {
44
setSourceLocaleId(sourceLocaleId: string): TranslateFileParameters {
55
this.set("sourceLocaleId", sourceLocaleId);
66
return this;

index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export * from "./api/context/params/list-parameters";
1616
export * from "./api/context/context-http-response";
1717
export * from "./api/exception/index";
1818
export * from "./api/file-translations/index";
19+
export * from "./api/file-translations/params/fts-callback-parameter";
20+
export * from "./api/file-translations/params/fts-callback-method";
1921
export * from "./api/file-translations/params/fts-upload-file-parameters";
22+
export * from "./api/file-translations/params/language-detection-parameters";
2023
export * from "./api/file-translations/params/translate-file-parameters";
2124
export * from "./api/file-translations/dto/error";
2225
export * from "./api/file-translations/dto/fts-uploaded-file-dto";

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "smartling-api-sdk-nodejs",
3-
"version": "2.17.0",
3+
"version": "2.18.0",
44
"description": "Package for Smartling API",
55
"main": "built/index.js",
66
"engines": {

test/file-translations.spec.ts

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { FtsUploadFileParameters } from "../api/file-translations/params/fts-upl
99
import { FileType } from "../api/files/params/file-type";
1010
import { streamToString } from "./stream-to-string";
1111
import { TranslateFileParameters } from "../api/file-translations/params/translate-file-parameters";
12+
import { FtsCallbackMethod } from "../api/file-translations/params/fts-callback-method";
13+
import { LanguageDetectionParameters } from "../api/file-translations/params/language-detection-parameters";
1214

1315
describe("SmartlingFileTranslationsApi class tests.", () => {
1416
const userAgent = "test_user_agent";
@@ -327,6 +329,78 @@ describe("SmartlingFileTranslationsApi class tests.", () => {
327329
);
328330
});
329331

332+
it("Translate file with callback and without user data", async () => {
333+
const params = new TranslateFileParameters();
334+
335+
params
336+
.setSourceLocaleId("en-US")
337+
.setTargetLocaleIds(["de-DE", "fr-FR"])
338+
.setCallback({
339+
url: "https://myhost.com",
340+
httpMethod: FtsCallbackMethod.POST
341+
});
342+
343+
await fileTranslationsApi.translateFile(accountUid, fileUid, params);
344+
345+
sinon.assert.calledOnceWithExactly(
346+
fileTranslationsApiFetchStub,
347+
`https://test.com/file-translations-api/v2/accounts/${accountUid}/files/${fileUid}/mt`,
348+
{
349+
method: "post",
350+
headers: {
351+
Authorization: "test_token_type test_access_token",
352+
"Content-Type": "application/json",
353+
"User-Agent": userAgent
354+
},
355+
body: JSON.stringify({
356+
sourceLocaleId: "en-US",
357+
targetLocaleIds: ["de-DE", "fr-FR"],
358+
callback: {
359+
url: "https://myhost.com",
360+
httpMethod: "POST"
361+
}
362+
})
363+
}
364+
);
365+
});
366+
367+
it("Translate file with callback and user data", async () => {
368+
const params = new TranslateFileParameters();
369+
370+
params
371+
.setSourceLocaleId("en-US")
372+
.setTargetLocaleIds(["de-DE", "fr-FR"])
373+
.setCallback({
374+
url: "https://myhost.com",
375+
httpMethod: FtsCallbackMethod.POST,
376+
userData: "test user data"
377+
});
378+
379+
await fileTranslationsApi.translateFile(accountUid, fileUid, params);
380+
381+
sinon.assert.calledOnceWithExactly(
382+
fileTranslationsApiFetchStub,
383+
`https://test.com/file-translations-api/v2/accounts/${accountUid}/files/${fileUid}/mt`,
384+
{
385+
method: "post",
386+
headers: {
387+
Authorization: "test_token_type test_access_token",
388+
"Content-Type": "application/json",
389+
"User-Agent": userAgent
390+
},
391+
body: JSON.stringify({
392+
sourceLocaleId: "en-US",
393+
targetLocaleIds: ["de-DE", "fr-FR"],
394+
callback: {
395+
url: "https://myhost.com",
396+
httpMethod: "POST",
397+
userData: "test user data"
398+
}
399+
})
400+
}
401+
);
402+
});
403+
330404
it("Get translation progress", async () => {
331405
await fileTranslationsApi.getTranslationProgress(accountUid, fileUid, mtUid);
332406

@@ -547,6 +621,86 @@ describe("SmartlingFileTranslationsApi class tests.", () => {
547621
);
548622
});
549623

624+
it("Detect file language with empty parameters", async () => {
625+
await fileTranslationsApi.detectFileLanguage(
626+
accountUid, fileUid, new LanguageDetectionParameters()
627+
);
628+
629+
sinon.assert.calledOnceWithExactly(
630+
fileTranslationsApiFetchStub,
631+
`https://test.com/file-translations-api/v2/accounts/${accountUid}/files/${fileUid}/language-detection`,
632+
{
633+
method: "post",
634+
headers: {
635+
Authorization: "test_token_type test_access_token",
636+
"Content-Type": "application/json",
637+
"User-Agent": userAgent
638+
},
639+
body: JSON.stringify({})
640+
}
641+
);
642+
});
643+
644+
it("Detect file language with callback and without user data", async () => {
645+
const params = new LanguageDetectionParameters()
646+
.setCallback({
647+
url: "https://myhost.com",
648+
httpMethod: FtsCallbackMethod.POST
649+
});
650+
651+
await fileTranslationsApi.detectFileLanguage(accountUid, fileUid, params);
652+
653+
sinon.assert.calledOnceWithExactly(
654+
fileTranslationsApiFetchStub,
655+
`https://test.com/file-translations-api/v2/accounts/${accountUid}/files/${fileUid}/language-detection`,
656+
{
657+
method: "post",
658+
headers: {
659+
Authorization: "test_token_type test_access_token",
660+
"Content-Type": "application/json",
661+
"User-Agent": userAgent
662+
},
663+
body: JSON.stringify({
664+
callback: {
665+
url: "https://myhost.com",
666+
httpMethod: "POST"
667+
}
668+
})
669+
}
670+
);
671+
});
672+
673+
it("Detect file language with callback and with user data", async () => {
674+
const params = new LanguageDetectionParameters()
675+
.setCallback({
676+
url: "https://myhost.com",
677+
httpMethod: FtsCallbackMethod.POST,
678+
userData: "test user data"
679+
});
680+
681+
await fileTranslationsApi.detectFileLanguage(accountUid, fileUid, params);
682+
683+
sinon.assert.calledOnceWithExactly(
684+
fileTranslationsApiFetchStub,
685+
`https://test.com/file-translations-api/v2/accounts/${accountUid}/files/${fileUid}/language-detection`,
686+
{
687+
method: "post",
688+
headers: {
689+
Authorization: "test_token_type test_access_token",
690+
"Content-Type": "application/json",
691+
"User-Agent": userAgent
692+
},
693+
body: JSON.stringify({
694+
callback: {
695+
url: "https://myhost.com",
696+
httpMethod: "POST",
697+
userData: "test user data"
698+
}
699+
})
700+
}
701+
);
702+
});
703+
550704
it("Get language detection progress", async () => {
551705
await fileTranslationsApi.getLanguageDetectionProgress(
552706
accountUid,

0 commit comments

Comments
 (0)