[docs]classConnector(BaseConnector):NAME="twilio_monitor_events"POINTER_PATH="event_date"LOG_ORDER=REVERSE_CHRONOLOGICAL@propertydefsecret(self):"""Fetches the API secret from the configuration. This is required as this is a third authentication element required by Twilio when using API authentication. :return: The value of the 'secret' field from the configuration. """try:returnself.configuration.secretexceptAttributeError:returnNone
[docs]defcollect(self):"""Collects all events from the Twilio Monitor Event API. This will first check whether there are any pointers cached to indicate previous collections. If not, the 7-days of data will be collected. """# Construct the client based on whether "API key" or "auth token" authentication# is configured.ifself.secret:client=Client(self.key,self.secret,self.identity)else:client=Client(self.identity,self.key)# If no pointer is stored then a previous run hasn't been performed, so set the# pointer to a week ago.try:_=self.pointerexceptNotFoundException:self.pointer=(datetime.now(timezone.utc)-timedelta(days=7)).strftime("%Y-%m-%dT%H:%M:%S%z")# Stream the data from the API, paging as required.entries=[]batch_size=1000try:events=client.monitor.events.stream(start_date=self.pointer)exceptTwilioExceptionaserr:raiseRequestFailedException(err)foreventinevents:# This is less than ideal, but as EventInstance returns datetime objects# for the event_date, and does not expose a way to return the entire set# of properties we'll need to break encapsulation to get all of the data# mapped into the object.candidate=event._propertiescandidate["event_date"]=event.event_date.strftime("%Y-%m-%dT%H:%M:%S%z")# Track the event.entries.append(candidate)# Save batches, and clear the list for the next page.iflen(entries)==batch_size:self.save(entries)entries=[]# Save any records not previously saved, before completion.self.save(entries)