-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Google Calendar - Pipedream API Key as a prop #18699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
components/google_calendar/sources/common/taskScheduler.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { axios } from "@pipedream/platform"; | ||
import { uuid } from "uuidv4"; | ||
|
||
export default { | ||
methods: { | ||
async _makeAPIRequest({ | ||
$ = this, apiKey, ...opts | ||
}) { | ||
if (!opts.headers) opts.headers = {}; | ||
opts.headers["Authorization"] = `Bearer ${apiKey}`; | ||
opts.headers["Content-Type"] = "application/json"; | ||
opts.headers["user-agent"] = "@PipedreamHQ/pipedream v0.1"; | ||
const { path } = opts; | ||
delete opts.path; | ||
opts.url = `https://api.pipedream.com/v1${path[0] === "/" | ||
? "" | ||
: "/" | ||
}${path}`; | ||
return axios($, opts); | ||
}, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
selfChannel() { | ||
return "self"; | ||
}, | ||
queuedEventsChannel() { | ||
return "$in"; | ||
}, | ||
async subscribe(emitter_id, listener_id, event_name = null, apiKey) { | ||
let params = { | ||
emitter_id, | ||
listener_id, | ||
}; | ||
if (event_name) { | ||
params.event_name = event_name; | ||
} | ||
return await this._makeAPIRequest({ | ||
method: "POST", | ||
path: "/subscriptions", | ||
params, | ||
apiKey, | ||
}); | ||
}, | ||
async selfSubscribe(apiKey) { | ||
const isSubscribedToSelf = this.db.get("isSubscribedToSelf"); | ||
if (!isSubscribedToSelf) { | ||
const componentId = process.env.PD_COMPONENT; | ||
const selfChannel = this.selfChannel(); | ||
console.log(`Subscribing to ${selfChannel} channel for event source`); | ||
console.log( | ||
await this.subscribe(componentId, componentId, selfChannel, apiKey), | ||
); | ||
this.db.set("isSubscribedToSelf", true); | ||
} | ||
}, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
emitScheduleEvent(event, timestamp) { | ||
const selfChannel = this.selfChannel(); | ||
const epoch = Date.parse(timestamp); | ||
const $id = uuid(); | ||
|
||
console.log(`Scheduled event to emit on: ${new Date(epoch)}`); | ||
|
||
this.$emit( | ||
{ | ||
...event, | ||
$channel: selfChannel, | ||
$id, | ||
}, | ||
{ | ||
name: selfChannel, | ||
id: $id, | ||
delivery_ts: epoch, | ||
}, | ||
); | ||
|
||
return $id; | ||
}, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async deleteScheduledEvent(event, apiKey) { | ||
const componentId = process.env.PD_COMPONENT; | ||
const inChannel = this.queuedEventsChannel(); | ||
|
||
// The user must pass a scheduled event UUID they'd like to cancel | ||
// We lookup the event by ID and delete it | ||
const { id } = event.body; | ||
|
||
// List events in the $in channel - the queue of scheduled events, to be emitted in the future | ||
const events = await this.listEvents( | ||
componentId, | ||
inChannel, | ||
apiKey, | ||
); | ||
console.log("Events: ", events); | ||
|
||
// Find the event in the list by id | ||
const eventToCancel = events.data.find((e) => { | ||
const { metadata } = e; | ||
return metadata.id === id; | ||
}); | ||
|
||
console.log("Event to cancel: ", eventToCancel); | ||
|
||
if (!eventToCancel) { | ||
console.log(`No event with ${id} found`); | ||
return false; | ||
} | ||
|
||
// Delete the event | ||
await this.deleteEvent( | ||
componentId, | ||
eventToCancel.id, | ||
inChannel, | ||
apiKey, | ||
); | ||
return true; | ||
}, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async listEvents(dcID, event_name, apiKey) { | ||
return await this._makeAPIRequest({ | ||
path: `/sources/${dcID}/event_summaries`, | ||
params: { | ||
event_name, | ||
}, | ||
apiKey, | ||
}); | ||
}, | ||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async deleteEvent(dcID, eventID, event_name, apiKey) { | ||
return await this._makeAPIRequest({ | ||
method: "DELETE", | ||
path: `/sources/${dcID}/events`, | ||
params: { | ||
start_id: eventID, | ||
end_id: eventID, | ||
event_name, | ||
}, | ||
apiKey, | ||
}); | ||
}, | ||
emitEvent(event, summary) { | ||
const id = event.$id; | ||
delete event.$channel; | ||
delete event.$id; | ||
|
||
this.$emit(event, { | ||
summary: summary ?? JSON.stringify(event), | ||
id, | ||
ts: +new Date(), | ||
}); | ||
}, | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.