|
| 1 | +/** |
| 2 | + * This file was auto-generated by Fern from our API Definition. |
| 3 | + */ |
| 4 | + |
| 5 | +import * as environments from "../../../../environments.js"; |
| 6 | +import * as core from "../../../../core/index.js"; |
| 7 | +import * as Pipedream from "../../../index.js"; |
| 8 | +import { mergeHeaders, mergeOnlyDefinedHeaders } from "../../../../core/headers.js"; |
| 9 | +import * as errors from "../../../../errors/index.js"; |
| 10 | + |
| 11 | +export declare namespace FileStash { |
| 12 | + export interface Options { |
| 13 | + environment?: core.Supplier<environments.PipedreamEnvironment | string>; |
| 14 | + /** Specify a custom URL to connect the client to. */ |
| 15 | + baseUrl?: core.Supplier<string>; |
| 16 | + projectId: string; |
| 17 | + token?: core.Supplier<core.BearerToken | undefined>; |
| 18 | + /** Override the x-pd-environment header */ |
| 19 | + projectEnvironment?: core.Supplier<Pipedream.ProjectEnvironment | undefined>; |
| 20 | + /** Additional headers to include in requests. */ |
| 21 | + headers?: Record<string, string | core.Supplier<string | null | undefined> | null | undefined>; |
| 22 | + } |
| 23 | + |
| 24 | + export interface RequestOptions { |
| 25 | + /** The maximum time to wait for a response in seconds. */ |
| 26 | + timeoutInSeconds?: number; |
| 27 | + /** The number of times to retry the request. Defaults to 2. */ |
| 28 | + maxRetries?: number; |
| 29 | + /** A hook to abort the request. */ |
| 30 | + abortSignal?: AbortSignal; |
| 31 | + /** Override the x-pd-environment header */ |
| 32 | + projectEnvironment?: Pipedream.ProjectEnvironment | undefined; |
| 33 | + /** Additional query string parameters to include in the request. */ |
| 34 | + queryParams?: Record<string, unknown>; |
| 35 | + /** Additional headers to include in the request. */ |
| 36 | + headers?: Record<string, string | core.Supplier<string | null | undefined> | null | undefined>; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +export class FileStash { |
| 41 | + protected readonly _options: FileStash.Options; |
| 42 | + |
| 43 | + constructor(_options: FileStash.Options) { |
| 44 | + this._options = _options; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Download a file from File Stash |
| 49 | + * |
| 50 | + * @param {Pipedream.FileStashDownloadFileRequest} request |
| 51 | + * @param {FileStash.RequestOptions} requestOptions - Request-specific configuration. |
| 52 | + * |
| 53 | + * @throws {@link Pipedream.TooManyRequestsError} |
| 54 | + * |
| 55 | + * @example |
| 56 | + * await client.fileStash.downloadFile({ |
| 57 | + * s3Key: "s3_key" |
| 58 | + * }) |
| 59 | + */ |
| 60 | + public downloadFile( |
| 61 | + request: Pipedream.FileStashDownloadFileRequest, |
| 62 | + requestOptions?: FileStash.RequestOptions, |
| 63 | + ): core.HttpResponsePromise<void> { |
| 64 | + return core.HttpResponsePromise.fromPromise(this.__downloadFile(request, requestOptions)); |
| 65 | + } |
| 66 | + |
| 67 | + private async __downloadFile( |
| 68 | + request: Pipedream.FileStashDownloadFileRequest, |
| 69 | + requestOptions?: FileStash.RequestOptions, |
| 70 | + ): Promise<core.WithRawResponse<void>> { |
| 71 | + const { s3Key } = request; |
| 72 | + const _queryParams: Record<string, string | string[] | object | object[] | null> = {}; |
| 73 | + _queryParams["s3_key"] = s3Key; |
| 74 | + let _headers: core.Fetcher.Args["headers"] = mergeHeaders( |
| 75 | + this._options?.headers, |
| 76 | + mergeOnlyDefinedHeaders({ |
| 77 | + Authorization: await this._getAuthorizationHeader(), |
| 78 | + "x-pd-environment": requestOptions?.projectEnvironment ?? this._options?.projectEnvironment, |
| 79 | + }), |
| 80 | + requestOptions?.headers, |
| 81 | + ); |
| 82 | + const _response = await core.fetcher({ |
| 83 | + url: core.url.join( |
| 84 | + (await core.Supplier.get(this._options.baseUrl)) ?? |
| 85 | + (await core.Supplier.get(this._options.environment)) ?? |
| 86 | + environments.PipedreamEnvironment.Prod, |
| 87 | + `v1/connect/${encodeURIComponent(this._options.projectId)}/file_stash/download`, |
| 88 | + ), |
| 89 | + method: "GET", |
| 90 | + headers: _headers, |
| 91 | + queryParameters: { ..._queryParams, ...requestOptions?.queryParams }, |
| 92 | + timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, |
| 93 | + maxRetries: requestOptions?.maxRetries, |
| 94 | + abortSignal: requestOptions?.abortSignal, |
| 95 | + }); |
| 96 | + if (_response.ok) { |
| 97 | + return { data: undefined, rawResponse: _response.rawResponse }; |
| 98 | + } |
| 99 | + |
| 100 | + if (_response.error.reason === "status-code") { |
| 101 | + switch (_response.error.statusCode) { |
| 102 | + case 429: |
| 103 | + throw new Pipedream.TooManyRequestsError(_response.error.body, _response.rawResponse); |
| 104 | + default: |
| 105 | + throw new errors.PipedreamError({ |
| 106 | + statusCode: _response.error.statusCode, |
| 107 | + body: _response.error.body, |
| 108 | + rawResponse: _response.rawResponse, |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + switch (_response.error.reason) { |
| 114 | + case "non-json": |
| 115 | + throw new errors.PipedreamError({ |
| 116 | + statusCode: _response.error.statusCode, |
| 117 | + body: _response.error.rawBody, |
| 118 | + rawResponse: _response.rawResponse, |
| 119 | + }); |
| 120 | + case "timeout": |
| 121 | + throw new errors.PipedreamTimeoutError( |
| 122 | + "Timeout exceeded when calling GET /v1/connect/{project_id}/file_stash/download.", |
| 123 | + ); |
| 124 | + case "unknown": |
| 125 | + throw new errors.PipedreamError({ |
| 126 | + message: _response.error.errorMessage, |
| 127 | + rawResponse: _response.rawResponse, |
| 128 | + }); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + protected async _getAuthorizationHeader(): Promise<string | undefined> { |
| 133 | + const bearer = await core.Supplier.get(this._options.token); |
| 134 | + if (bearer != null) { |
| 135 | + return `Bearer ${bearer}`; |
| 136 | + } |
| 137 | + |
| 138 | + return undefined; |
| 139 | + } |
| 140 | +} |
0 commit comments