|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | +const LIMIT = 50; |
| 3 | + |
1 | 4 | export default {
|
2 | 5 | type: "app",
|
3 | 6 | app: "influxdb_cloud",
|
4 |
| - propDefinitions: {}, |
| 7 | + propDefinitions: { |
| 8 | + bucketId: { |
| 9 | + type: "string", |
| 10 | + label: "Bucket ID", |
| 11 | + description: "The identifier of a bucket", |
| 12 | + async options({ page }) { |
| 13 | + const { buckets } = await this.listBuckets({ |
| 14 | + params: { |
| 15 | + limit: LIMIT, |
| 16 | + offset: page * LIMIT, |
| 17 | + }, |
| 18 | + }); |
| 19 | + return buckets?.map(({ |
| 20 | + id: value, name: label, |
| 21 | + }) => ({ |
| 22 | + label, |
| 23 | + value, |
| 24 | + })) || []; |
| 25 | + }, |
| 26 | + }, |
| 27 | + scriptId: { |
| 28 | + type: "string", |
| 29 | + label: "Script ID", |
| 30 | + description: "The identifier of a script", |
| 31 | + async options({ page }) { |
| 32 | + const { scripts } = await this.listScripts({ |
| 33 | + params: { |
| 34 | + limit: LIMIT, |
| 35 | + offset: page * LIMIT, |
| 36 | + }, |
| 37 | + }); |
| 38 | + return scripts?.map(({ |
| 39 | + id: value, name: label, |
| 40 | + }) => ({ |
| 41 | + label, |
| 42 | + value, |
| 43 | + })) || []; |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
5 | 47 | methods: {
|
6 |
| - // this.$auth contains connected account data |
7 |
| - authKeys() { |
8 |
| - console.log(Object.keys(this.$auth)); |
| 48 | + _baseUrl(version) { |
| 49 | + let { url } = this.$auth; |
| 50 | + if (version === "v2") { |
| 51 | + url += (url.endsWith("/") |
| 52 | + ? "" |
| 53 | + : "/") + "api/v2"; |
| 54 | + } else { |
| 55 | + url = url.endsWith("/") |
| 56 | + ? url.slice(0, -1) |
| 57 | + : url; |
| 58 | + } |
| 59 | + return url; |
| 60 | + }, |
| 61 | + _makeRequest({ |
| 62 | + $ = this, |
| 63 | + version = "v2", |
| 64 | + path, |
| 65 | + headers, |
| 66 | + ...opts |
| 67 | + }) { |
| 68 | + return axios($, { |
| 69 | + url: `${this._baseUrl(version)}${path}`, |
| 70 | + headers: { |
| 71 | + "Authorization": `Token ${this.$auth.token}`, |
| 72 | + "Content-Type": "application/json", |
| 73 | + ...headers, |
| 74 | + }, |
| 75 | + ...opts, |
| 76 | + }); |
| 77 | + }, |
| 78 | + listBuckets(opts = {}) { |
| 79 | + return this._makeRequest({ |
| 80 | + path: "/buckets", |
| 81 | + ...opts, |
| 82 | + }); |
| 83 | + }, |
| 84 | + listScripts(opts = {}) { |
| 85 | + return this._makeRequest({ |
| 86 | + path: "/scripts", |
| 87 | + ...opts, |
| 88 | + }); |
| 89 | + }, |
| 90 | + listTasks(opts = {}) { |
| 91 | + return this._makeRequest({ |
| 92 | + path: "/tasks", |
| 93 | + ...opts, |
| 94 | + }); |
| 95 | + }, |
| 96 | + updateBucket({ |
| 97 | + bucketId, ...opts |
| 98 | + }) { |
| 99 | + return this._makeRequest({ |
| 100 | + method: "PATCH", |
| 101 | + path: `/buckets/${bucketId}`, |
| 102 | + ...opts, |
| 103 | + }); |
| 104 | + }, |
| 105 | + writeData(opts = {}) { |
| 106 | + return this._makeRequest({ |
| 107 | + method: "POST", |
| 108 | + path: "/write", |
| 109 | + ...opts, |
| 110 | + }); |
| 111 | + }, |
| 112 | + invokeScript({ |
| 113 | + scriptId, ...opts |
| 114 | + }) { |
| 115 | + return this._makeRequest({ |
| 116 | + method: "POST", |
| 117 | + path: `/scripts/${scriptId}/invoke`, |
| 118 | + ...opts, |
| 119 | + }); |
| 120 | + }, |
| 121 | + async *paginate({ |
| 122 | + fn, |
| 123 | + resourceKey, |
| 124 | + params, |
| 125 | + max, |
| 126 | + }) { |
| 127 | + params = { |
| 128 | + ...params, |
| 129 | + limit: LIMIT, |
| 130 | + offset: 0, |
| 131 | + }; |
| 132 | + let total, count = 0; |
| 133 | + do { |
| 134 | + const response = await fn({ |
| 135 | + params, |
| 136 | + }); |
| 137 | + const results = resourceKey |
| 138 | + ? response[resourceKey] |
| 139 | + : response; |
| 140 | + for (const item of results) { |
| 141 | + yield item; |
| 142 | + if (max && ++count >= max) { |
| 143 | + return; |
| 144 | + } |
| 145 | + } |
| 146 | + total = results?.length; |
| 147 | + params.offset += params.limit; |
| 148 | + } while (total); |
9 | 149 | },
|
10 | 150 | },
|
11 | 151 | };
|
0 commit comments