private async Task SendImageUsingReferenceAsync(List <string> objectIDs, ImageRef imageRef, int page = 1, string locationName = null, string batchID = null) { //Create an image object. MultiProductImage image = new MultiProductImage { ImageReference = imageRef.ImageReference, ObjectIDs = objectIDs, //The type of display this image is meant for. //See Appendix A of the API reference for the full list. DisplayTypeName = "Chroma29", PageID = page, }; //Send the image as a local override image if (locationName != null) { image.LocationName = locationName; } //Use a user supplied batchID //For more information on how to use this feature see section 5.1 of the System Management documentation. if (batchID != null) { image.UserDefinedBatchID = batchID; } var response = await _client.PostApiAsync <MultiProductImage>("api/objects/imagetomultipleobjects", image); response.EnsureSuccessStatusCode(); }
public async Task SendImagesAsync() { //A batch API endpoint can be used to send multiple requests together. This cuts down on excess HTTP traffic. // Create the products we are going to assign the image to var products = new List <Product>(); for (int i = 0; i < 100; i++) { products.Add(await AddProduct()); } //Create the content body for the batch request var batchID = "batch_" + Guid.NewGuid(); MultipartContent content = new MultipartContent("mixed", batchID); // Create the request for each image send that the batch is going to contain. foreach (Product product in products) { //Create an image object. MultiProductImage image = new MultiProductImage { ObjectIDs = new List <string> { product.ObjectID }, //The type of display this image is meant for. //See Appendix A of the API reference for the full list. DisplayTypeName = "Chroma29", PageID = 1, //Send the image as a local override image LocationName = existingLocation1.Name, //Use a user supplied batchID //For more information on how to use this feature see section 5.1 of the System Management documentation. UserDefinedBatchID = batchID }; //Create a message to send an image. //Create the different parts of the multipart content MultipartContent imageContent = new MultipartContent("related") { new ObjectContent <MultiProductImage>(image, new JsonMediaTypeFormatter()) }; var imagePartContent = new ByteArrayContent(GetEmbeddedImageBytes("Chroma29_enquire_test_296x128.png")); imagePartContent.Headers.ContentType = new MediaTypeHeaderValue("image/png"); imageContent.Add(imagePartContent); HttpMessageContent sendImageContent = new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, $"{ApiServer}/API/api/objects/imagetomultipleobjects")); sendImageContent.HttpRequestMessage.Content = imageContent; content.Add(sendImageContent); } // Now send all the image assigns as a single batch request. await SendBatchAndCheckReturnResponsesSuccessAsync(content); }
private async Task SendImage(List <string> objectIDs, int page = 1, string locationName = null, string batchID = null) { //Create an image object. MultiProductImage image = new MultiProductImage { ObjectIDs = objectIDs, //The type of display this image is meant for. //See Appendix A of the API reference for the full list. DisplayTypeName = "Chroma29", PageID = page, }; //Send the image as a local override image if (locationName != null) { image.LocationName = locationName; } //Use a user supplied batchID //For more information on how to use this feature see section 5.1 of the System Management documentation. if (batchID != null) { image.UserDefinedBatchID = batchID; } //Add the image data to the request MultipartContent content = new MultipartContent("related"); content.Add(new ObjectContent <MultiProductImage>(image, new JsonMediaTypeFormatter())); var imagePartContent = new ByteArrayContent(GetEmbeddedImageBytes("Chroma29_enquire_test_296x128.png")); //The image type that is being supplied, either BMP, PBM or PNG. See API reference for details. imagePartContent.Headers.ContentType = new MediaTypeHeaderValue("image/png"); content.Add(imagePartContent); //Finally send the request and check the result var request = new HttpRequestMessage(HttpMethod.Post, "api/objects/imagetomultipleobjects") { Content = content }; var response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); }