This section focuses on how the Android client collects and serializes Listing data and then posts it to AuctionWorx Web API.
The first snippet (from the PostListing() method) starts by building the Uri (line 1) where AuctionWorx Web API handles the image upload. This address becomes the first parameter for the UploadImageBytes() method on line 5.
The goal is to upload the image, get an identifier for it, and then use the identifier to link the image with the rest of the listing details.
System.Uri uri = new System.Uri(siteurl + "/api/Media/"); Guid imageGuid; try { imageGuid = await UploadImageBytes(uri, username, password); } catch { NotifyUser("The picture has not been uploaded. " + "Please check the connectivity and the picture size/format"); return; }
The listing data is transferred as a UserInput object. The following code shows the Android (client) version of the UserInput class. As seen in lines 9 and 21, the Items collection (a Dictionary of name/value pairs) holds the core data.
using System.Collections.Generic; namespace EasyList { public class UserInput { public UserInput(string actingUserName, string fboUserName, string culture, string cultureUI) { this.Items = new Dictionary<string, string>(); this.ActingUserName = actingUserName; this.FBOUsername = fboUserName; this.CultureName = culture; this.CultureUIName = cultureUI; } public string ActingUserName { get; set; } public string CultureName { get; set; } public string CultureUIName { get; set; } public string FBOUsername { get; set; } public Dictionary<string, string> Items { get; set; } public string Raw { get; set; } } }
This following snippet from the PostListing() method shows a consumer of the preceding UserInput class. After line 1 instantiates the UserInput object (variable name input), the routine adds a series of name/value pairs representing the fields (built-in and custom) that match the AuctionWorx demo site. All values are passed as strings regardless of the target type.
Notice how lines 15 and 16 use the reference to the uploaded image. The GUID (in variable name imageGuid) forms part of the pair's name as well as its value.
Line 16 sets the order of images in the Listing's display. In this case, we only uploaded a single image so it becomes the zero-based first image ("0").
AuctionWorx Web API expects the UserInput object as a Json string. Therefore, line 17 passes the object (variable name input) to the static SerializeObject() method of the JsonConvert class.
The routine creates an instance of the HttpClient class (line 18) and an instance of the HttpContent class to contain the data payload.
After adding the Authentication header in lines 21 and 22, we're ready to post the data using the HttpClient's PostAsync() method on line 26. PostAsync() returns an HttpResponseMessage object.
The last line of this snippet extracts the Location object from the returned Headers collection.
Upon creating a listing, AuctionWorx Web API fills the Location response header with the Web API address for the listing. In the case of the demo site, the returned Location URI looks like https://www.rainworx.com/awedemo/api/listing/352369.
UserInput input = new UserInput("admin", username, "en-US", "en-US"); input.Items.Add("CategoryID", categoryId); input.Items.Add("RegionID", regionId); input.Items.Add("ListingType", listingType); input.Items.Add("Currency", "USD"); input.Items.Add("Title", listingTitle); input.Items.Add("Subtitle", listingSubtitle); input.Items.Add("Description", listingDescription); input.Items.Add("Price", listingPrice); input.Items.Add("ReservePrice", ""); input.Items.Add("FixedPrice", ""); input.Items.Add("Quantity", "1"); input.Items.Add("Duration", listingDuration); input.Items.Add("AutoRelist", "0"); input.Items.Add("media_guid_" + imageGuid.ToString(), imageGuid.ToString()); input.Items.Add("media_ordr_" + imageGuid.ToString(), "0"); string serialisedData = JsonConvert.SerializeObject(input); HttpClient client = new HttpClient(); HttpContent content = new StringContent (serialisedData, Encoding.UTF8, "application/json"); client.DefaultRequestHeaders.TryAddWithoutValidation ("Authorization", "RWX_BASIC " + username + ":" + password); HttpResponseMessage response = null; try { response = await client.PostAsync(siteurl + "/api/listing", content); } catch { NotifyUser("An error occurred during uploading. " + "Please check the connectivity or retry in few moments"); return; } System.Uri listinglocation = response.Headers.Location;
The next topic show how you can use the information in the returned Location Uri.
Copyright © 2002-2022. RainWorx Software. All rights reserved.