/// <summary> /// Sends the <see cref="HttpRequestMessage"/> instance. /// </summary> /// <param name="httpClient"><see cref="System.Net.Http.HttpClient"/> instance.</param> /// <param name="request"><see cref="HttpRequestMessage"/> instance.</param> /// <param name="validator"><see cref="ISchemaValidator"/> instance.</param> /// <param name="path">Schema path.</param> /// <param name="cancellationToken"><see cref="CancellationToken"/> instance.</param> /// <returns>Returns <see cref="HttpResponseMessage"/> instance.</returns> public static async Task <HttpResponseMessage> SendAsync(this System.Net.Http.HttpClient httpClient, HttpRequestMessage request, ISchemaValidator validator, string path, CancellationToken cancellationToken) { httpClient.ThrowIfNullOrDefault(); request.ThrowIfNullOrDefault(); cancellationToken.ThrowIfNullOrDefault(); validator.ThrowIfNullOrDefault(); path.ThrowIfNullOrWhiteSpace(); if (!request.Method.IsSupported()) { return(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)); } if (request.Method != HttpMethod.Get) { await ValidateAsync(request, validator, path).ConfigureAwait(false); } var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false); response.EnsureSuccessStatusCode(); if (request.Method == HttpMethod.Get) { await ValidateAsync(response, validator, path).ConfigureAwait(false); return(response); } return(response); }
/// <summary> /// Validates the message against the schema from the given path. /// </summary> /// <param name="message"><see cref="BrokeredMessage"/> instance.</param> /// <param name="validator"><see cref="ISchemaValidator"/> instance.</param> /// <param name="schemaPathPropertyKey">Property key for the schema path.</param> /// <returns>Returns the <see cref="BrokeredMessage"/> instance, if validated; otherwise throws an exception.</returns> public static async Task <BrokeredMessage> ValidateAsync(this BrokeredMessage message, ISchemaValidator validator, string schemaPathPropertyKey = "schemaPath") { message.ThrowIfNullOrDefault(); validator.ThrowIfNullOrDefault(); schemaPathPropertyKey.ThrowIfNullOrWhiteSpace(); var cloned = message.Clone(); var payload = default(string); using (var stream = cloned.GetBody <Stream>()) using (var reader = new StreamReader(stream)) { payload = await reader.ReadToEndAsync().ConfigureAwait(false); } if (payload.IsNullOrWhiteSpace()) { throw new MessageBodyZeroLengthException().WithServiceBusMessage(cloned); } var path = cloned.Properties[schemaPathPropertyKey] as string; if (path.IsNullOrWhiteSpace()) { throw new SchemaPathNotExistException().WithServiceBusMessage(cloned); } var validated = await validator.ValidateAsync(payload, path).ConfigureAwait(false); return(message); }
/// <summary> /// Validates the payload against the schema from the sink. /// </summary> /// <param name="payload">JSON payload.</param> /// <param name="validator"><see cref="ISchemaValidator"/> instance.</param> /// <param name="path">Schema path in the sink.</param> /// <returns>Returns the payload, if the payload is valid; otherwise throws <see cref="SchemaValidationException"/>.</returns> public static async Task <string> ValidateAsStringAsync(this string payload, ISchemaValidator validator, string path) { payload.ThrowIfNullOrWhiteSpace(); validator.ThrowIfNullOrDefault(); path.ThrowIfNullOrWhiteSpace(); var validated = await validator.ValidateAsync(payload, path).ConfigureAwait(false); return(payload); }
/// <summary> /// Sends the GET request to the given URI. /// </summary> /// <param name="httpClient"><see cref="System.Net.Http.HttpClient"/> instance.</param> /// <param name="requestUri">Request URI.</param> /// <param name="validator"><see cref="ISchemaValidator"/> instance.</param> /// <param name="path">Schema path.</param> /// <returns>Returns the string response.</returns> public static async Task <string> GetStringAsync(this System.Net.Http.HttpClient httpClient, Uri requestUri, ISchemaValidator validator, string path) { httpClient.ThrowIfNullOrDefault(); requestUri.ThrowIfNullOrDefault(); validator.ThrowIfNullOrDefault(); path.ThrowIfNullOrWhiteSpace(); var payload = await httpClient.GetStringAsync(requestUri).ConfigureAwait(false); await ValidateAsync(payload, validator, path).ConfigureAwait(false); return(payload); }
/// <summary> /// Sends the GET request to the given URI. /// </summary> /// <param name="httpClient"><see cref="System.Net.Http.HttpClient"/> instance.</param> /// <param name="requestUri">Request URI.</param> /// <param name="completionOption"><see cref="HttpCompletionOption"/> instance.</param> /// <param name="validator"><see cref="ISchemaValidator"/> instance.</param> /// <param name="path">Schema path.</param> /// <returns>Returns <see cref="HttpResponseMessage"/> instance.</returns> public static async Task <HttpResponseMessage> GetAsync(this System.Net.Http.HttpClient httpClient, string requestUri, HttpCompletionOption completionOption, ISchemaValidator validator, string path) { httpClient.ThrowIfNullOrDefault(); requestUri.ThrowIfNullOrWhiteSpace(); validator.ThrowIfNullOrDefault(); path.ThrowIfNullOrWhiteSpace(); var response = await httpClient.GetAsync(requestUri, completionOption).ConfigureAwait(false); response.EnsureSuccessStatusCode(); await ValidateAsync(response, validator, path).ConfigureAwait(false); return(response); }
/// <summary> /// Sends the PUT request to the given URI. /// </summary> /// <param name="httpClient"><see cref="System.Net.Http.HttpClient"/> instance.</param> /// <param name="requestUri">Request URI.</param> /// <param name="content"><see cref="HttpContent"/> instance.</param> /// <param name="validator"><see cref="ISchemaValidator"/> instance.</param> /// <param name="path">Schema path.</param> /// <param name="cancellationToken"><see cref="CancellationToken"/> instance.</param> /// <returns>Returns <see cref="HttpResponseMessage"/> instance.</returns> public static async Task <HttpResponseMessage> PutAsync(this System.Net.Http.HttpClient httpClient, string requestUri, HttpContent content, ISchemaValidator validator, string path, CancellationToken cancellationToken) { httpClient.ThrowIfNullOrDefault(); requestUri.ThrowIfNullOrWhiteSpace(); content.ThrowIfNullOrDefault(); cancellationToken.ThrowIfNullOrDefault(); validator.ThrowIfNullOrDefault(); path.ThrowIfNullOrWhiteSpace(); await ValidateAsync(content, validator, path).ConfigureAwait(false); var response = await httpClient.PutAsync(requestUri, content, cancellationToken).ConfigureAwait(false); response.EnsureSuccessStatusCode(); return(response); }
/// <summary> /// Sends the PATCH request to the given URI. /// </summary> /// <param name="httpClient"><see cref="System.Net.Http.HttpClient"/> instance.</param> /// <param name="requestUri">Request URI.</param> /// <param name="content"><see cref="HttpContent"/> instance.</param> /// <param name="validator"><see cref="ISchemaValidator"/> instance.</param> /// <param name="path">Schema path.</param> /// <returns>Returns <see cref="HttpResponseMessage"/> instance.</returns> public static async Task <HttpResponseMessage> PatchAsync(this System.Net.Http.HttpClient httpClient, Uri requestUri, HttpContent content, ISchemaValidator validator, string path) { httpClient.ThrowIfNullOrDefault(); requestUri.ThrowIfNullOrDefault(); content.ThrowIfNullOrDefault(); validator.ThrowIfNullOrDefault(); path.ThrowIfNullOrWhiteSpace(); await ValidateAsync(content, validator, path).ConfigureAwait(false); using (var request = new HttpRequestMessage(patch, requestUri) { Content = content }) { var response = await httpClient.SendAsync(request).ConfigureAwait(false); response.EnsureSuccessStatusCode(); return(response); } }
/// <inheritdoc /> public virtual ServiceBusPlugin WithValidator(ISchemaValidator validator) { this.Validator = validator.ThrowIfNullOrDefault(); return(this); }