private async Task <T> SendRequest <T>( HttpMethod method, string url, object parameters = null, IContentSerializer serializer = null, IContentDeserializer <T> deserializer = null) where T : class { if (string.IsNullOrEmpty(url)) { throw new ArgumentException($"'{nameof(url)}' cannot be null or empty", nameof(url)); } var ser = serializer ?? new JsonContentSerializer(false); var des = deserializer ?? new JsonContentDeserializer <T>(); using HttpRequestMessage msg = new HttpRequestMessage(method, url); if (parameters != null) { msg.Content = ser.Serialize(parameters); } T result = default(T); using (var client = this.CreateClient()) { var response = await client.SendAsync(msg).ConfigureAwait(false); if (response.IsSuccessStatusCode) { if (response.StatusCode != HttpStatusCode.NoContent) { string content = await response.Content .ReadAsStringAsync() .ConfigureAwait(false); result = des.Deserialize(content); } } else { string content = await response.Content .ReadAsStringAsync() .ConfigureAwait(false); var error = ErrorDeserializer.Deserialize(content); throw new MinifluxException(response.StatusCode, error.ErrorMessage); } } return(result); }
private Task <object> InternalDeserialize(Task <HttpResponseMessage> task, Type type) { return(task.Then(httpResponseMessage => { if (httpResponseMessage.IsSuccessStatusCode) { return httpResponseMessage.Content.ReadAsStreamAsync() .Then(stream => ContentDeserializer.Deserialize(stream, type, httpResponseMessage)); } else { throw new ApiException(httpResponseMessage); } })); }
private async Task <T> InternalDeserialize <T>(Task <HttpResponseMessage> task) { var httpResponseMessage = await task; if (httpResponseMessage.IsSuccessStatusCode) { using (var tr = new StreamReader(await httpResponseMessage.Content.ReadAsStreamAsync())) { return(ContentDeserializer.Deserialize <T>(tr, task.Result)); } } else { throw new ApiException(httpResponseMessage); } }
public static async Task <Maybe <TModel> > SendToDeserializer <TModel>(HttpResponseMessage responseMessage, IContentDeserializer contentSerializer) { return(await contentSerializer.Deserialize <TModel>(responseMessage.Content)); }