Skip to content

Commit 9331357

Browse files
authored
default async for translate (#26)
* improve tests for translate with default async * new response format for translate requests in readme * translate example responses * upgrade dependencies
1 parent 5b04181 commit 9331357

File tree

14 files changed

+692
-543
lines changed

14 files changed

+692
-543
lines changed

README.md

Lines changed: 36 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Intento nodejs
1+
# Intento SDK for Node.js
22

33
An adapter to query Intento API.
44

@@ -8,7 +8,7 @@ To get more information check out [the site](https://inten.to/).
88

99
[API User Manual](https://github.com/intento/intento-api)
1010

11-
In case you don't have a key to use Intento API, please register here [console.inten.to](https://console.inten.to)
11+
In case you don't have a key to use Intento API, please register here [inten.to](https://inten.to)
1212

1313
<!-- TOC depthFrom:2 -->
1414

@@ -31,9 +31,6 @@ In case you don't have a key to use Intento API, please register here [console.i
3131
- [Smart routing](#smart-routing)
3232
- [Basic smart routing](#basic-smart-routing)
3333
- [Specifying a custom routing strategy](#specifying-a-custom-routing-strategy)
34-
- [Async mode](#async-mode)
35-
- [Async mode with awaiting results](#async-mode-with-awaiting-results)
36-
- [Change request frequency](#change-request-frequency)
3734
- [Failover mode](#failover-mode)
3835
- [Using a service provider with your own keys](#using-a-service-provider-with-your-own-keys)
3936
- [Advanced Examples](#advanced-examples)
@@ -220,7 +217,7 @@ To use the smart routing, just omit the `provider` parameter:
220217
```js
221218
client.ai.text.translate
222219
.fulfill({
223-
text: 'A sample text',
220+
text: "How's it going?",
224221
to: 'es'
225222
})
226223
.then(console.log)
@@ -230,128 +227,7 @@ Response:
230227

231228
```json
232229
{
233-
"results": [
234-
"Un texto de ejemplo"
235-
],
236-
"meta": {},
237-
"service": {
238-
"provider": {
239-
"id": "ai.text.translate.microsoft.translator_text_api.2-0",
240-
"name": "Microsoft Translator API"
241-
}
242-
}
243-
}
244-
```
245-
246-
### Specifying a custom routing strategy
247-
248-
By default, when the provider is missing, requests are routed to a provider with the best expected price/performance ratio. This behavior may be controlled by specifying the desired routing strategy in the `routing` parameter. To set up routing for your account contact us at hello@inten.to.
249-
250-
```js
251-
client.ai.text.translate
252-
.fulfill({
253-
text: 'A sample text',
254-
to: 'es',
255-
routing: 'best-quality',
256-
})
257-
.then(console.log)
258-
```
259-
260-
### Async mode
261-
262-
If the server responded with a status of 413 (Request Entity Too Large), then the request data is too large for the synchronous processing. In this case, you should switch to the asynchronous mode by adding `async: true` to the parameters. The current approach to handling the oversized requests [is described in a separate document](https://github.com/intento/intento-api/blob/master/processing-oversized-requests.md).
263-
264-
```js
265-
client.ai.text.translate
266-
.fulfill({
267-
text: [
268-
'A sample text',
269-
'Another sample text'
270-
],
271-
from: 'en',
272-
to: 'es',
273-
async: true,
274-
provider: 'ai.text.translate.yandex.translate_api.1-5'
275-
})
276-
.then(console.log)
277-
```
278-
279-
The response contains `id` of the operation:
280-
281-
```json
282-
{
283-
"id": "ea1684f1-4ec7-431d-9b7e-bfbe98cf0bda"
284-
}
285-
```
286-
287-
Wait for processing to complete. To retrieve the result of the operation, call
288-
289-
```js
290-
client.operations
291-
.fulfill({
292-
id: "ea1684f1-4ec7-431d-9b7e-bfbe98cf0bda"
293-
})
294-
.then(console.log)
295-
```
296-
297-
TTL of the resource is 30 days.
298-
299-
The response
300-
301-
```json
302-
{
303-
"id": "ea1684f1-4ec7-431d-9b7e-bfbe98cf0bda",
304-
"done": true,
305-
"response": [
306-
{
307-
"results": [
308-
"Un texto de ejemplo 1",
309-
"Un texto de ejemplo 2"
310-
],
311-
"meta": {},
312-
"service": {
313-
"provider": {
314-
"id": "ai.text.translate.yandex.translate_api.1-5",
315-
"name": "Yandex Translate API"
316-
}
317-
}
318-
}
319-
]
320-
}
321-
```
322-
323-
If the operation is not completed the value of `done` is false. Wait and make request later.
324-
325-
```json
326-
{
327-
"id": "ea1684f1-4ec7-431d-9b7e-bfbe98cf0bda",
328-
"done": false,
329-
"response": null
330-
}
331-
```
332-
333-
### Async mode with awaiting results
334-
335-
Add `awaitAsync: true` to parameters to get results directly:
336-
337-
```js
338-
client.ai.text.translate
339-
.fulfill({
340-
text: "How's it going?",
341-
to: 'es',
342-
async: true,
343-
awaitAsync: true
344-
})
345-
.then(res => {
346-
console.log(JSON.stringify(res, null, 4))
347-
})
348-
```
349-
350-
The response will be similar to the following one:
351-
352-
```js
353-
{
354-
"id": "qa2wed4r-3e4r-5t6y-6y7u-23456789",
230+
"id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
355231
"done": true,
356232
"response": [
357233
{
@@ -375,49 +251,24 @@ The response will be similar to the following one:
375251
}
376252
],
377253
"meta": {
378-
"providers": [
379-
{
380-
"id": "ai.text.translate.deepl.api",
381-
"name": "DeepL API",
382-
"vendor": "DeepL",
383-
"description": "API",
384-
"logo": "https://inten.to/static/img/api/deepl_translate.png"
385-
}
386-
],
387-
"from": null,
388-
"to": "es",
389-
"format": null,
390-
"glossary": null,
391-
"smart_routing": "default",
392-
"category": "default",
393-
"own_keys": false,
394-
"size": 15,
395-
"intento_user_agent": [
396-
"Intento.NodeJS/0.9.0"
397-
],
398-
"userdata": {}
254+
// information about initial request
399255
},
400256
"error": null
401257
}
402258
```
403259

404-
#### Change request frequency
260+
### Specifying a custom routing strategy
405261

406-
Add `awaitDelay` (number in milliseconds) to parameters to change how often results are requested. It does not speed up the process.
407-
Default value is `1000`.
262+
By default, when the provider is missing, requests are routed to a provider with the best expected price/performance ratio. This behavior may be controlled by specifying the desired routing strategy in the `routing` parameter. To set up routing for your account contact us at hello@inten.to.
408263

409264
```js
410265
client.ai.text.translate
411266
.fulfill({
412-
text: "How's it going?",
267+
text: 'A sample text',
413268
to: 'es',
414-
async: true,
415-
awaitAsync: true,
416-
awaitDelay: 500
417-
})
418-
.then(res => {
419-
console.log(JSON.stringify(res, null, 4))
269+
routing: 'best-quality',
420270
})
271+
.then(console.log)
421272
```
422273

423274
## Failover mode
@@ -452,7 +303,7 @@ In the tech proxy mode, the custom credentials are passed in the `auth` service
452303

453304
```js
454305
client.ai.text.translate
455-
.fullfill({
306+
.fulfill({
456307
text: "A sample text",
457308
to: 'es',
458309
provider: 'some-provider-id',
@@ -482,7 +333,7 @@ For example for google translate custom auth structure is `{ key: YOUR_GOOGLE_KE
482333
483334
```js
484335
client.ai.text.translate
485-
.fullfill({
336+
.fulfill({
486337
text: "A sample text",
487338
to: 'es',
488339
provider: 'ai.text.translate.google.translate_api.2-0',
@@ -501,20 +352,33 @@ Response:
501352
502353
```json
503354
{
504-
"results": [
505-
"Un texto de muestra"
355+
"id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
356+
"done": true,
357+
"response": [
358+
{
359+
"results": [
360+
"Un texto de muestra"
361+
],
362+
"meta": {
363+
"detected_source_language": [
364+
"en"
365+
]
366+
},
367+
"service": {
368+
"provider": {
369+
"id": "ai.text.translate.google.translate_api.2-0",
370+
"name": "Google Cloud Basic Translation API",
371+
"vendor": "Google Cloud",
372+
"description": "Basic Translation API",
373+
"logo": "https://inten.to/static/img/api/ggl_translate.png"
374+
}
375+
}
376+
}
506377
],
507378
"meta": {
508-
"detected_source_language": [
509-
"en"
510-
]
379+
// information about initial request
511380
},
512-
"service": {
513-
"provider": {
514-
"id": "ai.text.translate.google.translate_api.2-0",
515-
"name": "Google Cloud Translation API"
516-
}
517-
}
381+
"error": null
518382
}
519383
```
520384

0 commit comments

Comments
 (0)