Пример #1
0
        /// <summary>
        /// Partially updates documents, the documents to update are specified
        /// by the _key attributes in the body objects. All attributes from the
        /// patch documents will be added to the existing documents if they do
        /// not yet exist, and overwritten in the existing documents if they do
        /// exist there.
        /// Setting an attribute value to null in the patch documents will cause a
        /// value of null to be saved for the attribute by default.
        /// If ignoreRevs is false and there is a _rev attribute in a
        /// document in the body and its value does not match the revision of
        /// the corresponding document in the database, the precondition is
        /// violated.
        /// PATCH /_api/document/{collection}
        /// </summary>
        /// <typeparam name="T">Type of the patch object used to partially update documents.</typeparam>
        /// <typeparam name="U">Type of the returned documents, only applies when
        /// <see cref="PatchDocumentsQuery.ReturnNew"/> or <see cref="PatchDocumentsQuery.ReturnOld"/>
        /// are used.</typeparam>
        /// <param name="collectionName"></param>
        /// <param name="patches"></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 <PatchDocumentsResponse <U> > PatchDocumentsAsync <T, U>(
            string collectionName,
            IList <T> patches,
            PatchDocumentsQuery query = null,
            ApiClientSerializationOptions serializationOptions = null)
        {
            string uri = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);

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

            using (var response = await _client.PatchAsync(uri, content))
            {
                if (response.IsSuccessStatusCode)
                {
                    if (query != null && query.Silent.HasValue && query.Silent.Value)
                    {
                        return(PatchDocumentsResponse <U> .Empty());
                    }
                    else
                    {
                        var stream = await response.Content.ReadAsStreamAsync();

                        return(DeserializeJsonFromStream <PatchDocumentsResponse <U> >(stream));
                    }
                }
                throw await GetApiErrorException(response);
            }
        }
        /// <summary>
        /// Partially updates documents, the documents to update are specified
        /// by the _key attributes in the body objects.The body of the
        /// request must contain a JSON array of document updates with the
        /// attributes to patch(the patch documents). All attributes from the
        /// patch documents will be added to the existing documents if they do
        /// not yet exist, and overwritten in the existing documents if they do
        /// exist there.
        /// Setting an attribute value to null in the patch documents will cause a
        /// value of null to be saved for the attribute by default.
        /// If ignoreRevs is false and there is a _rev attribute in a
        /// document in the body and its value does not match the revision of
        /// the corresponding document in the database, the precondition is
        /// violated.
        /// PATCH/_api/document/{collection}
        /// </summary>
        /// <typeparam name="T">Type of the patch object used to partially update documents.</typeparam>
        /// <typeparam name="U">Type of the returned documents, only applies when
        /// <see cref="PatchDocumentsQuery.ReturnNew"/> or <see cref="PatchDocumentsQuery.ReturnOld"/>
        /// are used.</typeparam>
        /// <param name="collectionName"></param>
        /// <param name="patches"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task <IList <PatchDocumentsResponse <U> > > PatchDocumentsAsync <T, U>(
            string collectionName,
            IList <T> patches,
            PatchDocumentsQuery query = null)
        {
            string uri = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);

            if (query != null)
            {
                uri += "?" + query.ToQueryString();
            }
            var content = GetContent(patches, false, false);

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

                    return(DeserializeJsonFromStream <IList <PatchDocumentsResponse <U> > >(stream));
                }
                throw await GetApiErrorException(response);
            }
        }