/// <summary> /// Executes <see cref="HttpGet" /> method against the entry. /// </summary> /// <param name="id">The unique identifier.</param> /// <param name="action">The action to execute against entry.</param> /// <returns> /// The asynchronous operation of <see cref="IActionResult" />. /// </returns> protected async virtual Task <IActionResult> HttpGet <TModel>(Guid id, Func <Task <HttpMethodResult <TModel> > > action) where TModel : IViewModel { if (id == Guid.Empty) { return(this.BadRequest("The specified unique identifier is empty")); } HttpMethodResult <TModel> result = await action(); if (result.HasErrors) { return(this.UnprocessableEntity(new ErrorViewModel() { Errors = result.Errors })); } if (result.Data == null) { return(this.NotFound()); } return(this.Ok(result.Data)); }
/// <summary> /// Executes <see cref="HttpGet" /> method against the entry. /// </summary> /// <param name="offset">The offset index.</param> /// <param name="limit">The number of records to return..</param> /// <param name="action">The action to execute against entry.</param> /// <returns> /// The asynchronous operation of <see cref="IActionResult" />. /// </returns> protected async virtual Task <IActionResult> HttpGet <TModel>(int offset, int limit, Func <Task <HttpMethodResult <IEnumerable <TModel> > > > action) where TModel : IViewModel { if (offset < 0) { return(this.BadRequest("The offset parameter should be positive number.")); } if (limit <= 0) { return(this.BadRequest("The limit parameter cannot be less or equal zero.")); } HttpMethodResult <IEnumerable <TModel> > result = await action(); if (result.HasErrors) { return(this.UnprocessableEntity(new ErrorViewModel() { Errors = result.Errors })); } return(this.Ok(result.Data)); }
/// <summary> /// Executes <see cref="HttpPost" /> method against the entry. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="model">The model.</param> /// <param name="action">The action to execute against entry.</param> /// <returns> /// The asynchronous operation of <see cref="IActionResult" />. /// </returns> protected async virtual Task <IActionResult> HttpPost <TModel>(TModel model, Func <Task <HttpMethodResult <TModel> > > action) where TModel : IViewModel { if (model == null) { return(this.BadRequest()); } HttpMethodResult <TModel> result = await action(); }
private async Task <HttpMethodResult> Post(bool pPrivate, string pEndPoint, object pBody) { CustomDelegatingHandler customDelegatingHandler = new CustomDelegatingHandler(_APIKey, _APPId); HttpClient client; string apiAcessType = ""; string responseString = ""; if (pPrivate) { client = HttpClientFactory.Create(customDelegatingHandler); apiAcessType = "api/v1/private/"; } else { client = HttpClientFactory.Create(); apiAcessType = "api/v1/public/"; } HttpResponseMessage response = await client.PostAsJsonAsync(_apiBaseAddress + apiAcessType + pEndPoint, pBody); if (response.IsSuccessStatusCode) { responseString = await response.Content.ReadAsStringAsync(); Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine(responseString); Console.WriteLine("HTTP Status: {0}, Reason {1}. Press ENTER to exit", response.StatusCode, response.ReasonPhrase); Console.ResetColor(); } else { responseString = await response.Content.ReadAsStringAsync(); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine(responseString); Console.WriteLine("Failed to call the API. HTTP Status: {0}, Reason {1}", response.StatusCode, response.ReasonPhrase); Console.ResetColor(); Console.Beep(300, 1500); } HttpMethodResult ret = new HttpMethodResult() { Data = responseString, IsSuccessStatusCode = response.IsSuccessStatusCode, }; return(ret); }