public async Task <AttachmentResult> UploadAttachment(int applicationId, AttachmentResource attachment) { var requestUri = $"application/{applicationId}/attachment"; var response = await GetStatusAndContent(CreateRequestMessage(HttpMethod.Put, requestUri, retailerApiToken, attachment)); int attachmentId = -1; if (response.StatusCode == HttpStatusCode.Created) { JObject attachmentResultObj = JObject.Parse(response.Content); attachmentId = int.Parse(attachmentResultObj.GetValue("attachmentId").ToString()); } return(new AttachmentResult { StatusCode = response.StatusCode, AttachmentId = attachmentId }); }
/// <summary> /// Creates a new <see cref="RealEstateItem"/> instance /// </summary> /// <param name="realEstate"><see cref="RealEstate"/> data item</param> /// <param name="connection">The <see cref="IIS24Connection"/> used for querying the API</param> public RealEstateItem(RealEstate realEstate, IIS24Connection connection) { RealEstate = realEstate; Attachments = new AttachmentResource(realEstate, connection); }
/// <summary> /// Creates a http request that will upload a file binary to an existing MessageResource draft. The payload /// also includes information about the attachment or metadata /// </summary> /// <param name="httpClient"></param> /// <param name="attachmentsUri"></param> /// <param name="fileName"></param> /// <param name="attachment"></param> /// <returns></returns> public static AttachmentResource UploadAttachment(HttpClient httpClient, Uri attachmentsUri, string fileName, AttachmentResource attachment) { (" √ creating attachment metadata for file » " + attachment.NativeFileName).ToConsole(); (" + confidentiality » " + attachment.AttachmentMeta.Confidentiality).ToConsole(); (" + documentType » " + attachment.AttachmentMeta.DocumentType).ToConsole(); (" + documentDate » " + attachment.AttachmentMeta.DocumentDate).ToConsole(); (" + documentDescription » " + attachment.AttachmentMeta.DocumentDescription).ToConsole(); string serializeObject = Serialize <AttachmentMetaResource>(attachment.AttachmentMeta, httpClient.DefaultMediaType()); (" √ serializing request object to " + httpClient.DefaultMediaType()).ToConsole(); // This API requires a POST of both text based and binary data using MultipartContent // https://msdn.microsoft.com/System.Net.Http.MultipartContent var multipartContent = new MultipartFormDataContent(); // Using the StringContent (https://msdn.microsoft.com/System.Net.Http.StringContent) class to encode // and setup the required mime type for this endpoint var contentString = new StringContent(serializeObject, Encoding.UTF8, httpClient.DefaultMediaType().Description()); string.Format(" √ creating request content (string) object using as {0}", httpClient.DefaultMediaType().Description()).ToConsole(); // Using the ByteArrayContent (https://msdn.microsoft.com/System.Net.Http.ByteArrayContent) class to encode // and setup the required array buffer ByteArrayContent byteArrayContent = new ByteArrayContent(attachment.NativeFileBytes, 0, attachment.NativeFileBytes.Length); // Add the two content httpcontent based instances to the collection to be sent up to the API multipartContent.Add(contentString); multipartContent.Add(byteArrayContent, fileName, fileName); // Make a POST request to the draft id endpoint. It will return a draft id response as a bare string. // (example is also showing some simple timing diagnostics) var stopwatch = new Stopwatch(); stopwatch.Start(); HttpResponseMessage result = httpClient.PostAsync(attachmentsUri, multipartContent).Result; result.CheckStatus(); string responseJson = result.Content.ReadAsStringAsync().Result; WriteTimingOutput("making attachment upload request against", attachmentsUri, stopwatch.ElapsedMilliseconds); var attachmentResource = Deserialize <AttachmentResource>(responseJson, httpClient.DefaultMediaType()); (" √ sent " + attachment.NativeFileBytes.Length + " bytes To API").ToConsole(); return(attachmentResource); }