private async Task <GenericResponse> Save(Expense expense) { GenericResponse genericResponse = new GenericResponse(); string resourceUri = _config.GetSection("api").GetSection("postExpenses").Value; string url = $"{ _config.GetSection("api").GetSection("baseUrl").Value}{resourceUri}"; using (var client = new HttpClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, url)) using (var httpContent = new UtilHttpContent().CreateHttpContent(expense)) { request.Content = httpContent; using (var response = await client .SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.Created) { genericResponse = await response.Content.ReadAsAsync <GenericResponse>(); return(genericResponse); } else { var content = await response.Content.ReadAsStringAsync(); genericResponse.Message = content; genericResponse.Status = (int)response.StatusCode; return(genericResponse); } } } }
private async void SendEmail(string userId, int expenseId) { // Fire and forget try { string resourceUri = _config.GetSection("api").GetSection("postSendEmail").Value; string url = $"{ _config.GetSection("api").GetSection("baseUrl").Value}{resourceUri}"; RequestSendEmail reqSendEmail = new RequestSendEmail() { UserId = userId, ExpenseId = expenseId }; using (var client = new HttpClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, url)) using (var httpContent = new UtilHttpContent().CreateHttpContent(reqSendEmail)) { request.Content = httpContent; using (var response = await client .SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { if (response.StatusCode == System.Net.HttpStatusCode.OK) { // var content = await response.Content.ReadAsStringAsync(); // return StatusCode((int)response.StatusCode, content); // LOG Success } } } } catch (Exception ex) { string msg = ex.Message; // LOG ERROR } }
public async Task <HttpResponseMessage> Post <T>(T value, string resourseUri) { try { string url = $"{ _config.GetSection("api").GetSection("baseUrl").Value}{resourseUri}"; using (var client = new HttpClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, url)) using (var httpContent = new UtilHttpContent().CreateHttpContent(value)) { request.Content = httpContent; using (var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { return(response); } } } catch (Exception ex) { throw new Exception("Error accessing POST Api: " + ex.Message); } }