Skip to content

Commit 7725901

Browse files
committed
Implement SurveyBot actions and update package version
1 parent 7f7145f commit 7725901

File tree

5 files changed

+230
-6
lines changed

5 files changed

+230
-6
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import surveybot from "../../surveybot.app.mjs";
2+
3+
export default {
4+
key: "surveybot-get-survey-respondent",
5+
name: "Get Survey Respondent",
6+
description: "Get a respondent for a survey from SurveyBot. [See the documentation](https://app.surveybot.io/accounts/api_use_cases)",
7+
version: "0.0.1",
8+
type: "action",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: true,
13+
},
14+
props: {
15+
surveybot,
16+
surveyId: {
17+
propDefinition: [
18+
surveybot,
19+
"surveyId",
20+
],
21+
},
22+
respondentId: {
23+
propDefinition: [
24+
surveybot,
25+
"respondentId",
26+
({ surveyId }) => ({
27+
surveyId,
28+
}),
29+
],
30+
},
31+
},
32+
async run({ $ }) {
33+
const respondent = await this.surveybot.getSurveyRespondent({
34+
$,
35+
respondentId: this.respondentId,
36+
});
37+
38+
$.export("$summary", `Successfully retrieved respondent with ID: "${this.respondentId}" for survey with ID: "${this.surveyId}"`);
39+
return respondent;
40+
},
41+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import surveybot from "../../surveybot.app.mjs";
2+
3+
export default {
4+
key: "surveybot-get-survey-responses",
5+
name: "Get Survey Responses",
6+
description: "Get responses for a survey from SurveyBot. [See the documentation](https://app.surveybot.io/accounts/api_use_cases)",
7+
version: "0.0.1",
8+
type: "action",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: true,
13+
},
14+
props: {
15+
surveybot,
16+
surveyId: {
17+
propDefinition: [
18+
surveybot,
19+
"surveyId",
20+
],
21+
},
22+
},
23+
async run({ $ }) {
24+
const responses = await this.surveybot.getSurveyResponses({
25+
$,
26+
surveyId: this.surveyId,
27+
});
28+
29+
$.export("$summary", `Successfully retrieved survey responses for survey with ID: "${this.surveyId}"`);
30+
return responses;
31+
},
32+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import surveybot from "../../surveybot.app.mjs";
2+
3+
export default {
4+
key: "surveybot-list-surveys",
5+
name: "List Surveys",
6+
description: "List all surveys from SurveyBot. [See the documentation](https://app.surveybot.io/accounts/api_use_cases)",
7+
version: "0.0.1",
8+
type: "action",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: true,
13+
},
14+
props: {
15+
surveybot,
16+
maxResults: {
17+
type: "integer",
18+
label: "Max Results",
19+
description: "The maximum number of surveys to return",
20+
default: 100,
21+
optional: true,
22+
},
23+
},
24+
async run({ $ }) {
25+
const surveys = this.surveybot.paginate({
26+
fn: this.surveybot.listSurveys,
27+
$,
28+
max: this.maxResults,
29+
dataField: "surveys",
30+
});
31+
32+
const results = [];
33+
for await (const survey of surveys) {
34+
results.push(survey);
35+
}
36+
37+
$.export("$summary", `Successfully retrieved ${results.length} survey${results.length === 1
38+
? ""
39+
: "s"}`);
40+
return results;
41+
},
42+
};

components/surveybot/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/surveybot",
3-
"version": "0.6.0",
3+
"version": "0.1.0",
44
"description": "Pipedream surveybot Components",
55
"main": "surveybot.app.mjs",
66
"keywords": [
@@ -13,6 +13,6 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16-
"@pipedream/platform": "^3.0.0"
16+
"@pipedream/platform": "^3.1.0"
1717
}
1818
}
Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,120 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "surveybot",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
surveyId: {
8+
type: "string",
9+
label: "Survey ID",
10+
description: "The ID of the survey to get responses for",
11+
async options({ page }) {
12+
const { surveys } = await this.listSurveys({
13+
params: {
14+
page: page + 1,
15+
},
16+
});
17+
return surveys.map(({
18+
id: value, name: label,
19+
}) => ({
20+
label,
21+
value,
22+
}));
23+
},
24+
},
25+
respondentId: {
26+
type: "string",
27+
label: "Respondent ID",
28+
description: "The ID of the respondent to get",
29+
async options({ surveyId }) {
30+
const { responses } = await this.getSurveyResponses({
31+
surveyId,
32+
});
33+
34+
return responses.map(({
35+
respondent: {
36+
id: value,
37+
name: label,
38+
},
39+
}) => ({
40+
label,
41+
value,
42+
}));
43+
},
44+
},
45+
},
546
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
47+
_apiUrl() {
48+
return "https://surveybot.io/api/v1";
49+
},
50+
_getHeaders() {
51+
return {
52+
"X-Api-Key": `${this.$auth.api_key}`,
53+
};
54+
},
55+
_makeRequest({
56+
$ = this, path, ...opts
57+
}) {
58+
return axios($, {
59+
url: `${this._apiUrl()}/${path}`,
60+
headers: this._getHeaders(),
61+
...opts,
62+
});
63+
},
64+
listSurveys(args = {}) {
65+
return this._makeRequest({
66+
path: "surveys",
67+
...args,
68+
});
69+
},
70+
getSurvey(args = {}) {
71+
return this._makeRequest({
72+
path: "surveys",
73+
...args,
74+
});
75+
},
76+
getSurveyResponses({
77+
surveyId, ...args
78+
}) {
79+
return this._makeRequest({
80+
path: `surveys/${surveyId}/responses`,
81+
...args,
82+
});
83+
},
84+
getSurveyRespondent({
85+
respondentId, ...args
86+
}) {
87+
return this._makeRequest({
88+
path: `respondents/${respondentId}`,
89+
...args,
90+
});
91+
},
92+
async *paginate({
93+
fn, params = {}, maxResults = null, dataField, ...opts
94+
}) {
95+
let hasMore = false;
96+
let count = 0;
97+
let page = 0;
98+
99+
do {
100+
params.page = ++page;
101+
const response = await fn({
102+
params,
103+
...opts,
104+
});
105+
106+
const data = response[dataField];
107+
for (const d of data) {
108+
yield d;
109+
110+
if (maxResults && ++count === maxResults) {
111+
return count;
112+
}
113+
}
114+
115+
hasMore = data.length;
116+
117+
} while (hasMore);
9118
},
10119
},
11120
};

0 commit comments

Comments
 (0)