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('"'); })); }
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)); }
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)); }
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)); }
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(); } })); }
/// <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(); }); }
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)); }
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); } }
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); }
/// <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(); })); }
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)); }
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); })); }
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); } }
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)); }
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)); }
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); }
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); }
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); }); }
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); }
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(); } }); }
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); }
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); }
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('"'); }); }
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); }