assemblyai 1.0.1 → 2.0.1-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +192 -83
- package/dist/index.d.ts +4 -0
- package/dist/index.esm.js +473 -0
- package/dist/index.js +482 -0
- package/dist/services/base.d.ts +13 -0
- package/dist/services/files/index.d.ts +9 -0
- package/dist/services/index.d.ts +29 -0
- package/dist/services/lemur/index.d.ts +8 -0
- package/dist/services/realtime/factory.d.ts +10 -0
- package/dist/services/realtime/index.d.ts +2 -0
- package/dist/services/realtime/service.d.ts +22 -0
- package/dist/services/transcripts/index.d.ts +59 -0
- package/dist/types/asyncapi.generated.d.ts +87 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/openapi.generated.d.ts +685 -0
- package/dist/types/realtime/index.d.ts +36 -0
- package/dist/types/services/abstractions.d.ts +52 -0
- package/dist/types/services/index.d.ts +6 -0
- package/dist/types/transcripts/index.d.ts +5 -0
- package/dist/utils/axios.d.ts +3 -0
- package/dist/utils/errors/index.d.ts +1 -0
- package/dist/utils/errors/realtime.d.ts +23 -0
- package/package.json +58 -21
- package/src/index.ts +5 -0
- package/src/services/base.ts +14 -0
- package/src/services/files/index.ts +22 -0
- package/src/services/index.ts +49 -0
- package/src/services/lemur/index.ts +49 -0
- package/src/services/realtime/factory.ts +32 -0
- package/src/services/realtime/index.ts +2 -0
- package/src/services/realtime/service.ts +184 -0
- package/src/services/transcripts/index.ts +178 -0
- package/src/types/asyncapi.generated.ts +124 -0
- package/src/types/index.ts +5 -0
- package/src/types/openapi.generated.ts +834 -0
- package/src/types/realtime/index.ts +68 -0
- package/src/types/services/abstractions.ts +56 -0
- package/src/types/services/index.ts +7 -0
- package/src/types/transcripts/index.ts +5 -0
- package/src/utils/.gitkeep +0 -0
- package/src/utils/axios.ts +19 -0
- package/src/utils/errors/index.ts +5 -0
- package/src/utils/errors/realtime.ts +45 -0
- package/.eslintrc.json +0 -3
- package/index.js +0 -15
- package/src/Client.js +0 -28
- package/src/api/Http/Request.js +0 -108
- package/src/api/Http/Response.js +0 -23
- package/src/api/Model.js +0 -17
- package/src/api/Transcript.js +0 -16
- package/src/api/Upload.js +0 -41
- package/src/api/util.js +0 -48
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FinalTranscript,
|
|
3
|
+
PartialTranscript,
|
|
4
|
+
RealtimeTranscript,
|
|
5
|
+
RealtimeTranscriptType,
|
|
6
|
+
} from "../asyncapi.generated";
|
|
7
|
+
|
|
8
|
+
type CreateRealtimeServiceParams = {
|
|
9
|
+
realtimeUrl?: string;
|
|
10
|
+
sampleRate?: number;
|
|
11
|
+
wordBoost?: string[];
|
|
12
|
+
} & (
|
|
13
|
+
| {
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
}
|
|
16
|
+
| {
|
|
17
|
+
token: string;
|
|
18
|
+
}
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
type RealtimeServiceParams = {
|
|
22
|
+
realtimeUrl?: string;
|
|
23
|
+
sampleRate?: number;
|
|
24
|
+
wordBoost?: string[];
|
|
25
|
+
} & (
|
|
26
|
+
| {
|
|
27
|
+
apiKey: string;
|
|
28
|
+
}
|
|
29
|
+
| {
|
|
30
|
+
token: string;
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
type RealtimeEvents =
|
|
35
|
+
| "open"
|
|
36
|
+
| "close"
|
|
37
|
+
| "transcript"
|
|
38
|
+
| "transcript.partial"
|
|
39
|
+
| "transcript.final"
|
|
40
|
+
| "error";
|
|
41
|
+
|
|
42
|
+
type SessionBeginsEventData = {
|
|
43
|
+
sessionId: string;
|
|
44
|
+
expiresAt: Date;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type RealtimeListeners = {
|
|
48
|
+
open?: (event: SessionBeginsEventData) => void;
|
|
49
|
+
close?: (code: number, reason: string) => void;
|
|
50
|
+
transcript?: (transcript: RealtimeTranscript) => void;
|
|
51
|
+
"transcript.partial"?: (transcript: PartialTranscript) => void;
|
|
52
|
+
"transcript.final"?: (transcript: FinalTranscript) => void;
|
|
53
|
+
error?: (error: Error) => void;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
type RealtimeTokenParams = {
|
|
57
|
+
expires_in: number;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type {
|
|
61
|
+
CreateRealtimeServiceParams,
|
|
62
|
+
RealtimeServiceParams,
|
|
63
|
+
RealtimeEvents,
|
|
64
|
+
RealtimeTranscriptType,
|
|
65
|
+
SessionBeginsEventData,
|
|
66
|
+
RealtimeListeners,
|
|
67
|
+
RealtimeTokenParams,
|
|
68
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for classes that can create resources.
|
|
3
|
+
* @template T The type of the resource.
|
|
4
|
+
* @template Parameters The type of the parameters required to create the resource.
|
|
5
|
+
*/
|
|
6
|
+
interface Createable<T, Parameters, Options = Record<string, any>> {
|
|
7
|
+
/**
|
|
8
|
+
* Create a new resource.
|
|
9
|
+
* @param params The parameters of the new resource.
|
|
10
|
+
* @param options The options used for creating the new resource.
|
|
11
|
+
* @return A promise that resolves to the newly created resource.
|
|
12
|
+
*/
|
|
13
|
+
create(params: Parameters, options?: Options): Promise<T>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Interface for classes that can retrieve resources.
|
|
18
|
+
* @template T The type of the resource.
|
|
19
|
+
* @template Id The type of the resource's identifier. Defaults to string.
|
|
20
|
+
*/
|
|
21
|
+
interface Retrieveable<T, Id = string> {
|
|
22
|
+
/**
|
|
23
|
+
* Get a resource.
|
|
24
|
+
* @param id The identifier of the resource to retrieve.
|
|
25
|
+
* @return A promise that resolves to the retrieved resource.
|
|
26
|
+
*/
|
|
27
|
+
get(id: Id): Promise<T>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Interface for classes that can delete resources.
|
|
32
|
+
* @template T The type of the resource.
|
|
33
|
+
* @template Id The type of the resource's identifier. Defaults to string.
|
|
34
|
+
*/
|
|
35
|
+
interface Deletable<T, Id = string> {
|
|
36
|
+
/**
|
|
37
|
+
* Delete a resource.
|
|
38
|
+
* @param id The identifier of the resource to delete.
|
|
39
|
+
* @return A promise that resolves to a boolean indicating whether the deletion was successful.
|
|
40
|
+
*/
|
|
41
|
+
delete(id: Id): Promise<T>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Interface for classes that can list resources.
|
|
46
|
+
* @template T The type of the resource.
|
|
47
|
+
*/
|
|
48
|
+
interface Listable<T, Page = string> {
|
|
49
|
+
/**
|
|
50
|
+
* List all resources.
|
|
51
|
+
* @return A promise that resolves to an array of resources.
|
|
52
|
+
*/
|
|
53
|
+
list(page?: Page): Promise<T>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type { Createable, Retrieveable, Deletable, Listable };
|
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import axios, { isAxiosError } from "axios";
|
|
2
|
+
import { BaseServiceParams } from "../.";
|
|
3
|
+
|
|
4
|
+
export function createAxiosClient(params: BaseServiceParams) {
|
|
5
|
+
const client = axios.create({
|
|
6
|
+
baseURL: params.baseUrl,
|
|
7
|
+
headers: { Authorization: params.apiKey },
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
client.interceptors.response.use(undefined, throwApiError);
|
|
11
|
+
return client;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function throwApiError(error: unknown) {
|
|
15
|
+
if (isAxiosError(error) && error.response?.data?.error) {
|
|
16
|
+
return Promise.reject(new Error(error.response.data.error));
|
|
17
|
+
}
|
|
18
|
+
return Promise.reject(error);
|
|
19
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
enum RealtimeErrorType {
|
|
2
|
+
BadSampleRate = 4000,
|
|
3
|
+
AuthFailed = 4001,
|
|
4
|
+
// Both InsufficientFunds and FreeAccount error use 4002
|
|
5
|
+
InsufficientFundsOrFreeAccount = 4002,
|
|
6
|
+
NonexistentSessionId = 4004,
|
|
7
|
+
SessionExpired = 4008,
|
|
8
|
+
ClosedSession = 4010,
|
|
9
|
+
RateLimited = 4029,
|
|
10
|
+
UniqueSessionViolation = 4030,
|
|
11
|
+
SessionTimeout = 4031,
|
|
12
|
+
AudioTooShort = 4032,
|
|
13
|
+
AudioTooLong = 4033,
|
|
14
|
+
BadJson = 4100,
|
|
15
|
+
BadSchema = 4101,
|
|
16
|
+
TooManyStreams = 4102,
|
|
17
|
+
Reconnected = 4103,
|
|
18
|
+
ReconnectAttemptsExhausted = 1013,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const RealtimeErrorMessages: Record<RealtimeErrorType, string> = {
|
|
22
|
+
[RealtimeErrorType.BadSampleRate]: "Sample rate must be a positive integer",
|
|
23
|
+
[RealtimeErrorType.AuthFailed]: "Not Authorized",
|
|
24
|
+
[RealtimeErrorType.InsufficientFundsOrFreeAccount]:
|
|
25
|
+
"Insufficient funds or you are using a free account. This feature is paid-only and requires you to add a credit card. Please visit https://assemblyai.com/dashboard/ to add a credit card to your account.",
|
|
26
|
+
[RealtimeErrorType.NonexistentSessionId]: "Session ID does not exist",
|
|
27
|
+
[RealtimeErrorType.SessionExpired]: "Session has expired",
|
|
28
|
+
[RealtimeErrorType.ClosedSession]: "Session is closed",
|
|
29
|
+
[RealtimeErrorType.RateLimited]: "Rate limited",
|
|
30
|
+
[RealtimeErrorType.UniqueSessionViolation]: "Unique session violation",
|
|
31
|
+
[RealtimeErrorType.SessionTimeout]: "Session Timeout",
|
|
32
|
+
[RealtimeErrorType.AudioTooShort]: "Audio too short",
|
|
33
|
+
[RealtimeErrorType.AudioTooLong]: "Audio too long",
|
|
34
|
+
[RealtimeErrorType.BadJson]: "Bad JSON",
|
|
35
|
+
[RealtimeErrorType.BadSchema]: "Bad schema",
|
|
36
|
+
[RealtimeErrorType.TooManyStreams]: "Too many streams",
|
|
37
|
+
[RealtimeErrorType.Reconnected]: "Reconnected",
|
|
38
|
+
[RealtimeErrorType.ReconnectAttemptsExhausted]:
|
|
39
|
+
"Reconnect attempts exhausted",
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
class RealtimeError extends Error {}
|
|
43
|
+
|
|
44
|
+
export { RealtimeErrorType, RealtimeErrorMessages };
|
|
45
|
+
export default RealtimeError;
|
package/.eslintrc.json
DELETED
package/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const Transcript = require('./src/api/Transcript')
|
|
2
|
-
const Model = require('./src/api/Model')
|
|
3
|
-
const Upload = require('./src/api/Upload')
|
|
4
|
-
const Client = require('./src/Client')
|
|
5
|
-
|
|
6
|
-
module.exports = {
|
|
7
|
-
Transcript,
|
|
8
|
-
Model,
|
|
9
|
-
Upload,
|
|
10
|
-
setAPIKey (key) {
|
|
11
|
-
Client.API_KEY = key
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
package/src/Client.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
let _apiKey = ''
|
|
2
|
-
|
|
3
|
-
class Client {
|
|
4
|
-
static set API_KEY (key) {
|
|
5
|
-
_apiKey = key
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
static get API_KEY () {
|
|
9
|
-
return _apiKey
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
static checkKey () {
|
|
13
|
-
if (process.env.ASSEMBLYAI_API_KEY) {
|
|
14
|
-
_apiKey = process.env.ASSEMBLYAI_API_KEY
|
|
15
|
-
}
|
|
16
|
-
if (!_apiKey) {
|
|
17
|
-
throw new Error(`
|
|
18
|
-
Unable to find the API Key.
|
|
19
|
-
You can set this value by using the setAPIKey method.
|
|
20
|
-
Example: """ const assemblyai = require('assemblyai')
|
|
21
|
-
assemblyai.setAPIKey('example') """
|
|
22
|
-
`)
|
|
23
|
-
}
|
|
24
|
-
return true
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
module.exports = Client
|
package/src/api/Http/Request.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
const request = require('request')
|
|
2
|
-
const Client = require('../../Client')
|
|
3
|
-
|
|
4
|
-
class Request {
|
|
5
|
-
/**
|
|
6
|
-
* Initializes the class
|
|
7
|
-
* @param {Object} options The HTTP Options
|
|
8
|
-
* @param {String} options.method The HTTP Method
|
|
9
|
-
* @param {String} options.url THE HTTP URL
|
|
10
|
-
* @param {Object} options.body optional HTTP Body
|
|
11
|
-
*/
|
|
12
|
-
constructor (options) {
|
|
13
|
-
this.method = options.method || 'GET'
|
|
14
|
-
this.url = options.url || ''
|
|
15
|
-
this.body = options.body || ''
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Sends an HTTP request using the node HTTP module
|
|
20
|
-
*
|
|
21
|
-
* @returns {Promise<Object>} Parsed JSON of the response
|
|
22
|
-
*/
|
|
23
|
-
_request (isJSON) {
|
|
24
|
-
const options = {
|
|
25
|
-
uri: this.url,
|
|
26
|
-
method: this.method,
|
|
27
|
-
headers: {
|
|
28
|
-
authorization: Client.API_KEY
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (this.method === 'POST' || this.method === 'PUT') {
|
|
33
|
-
if (this.body && this.body.constructor !== String) {
|
|
34
|
-
options.body = JSON.stringify(this.body)
|
|
35
|
-
} else {
|
|
36
|
-
options.body = ''
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return new Promise((resolve, reject) => {
|
|
41
|
-
request(options, (err, response, body) => {
|
|
42
|
-
if (err) return reject(err)
|
|
43
|
-
try {
|
|
44
|
-
if (isJSON) {
|
|
45
|
-
resolve(JSON.parse(body))
|
|
46
|
-
} else {
|
|
47
|
-
resolve(body)
|
|
48
|
-
}
|
|
49
|
-
} catch (e) {
|
|
50
|
-
console.log('HERE:', e)
|
|
51
|
-
reject(new Error(`
|
|
52
|
-
Unable to recieve a proper JSON response from the API.
|
|
53
|
-
Please contact customer service.
|
|
54
|
-
`))
|
|
55
|
-
}
|
|
56
|
-
})
|
|
57
|
-
})
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Sends a request to the API
|
|
62
|
-
*
|
|
63
|
-
* @throws {Error} the error receieved from the API
|
|
64
|
-
*/
|
|
65
|
-
async send (isJSON = true) {
|
|
66
|
-
Client.checkKey()
|
|
67
|
-
let response = null
|
|
68
|
-
let retries = 1
|
|
69
|
-
while (!response) {
|
|
70
|
-
if (retries === 5) {
|
|
71
|
-
throw new Error(`
|
|
72
|
-
Retry limit reached. Some things that could cause this to happen
|
|
73
|
-
would be your network connection or slow internet (request timeout).
|
|
74
|
-
Please look into this before continuing to use this SDK
|
|
75
|
-
`)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
response = await new Promise(resolve => {
|
|
79
|
-
setTimeout(async () => {
|
|
80
|
-
try {
|
|
81
|
-
const res = await this._request(isJSON)
|
|
82
|
-
resolve(res)
|
|
83
|
-
} catch (e) {
|
|
84
|
-
retries += 1
|
|
85
|
-
/**
|
|
86
|
-
* The reason for resolving the null value would be so response is still *falsey*
|
|
87
|
-
* allowing the loop to run again. If no value is resolved here, the loop will hang
|
|
88
|
-
* until the value is resolved (which would be never)
|
|
89
|
-
*/
|
|
90
|
-
resolve(null)
|
|
91
|
-
}
|
|
92
|
-
}, (retries * retries) * 100) // TODO: exponential timeout. Change this value to whatever you want
|
|
93
|
-
})
|
|
94
|
-
}
|
|
95
|
-
return response
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
module.exports = Request
|
|
100
|
-
|
|
101
|
-
// curl --request POST \
|
|
102
|
-
// --url https://api.assemblyai.com/transcript \
|
|
103
|
-
// --header 'authorization: 6f33815060fa4eb29e96356a3ec536c8' \
|
|
104
|
-
// --data '
|
|
105
|
-
// {
|
|
106
|
-
// "audio_src_url": "https://s3-us-west-2.amazonaws.com/blog.assemblyai.com/audio/8-7-2018-post/7510.mp3",
|
|
107
|
-
// "model_id": 265
|
|
108
|
-
// }'
|
package/src/api/Http/Response.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
class Response {
|
|
2
|
-
constructor (responseJSON) {
|
|
3
|
-
this.json = responseJSON
|
|
4
|
-
if (responseJSON.error) {
|
|
5
|
-
throw new Error(responseJSON.error)
|
|
6
|
-
}
|
|
7
|
-
this.type = responseJSON.transcript ? 'transcript' : 'model'
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
toString () {
|
|
11
|
-
return JSON.stringify(this.json[this.type])
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
stringify () {
|
|
15
|
-
return this.toString()
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
get () {
|
|
19
|
-
return this.json[this.type]
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
module.exports = Response
|
package/src/api/Model.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
const { Poll, Create } = require('./util')
|
|
2
|
-
|
|
3
|
-
class Model {
|
|
4
|
-
constructor () {
|
|
5
|
-
this.url = 'https://api.assemblyai.com/model'
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
poll (id) {
|
|
9
|
-
return Poll(this.url, id)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
create (options) {
|
|
13
|
-
return Create(this.url, options)
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
module.exports = Model
|
package/src/api/Transcript.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
const { Poll, Create } = require('./util')
|
|
2
|
-
class Transcript {
|
|
3
|
-
constructor () {
|
|
4
|
-
this.url = 'https://api.assemblyai.com/transcript'
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
poll (id) {
|
|
8
|
-
return Poll(this.url, id)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
create (options) {
|
|
12
|
-
return Create(this.url, options)
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
module.exports = Transcript
|
package/src/api/Upload.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const fs = require('fs')
|
|
2
|
-
const Request = require('./Http/Request')
|
|
3
|
-
const request = require('request')
|
|
4
|
-
|
|
5
|
-
const Transcribe = require('./Transcript')
|
|
6
|
-
|
|
7
|
-
class Upload {
|
|
8
|
-
constructor (filePath) {
|
|
9
|
-
this.url = 'https://api.assemblyai.com/upload'
|
|
10
|
-
this.filePath = filePath
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
async create () {
|
|
14
|
-
const req = new Request({
|
|
15
|
-
method: 'POST',
|
|
16
|
-
url: this.url
|
|
17
|
-
})
|
|
18
|
-
const uploadUrl = await req.send(false)
|
|
19
|
-
|
|
20
|
-
await new Promise((resolve, reject) => {
|
|
21
|
-
fs.readFile(this.filePath, (err, data) => {
|
|
22
|
-
if (err) return reject(err)
|
|
23
|
-
request.put(uploadUrl, { body: data }, (err, response, body) => {
|
|
24
|
-
if (err) return reject(err)
|
|
25
|
-
resolve(body)
|
|
26
|
-
})
|
|
27
|
-
})
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
const url = uploadUrl.split('?')[0]
|
|
31
|
-
const transcribe = new Transcribe()
|
|
32
|
-
const response = await transcribe.create({
|
|
33
|
-
audio_src_url: url
|
|
34
|
-
})
|
|
35
|
-
const { id } = response.get()
|
|
36
|
-
|
|
37
|
-
return transcribe.poll(id)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
module.exports = Upload
|
package/src/api/util.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
const Request = require('./Http/Request')
|
|
2
|
-
const Response = require('./Http/Response')
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
Poll (url, id) {
|
|
6
|
-
return new Promise((resolve, reject) => {
|
|
7
|
-
const request = new Request({
|
|
8
|
-
method: 'GET',
|
|
9
|
-
url: `${url}/${id}`
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
const interval = setInterval(async () => {
|
|
13
|
-
try {
|
|
14
|
-
const apiResponse = await request.send()
|
|
15
|
-
const response = new Response(apiResponse)
|
|
16
|
-
const json = response.get()
|
|
17
|
-
if (json.status === 'completed') {
|
|
18
|
-
clearInterval(interval)
|
|
19
|
-
resolve(response)
|
|
20
|
-
}
|
|
21
|
-
if (json.status === 'trained') {
|
|
22
|
-
clearInterval(interval)
|
|
23
|
-
resolve(response)
|
|
24
|
-
}
|
|
25
|
-
if (json.status === 'error') {
|
|
26
|
-
clearInterval(interval)
|
|
27
|
-
reject(json.error)
|
|
28
|
-
}
|
|
29
|
-
} catch (e) {
|
|
30
|
-
clearInterval(interval)
|
|
31
|
-
reject(e)
|
|
32
|
-
}
|
|
33
|
-
}, 3000)
|
|
34
|
-
})
|
|
35
|
-
},
|
|
36
|
-
async Create (url, options) {
|
|
37
|
-
const request = new Request({
|
|
38
|
-
method: 'POST',
|
|
39
|
-
url: url,
|
|
40
|
-
body: options.upload ? '' : options || {}
|
|
41
|
-
})
|
|
42
|
-
const apiResponse = await request.send()
|
|
43
|
-
if (options.json === false) {
|
|
44
|
-
return apiResponse
|
|
45
|
-
}
|
|
46
|
-
return new Response(apiResponse)
|
|
47
|
-
}
|
|
48
|
-
}
|