Esempio n. 1
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('"');
                }));
        }
Esempio n. 2
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));
        }
Esempio n. 3
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));
        }
Esempio n. 4
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));
        }
Esempio n. 5
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);
                }));
        }
Esempio n. 6
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);
						});
		}
Esempio n. 7
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('"');
					});
		}
Esempio n. 8
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);
		}
Esempio n. 9
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);
		}
Esempio n. 10
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);
		}