@databricks/sdk-statementexecution 0.0.0-dev → 0.1.0-dev.1

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.
@@ -0,0 +1,259 @@
1
+ // Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
2
+
3
+ import {VERSION as AUTH_VERSION} from '@databricks/sdk-auth';
4
+ import type {Call} from '@databricks/sdk-core/api';
5
+ import {createDefault} from '@databricks/sdk-core/clientinfo';
6
+ import type {Logger} from '@databricks/sdk-core/logger';
7
+ import {NoOpLogger} from '@databricks/sdk-core/logger';
8
+ import type {CallOptions} from '@databricks/sdk-options/call';
9
+ import type {ClientOptions} from '@databricks/sdk-options/client';
10
+ import type {HttpClient} from '@databricks/sdk-core/http';
11
+ import {newHttpClient} from './transport';
12
+ import {
13
+ buildHttpRequest,
14
+ executeCall,
15
+ executeHttpCall,
16
+ marshalRequest,
17
+ parseResponse,
18
+ } from './utils';
19
+ import pkgJson from '../../package.json' with {type: 'json'};
20
+ import type {
21
+ CancelStatementRequest,
22
+ CancelStatementResponse,
23
+ ExecuteStatementRequest,
24
+ GetResultDataRequest,
25
+ GetStatementResultRequest,
26
+ ResultData,
27
+ StatementResponse,
28
+ } from './model';
29
+ import {
30
+ marshalCancelStatementRequestSchema,
31
+ marshalExecuteStatementRequestSchema,
32
+ unmarshalCancelStatementResponseSchema,
33
+ unmarshalResultDataSchema,
34
+ unmarshalStatementResponseSchema,
35
+ } from './model';
36
+
37
+ // Package identity segment for this client to be used in the User-Agent header.
38
+ const PACKAGE_SEGMENT = {
39
+ key: 'sdk-js-' + pkgJson.name.replace(/^@[^/]+\/sdk-/, ''),
40
+ value: pkgJson.version,
41
+ };
42
+
43
+ export class Client {
44
+ private readonly host: string;
45
+ // Workspace ID used to route workspace-level calls on unified hosts (SPOG).
46
+ // When set, workspace-level methods send X-Databricks-Org-Id on every
47
+ // request.
48
+ private readonly workspaceId: string | undefined;
49
+ private readonly httpClient: HttpClient;
50
+ private readonly logger: Logger;
51
+ // User-Agent header value. Composed once at construction from
52
+ // createDefault() merged with this package's identity and the active
53
+ // credential's name.
54
+ private readonly userAgent: string;
55
+
56
+ constructor(options: ClientOptions) {
57
+ if (options.host === undefined) {
58
+ throw new Error('Host is required.');
59
+ }
60
+ this.host = options.host.replace(/\/$/, '');
61
+ this.workspaceId = options.workspaceId;
62
+ this.logger = options.logger ?? new NoOpLogger();
63
+ let info = createDefault().with(PACKAGE_SEGMENT);
64
+ if (options.credentials !== undefined) {
65
+ info = info
66
+ .with({key: 'sdk-js-auth', value: AUTH_VERSION})
67
+ .with({key: 'auth', value: options.credentials.name()});
68
+ }
69
+ this.userAgent = info.toString();
70
+ this.httpClient = newHttpClient(options);
71
+ }
72
+
73
+ /**
74
+ * Requests that an executing statement be canceled. Callers must poll for status to see the
75
+ * terminal state. Cancel response is empty; receiving response indicates successful receipt.
76
+ */
77
+ async cancelStatement(
78
+ req: CancelStatementRequest,
79
+ options?: CallOptions
80
+ ): Promise<CancelStatementResponse> {
81
+ const url = `${this.host}/api/2.0/sql/statements/${req.statementId ?? ''}/cancel`;
82
+ const body = marshalRequest(req, marshalCancelStatementRequestSchema);
83
+ let resp: CancelStatementResponse | undefined;
84
+ const call: Call = async (callSignal?: AbortSignal): Promise<void> => {
85
+ const headers = new Headers({'Content-Type': 'application/json'});
86
+ if (this.workspaceId !== undefined) {
87
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
88
+ }
89
+ headers.set('User-Agent', this.userAgent);
90
+ const httpReq = buildHttpRequest('POST', url, headers, callSignal, body);
91
+ const respBody = await executeHttpCall({
92
+ request: httpReq,
93
+ httpClient: this.httpClient,
94
+ logger: this.logger,
95
+ });
96
+ resp = parseResponse(respBody, unmarshalCancelStatementResponseSchema);
97
+ };
98
+ await executeCall(call, options);
99
+ if (resp === undefined) {
100
+ throw new Error('API call completed without a result.');
101
+ }
102
+ return resp;
103
+ }
104
+
105
+ /**
106
+ * Execute a SQL statement and optionally await its results for a specified time.
107
+ *
108
+ * **Use case: small result sets with INLINE + JSON_ARRAY**
109
+ *
110
+ * For flows that generate small and predictable result sets (<= 25 MiB), `INLINE` responses of `JSON_ARRAY` result
111
+ * data are typically the simplest way to execute and fetch result data.
112
+ *
113
+ * **Use case: large result sets with EXTERNAL_LINKS**
114
+ *
115
+ * Using `EXTERNAL_LINKS` to fetch result data allows you to fetch large result sets efficiently.
116
+ * The main differences from using `INLINE` disposition are that the result data is accessed
117
+ * with URLs, and
118
+ * that there are 3 supported formats: `JSON_ARRAY`, `ARROW_STREAM` and `CSV` compared to only `JSON_ARRAY` with
119
+ * `INLINE`.
120
+ *
121
+ * ** URLs**
122
+ *
123
+ * External links point to data stored within your workspace's internal storage, in the form of
124
+ * a URL. The URLs are valid for only a short
125
+ * period, <= 15 minutes. Alongside each `external_link` is an expiration field indicating the time at which the URL
126
+ * is no longer valid. In `EXTERNAL_LINKS` mode, chunks can be resolved and fetched multiple times and in parallel.
127
+ *
128
+ * ----
129
+ *
130
+ * ### **Warning: Databricks strongly recommends that you protect the URLs that are returned by the `EXTERNAL_LINKS` disposition.**
131
+ *
132
+ * When you use the `EXTERNAL_LINKS` disposition, a
133
+ * short-lived, URL is generated, which can be
134
+ * used to download the results directly
135
+ * from . As a
136
+ * short-lived is
137
+ * embedded in this URL, you should protect
138
+ * the URL.
139
+ *
140
+ * Because URLs are already generated with
141
+ * embedded temporary s,
142
+ * you must not set an `Authorization` header in the download requests.
143
+ *
144
+ * The `EXTERNAL_LINKS` disposition can be disabled upon request by creating a support
145
+ * case.
146
+ *
147
+ * See also [Security best practices](/sql/admin/sql-execution-tutorial.html#security-best-practices).
148
+ *
149
+ * ----
150
+ *
151
+ * StatementResponse contains `statement_id` and `status`; other fields might be absent or present depending on
152
+ * context. If the SQL warehouse fails to execute the provided statement, a 200 response is returned with
153
+ * `status.state` set to `FAILED` (in contrast to a failure when accepting the request, which results in a non-200
154
+ * response). Details of the error can be found at `status.error` in case of execution failures.
155
+ */
156
+ async executeStatement(
157
+ req: ExecuteStatementRequest,
158
+ options?: CallOptions
159
+ ): Promise<StatementResponse> {
160
+ const url = `${this.host}/api/2.0/sql/statements/`;
161
+ const body = marshalRequest(req, marshalExecuteStatementRequestSchema);
162
+ let resp: StatementResponse | undefined;
163
+ const call: Call = async (callSignal?: AbortSignal): Promise<void> => {
164
+ const headers = new Headers({'Content-Type': 'application/json'});
165
+ if (this.workspaceId !== undefined) {
166
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
167
+ }
168
+ headers.set('User-Agent', this.userAgent);
169
+ const httpReq = buildHttpRequest('POST', url, headers, callSignal, body);
170
+ const respBody = await executeHttpCall({
171
+ request: httpReq,
172
+ httpClient: this.httpClient,
173
+ logger: this.logger,
174
+ });
175
+ resp = parseResponse(respBody, unmarshalStatementResponseSchema);
176
+ };
177
+ await executeCall(call, options);
178
+ if (resp === undefined) {
179
+ throw new Error('API call completed without a result.');
180
+ }
181
+ return resp;
182
+ }
183
+
184
+ /**
185
+ * After the statement execution has `SUCCEEDED`, this request can be used to fetch any chunk by
186
+ * index. Whereas the first chunk with `chunk_index=0` is typically fetched with
187
+ * :method:statementexecution/executeStatement or :method:statementexecution/getStatement, this
188
+ * request can be used to fetch subsequent chunks. The response structure is identical to the
189
+ * nested `result` element described in the :method:statementexecution/getStatement request, and
190
+ * similarly includes the `next_chunk_index` and `next_chunk_internal_link` fields for simple
191
+ * iteration through the result set. Depending on `disposition`, the response returns chunks of
192
+ * data either inline, or as links.
193
+ */
194
+ async getResultData(
195
+ req: GetResultDataRequest,
196
+ options?: CallOptions
197
+ ): Promise<ResultData> {
198
+ const url = `${this.host}/api/2.0/sql/statements/${req.statementId ?? ''}/result/chunks/${String(req.chunkIndex ?? '')}`;
199
+ let resp: ResultData | undefined;
200
+ const call: Call = async (callSignal?: AbortSignal): Promise<void> => {
201
+ const headers = new Headers();
202
+ if (this.workspaceId !== undefined) {
203
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
204
+ }
205
+ headers.set('User-Agent', this.userAgent);
206
+ const httpReq = buildHttpRequest('GET', url, headers, callSignal);
207
+ const respBody = await executeHttpCall({
208
+ request: httpReq,
209
+ httpClient: this.httpClient,
210
+ logger: this.logger,
211
+ });
212
+ resp = parseResponse(respBody, unmarshalResultDataSchema);
213
+ };
214
+ await executeCall(call, options);
215
+ if (resp === undefined) {
216
+ throw new Error('API call completed without a result.');
217
+ }
218
+ return resp;
219
+ }
220
+
221
+ /**
222
+ * This request can be used to poll for the statement's status. StatementResponse contains
223
+ * `statement_id` and `status`; other fields might be absent or present depending on context.
224
+ * When the `status.state` field is `SUCCEEDED` it will also return the result manifest and the
225
+ * first chunk of the result data. When the statement is in the terminal states `CANCELED`,
226
+ * `CLOSED` or `FAILED`, it returns HTTP 200 with the state set. After at least 12 hours in
227
+ * terminal state, the statement is removed from the warehouse and further calls will receive an
228
+ * HTTP 404 response.
229
+ *
230
+ * **NOTE**
231
+ * This call currently might take up to 5 seconds to get the latest status and result.
232
+ */
233
+ async getStatementResult(
234
+ req: GetStatementResultRequest,
235
+ options?: CallOptions
236
+ ): Promise<StatementResponse> {
237
+ const url = `${this.host}/api/2.0/sql/statements/${req.statementId ?? ''}`;
238
+ let resp: StatementResponse | undefined;
239
+ const call: Call = async (callSignal?: AbortSignal): Promise<void> => {
240
+ const headers = new Headers();
241
+ if (this.workspaceId !== undefined) {
242
+ headers.set('X-Databricks-Org-Id', this.workspaceId);
243
+ }
244
+ headers.set('User-Agent', this.userAgent);
245
+ const httpReq = buildHttpRequest('GET', url, headers, callSignal);
246
+ const respBody = await executeHttpCall({
247
+ request: httpReq,
248
+ httpClient: this.httpClient,
249
+ logger: this.logger,
250
+ });
251
+ resp = parseResponse(respBody, unmarshalStatementResponseSchema);
252
+ };
253
+ await executeCall(call, options);
254
+ if (resp === undefined) {
255
+ throw new Error('API call completed without a result.');
256
+ }
257
+ return resp;
258
+ }
259
+ }
@@ -0,0 +1,32 @@
1
+ // Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
2
+
3
+ export {Client} from './client';
4
+
5
+ export {
6
+ ColumnTypeName,
7
+ Disposition,
8
+ Format,
9
+ ServiceErrorCode,
10
+ TimeoutAction,
11
+ StatementStatus_State,
12
+ } from './model';
13
+
14
+ export type {
15
+ CancelStatementRequest,
16
+ CancelStatementResponse,
17
+ ChunkInfo,
18
+ ColumnInfo,
19
+ ExecuteStatementRequest,
20
+ ExternalLink,
21
+ ExternalLink_HttpHeadersEntry,
22
+ GetResultDataRequest,
23
+ GetStatementResultRequest,
24
+ QueryTag,
25
+ ResultData,
26
+ ResultManifest,
27
+ Schema,
28
+ ServiceError,
29
+ StatementParameter,
30
+ StatementResponse,
31
+ StatementStatus,
32
+ } from './model';