Exemplo n.º 1
0
 public Task Create(bool throwIfExists = true)
 {
     using (SyncContext.SwitchToDefault())
         return(parent
                .RequestCouchDb(new HttpRequestMessage(HttpMethod.Put, uriConstructor.DatabaseUri))
                .ContinueWith(
                    rt => {
             var response = rt.Result;
             if (response.IsSuccessStatusCode)
             {
                 return;
             }
             var couchError = new CouchError(parent.Settings.Serializer, response);
             if (couchError.IsAlreadyDatabaseExists)
             {
                 if (throwIfExists)
                 {
                     throw new CouchCommunicationException("Database {0} already exists", uriConstructor.DatabaseName);
                 }
                 else
                 {
                     return;
                 }
             }
             couchError.ThrowCouchCommunicationException();
         }));
 }
Exemplo n.º 2
0
        public Task <string> RequestLastestDocumentRevision(string documentId)
        {
            if (string.IsNullOrEmpty(documentId))
            {
                throw new ArgumentNullException("documentId");
            }

            var documentUri = uriConstructor.GetFullDocumentUri(documentId);
            var request     = new HttpRequestMessage(HttpMethod.Head, documentUri);

            using (SyncContext.SwitchToDefault())
                return(parent.RequestCouchDb(request).ContinueWith(
                           rt => {
                    var response = rt.Result;
                    if (!response.IsSuccessStatusCode)
                    {
                        var couchApiError = new CouchError(parent.Settings.Serializer, response);
                        couchApiError.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
                        couchApiError.ThrowStaleStateExceptionIfNedded("update", documentId);
                        couchApiError.ThrowInvalidDocumentExceptionIfNedded(documentId);
                        if (response.StatusCode == HttpStatusCode.NotFound)
                        {
                            return null;
                        }
                        couchApiError.ThrowCouchCommunicationException();
                    }

                    var etag = response.Headers.ETag;
                    if (etag == null || etag.Tag == null)
                    {
                        throw new ParseException("Etag header expected but was not found.");
                    }
                    return etag.Tag.Trim('"');
                }));
        }
Exemplo n.º 3
0
 private static string ProccessResponse(string responseJsonString)
 {
     var error =
         new CouchError(new NewtonsoftSerializer(), new HttpResponseMessage(HttpStatusCode.InternalServerError) {
             Content = new JsonContent(responseJsonString)
         });
     return error.ToString();
 }
Exemplo n.º 4
0
        private Task <IDictionary <string, DocumentInfo> > HandleResponse(Task <HttpResponseMessage> requestTask)
        {
            var response = requestTask.Result;

            if (!response.IsSuccessStatusCode)
            {
                var error = new CouchError(serializer, response);
                error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
                error.ThrowCouchCommunicationException();
            }
            return(response.Content.ReadAsJsonArrayAsync().ContinueWith <IDictionary <string, DocumentInfo> >(ProcessResponseData));
        }
Exemplo n.º 5
0
        async Task <DocumentInfo> DeleteDocumentInternal(string documentId, string revision, HttpRequestMessage request)
        {
            var response = await parent.RequestCouchDb(request);

            if (!response.IsSuccessStatusCode)
            {
                var error = new CouchError(parent.Settings.Serializer, response);
                error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
                error.ThrowStaleStateExceptionIfNedded("delete", documentId, revision);
                error.ThrowCouchCommunicationException();
            }
            return(await ReadDocumentInfo(response));
        }
Exemplo n.º 6
0
        async Task <ILuceneQueryResult> QueryLuceneInternal(LuceneQuery query)
        {
            var response = await StartQuery(uriConstructor.GetQueryUri(query));

            if (!response.IsSuccessStatusCode)
            {
                var error = new CouchError(parent.Settings.Serializer, response);
                error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
                error.ThrowLuceneIndexNotFoundExceptionIfNedded(query);
                error.ThrowCouchCommunicationException();
            }
            using (var reader = await response.Content.ReadAsUtf8TextReaderAsync())
                return(LuceneQueryResultParser.Parse(reader, query));
        }
Exemplo n.º 7
0
 public Task Delete()
 {
     using (SyncContext.SwitchToDefault())
         return(parent
                .RequestCouchDb(new HttpRequestMessage(HttpMethod.Delete, uriConstructor.DatabaseUri))
                .ContinueWith(
                    rt => {
             var response = rt.Result;
             if (!response.IsSuccessStatusCode)
             {
                 var error = new CouchError(parent.Settings.Serializer, response);
                 error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor.DatabaseName);
                 error.ThrowCouchCommunicationException();
             }
         }));
 }
Exemplo n.º 8
0
		/// <summary>Updates database security descriptor.</summary>
		public Task UpdateSecurityDescriptor(DatabaseSecurityDescriptor securityDescriptor)
		{
			var serializer = parent.Settings.Serializer;
			var request = new HttpRequestMessage(HttpMethod.Put, uriConstructor.SecurityDescriptorUri) {
				Content = new JsonContent(serializer.ConvertToJson(securityDescriptor, throwOnError: true))
			};

			using (SyncContext.SwitchToDefault())
				return parent.RequestCouchDb(request).ContinueWith(
					rt => {
						var response = rt.Result;
						if (response.IsSuccessStatusCode) return;
						var couchError = new CouchError(serializer, response);
						couchError.ThrowDatabaseMissingExceptionIfNedded(uriConstructor.DatabaseName);
						couchError.ThrowCouchCommunicationException();
					});
		}
Exemplo n.º 9
0
		public Task Create(bool throwIfExists = true)
		{
			using (SyncContext.SwitchToDefault())
				return parent
					.RequestCouchDb(new HttpRequestMessage(HttpMethod.Put, uriConstructor.DatabaseUri))
					.ContinueWith(
						rt => {
							var response = rt.Result;
							if (response.IsSuccessStatusCode) return;
							var couchError = new CouchError(parent.Settings.Serializer, response);
							if (couchError.IsAlreadyDatabaseExists)
								if (throwIfExists)
									throw new CouchCommunicationException("Database {0} already exists", uriConstructor.DatabaseName);
								else
									return;
							couchError.ThrowCouchCommunicationException();
						});
		}
Exemplo n.º 10
0
        async Task <DatabaseInfo> RequestInfoInternal()
        {
            var response = await parent.RequestCouchDb(new HttpRequestMessage(HttpMethod.Get, uriConstructor.DatabaseUri));

            if (!response.IsSuccessStatusCode)
            {
                var error = new CouchError(parent.Settings.Serializer, response);
                if (!error.IsDatabaseMissing)
                {
                    error.ThrowCouchCommunicationException();
                }
                return(new DatabaseInfo(false, uriConstructor.DatabaseName));
            }

            var infoJson = await response.Content.ReadAsJsonObjectAsync();

            return(new DatabaseInfo(true, uriConstructor.DatabaseName, infoJson));
        }
Exemplo n.º 11
0
        async Task <DocumentInfo> DeleteAttachmentInternal(string attachmentId, string documentId, string documentRevision)
        {
            var attachmentUri  = uriConstructor.GetFullAttachmentUri(attachmentId, documentId, documentRevision);
            var requestMessage = new HttpRequestMessage(HttpMethod.Delete, attachmentUri);
            var response       = await parent.RequestCouchDb(requestMessage);

            if (!response.IsSuccessStatusCode)
            {
                var error = new CouchError(parent.Settings.Serializer, response);
                error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor.DatabaseName);
                error.ThrowAttachmentMissingException(attachmentId, documentId, documentRevision);
                error.ThrowDocumentNotFoundIfNedded(documentId, documentRevision);
                error.ThrowStaleStateExceptionIfNedded(
                    string.Format("deleting attachment ID '{0}'", attachmentId), documentId, documentRevision);
                error.ThrowCouchCommunicationException();
            }
            return(await ReadDocumentInfo(response));
        }
Exemplo n.º 12
0
        public static async Task <Document> Start(
            DbUriConstructor uriConstructor,
            DatabaseApi databaseApi,
            CouchApi couchApi,
            string documentId,
            string revision,
            AdditionalDocumentProperty additionalProperties)
        {
            var documentUri = uriConstructor.GetFullDocumentUri(documentId, revision, additionalProperties);
            var request     = new HttpRequestMessage(HttpMethod.Get, documentUri);

            var response = await couchApi.RequestCouchDb(request);

            if (!response.IsSuccessStatusCode)
            {
                var error = new CouchError(couchApi.Settings.Serializer, response);
                error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }
                error.ThrowCouchCommunicationException();
            }

            var content   = response.Content;
            var mediaType = content.Headers.ContentType != null? content.Headers.ContentType.MediaType: "<unknown>";

            switch (mediaType)
            {
            case MediaType.Json:
                return(ReadDocument(
                           databaseApi,
                           await content.ReadAsUtf8TextReaderAsync()
                           ));

            case MediaType.Multipart:
                return(await ReadMultipart(databaseApi, content));

            default:
                throw new CouchCommunicationException(
                          "Unexpected media type response recived requesting CouchDB document: {0}", mediaType);
            }
        }
Exemplo n.º 13
0
			private async Task<DocumentInfo> Save()
			{
				var request = new HttpRequestMessage(HttpMethod.Put, parent.uriConstructor.GetFullDocumentUri(document.Id)) {
					Content = new JsonContent(document.RawJsonObject)
				};

				var response = await parent.parent.RequestCouchDb(request);
				var documentId = document.Id;
				if (!response.IsSuccessStatusCode)
				{
					var error = new CouchError(parent.parent.Settings.Serializer, response);
					error.ThrowDatabaseMissingExceptionIfNedded(parent.uriConstructor);
					if (error.IsConflict)
						return conflictAction(error);
					error.ThrowInvalidDocumentExceptionIfNedded(documentId);
					error.ThrowCouchCommunicationException();
				}
				return await ReadDocumentInfo(response);
			}
Exemplo n.º 14
0
        private void CollectError(string documentId, string errorString)
        {
            var docIdToUpdateDescriptor = docIdToUpdateDescriptorMap[documentId];
            var operation = docIdToUpdateDescriptor.Operation.ToString().ToLower();

            var error = new CouchError(serializer, errorString);

            if (error.IsConflict)
            {
                exceptions.Add(error.CreateStaleStateException(operation, documentId, docIdToUpdateDescriptor.DocumentRevision));
            }
            else if (error.IsForbidden)
            {
                exceptions.Add(error.CreateInvalidDocumentException(documentId));
            }
            else
            {
                exceptions.Add(error.CreateCouchCommunicationException());
            }
        }
Exemplo n.º 15
0
        /// <summary>Updates database security descriptor.</summary>
        public Task UpdateSecurityDescriptor(DatabaseSecurityDescriptor securityDescriptor)
        {
            var serializer = parent.Settings.Serializer;
            var request    = new HttpRequestMessage(HttpMethod.Put, uriConstructor.SecurityDescriptorUri)
            {
                Content = new JsonContent(serializer.ConvertToJson(securityDescriptor, throwOnError: true))
            };

            using (SyncContext.SwitchToDefault())
                return(parent.RequestCouchDb(request).ContinueWith(
                           rt => {
                    var response = rt.Result;
                    if (response.IsSuccessStatusCode)
                    {
                        return;
                    }
                    var couchError = new CouchError(serializer, response);
                    couchError.ThrowDatabaseMissingExceptionIfNedded(uriConstructor.DatabaseName);
                    couchError.ThrowCouchCommunicationException();
                }));
        }
Exemplo n.º 16
0
        async Task <DocumentInfo> SaveAttachmentInternal(Attachment attachment, string documentId, string documentRevision)
        {
            var attachmentUri  = uriConstructor.GetFullAttachmentUri(attachment.Id, documentId, documentRevision);
            var requestMessage = new HttpRequestMessage(HttpMethod.Put, attachmentUri);
            HttpResponseMessage response;

            using (var requestContentStream = await attachment.OpenRead())
            {
                requestMessage.Content = new StreamContent(requestContentStream);
                requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(attachment.ContentType);
                response = await parent.RequestCouchDb(requestMessage);
            }
            if (!response.IsSuccessStatusCode)
            {
                var error = new CouchError(parent.Settings.Serializer, response);
                error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor.DatabaseName);
                error.ThrowStaleStateExceptionIfNedded(
                    string.Format("saving attachment ID '{0}'", attachment.Id), documentId, documentRevision);
                error.ThrowCouchCommunicationException();
            }
            return(await ReadDocumentInfo(response));
        }
Exemplo n.º 17
0
        public Task <Attachment> RequestAttachment(string attachmentId, string documentId, string documentRevision = null)
        {
            if (attachmentId.HasNoValue())
            {
                throw new ArgumentNullException("attachmentId");
            }
            if (documentId.HasNoValue())
            {
                throw new ArgumentNullException("documentId");
            }

            var attachmentUri  = uriConstructor.GetFullAttachmentUri(attachmentId, documentId, documentRevision);
            var requestMessage = new HttpRequestMessage(HttpMethod.Get, attachmentUri);

            requestMessage.Headers.Accept.Clear();

            using (SyncContext.SwitchToDefault())
                return(parent
                       .RequestCouchDb(requestMessage)
                       .ContinueWith <Attachment>(
                           rt => {
                    var response = rt.Result;
                    if (!response.IsSuccessStatusCode)
                    {
                        var error = new CouchError(parent.Settings.Serializer, response);
                        error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor.DatabaseName);
                        if (error.IsAttachmentMissingFromDocument)
                        {
                            return null;
                        }
                        error.ThrowDocumentNotFoundIfNedded(documentId, documentRevision);
                        error.ThrowStaleStateExceptionIfNedded(
                            string.Format("request attachment ID '{0}'", attachmentId), documentId, documentRevision);
                        error.ThrowCouchCommunicationException();
                    }
                    return new HttpResponseMessageAttachment(attachmentId, response);
                }));
        }
Exemplo n.º 18
0
		public static async Task<Document> Start(
			DbUriConstructor uriConstructor,
			DatabaseApi databaseApi,
			CouchApi couchApi,
			string documentId, 
			string revision, 
			AdditionalDocumentProperty additionalProperties)
		{
			var documentUri = uriConstructor.GetFullDocumentUri(documentId, revision, additionalProperties);
			var request = new HttpRequestMessage(HttpMethod.Get, documentUri);

			var response = await couchApi.RequestCouchDb(request);
			if (!response.IsSuccessStatusCode)
			{
				var error = new CouchError(couchApi.Settings.Serializer, response);
				error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
				if (response.StatusCode == HttpStatusCode.NotFound)
					return null;
				error.ThrowCouchCommunicationException();
			}

			var content = response.Content;
			var mediaType = content.Headers.ContentType != null? content.Headers.ContentType.MediaType: "<unknown>";
			switch (mediaType)
			{
				case MediaType.Json:
					return ReadDocument(
						databaseApi, 
						await content.ReadAsUtf8TextReaderAsync()
					);
				case MediaType.Multipart:
					return await ReadMultipart(databaseApi, content);
				default:
					throw new CouchCommunicationException(
						"Unexpected media type response recived requesting CouchDB document: {0}", mediaType);
			}
		}
Exemplo n.º 19
0
        async Task <DocumentInfo> CopyDocumentInternal(
            string originalDocumentId, string originalDocumentRevision, string targetDocumentId,
            string targetDocumentRevision)
        {
            var fullOriginalDocumentUri = uriConstructor.GetFullDocumentUri(
                originalDocumentId, originalDocumentRevision);
            var request = new HttpRequestMessage(CopyHttpMethod, fullOriginalDocumentUri);
            var targetDocumentUriString = uriConstructor.GetDocumentUriString(
                targetDocumentId, targetDocumentRevision);

            request.Headers.TryAddWithoutValidation("Destination", targetDocumentUriString);

            var response = await parent.RequestCouchDb(request);

            if (!response.IsSuccessStatusCode)
            {
                var couchApiError = new CouchError(parent.Settings.Serializer, response);
                couchApiError.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
                couchApiError.ThrowStaleStateExceptionForDocumentCopyIfNedded(
                    originalDocumentId, originalDocumentRevision, targetDocumentId, targetDocumentRevision);
                couchApiError.ThrowCouchCommunicationException();
            }
            return(await ReadDocumentInfo(response));
        }
Exemplo n.º 20
0
            private async Task <DocumentInfo> Save()
            {
                var request = new HttpRequestMessage(HttpMethod.Put, parent.uriConstructor.GetFullDocumentUri(document.Id))
                {
                    Content = new JsonContent(document.RawJsonObject)
                };

                var response = await parent.parent.RequestCouchDb(request);

                var documentId = document.Id;

                if (!response.IsSuccessStatusCode)
                {
                    var error = new CouchError(parent.parent.Settings.Serializer, response);
                    error.ThrowDatabaseMissingExceptionIfNedded(parent.uriConstructor);
                    if (error.IsConflict)
                    {
                        return(conflictAction(error));
                    }
                    error.ThrowInvalidDocumentExceptionIfNedded(documentId);
                    error.ThrowCouchCommunicationException();
                }
                return(await ReadDocumentInfo(response));
            }
Exemplo n.º 21
0
 private DocumentInfo ThrowConflict(CouchError error)
 {
     throw error.CreateStaleStateException("update", document.Id);
 }
Exemplo n.º 22
0
		public Task Delete()
		{
			using (SyncContext.SwitchToDefault())
				return parent
					.RequestCouchDb(new HttpRequestMessage(HttpMethod.Delete, uriConstructor.DatabaseUri))
					.ContinueWith(
						rt => {
							var response = rt.Result;
							if (!response.IsSuccessStatusCode)
							{
								var error = new CouchError(parent.Settings.Serializer, response);
								error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor.DatabaseName);
								error.ThrowCouchCommunicationException();
							}
						});
		}
Exemplo n.º 23
0
        private Task<IDictionary<string, DocumentInfo>> HandleResponse(Task<HttpResponseMessage> requestTask)
        {
            var response = requestTask.Result;

            if (!response.IsSuccessStatusCode)
            {
                var error = new CouchError(serializer, response);
                error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
                error.ThrowCouchCommunicationException();
            }
            return response.Content.ReadAsJsonArrayAsync().ContinueWith<IDictionary<string, DocumentInfo>>(ProcessResponseData);
        }
Exemplo n.º 24
0
 private static DocumentInfo ReturnDefault(CouchError error)
 {
     return(default(DocumentInfo));
 }
Exemplo n.º 25
0
			private DocumentInfo ThrowConflict(CouchError error)
			{
				throw error.CreateStaleStateException("update", document.Id);
			}
Exemplo n.º 26
0
        private void CollectError(string documentId, string errorString)
        {
            var docIdToUpdateDescriptor = docIdToUpdateDescriptorMap[documentId];
            var operation = docIdToUpdateDescriptor.Operation.ToString().ToLower();

            var error = new CouchError(serializer, errorString);

            if (error.IsConflict)
                exceptions.Add(error.CreateStaleStateException(operation, documentId, docIdToUpdateDescriptor.DocumentRevision));
            else if (error.IsForbidden)
                exceptions.Add(error.CreateInvalidDocumentException(documentId));
            else
                exceptions.Add(error.CreateCouchCommunicationException());
        }
Exemplo n.º 27
0
		public Task<Attachment> RequestAttachment(string attachmentId, string documentId, string documentRevision = null)
		{
			if (attachmentId.HasNoValue()) throw new ArgumentNullException("attachmentId");
			if (documentId.HasNoValue()) throw new ArgumentNullException("documentId");

			var attachmentUri = uriConstructor.GetFullAttachmentUri(attachmentId, documentId, documentRevision);
			var requestMessage = new HttpRequestMessage(HttpMethod.Get, attachmentUri);
			requestMessage.Headers.Accept.Clear();

			using (SyncContext.SwitchToDefault())
				return parent
					.RequestCouchDb(requestMessage)
					.ContinueWith<Attachment>(
						rt => {
							var response = rt.Result;
							if (!response.IsSuccessStatusCode)
							{
								var error = new CouchError(parent.Settings.Serializer, response);
								error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor.DatabaseName);
								if (error.IsAttachmentMissingFromDocument)
									return null;
								error.ThrowDocumentNotFoundIfNedded(documentId, documentRevision);
								error.ThrowStaleStateExceptionIfNedded(
									string.Format("request attachment ID '{0}'", attachmentId), documentId, documentRevision);
								error.ThrowCouchCommunicationException();
							}
							return new HttpResponseMessageAttachment(attachmentId, response);
						});
		}
Exemplo n.º 28
0
		async Task<DocumentInfo> DeleteAttachmentInternal(string attachmentId, string documentId, string documentRevision)
		{
			var attachmentUri = uriConstructor.GetFullAttachmentUri(attachmentId, documentId, documentRevision);
			var requestMessage = new HttpRequestMessage(HttpMethod.Delete, attachmentUri);
			var response = await parent.RequestCouchDb(requestMessage);
			if (!response.IsSuccessStatusCode)
			{
				var error = new CouchError(parent.Settings.Serializer, response);
				error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor.DatabaseName);
				error.ThrowAttachmentMissingException(attachmentId, documentId, documentRevision);
				error.ThrowDocumentNotFoundIfNedded(documentId, documentRevision);
				error.ThrowStaleStateExceptionIfNedded(
					string.Format("deleting attachment ID '{0}'", attachmentId), documentId, documentRevision);
				error.ThrowCouchCommunicationException();
			}
			return await ReadDocumentInfo(response);
		}
Exemplo n.º 29
0
		async Task<DocumentInfo> CopyDocumentInternal(
			string originalDocumentId, string originalDocumentRevision, string targetDocumentId,
			string targetDocumentRevision)
		{
			var fullOriginalDocumentUri = uriConstructor.GetFullDocumentUri(
				originalDocumentId, originalDocumentRevision);
			var request = new HttpRequestMessage(CopyHttpMethod, fullOriginalDocumentUri);
			var targetDocumentUriString = uriConstructor.GetDocumentUriString(
				targetDocumentId, targetDocumentRevision);
			request.Headers.TryAddWithoutValidation("Destination", targetDocumentUriString);

			var response = await parent.RequestCouchDb(request);

			if (!response.IsSuccessStatusCode)
			{
				var couchApiError = new CouchError(parent.Settings.Serializer, response);
				couchApiError.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
				couchApiError.ThrowStaleStateExceptionForDocumentCopyIfNedded(
					originalDocumentId, originalDocumentRevision, targetDocumentId, targetDocumentRevision);
				couchApiError.ThrowCouchCommunicationException();
			}
			return await ReadDocumentInfo(response);
		}
Exemplo n.º 30
0
		public Task<string> RequestLastestDocumentRevision(string documentId)
		{
			if (string.IsNullOrEmpty(documentId)) throw new ArgumentNullException("documentId");

			var documentUri = uriConstructor.GetFullDocumentUri(documentId);
			var request = new HttpRequestMessage(HttpMethod.Head, documentUri);

			using (SyncContext.SwitchToDefault())
				return parent.RequestCouchDb(request).ContinueWith(
					rt => {
						var response = rt.Result;
						if (!response.IsSuccessStatusCode)
						{
							var couchApiError = new CouchError(parent.Settings.Serializer, response);
							couchApiError.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
							couchApiError.ThrowStaleStateExceptionIfNedded("update", documentId);
							couchApiError.ThrowInvalidDocumentExceptionIfNedded(documentId);
							if (response.StatusCode == HttpStatusCode.NotFound)
								return null;
							couchApiError.ThrowCouchCommunicationException();
						}

						var etag = response.Headers.ETag;
						if (etag == null || etag.Tag == null)
							throw new ParseException("Etag header expected but was not found.");
						return etag.Tag.Trim('"');
					});
		}
Exemplo n.º 31
0
		async Task<ILuceneQueryResult> QueryLuceneInternal(LuceneQuery query)
		{
			var response = await StartQuery(uriConstructor.GetQueryUri(query));
			if (!response.IsSuccessStatusCode)
			{
				var error = new CouchError(parent.Settings.Serializer, response);
				error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
				error.ThrowLuceneIndexNotFoundExceptionIfNedded(query);
				error.ThrowCouchCommunicationException();
			}
			using (var reader = await response.Content.ReadAsUtf8TextReaderAsync())
				return LuceneQueryResultParser.Parse(reader, query);
		}
Exemplo n.º 32
0
		async Task<DatabaseInfo> RequestInfoInternal()
		{
			var response = await parent.RequestCouchDb(new HttpRequestMessage(HttpMethod.Get, uriConstructor.DatabaseUri));
			if (!response.IsSuccessStatusCode)
			{
				var error = new CouchError(parent.Settings.Serializer, response);
				if (!error.IsDatabaseMissing)
					error.ThrowCouchCommunicationException();
				return new DatabaseInfo(false, uriConstructor.DatabaseName);
			}

			var infoJson = await response.Content.ReadAsJsonObjectAsync();
			return new DatabaseInfo(true, uriConstructor.DatabaseName, infoJson);
		}
Exemplo n.º 33
0
		async Task<DocumentInfo> SaveAttachmentInternal(Attachment attachment, string documentId, string documentRevision)
		{
			var attachmentUri = uriConstructor.GetFullAttachmentUri(attachment.Id, documentId, documentRevision);
			var requestMessage = new HttpRequestMessage(HttpMethod.Put, attachmentUri);
			HttpResponseMessage response;
			using (var requestContentStream = await attachment.OpenRead())
			{
				requestMessage.Content = new StreamContent(requestContentStream);
				requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(attachment.ContentType);
				response = await parent.RequestCouchDb(requestMessage);
			}
			if (!response.IsSuccessStatusCode)
			{
				var error = new CouchError(parent.Settings.Serializer, response);
				error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor.DatabaseName);
				error.ThrowStaleStateExceptionIfNedded(
					string.Format("saving attachment ID '{0}'", attachment.Id), documentId, documentRevision);
				error.ThrowCouchCommunicationException();
			}
			return await ReadDocumentInfo(response);
		}
Exemplo n.º 34
0
		async Task<DocumentInfo> DeleteDocumentInternal(string documentId, string revision, HttpRequestMessage request)
		{
			var response = await parent.RequestCouchDb(request);
			if (!response.IsSuccessStatusCode)
			{
				var error = new CouchError(parent.Settings.Serializer, response);
				error.ThrowDatabaseMissingExceptionIfNedded(uriConstructor);
				error.ThrowStaleStateExceptionIfNedded("delete", documentId, revision);
				error.ThrowCouchCommunicationException();
			}
			return await ReadDocumentInfo(response);
		}
Exemplo n.º 35
0
			private static DocumentInfo ReturnDefault(CouchError error) { return default(DocumentInfo); }