public static HttpRequestMessage CreatePutCommandRequest( object command, Guid commandId, string basePath, ResolveMediaType resolveMediaType, SerializeCommand serializeCommand = null) { Ensure.That(command, "command").IsNotNull(); Ensure.That(commandId, "commandId").IsNotEmpty(); Ensure.That(resolveMediaType).IsNotNull(); serializeCommand = serializeCommand ?? DefaultSerializeCommand; using (var writer = new StringWriter()) { serializeCommand(writer, command); var httpContent = new StringContent(writer.ToString()); var mediaType = resolveMediaType(command.GetType(), "json"); httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType); var request = new HttpRequestMessage(HttpMethod.Put, basePath + "/{0}".FormatWith(commandId)) { Content = httpContent }; request.Headers.UserAgent.Add(UserAgent); request.Headers.Accept.Add(HttpProblemDetails.MediaTypeWithQualityHeaderValue); return(request); } }
public static async Task PutCommand( this HttpClient client, object command, Guid commandId, ResolveMediaType resolveMediaType, string basePath = null, Action <HttpRequestMessage> customizeRequest = null, SerializeCommand serializeCommand = null) { Ensure.That(client, "client").IsNotNull(); Ensure.That(command, "command").IsNotNull(); Ensure.That(commandId, "commandId").IsNotEmpty(); Ensure.That(resolveMediaType, "resolveMediaType").IsNotNull(); basePath = basePath ?? string.Empty; var request = CreatePutCommandRequest(command, commandId, basePath, resolveMediaType, serializeCommand); if (customizeRequest != null) { customizeRequest(request); } Logger.InfoFormat("Put Command {0}. Type: {1}", commandId, command.GetType()); HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); Logger.InfoFormat("Put Command {0}. Response: {1}", commandId, response.ReasonPhrase); await response.EnsureCommandSuccess(); }
public static async Task PutCommand( this HttpClient client, object command, Guid commandId, ResolveMediaType resolveMediaType, string basePath = null, Action<HttpRequestMessage> customizeRequest = null, SerializeCommand serializeCommand = null) { Ensure.That(client, "client").IsNotNull(); Ensure.That(command, "command").IsNotNull(); Ensure.That(commandId, "commandId").IsNotEmpty(); Ensure.That(resolveMediaType, "resolveMediaType").IsNotNull(); basePath = basePath ?? string.Empty; var request = CreatePutCommandRequest(command, commandId, basePath, resolveMediaType, serializeCommand); if(customizeRequest != null) { customizeRequest(request); } Logger.InfoFormat("Put Command {0}. Type: {1}", commandId, command.GetType()); HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); Logger.InfoFormat("Put Command {0}. Response: {1}", commandId, response.ReasonPhrase); await response.EnsureCommandSuccess(); }
public static HttpRequestMessage CreatePutCommandRequest( object command, Guid commandId, string basePath, ResolveMediaType resolveMediaType, SerializeCommand serializeCommand = null) { Ensure.That(command, "command").IsNotNull(); Ensure.That(commandId, "commandId").IsNotEmpty(); Ensure.That(resolveMediaType).IsNotNull(); serializeCommand = serializeCommand ?? DefaultSerializeCommand; using(var writer = new StringWriter()) { serializeCommand(writer, command); var httpContent = new StringContent(writer.ToString()); var mediaType = resolveMediaType(command.GetType(), "json"); httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType); var request = new HttpRequestMessage(HttpMethod.Put, basePath + "/{0}".FormatWith(commandId)) { Content = httpContent }; request.Headers.UserAgent.Add(UserAgent); request.Headers.Accept.Add(HttpProblemDetails.MediaTypeWithQualityHeaderValue); return request; } }