Skip to content

Commit

Permalink
updated per comments
Browse files Browse the repository at this point in the history
  • Loading branch information
michelle0927 committed Sep 18, 2020
1 parent 9afae2c commit 7d244ca
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 207 deletions.
153 changes: 46 additions & 107 deletions components/asana/asana.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,41 @@ module.exports = {
},
},
methods: {
async _getBaseUrl() {
return "https://app.asana.com/api/1.0"
},
async _getHeaders() {
return {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
};
},
async _getAuthorizationHeader({ data, method, url, headers }) {
const token = {
key: this.$auth.oauth_access_token,
secret: this.$auth.oauth_refresh_token,
async _makeRequest(endpoint) {
config = {
url: `${await this._getBaseUrl()}/${endpoint}`,
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
}
};
try {
return await axios(config);
} catch (err) {
console.log(err);
}
},
async _getAuthorizationHeader({ data, method, url, headers }) {
return await axios({
method: "POST",
url: `https://app.asana.com/api/1.0/webhooks`,
url: `${await this._getBaseUrl()}/webhooks`,
data: data.body,
headers,
});
},
async _makeWebhookRequest(config) {
if (!config.headers) config.headers = {};
const authorization = await this._getAuthorizationHeader(config);
config.headers.authorization = authorization;
try {
await axios(config);
} catch (err) {
console.log(err);
}
return authorization.data;
},
async createHook(body) {
const resp = await this._makeWebhookRequest({
const config = {
method: "post",
url: `https://app.asana.com/api/1.0/webhooks`,
url: `${await this._getBaseUrl()}/webhooks`,
headers: {
"Content-Type": "applicaton/json",
Accept: "application/json",
Expand All @@ -100,13 +102,20 @@ module.exports = {
data: {
body,
},
});
return resp;
};
const authorization = await this._getAuthorizationHeader(config);
config.headers.authorization = authorization;
try {
await axios(config);
} catch (err) {
console.log(err);
}
return authorization.data;
},
async deleteHook(hookId) {
const config = {
method: "delete",
url: `https://app.asana.com/api/1.0/webhooks/${hookId}`,
url: `${await this._getBaseUrl()}/webhooks/${hookId}`,
headers: await this._getHeaders(),
};
try {
Expand All @@ -123,28 +132,16 @@ module.exports = {
var content = JSON.stringify(request.body);
var doubleHash = base64Digest(content);
var headerHash = request.headers["x-hook-secret"];
return doubleHash == headerHash;
return doubleHash === headerHash;
},
async getWorkspace(workspaceId) {
const workspace = await axios.get(
`https://app.asana.com/api/1.0/workspaces/${workspaceId}`,
{
headers: await this._getHeaders(),
}
);
return workspace.data.data;
return (await this._makeRequest(`workspaces/${workspaceId}`)).data.data;
},
async getWorkspaces() {
const workspaces = await axios.get(
`https://app.asana.com/api/1.0/workspaces/`,
{
headers: await this._getHeaders(),
}
);
return workspaces.data.data;
return (await this._makeRequest("workspaces")).data.data;
},
async getOrganizations() {
let organizations = [];
const organizations = [];
const workspaces = await this.getWorkspaces();
for (const workspace of workspaces) {
let w = await this.getWorkspace(workspace.gid);
Expand All @@ -153,99 +150,41 @@ module.exports = {
return organizations;
},
async getProject(projectId) {
const project = await axios.get(
`https://app.asana.com/api/1.0/projects/${projectId}`,
{
headers: await this._getHeaders(),
}
);
return project.data.data;
return (await this._makeRequest(`projects/${projectId}`)).data.data;
},
async getProjects(workspaceId) {
const projects = await axios.get(
`https://app.asana.com/api/1.0/projects?workspace=${workspaceId}`,
{
headers: await this._getHeaders(),
}
);
return projects.data.data;
return (await this._makeRequest(`projects?workspace=${workspaceId}`)).data.data;
},
async getStory(storyId) {
const story = await axios.get(
`https://app.asana.com/api/1.0/stories/${storyId}`,
{
headers: await this._getHeaders(),
}
);
return story.data.data;
return (await this._makeRequest(`stories/${storyId}`)).data.data;
},
async getTask(taskId) {
const task = await axios.get(
`https://app.asana.com/api/1.0/tasks/${taskId}`,
{
headers: await this._getHeaders(),
}
);
return task.data.data;
return (await this._makeRequest(`tasks/${taskId}`)).data.data;
},
async getTasks(projectId) {
let incompleteTasks = [];
const tasks = await axios.get(
`https://app.asana.com/api/1.0/projects/${projectId}/tasks`,
{
headers: await this._getHeaders(),
}
);
for (const task of tasks.data.data) {
const tasks = (await this._makeRequest(`projects/${projectId}/tasks`)).data.data;
for (const task of tasks) {
let t = await this.getTask(task.gid);
if (t.completed == false) incompleteTasks.push(task);
}
return incompleteTasks;
},
async getTag(tagId) {
const tag = await axios.get(
`https://app.asana.com/api/1.0/tags/${tagId}`,
{
headers: await this._getHeaders(),
}
);
return tag.data.data;
return (await this._makeRequest(`tags/${tagId}`)).data.data;
},
async getTeam(teamId) {
const team = await axios.get(
`https://app.asana.com/api/1.0/teams/${teamId}`,
{
headers: await this._getHeaders(),
}
);
return team.data.data;
return (await this._makeRequest(`teams/${teamId}`)).data.data;
},
async getTeams(organizationId) {
const teams = await axios.get(
`https://app.asana.com/api/1.0/organizations/${organizationId}/teams`,
{
headers: await this._getHeaders(),
}
);
return teams.data.data;
return (await this._makeRequest(`organizations/${organizationId}/teams`)).data.data;
},
async getUser(userId) {
const user = await axios.get(
`https://app.asana.com/api/1.0/users/${userId}`,
{
headers: await this._getHeaders(),
}
);
return user.data.data;

return (await this._makeRequest(`users/${userId}`)).data.data;
},
async getUsers(workspaceId) {
const users = await axios.get(
`https://app.asana.com/api/1.0/workspaces/${workspaceId}/users`,
{
headers: await this._getHeaders(),
}
);
return users.data.data;
return (await this._makeRequest(`workspaces/${workspaceId}/users`)).data.data;
},
},
};
14 changes: 4 additions & 10 deletions components/asana/completed-task.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const asana = require("https://github.com/PipedreamHQ/pipedream/components/asana/asana.app.js");
const get = require("lodash.get");

module.exports = {
name: "Task Completed",
name: "Task Completed (Instant)",
description: "Emits an event for each task completed in a project.",
version: "0.0.1",
dedupe: "unique",
Expand Down Expand Up @@ -56,23 +55,18 @@ module.exports = {
},
});

const body = get(event, "body");
const { body } = event;
if (!body || !body.events) {
return;
}

let tasks = [];

for (const e of body.events) {
tasks.push(await this.asana.getTask(e.resource.gid));
}

for (const task of tasks) {
let task = await this.asana.getTask(e.resource.gid);
this.$emit(task, {
id: task.gid,
summary: task.name,
ts: Date.now(),
});
}
}
},
};
12 changes: 3 additions & 9 deletions components/asana/new-project.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const asana = require("https://github.com/PipedreamHQ/pipedream/components/asana/asana.app.js");
const get = require("lodash.get");

module.exports = {
name: "Project Added To Workspace",
name: "Project Added To Workspace (Instant)",
description: "Emits an event for each new project added to a workspace.",
version: "0.0.1",
dedupe: "unique",
Expand Down Expand Up @@ -48,18 +47,13 @@ module.exports = {
},
});

const body = get(event, "body");
const { body } = event;
if (!body || !body.events) {
return;
}

let projects = [];

for (const e of body.events) {
projects.push(await this.asana.getProject(e.resource.gid));
}

for (const project of projects) {
let project = await this.asana.getProject(e.resource.gid);
this.$emit(project, {
id: project.gid,
summary: project.name,
Expand Down
14 changes: 4 additions & 10 deletions components/asana/new-story.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const asana = require("https://github.com/PipedreamHQ/pipedream/components/asana/asana.app.js");
const get = require("lodash.get");

module.exports = {
name: "Story Added To Project",
name: "Story Added To Project (Instant)",
description: "Emits an event for each story added to a project.",
version: "0.0.1",
dedupe: "unique",
Expand Down Expand Up @@ -55,23 +54,18 @@ module.exports = {
},
});

const body = get(event, "body");
const { body } = event;
if (!body || !body.events) {
return;
}

let stories = [];

for (const e of body.events) {
stories.push(await this.asana.getStory(e.resource.gid));;
}

for (const story of stories) {
let story = await this.asana.getStory(e.resource.gid);
this.$emit(story, {
id: story.gid,
summary: story.text,
ts: Date.now(),
});
}
}
},
};
21 changes: 8 additions & 13 deletions components/asana/new-subtask.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const asana = require("https://github.com/PipedreamHQ/pipedream/components/asana/asana.app.js");
const get = require("lodash.get");

module.exports = {
name: "New Subtask",
name: "New Subtask (Instant)",
description: "Emits an event for each subtask added to a project.",
version: "0.0.1",
dedupe: "unique",
Expand Down Expand Up @@ -60,26 +59,22 @@ module.exports = {
},
});

const body = get(event, "body");
const { body } = event;
if (!body || !body.events) {
return;
}

let tasks = [];
const taskIds = this.db.get("taskIds");

for (const e of body.events) {
if (e.parent.resource_type == "task" && (!taskIds || (taskIds.length < 0) || (Object.keys(taskIds).length === 0) || (taskIds && taskIds.includes(e.parent.gid)))) {
tasks.push(await this.asana.getTask(e.resource.gid));
let task = await this.asana.getTask(e.resource.gid);
this.$emit(task, {
id: task.gid,
summary: task.name,
ts: Date.now(),
});
}
}

for (const task of tasks) {
this.$emit(task, {
id: task.gid,
summary: task.name,
ts: Date.now(),
});
}
},
};
Loading

0 comments on commit 7d244ca

Please sign in to comment.