Пример #1
0
        /// <summary>
        /// Post a single document with the possibility to specify a different type
        /// for the new document object returned in the response.
        /// </summary>
        /// <typeparam name="T">The type of the post object used to record a new document.</typeparam>
        /// <typeparam name="U">Type of the returned document, only applies when
        /// <see cref="PostDocumentsQuery.ReturnNew"/> or <see cref="PostDocumentsQuery.ReturnOld"/>
        /// are used.</typeparam>
        /// <param name="collectionName"></param>
        /// <param name="document"></param>
        /// <param name="query"></param>
        /// <param name="serializationOptions">The serialization options. When the value is null the
        /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
        /// <returns></returns>
        public virtual async Task <PostDocumentResponse <U> > PostDocumentAsync <T, U>(
            string collectionName,
            T document,
            PostDocumentsQuery query = null,
            ApiClientSerializationOptions serializationOptions = null)
        {
            string uriString = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);

            if (query != null)
            {
                uriString += "?" + query.ToQueryString();
            }
            var content = GetContent(document, serializationOptions);

            using (var response = await _client.PostAsync(uriString, content))
            {
                if (response.IsSuccessStatusCode)
                {
                    var stream = await response.Content.ReadAsStreamAsync();

                    return(DeserializeJsonFromStream <PostDocumentResponse <U> >(stream));
                }
                throw await GetApiErrorException(response);
            }
        }
Пример #2
0
 /// <summary>
 /// Post a single document.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="collectionName"></param>
 /// <param name="document"></param>
 /// <param name="query"></param>
 /// <param name="serializationOptions">The serialization options. When the value is null the
 /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
 /// <returns></returns>
 public virtual Task <PostDocumentResponse <T> > PostDocumentAsync <T>(
     string collectionName,
     T document,
     PostDocumentsQuery query = null,
     ApiClientSerializationOptions serializationOptions = null)
 {
     return(PostDocumentAsync <T, T>(
                collectionName,
                document,
                query,
                serializationOptions));
 }
Пример #3
0
        /// <summary>
        /// Post multiple documents in a single request.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collectionName"></param>
        /// <param name="documents"></param>
        /// <param name="query"></param>
        /// <param name="serializationOptions">The serialization options. When the value is null the
        /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
        /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
        /// <returns></returns>
        public virtual async Task <PostDocumentsResponse <T> > PostDocumentsAsync <T>(
            string collectionName,
            IList <T> documents,
            PostDocumentsQuery query = null,
            ApiClientSerializationOptions serializationOptions = null,
            DocumentHeaderProperties headers = null)
        {
            string uriString = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);

            if (query != null)
            {
                uriString += "?" + query.ToQueryString();
            }

            var content          = GetContent(documents, serializationOptions);
            var headerCollection = GetHeaderCollection(headers);

            using (var response = await _client.PostAsync(uriString, content, headerCollection).ConfigureAwait(false))
            {
                if (response.IsSuccessStatusCode)
                {
                    if (query != null && query.Silent.HasValue && query.Silent.Value)
                    {
                        return(PostDocumentsResponse <T> .Empty());
                    }
                    else
                    {
                        var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                        return(DeserializeJsonFromStream <PostDocumentsResponse <T> >(stream));
                    }
                }

                throw await GetApiErrorException(response).ConfigureAwait(false);
            }
        }
Пример #4
0
        /// <summary>
        /// Post multiple documents in a single request.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collectionName"></param>
        /// <param name="documents"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <PostDocumentsResponse <T> > PostDocumentsAsync <T>(string collectionName, IList <T> documents, PostDocumentsQuery query = null)
        {
            string uriString = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);

            if (query != null)
            {
                uriString += "?" + query.ToQueryString();
            }
            StringContent content = GetStringContent(documents, false, false);

            using (var response = await _client.PostAsync(uriString, content))
            {
                if (response.IsSuccessStatusCode)
                {
                    var stream = await response.Content.ReadAsStreamAsync();

                    return(DeserializeJsonFromStream <PostDocumentsResponse <T> >(stream));
                }
                throw await GetApiErrorException(response);
            }
        }