Esempio n. 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);
            }
        }
        public override PatchDocumentsResponse <T> ReadJson(JsonReader reader, Type objectType, PatchDocumentsResponse <T> existingValue,
                                                            bool hasExistingValue, JsonSerializer serializer)
        {
            List <PatchDocumentResponse <T> > returnValues = new List <PatchDocumentResponse <T> >();

            if (reader.TokenType == JsonToken.StartArray)
            {
                JArray jArray = JArray.Load(reader);
                foreach (JToken token in jArray)
                {
                    if (token.Children <JProperty>().Any(x => x.Name == "_id"))
                    {
                        //JsonReader copyReaderForObject = token.CreateReaderWithSettings(reader);
                        //returnValues.Add(serializer.Deserialize<PostDocumentResponse<T>>(copyReaderForObject));
                        returnValues.Add(token.ToObject <PatchDocumentResponse <T> >());
                    }
                    else
                    {
                        returnValues.Add(new PatchDocumentResponse <T>(token.ToObject <ApiResponse>()));
                    }
                }
            }
            return(new PatchDocumentsResponse <T>(returnValues));
        }
 public override void WriteJson(JsonWriter writer, PatchDocumentsResponse <T> value, JsonSerializer serializer)
 {
     throw new NotImplementedException();
 }