public bool ValidateSchema(EddnEvent @event) { try { var schemaRef = @event.SchemaRef.Replace("/test", ""); if (!schemaCache.ContainsKey(schemaRef)) { logger.Error("Schema not found: {0}", schemaRef); return(false); } var schema = schemaCache[schemaRef]; var validationErrors = schema.Validate(JObject.FromObject(@event)); foreach (var error in validationErrors) { logger.Error(error.ToString()); } return(validationErrors.Count == 0); } catch (Exception e) { logger.Error(e, "Error validating event"); return(false); } }
public async Task PostEventAsync(EddnEvent @event) { var eventData = Serialize.ToJson(@event); try { var result = await restClient.PostAsync(eventData); if (result != "OK") { Log.Error() .Message(result) .Property("input", eventData) .Write(); } else { Log.Info("Pushed event {0}", @event.GetType()); } } catch (Exception e) { Log.Error() .Message("Error pushing event") .Exception(e) .Property("input", eventData) .Write(); } }
/// <summary> /// Check event against list of last few sent events, excluding timestamp from comparison /// </summary> /// <param name="e">Event to check uniqueness</param> /// <returns>true if event wasn't sent before</returns> private bool IsUnique(EddnEvent e) { try { JObject jObject; while (lastPushedEvents.Count > 30) { lastPushedEvents.TryDequeue(out jObject); } jObject = JObject.FromObject(e); var messageObject = jObject.Property("message")?.Value as JObject; messageObject?.Property("timestamp")?.Remove(); foreach (var recent in lastPushedEvents) { if (JToken.DeepEquals(jObject, recent)) { return(false); } } lastPushedEvents.Enqueue(jObject); return(true); } catch (Exception ex) { Log.Error(ex, "Error in deduplication"); return(true); // by default, we consider any message unique and so will send it } }
public bool ValidateSchema(EddnEvent @event) { try { var schemaRef = @event.SchemaRef.Replace("/test", string.Empty); if (!schemaCache.ContainsKey(schemaRef)) { Log.Error("Schema not found: {0}", schemaRef); return(false); } var schema = schemaCache[schemaRef]; var validationErrors = schema.Validate(JObject.FromObject(@event)) .Where(ve => ve.Path != "#/message.prohibited") // Bug in NJsonSchema, TODO .Where(ve => ve.Path != "#/message.economies") // Bug in NJsonSchema, TODO .ToList(); foreach (var error in validationErrors) { Log.Error(error.ToString()); } return(validationErrors.Count == 0); } catch (Exception e) { Log.Error(e, "Error validating event"); return(false); } }
private async Task PostAsync(EddnEvent e) { var result = await restClient.PostAsync(Serialize.ToJson(e)); if (result != "OK") { logger.Error(result); } }