Пример #1
0
        /// <summary>
        /// Creates a new <see cref="Attachment"/>
        /// </summary>
        /// <param name="attachment">The <see cref="Attachment"/> data</param>
        /// <param name="content">The content to be uploaded to IS24</param>
        /// <param name="fileName">The filename of the content transfered</param>
        /// <param name="mimeType">the mime-type of the file transfered</param>
        /// <returns>The updated <see cref="Attachment"/> data. It now contains the ScoutId if uploaded successfully</returns>
        public async Task<Attachment> CreateAsync(Attachment attachment, Stream content, string fileName, string mimeType)
        {
            var request = Connection.CreateRequest("realestate/{id}/attachment", Method.POST);
            request.AddParameter("id", RealEstate.Id, ParameterType.UrlSegment);

            byte[] binaryContent = null;
            using (var ms = new MemoryStream())
            {
                await content.CopyToAsync(ms);
                binaryContent = ms.ToArray();
            }

            request.AddFile("attachment", binaryContent, fileName, mimeType);
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Attachment), "http://rest.immobilienscout24.de/schema/common/1.0");

            var sw = new Utf8StringWriter();
            serializer.Serialize(sw, attachment);
            var metaData = Encoding.UTF8.GetBytes(sw.ToString());
            request.AddFile("metadata", metaData, "body.xml", "application/xml");

            var resp = await ExecuteAsync<Messages>(Connection, request);
            var id = resp.ExtractCreatedResourceId();

            if (!id.HasValue)
            {
                throw new IS24Exception(string.Format("Error creating attachment {0}: {1}", fileName, resp.ToMessage())) { Messages = resp };
            }

            attachment.Id = id.Value;

            return attachment;
        }