|
1 | 1 | import { axios } from "@pipedream/platform"; |
| 2 | +import xml2js from "xml2js"; |
2 | 3 |
|
3 | 4 | export default { |
4 | 5 | type: "app", |
5 | 6 | app: "gocanvas", |
6 | 7 | propDefinitions: { |
7 | | - applicationId: { |
| 8 | + dispatchId: { |
8 | 9 | type: "string", |
9 | | - label: "Application ID", |
10 | | - description: "The ID of the GoCanvas application", |
11 | | - }, |
12 | | - submissionId: { |
13 | | - type: "string", |
14 | | - label: "Submission ID", |
15 | | - description: "The ID of the submission to get dynamic fields", |
16 | | - optional: true, |
17 | | - }, |
18 | | - dispatchApp: { |
19 | | - type: "string", |
20 | | - label: "Dispatch App", |
21 | | - description: "The name of a dispatch-enabled GoCanvas app", |
22 | | - }, |
23 | | - dispatchDescription: { |
24 | | - type: "string", |
25 | | - label: "Dispatch Description", |
26 | | - description: "The 'description' of the dispatch to be deleted", |
27 | | - }, |
28 | | - referenceDataFile: { |
29 | | - type: "string", |
30 | | - label: "Reference Data File", |
31 | | - description: "The name of the GoCanvas reference data file to be synced", |
| 10 | + label: "Dispatch ID", |
| 11 | + description: "Identifier of a dispatch", |
| 12 | + async options({ page }) { |
| 13 | + const dispatches = await this.getActiveDispatches({ |
| 14 | + data: { |
| 15 | + page: page + 1, |
| 16 | + }, |
| 17 | + }); |
| 18 | + return dispatches?.map(({ |
| 19 | + $, Description: desc, |
| 20 | + }) => ({ |
| 21 | + value: $.Id, |
| 22 | + label: desc[0], |
| 23 | + })) || []; |
| 24 | + }, |
32 | 25 | }, |
33 | 26 | }, |
34 | 27 | methods: { |
35 | | - authKeys() { |
36 | | - console.log(Object.keys(this.$auth)); |
37 | | - }, |
38 | 28 | _baseUrl() { |
39 | | - return "https://api.gocanvas.com"; |
| 29 | + return "https://www.gocanvas.com/apiv2/"; |
40 | 30 | }, |
41 | | - async _makeRequest(opts = {}) { |
| 31 | + _makeRequest(opts = {}) { |
42 | 32 | const { |
43 | | - $ = this, method = "GET", path, headers, ...otherOpts |
| 33 | + $ = this, |
| 34 | + path, |
| 35 | + params, |
| 36 | + ...otherOpts |
44 | 37 | } = opts; |
45 | 38 | return axios($, { |
46 | 39 | ...otherOpts, |
47 | | - method, |
48 | | - url: this._baseUrl() + path, |
| 40 | + url: `${this._baseUrl()}${path}`, |
| 41 | + params: { |
| 42 | + ...params, |
| 43 | + username: `${this.$auth.username}`, |
| 44 | + }, |
49 | 45 | headers: { |
50 | | - ...headers, |
51 | | - "Authorization": `Bearer ${this.$auth.oauth_access_token}`, |
| 46 | + Authorization: `Bearer ${this.$auth.api_key}`, |
52 | 47 | }, |
53 | 48 | }); |
54 | 49 | }, |
55 | | - async emitNewEvent(opts = {}) { |
56 | | - return this._makeRequest({ |
57 | | - path: `/submissions/${opts.submissionId}`, |
| 50 | + async getActiveDispatches(opts = {}) { |
| 51 | + const response = await this._makeRequest({ |
| 52 | + path: "/dispatch_export", |
58 | 53 | ...opts, |
59 | 54 | }); |
| 55 | + const { CanvasResult: { Dispatches: dispatches } } = await new xml2js |
| 56 | + .Parser().parseStringPromise(response); |
| 57 | + const dispatchList = []; |
| 58 | + for (const dispatch of dispatches) { |
| 59 | + if (!dispatch.Dispatch) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + const activeDispatches = dispatch.Dispatch.filter((d) => d.Status[0] !== "deleted"); |
| 63 | + dispatchList.push(...activeDispatches); |
| 64 | + } |
| 65 | + return dispatchList; |
60 | 66 | }, |
61 | | - async createDispatchItem(opts = {}) { |
62 | | - return this._makeRequest({ |
63 | | - method: "POST", |
64 | | - path: "/dispatches", |
65 | | - data: { |
66 | | - app_name: opts.dispatchApp, |
67 | | - }, |
| 67 | + async getDispatchDescription({ |
| 68 | + dispatchId, ...opts |
| 69 | + }) { |
| 70 | + const dispatches = await this.getActiveDispatches({ |
68 | 71 | ...opts, |
69 | 72 | }); |
| 73 | + const dispatch = dispatches.find(({ $ }) => $.Id === dispatchId); |
| 74 | + return dispatch.Description[0]; |
70 | 75 | }, |
71 | | - async removeDispatch(opts = {}) { |
| 76 | + dispatchItems(opts = {}) { |
72 | 77 | return this._makeRequest({ |
73 | | - method: "DELETE", |
74 | | - path: "/dispatches", |
75 | | - params: { |
76 | | - description: opts.dispatchDescription, |
77 | | - }, |
78 | | - ...opts, |
79 | | - }); |
80 | | - }, |
81 | | - async syncReferenceDataFile(opts = {}) { |
82 | | - return this._makeRequest({ |
83 | | - method: "PUT", |
84 | | - path: `/reference_data_files/${opts.referenceDataFile}`, |
| 78 | + method: "POST", |
| 79 | + path: "/dispatch_items", |
85 | 80 | ...opts, |
86 | 81 | }); |
87 | 82 | }, |
|
0 commit comments