|
| 1 | +# Advanced Topics for the Smartsheet SDK for Javascript |
| 2 | + |
| 3 | +## Event Reporting |
| 4 | +The following sample demonstrates best practices for consuming the event stream returned from the Smartsheet Event Reporting feature. |
| 5 | + |
| 6 | +The sample uses the `smartsheet.events.getEvents` method to request lists of events from the stream. The first request sets the `since` parameter with the point in time (i.e. event occurrence datetime) in the stream from which to start consuming events. The `since` parameter can be set with a datetime value that is either formatted as ISO 8601 (e.g. 2010-01-01T00:00:00Z) or as UNIX epoch (in which case the `numericDates` parameter must also be set to `true`. By default the `numericDates` parameter is set to `false`). |
| 7 | + |
| 8 | +To consume the next list of events after the initial list of events is returned, set the `streamPosition` parameter with the `nextStreamPosition` attribute obtained from the previous request and don't set the `since` parameter with any values. This is because when using the `get` method, either the `since` parameter or the `streamPosition` parameter should be set, but never both. |
| 9 | + |
| 10 | +Note that the `moreAvailable` attribute in a response indicates whether more events are immediately available for consumption. If events aren't immediately available, they may still be generating so subsequent requests should keep using the same `streamPosition` value until the next list of events is retrieved. |
| 11 | + |
| 12 | +Many events have additional information available as a part of the event. That information can be accessed from the data stored in the `additionalDetails` attribute. Information about the additional details provided can be found [here](https://smartsheet-platform.github.io/event-reporting-docs/). |
| 13 | + |
| 14 | + |
| 15 | +```javascript |
| 16 | +// Initialize the client |
| 17 | +var client = require('smartsheet'); |
| 18 | +var smartsheet = client.createClient({ |
| 19 | + accessToken: '1234', |
| 20 | + logLevel: 'info' |
| 21 | +}); |
| 22 | + |
| 23 | +const currentDate = new Date(); |
| 24 | +const dateWeekAgo = currentDate.setDate(currentDate.getDate() - 7); |
| 25 | +// The first call to the events reporting API |
| 26 | +// requires the since query parameter. |
| 27 | +// If you pass in an UNIX epoch date, numericDates must be true |
| 28 | +let options = { |
| 29 | + queryParameters: { |
| 30 | + since: dateWeekAgo, |
| 31 | + maxCount: 10, |
| 32 | + numericDates: true |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function getEvents(options) { |
| 37 | + smartsheet.events.getEvents(options) |
| 38 | + .then((result) => { |
| 39 | + printNewSheetEvents(result); |
| 40 | + getNextStreamOfEvents(result.moreAvailable, result.nextStreamPosition); |
| 41 | + }) |
| 42 | + .catch((error) => console.log(JSON.stringify(error))); |
| 43 | +} |
| 44 | + |
| 45 | +function getNextStreamOfEvents(moreEventsAvailable, nextStreamPosition) { |
| 46 | + // Subsequent calls require the streamPosition property |
| 47 | + options = { |
| 48 | + queryParameters: { |
| 49 | + streamPosition: nextStreamPosition, |
| 50 | + maxCount: 10 |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (moreEventsAvailable) { |
| 55 | + getEvents(options); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// This example is looking specifically for new sheet events |
| 60 | +function printNewSheetEvents(result) { |
| 61 | + // Find all created sheets |
| 62 | + result.data.forEach(function (item) { |
| 63 | + if (item.objectType === "SHEET" && item.action === "CREATE") { |
| 64 | + console.log(item.additionalDetails.sheetName) |
| 65 | + } |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +getEvents(options); |
| 70 | +``` |
0 commit comments