public async Task <HttpResponseMessage> PostAsync(CopyImageRequest request) { Log.Information("Create CopyImage from file has been requested : {@createCopyImageRequest}", request); try { if (!ModelState.IsValid) { Log.Error("Could not create CopyImage from file: {errors}", ModelState.SerializeForLog()); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } await copyImageExchangePublisher.PublishAsync(request.FileName, request.FileName, string.IsNullOrEmpty(request.RoutingKey)?string.Empty : request.RoutingKey); return(Request.CreateResponse(HttpStatusCode.OK, new ApiResponse { message = string.Format("The request has completed successfully. A CopyImage has been queued for {0}.", request) })); } catch (Exception ex) { const string message = "An error occurred while creating the CopyImage."; Log.Error(ex, message); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message)); } }
public async Task <CopyImageResponse> CopyImageAsync(CopyImageRequest model) { try { string json = JsonConvert.SerializeObject(model); StringContent stringContent = new StringContent(json, Encoding.Default, "application/json"); var result = await client.PutAsync(API_VERSION + "/copyImage", stringContent); if (result.StatusCode == HttpStatusCode.Unauthorized) { return(new CopyImageResponse { Success = false, Errors = new List <string> { "Unauthorized. Please check your API Key and API Secret." } }); } var jsonResponse = await result.Content.ReadAsStringAsync(); var response = JsonConvert.DeserializeObject <CopyImageResponse>(jsonResponse); return(response); } catch (Exception e) { var ex = new Image4ioException("There is an error while copying image", e); throw ex; } }
public async Task WhenPostFromFile_ThenReturnOk() { var request = new CopyImageRequest { FileName = FileName, RoutingKey = It.IsAny <string>() }; ExpectExchangeToPublish(); var sut = CreateController(new HttpRequestMessage()); var result = await sut.PostAsync(request); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); }
public async Task WhenPostFromFile_ThenPublishAsync() { var request = new CopyImageRequest { FileName = FileName, RoutingKey = It.IsAny <string>() }; ExpectExchangeToPublish(); var sut = CreateController(new HttpRequestMessage()); await sut.PostAsync(request); exchange.Verify(x => x.PublishAsync(request.FileName, It.IsAny <string>(), It.IsAny <string>())); }
public async Task GivenInvalidModelState_WhenPostFromFile_ThenReturnBadRequest() { var request = new CopyImageRequest { FileName = FileName, RoutingKey = It.IsAny <string>() }; var sut = CreateController(new HttpRequestMessage()); sut.ModelState.AddModelError("FileName", "Required"); var result = await sut.PostAsync(request); Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode); }
public async Task WhenPublishAsync_AndUnexpectedException_ThenReturnInternalServerError() { var request = new CopyImageRequest { FileName = FileName }; exchange.Setup(x => x.PublishAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Throws(new Exception()); var sut = CreateController(new HttpRequestMessage()); var result = await sut.PostAsync(request); Assert.AreEqual(HttpStatusCode.InternalServerError, result.StatusCode); }
public void WhenANewCopyImageMessageRequestBodyIs(string name, string routingKey) { var message = new CopyImageRequest { FileName = name, RoutingKey = routingKey == "empty" ? null : routingKey }; var httpClient = new HttpClient(); var url = string.Format("{0}copyimages", ConfigurationHelper.MftAdapterApiUrl); var task = httpClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(message), System.Text.Encoding.UTF8, "application/json")); task.Wait(); var response = task.Result; response.EnsureSuccessStatusCode(); }
public CopyImageResponse CopyImage(CopyImageRequest model) => CopyImageAsync(model).ConfigureAwait(false).GetAwaiter().GetResult();
public Task <CopyImageResponse> CopyImageAsync(CopyImageRequest model) { throw new NotImplementedException(); }
public CopyImageResponse CopyImage(CopyImageRequest model) { throw new NotImplementedException(); }