Esempio n. 1
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>
        /// <returns></returns>
        public virtual async Task <PostDocumentsResponse <T> > PostDocumentsAsync <T>(
            string collectionName,
            IList <T> documents,
            PostDocumentsQuery query = null,
            ApiClientSerializationOptions serializationOptions = null)
        {
            string uriString = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);

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

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

                        return(DeserializeJsonFromStream <PostDocumentsResponse <T> >(stream));
                    }
                }
                throw await GetApiErrorException(response);
            }
        }
Esempio n. 2
0
        public async Task PostDocuments_ShouldSucceed()
        {
            var document1 = new { test = "value" };
            var document2 = new { test = "value" };
            PostDocumentsResponse <dynamic> response = await _docClient.PostDocumentsAsync("TestCollection", new dynamic[] { document1, document2 });

            Assert.Equal(2, response.Count);
            foreach (var innerResponse in response)
            {
                Assert.False(string.IsNullOrWhiteSpace(innerResponse._id));
                Assert.False(string.IsNullOrWhiteSpace(innerResponse._key));
                Assert.False(string.IsNullOrWhiteSpace(innerResponse._rev));
                Assert.Null(innerResponse.New);
                Assert.Null(innerResponse.Old);
            }
        }
Esempio n. 3
0
        public override PostDocumentsResponse <T> ReadJson(JsonReader reader, Type objectType, PostDocumentsResponse <T> existingValue,
                                                           bool hasExistingValue, JsonSerializer serializer)
        {
            List <PostDocumentResponse <T> > returnValues = new List <PostDocumentResponse <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 <PostDocumentResponse <T> >());
                    }
                    else
                    {
                        returnValues.Add(new PostDocumentResponse <T>(token.ToObject <ApiResponse>()));
                    }
                }
            }
            return(new PostDocumentsResponse <T>(returnValues));
        }
Esempio n. 4
0
 public override void WriteJson(JsonWriter writer, PostDocumentsResponse <T> value, JsonSerializer serializer)
 {
     throw new NotImplementedException();
 }