public async Task <T> Create <T>(HttpMethod method, string uri, T content) { var requestMessage = new HttpRequestMessage(method, uri); HttpResponseMessage response; using (Stream requestStream = new MemoryStream()) { requestMessage.Content = new StreamContent(requestStream); ApiSerializer.WriteToStream(requestStream, content); response = await _Client.SendAsync(requestMessage); } switch (response.StatusCode) { case HttpStatusCode.Created: break; default: throw new ApiCreateException <T>(response, content); } var createdResource = ApiSerializer.ParseResponse <T>(response); return(createdResource); }
public async Task <T> Get <T>(string url) { var requestMessage = new HttpRequestMessage(HttpMethod.Get, url); Console.WriteLine("GET!"); requestMessage.Headers.Add("X-Hello", "world"); Console.WriteLine(url); Console.WriteLine(requestMessage); Console.WriteLine(requestMessage.Headers); Console.WriteLine(requestMessage.Method); Console.WriteLine(requestMessage.Properties); Console.WriteLine(_Client.BaseAddress); Console.WriteLine(_Client.DefaultRequestHeaders); Console.WriteLine("!!!"); System.Threading.Thread.Sleep(10000); HttpResponseMessage responseMessage = await _Client.SendAsync(requestMessage); Console.WriteLine("Response"); Console.WriteLine(responseMessage.IsSuccessStatusCode); Console.WriteLine(responseMessage); System.Threading.Thread.Sleep(1000); if (!responseMessage.IsSuccessStatusCode) { Console.WriteLine("Error?"); System.Threading.Thread.Sleep(1000); throw new ApiException(responseMessage); } var response = ApiSerializer.ParseResponse <T>(responseMessage); Console.WriteLine(response); System.Threading.Thread.Sleep(1000); return(response); }
public ApiException(HttpResponseMessage response) : base("Received a " + response.StatusCode + " from server.") { Response = response; ErrorBody = ApiSerializer.ParseResponse <ErrorBody>(response); }