Ejemplo n.º 1
0
        public async Task PostDocumentChunk(Lpp.Dns.DTO.DataMartClient.Criteria.DocumentMetadata doc, byte[] data)
        {
            string identifier;

            _log.Debug(ExecutingMessage("PostResponseDocumentChunk?documentID=" + doc.ID.ToString("D"), out identifier));
            using (var content = new MultipartFormDataContent())
            {
                var docContent = JsonConvert.SerializeObject(doc, HttpClientHelpers.SerializerSettings());
                var sContent   = new StringContent(docContent, Encoding.UTF8, "application/json");

                content.Add(sContent, "metadata");

                var docByteContent = new ByteArrayContent(data);
                docByteContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");

                content.Add(docByteContent, "files", doc.Name);

                var result = await this._Client.PostAsync(this._Host + Path + "/PostDocumentChunk", content);

                if (!result.IsSuccessStatusCode)
                {
                    var errorMsg = await result.GetMessage();

                    _log.Error(CompletionMessage(identifier, "PostResponseDocumentChunk?documentID=" + doc.ID.ToString("D"), false) + ".\r\n" + errorMsg);

                    throw new Exception("An error occured during upload of document data: " + errorMsg);
                }

                _log.Debug(CompletionMessage(identifier, "PostResponseDocumentChunk?documentID=" + doc.ID.ToString("D"), true));
            }
        }
Ejemplo n.º 2
0
        public async Task <HttpResponseMessage> Put <T>(string path, T content)
        {
            var URL           = this._Host + path;
            var contentString = JsonConvert.SerializeObject(content, HttpClientHelpers.SerializerSettings());
            var sContent      = new StringContent(contentString, Encoding.UTF8, "application/json");
            var response      = await _Client.PutAsync(URL, sContent);

            return(response);
        }
Ejemplo n.º 3
0
        public async Task <BaseResponse <R> > Put <T, R>(string path, T content)
        {
            var URL           = this._Host + path;
            var contentString = JsonConvert.SerializeObject(content, HttpClientHelpers.SerializerSettings());
            var sContent      = new StringContent(contentString, Encoding.UTF8, "application/json");
            var response      = await _Client.PutAsync(URL, sContent);

            var result = await response.Content.ReadAsStringAsync();

            return(DeserializeResponse <BaseResponse <R> >(result));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns the string message from an HttpResponseMessage
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        public static async Task <string> GetMessage(this HttpResponseMessage response)
        {
            var result = await response.Content.ReadAsStringAsync();

            try
            {
                //Try and convert the string to a Base REsponse so we can get the errors
                var obj = JsonConvert.DeserializeObject <BaseResponse <string> >(result, HttpClientHelpers.SerializerSettings());

                return(string.Join("\r\n", obj.errors.Select(e => e.Description).ToArray()));
            }
            catch
            {
                return(result);
            }
        }
Ejemplo n.º 5
0
        private T DeserializeResponse <T>(string response)
        {
            var item = JsonConvert.DeserializeObject <T>(response, HttpClientHelpers.SerializerSettings());

            return(item);
        }