Ejemplo n.º 1
0
        /// <summary>
        /// Delete multiple documents based on the passed document selectors.
        /// A document selector is either the document ID or the document Key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collectionName"></param>
        /// <param name="selectors"></param>
        /// <param name="query"></param>
        /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
        /// <returns></returns>
        public virtual async Task <DeleteDocumentsResponse <T> > DeleteDocumentsAsync <T>(
            string collectionName,
            IList <string> selectors,
            DeleteDocumentsQuery query       = null,
            DocumentHeaderProperties headers = null)
        {
            string uri = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);

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

            var content          = GetContent(selectors, new ApiClientSerializationOptions(false, false));
            var headerCollection = GetHeaderCollection(headers);

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

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

                throw await GetApiErrorException(response).ConfigureAwait(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Delete a document based on its document ID.
        /// </summary>
        /// <param name="documentId"></param>
        /// <param name="query"></param>
        /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
        /// <returns></returns>
        public virtual async Task <DeleteDocumentResponse <T> > DeleteDocumentAsync <T>(
            string documentId,
            DeleteDocumentQuery query        = null,
            DocumentHeaderProperties headers = null)
        {
            ValidateDocumentId(documentId);
            string uri = _docApiPath + "/" + documentId;

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

            var headerCollection = GetHeaderCollection(headers);

            using (var response = await _client.DeleteAsync(uri, headerCollection).ConfigureAwait(false))
            {
                if (response.IsSuccessStatusCode)
                {
                    var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    var responseModel = DeserializeJsonFromStream <DeleteDocumentResponse <T> >(stream);
                    return(responseModel);
                }

                throw await GetApiErrorException(response).ConfigureAwait(false);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Get an existing document.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="collectionName"></param>
 /// <param name="documentKey"></param>
 /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
 /// <returns></returns>
 public virtual async Task <T> GetDocumentAsync <T>(
     string collectionName, string documentKey, DocumentHeaderProperties headers = null)
 {
     return(await GetDocumentAsync <T>(
                $"{WebUtility.UrlEncode(collectionName)}/{WebUtility.UrlEncode(documentKey)}", headers)
            .ConfigureAwait(false));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Replaces the document with the provided document ID with the one in
        /// the body, provided there is such a document and no precondition is
        /// violated.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="documentId"></param>
        /// <param name="doc"></param>
        /// <param name="opts"></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 <PutDocumentResponse <T> > PutDocumentAsync <T>(
            string documentId,
            T doc,
            PutDocumentQuery opts = null,
            ApiClientSerializationOptions serializationOptions = null,
            DocumentHeaderProperties headers = null)
        {
            ValidateDocumentId(documentId);
            string uri = _docApiPath + "/" + documentId;

            if (opts != null)
            {
                uri += "?" + opts.ToQueryString();
            }

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

            using (var response = await _client.PutAsync(uri, content, headerCollection).ConfigureAwait(false))
            {
                if (response.IsSuccessStatusCode)
                {
                    var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

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

                throw await GetApiErrorException(response).ConfigureAwait(false);
            }
        }
Ejemplo n.º 5
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>
        /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
        /// <returns></returns>
        public virtual async Task <PostDocumentResponse <U> > PostDocumentAsync <T, U>(
            string collectionName,
            T document,
            PostDocumentsQuery query = null,
            ApiClientSerializationOptions serializationOptions = null,
            DocumentHeaderProperties headers = null)
        {
            string uriString = _docApiPath + "/" + WebUtility.UrlEncode(collectionName);

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

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

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

                    return(DeserializeJsonFromStream <PostDocumentResponse <U> >(stream));
                }

                throw await GetApiErrorException(response).ConfigureAwait(false);
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Delete a document based on its document ID.
 /// </summary>
 /// <remarks>
 /// This method overload is provided as a convenience when the client does not care about the type of <see cref="DeleteDocumentResponse{T}.Old"/>
 /// in the returned <see cref="DeleteDocumentResponse{T}"/>. Its value will be <c>null</c> when
 /// <see cref="DeleteDocumentQuery.ReturnOld"/> is either <c>false</c> or not set, so this overload is useful in the default case
 /// when deleting documents.
 /// </remarks>
 /// <param name="documentId"></param>
 /// <param name="query"></param>
 /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
 /// <returns></returns>
 public virtual async Task <DeleteDocumentResponse <object> > DeleteDocumentAsync(
     string documentId,
     DeleteDocumentQuery query        = null,
     DocumentHeaderProperties headers = null)
 {
     return(await DeleteDocumentAsync <object>(documentId, query, headers).ConfigureAwait(false));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Delete multiple documents based on the passed document selectors.
 /// A document selector is either the document ID or the document Key.
 /// </summary>
 /// <remarks>
 /// This method overload is provided as a convenience when the client does not care about the type of <see cref="DeleteDocumentResponse{T}.Old"/>
 /// in the returned <see cref="DeleteDocumentsResponse{T}"/>. These will be <c>null</c> when
 /// <see cref="DeleteDocumentsQuery.ReturnOld"/> is either <c>false</c> or not set, so this overload is useful in the default case
 /// when deleting documents.
 /// </remarks>
 /// <param name="collectionName"></param>
 /// <param name="selectors"></param>
 /// <param name="query"></param>
 /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
 /// <returns></returns>
 public virtual async Task <DeleteDocumentsResponse <object> > DeleteDocumentsAsync(
     string collectionName,
     IList <string> selectors,
     DeleteDocumentsQuery query       = null,
     DocumentHeaderProperties headers = null)
 {
     return(await DeleteDocumentsAsync <object>(collectionName, selectors, query, headers).ConfigureAwait(false));
 }
Ejemplo n.º 8
0
        public void GetEnumerator_ObjectOfClassExists_ReturnsEnumerator()
        {
            var documentHeaderProperties = new DocumentHeaderProperties();
            var actual = ((IEnumerable)documentHeaderProperties).GetEnumerator();

            Assert.That(actual, Is.Not.Null);
            Assert.That(actual.MoveNext(), Is.True);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Delete a document.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="collectionName"></param>
 /// <param name="documentKey"></param>
 /// <param name="query"></param>
 /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
 /// <returns></returns>
 public virtual async Task <DeleteDocumentResponse <T> > DeleteDocumentAsync <T>(
     string collectionName,
     string documentKey,
     DeleteDocumentQuery query        = null,
     DocumentHeaderProperties headers = null)
 {
     return(await DeleteDocumentAsync <T>(
                $"{WebUtility.UrlEncode(collectionName)}/{WebUtility.UrlEncode(documentKey)}",
                query, headers).ConfigureAwait(false));
 }
Ejemplo n.º 10
0
        public void GetProperFileNameCapitalization_DocumentHeaderNull_ThrowsArgumentNullException()
        {
            var documentHeaderProperties = new DocumentHeaderProperties();

            var property = documentHeaderProperties.ToArray()[1];

            Assert.That(property.Token, Is.EqualTo("%FileName%"));

            var documentHeaderStub = MockRepository.GenerateStub <IDocumentHeader>();

            Assert.That(() => property.CreateValue(documentHeaderStub), Throws.ArgumentNullException);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Partially updates the document identified by document-handle.
        /// The body of the request must contain a JSON document with the
        /// attributes to patch(the patch document). All attributes from the
        /// patch document will be added to the existing document if they do not
        /// yet exist, and overwritten in the existing document if they do exist
        /// there.
        /// PATCH/_api/document/{document-handle}
        /// </summary>
        /// <typeparam name="T">Type of the patch object used to partially update a document.</typeparam>
        /// <typeparam name="U">Type of the returned document, only applies when
        /// <see cref="PatchDocumentQuery.ReturnNew"/> or <see cref="PatchDocumentQuery.ReturnOld"/>
        /// are used.</typeparam>
        /// <param name="collectionName"></param>
        /// <param name="documentKey"></param>
        /// <param name="body"></param>
        /// <param name="query"></param>
        /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
        /// <returns></returns>
        public virtual async Task <PatchDocumentResponse <U> > PatchDocumentAsync <T, U>(
            string collectionName,
            string documentKey,
            T body,
            PatchDocumentQuery query         = null,
            DocumentHeaderProperties headers = null)
        {
            string documentHandle = WebUtility.UrlEncode(collectionName) +
                                    "/" + WebUtility.UrlEncode(documentKey);

            return(await PatchDocumentAsync <T, U>(documentHandle, body, query, headers : headers).ConfigureAwait(false));
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Replaces the document based on its Document ID with the one in
 /// the body, provided there is such a document and no precondition is
 /// violated.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="collectionName"></param>
 /// <param name="documentKey"></param>
 /// <param name="doc"></param>
 /// <param name="opts"></param>
 /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
 /// <returns></returns>
 public virtual Task <PutDocumentResponse <T> > PutDocumentAsync <T>(
     string collectionName,
     string documentKey,
     T doc,
     PutDocumentQuery opts            = null,
     DocumentHeaderProperties headers = null)
 {
     return(PutDocumentAsync <T>(
                $"{WebUtility.UrlEncode(collectionName)}/{WebUtility.UrlEncode(documentKey)}",
                doc,
                opts,
                headers: headers));
 }
Ejemplo n.º 13
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>
 /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
 /// <returns></returns>
 public virtual Task <PostDocumentResponse <T> > PostDocumentAsync <T>(
     string collectionName,
     T document,
     PostDocumentsQuery query = null,
     ApiClientSerializationOptions serializationOptions = null,
     DocumentHeaderProperties headers = null)
 {
     return(PostDocumentAsync <T, T>(
                collectionName,
                document,
                query,
                serializationOptions,
                headers));
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Get an existing document based on its Document ID.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="documentId"></param>
        /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
        /// <returns></returns>
        public virtual async Task <T> GetDocumentAsync <T>(string documentId, DocumentHeaderProperties headers = null)
        {
            ValidateDocumentId(documentId);
            var headerCollection = GetHeaderCollection(headers);
            var response         = await _client.GetAsync(_docApiPath + "/" + documentId, headerCollection).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                var document = DeserializeJsonFromStream <T>(stream);
                return(document);
            }

            throw await GetApiErrorException(response).ConfigureAwait(false);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Like GET, but only returns the header fields and not the body. You
        /// can use this call to get the current revision of a document or check if
        /// the document was deleted.
        /// HEAD/_api/document/{document-handle}
        /// </summary>
        /// <param name="documentId"></param>
        /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
        /// <exception cref="System.ArgumentException">Document ID is invalid.</exception>
        /// <remarks>
        /// 200: is returned if the document was found.
        /// 304: is returned if the “If-None-Match” header is given and the document has the same version.
        /// 400: is returned if the "TransactionId" header is given and the transactionId does not exist.
        /// 404: is returned if the document or collection was not found.
        /// 412: is returned if an “If-Match” header is given and the found document has a different version. The response will also contain the found document’s current revision in the Etag header.
        /// </remarks>
        /// <returns></returns>
        public virtual async Task <HeadDocumentResponse> HeadDocumentAsync(
            string documentId,
            DocumentHeaderProperties headers = null)
        {
            ValidateDocumentId(documentId);
            string uri = _docApiPath + "/" + documentId;
            WebHeaderCollection headerCollection = GetHeaderCollection(headers);

            using (var response = await _client.HeadAsync(uri, headerCollection))
            {
                return(new HeadDocumentResponse
                {
                    Code = (HttpStatusCode)response.StatusCode,
                    Etag = response.Headers.ETag
                });
            }
        }
Ejemplo n.º 16
0
        public void DocumentHeaderProperties_AdditionalProperties_AdditionalPropertiesAreAddedToCollection()
        {
            var additionalProperties = new List <AdditionalProperty>
            {
                new AdditionalProperty("%AdditionalProperty1%", "property 1"),
                new AdditionalProperty("%AdditionalProperty2%", "property 2")
            };

            var documentHeaderProperties = new DocumentHeaderProperties(additionalProperties);

            foreach (var additionalProperty in additionalProperties)
            {
                Assert.That(additionalProperty, Is.Not.Null);
            }

            Assert.That(documentHeaderProperties, Has.Exactly(1).Matches <DocumentHeaderProperty> (p => p.Token == "%AdditionalProperty1%"));
            Assert.That(documentHeaderProperties, Has.Exactly(1).Matches <DocumentHeaderProperty> (p => p.Token == "%AdditionalProperty2%"));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Get multiple documents.
        /// </summary>
        /// <typeparam name="T">The type of the documents
        /// deserialized from the response.</typeparam>
        /// <param name="collectionName">Collection name</param>
        /// <param name="selectors">Document keys to fetch documents for</param>
        /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
        /// <returns></returns>
        public virtual async Task <List <T> > GetDocumentsAsync <T>(
            string collectionName,
            IList <string> selectors,
            DocumentHeaderProperties headers = null)
        {
            string uri              = $"{_docApiPath}/{WebUtility.UrlEncode(collectionName)}?onlyget=true";
            var    content          = GetContent(selectors, new ApiClientSerializationOptions(false, true));
            var    headerCollection = GetHeaderCollection(headers);

            using (var response = await _client.PutAsync(uri, content, headerCollection).ConfigureAwait(false))
            {
                if (response.IsSuccessStatusCode)
                {
                    var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    var documents = DeserializeJsonFromStream <List <T> >(stream);
                    return(documents);
                }

                throw await GetApiErrorException(response).ConfigureAwait(false);
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Method to get the header collection.
        /// </summary>
        /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param>
        /// <returns><see cref="WebHeaderCollection"/> values.</returns>
        protected virtual WebHeaderCollection GetHeaderCollection(DocumentHeaderProperties headers)
        {
            var headerCollection = headers == null ? new WebHeaderCollection() : headers.ToWebHeaderCollection();

            return(headerCollection);
        }