Skip to content

Instantly share code, notes, and snippets.

@sperand-io
Last active March 30, 2020 21:24
Show Gist options
  • Save sperand-io/85f5a1c2a500c7e0e4cdbe84fbf8ed00 to your computer and use it in GitHub Desktop.
Save sperand-io/85f5a1c2a500c7e0e4cdbe84fbf8ed00 to your computer and use it in GitHub Desktop.
Segment OCI Streaming Function
async function track(e, settings) {
return await send(e, settings)
}
async function identify(e, settings) {
return await send(e, settings)
}
async function group(e, settings) {
return await send(e, settings)
}
async function alias(e, settings) {
return await send(e, settings)
}
async function screen(e, settings) {
return await send(e, settings)
}
async function page(e, settings) {
return await send(e, settings)
}
async function send(e, { eventMapping, authToken, region }) {
const mappedStreamId = eventMapping[(e.type === 'track' ? e.event : e.type)]
if (!mappedStreamId) return
// is this the correct host ??
const endpoint = `https://streaming-api.${region}.oraclecloud.com/20180418/streams/${mappedStreamId}/messages`
// do we have to sign this request or does this endpoint support authToken
// if so, how do we encode that in the request?
return await fetch(endpoint, {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
'Authorization': btoa(`:${authToken}`) // how is this formatted???
}),
body: JSON.stringify({
messages: [{ key: e.messageId, value: new Buffer(JSON.stringify(e)) }]
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment