/// <summary> /// Copies a <see cref="CloudEvent"/> batch into the specified <see cref="HttpWebRequest"/>. /// </summary> /// <param name="cloudEvents">CloudEvent batch to copy. Must not be null, and must be a valid CloudEvent.</param> /// <param name="destination">The request to populate. Must not be null.</param> /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param> /// <returns>A task representing the asynchronous operation.</returns> public static async Task CopyToHttpWebRequestAsync(this IReadOnlyList <CloudEvent> cloudEvents, HttpWebRequest destination, CloudEventFormatter formatter) { Validation.CheckCloudEventBatchArgument(cloudEvents, nameof(cloudEvents)); Validation.CheckNotNull(destination, nameof(destination)); Validation.CheckNotNull(formatter, nameof(formatter)); byte[] content = formatter.EncodeBatchModeMessage(cloudEvents, out var contentType); destination.ContentType = contentType.ToString(); await destination.GetRequestStream().WriteAsync(content, 0, content.Length).ConfigureAwait(false); }
/// <summary> /// Copies a <see cref="CloudEvent"/> batch into the specified <see cref="HttpWebRequest"/>. /// </summary> /// <param name="cloudEvents">CloudEvent batch to copy. Must not be null, and must be a valid CloudEvent.</param> /// <param name="destination">The request to populate. Must not be null.</param> /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param> /// <returns>A task representing the asynchronous operation.</returns> public static async Task CopyToHttpWebRequestAsync(this IReadOnlyList <CloudEvent> cloudEvents, HttpWebRequest destination, CloudEventFormatter formatter) { Validation.CheckCloudEventBatchArgument(cloudEvents, nameof(cloudEvents)); Validation.CheckNotNull(destination, nameof(destination)); Validation.CheckNotNull(formatter, nameof(formatter)); ReadOnlyMemory <byte> content = formatter.EncodeBatchModeMessage(cloudEvents, out var contentType); destination.ContentType = contentType.ToString(); await BinaryDataUtilities.CopyToStreamAsync(content, destination.GetRequestStream()).ConfigureAwait(false); }
/// <summary> /// Copies a <see cref="CloudEvent"/> batch into an <see cref="HttpListenerResponse" />. /// </summary> /// <param name="cloudEvent">The CloudEvent to copy. Must not be null, and must be a valid CloudEvent.</param> /// <param name="destination">The response to copy the CloudEvent to. Must not be null.</param> /// <param name="contentMode">Content mode (structured or binary)</param> /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param> /// <returns>A task representing the asynchronous operation.</returns> public static async Task CopyToHttpListenerResponseAsync(this IReadOnlyList <CloudEvent> cloudEvents, HttpListenerResponse destination, CloudEventFormatter formatter) { Validation.CheckCloudEventBatchArgument(cloudEvents, nameof(cloudEvents)); Validation.CheckNotNull(destination, nameof(destination)); Validation.CheckNotNull(formatter, nameof(formatter)); // TODO: Validate that all events in the batch have the same version? // See https://github.com/cloudevents/spec/issues/807 byte[] content = formatter.EncodeBatchModeMessage(cloudEvents, out var contentType); destination.ContentType = contentType.ToString(); await destination.OutputStream.WriteAsync(content, 0, content.Length).ConfigureAwait(false); }
/// <summary> /// Converts a CloudEvent batch to <see cref="HttpContent"/>. /// </summary> /// <param name="cloudEvents">The CloudEvent batch to convert. Must not be null, and every element must be non-null reference to a valid CloudEvent.</param> /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param> public static HttpContent ToHttpContent(this IReadOnlyList <CloudEvent> cloudEvents, CloudEventFormatter formatter) { Validation.CheckCloudEventBatchArgument(cloudEvents, nameof(cloudEvents)); Validation.CheckNotNull(formatter, nameof(formatter)); // TODO: Validate that all events in the batch have the same version? // See https://github.com/cloudevents/spec/issues/807 ReadOnlyMemory <byte> content = formatter.EncodeBatchModeMessage(cloudEvents, out var contentType); // Note: we don't populate any other headers for batch mode. var ret = ToByteArrayContent(content); ret.Headers.ContentType = MimeUtilities.ToMediaTypeHeaderValue(contentType); return(ret); }
/// <summary> /// Copies a <see cref="CloudEvent"/> batch into an <see cref="HttpResponse" />. /// </summary> /// <param name="cloudEvents">The CloudEvent batch to copy. Must not be null, and must be a valid CloudEvent.</param> /// <param name="destination">The response to copy the CloudEvent to. Must not be null.</param> /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param> /// <returns>A task representing the asynchronous operation.</returns> public static async Task CopyToHttpResponseAsync(this IReadOnlyList <CloudEvent> cloudEvents, HttpResponse destination, CloudEventFormatter formatter) { Validation.CheckCloudEventBatchArgument(cloudEvents, nameof(cloudEvents)); Validation.CheckNotNull(destination, nameof(destination)); Validation.CheckNotNull(formatter, nameof(formatter)); // TODO: Validate that all events in the batch have the same version? // See https://github.com/cloudevents/spec/issues/807 ReadOnlyMemory <byte> content = formatter.EncodeBatchModeMessage(cloudEvents, out var contentType); destination.ContentType = contentType.ToString(); destination.ContentLength = content.Length; await BinaryDataUtilities.CopyToStreamAsync(content, destination.Body).ConfigureAwait(false); }