Ejemplo n.º 1
0
 public Document RequestDocument(
     string documentId,
     string revision = null,
     AdditionalDocumentProperty additionalProperties = default(AdditionalDocumentProperty))
 {
     return(databaseApi.RequestDocument(documentId, revision, additionalProperties).WaitForResult());
 }
Ejemplo n.º 2
0
        public Uri GetFullDocumentUri(
            string docId,
            string revision = null,
            AdditionalDocumentProperty additionalProperties = default(AdditionalDocumentProperty))
        {
            var documentUriString = GetDocumentUriString(docId, revision, additionalProperties);

            return(new Uri(databaseUriWithSlash, documentUriString).LeaveDotsAndSlashesEscaped());
        }
Ejemplo n.º 3
0
 public Task <Document> RequestDocument(
     string documentId, string revision, AdditionalDocumentProperty additionalProperties = default(AdditionalDocumentProperty))
 {
     if (string.IsNullOrEmpty(documentId))
     {
         throw new ArgumentNullException("documentId");
     }
     using (SyncContext.SwitchToDefault())
         return(DocumentRequestTask.Start(uriConstructor, this, parent, documentId, revision, additionalProperties));
 }
Ejemplo n.º 4
0
        public string GetDocumentUriString(
            string documentId,
            string revision,
            AdditionalDocumentProperty additionalProperties = default(AdditionalDocumentProperty))
        {
            var uriStringBuilder = new StringBuilder();
            AppendDocId(uriStringBuilder, documentId);

            var delimiter = '?';
            foreach (var documentQueryParam in GetDocumentQueryParams(revision, additionalProperties))
            {
                uriStringBuilder.Append(delimiter);
                if(delimiter == '?')
                    delimiter = '&';
                uriStringBuilder.Append(documentQueryParam.Key).Append('=').Append(documentQueryParam.Value);
            }

            return uriStringBuilder.ToString();
        }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
0
        private static IEnumerable <KeyValuePair <string, string> > GetDocumentQueryParams(
            string revision, AdditionalDocumentProperty additionalProperties)
        {
            if (!string.IsNullOrEmpty(revision))
            {
                yield return(new KeyValuePair <string, string>("rev", revision));
            }

            if (additionalProperties == default(AdditionalDocumentProperty))
            {
                yield break;
            }

            if ((additionalProperties & AdditionalDocumentProperty.Attachments) == AdditionalDocumentProperty.Attachments)
            {
                yield return(new KeyValuePair <string, string>("attachments", "true"));
            }

            if ((additionalProperties & AdditionalDocumentProperty.Conflicts) == AdditionalDocumentProperty.Conflicts)
            {
                yield return(new KeyValuePair <string, string>("conflicts", "true"));
            }

            if ((additionalProperties & AdditionalDocumentProperty.DeletedConflicts) == AdditionalDocumentProperty.DeletedConflicts)
            {
                yield return(new KeyValuePair <string, string>("deleted_conflicts", "true"));
            }

            if ((additionalProperties & AdditionalDocumentProperty.RevisionHistory) == AdditionalDocumentProperty.RevisionHistory)
            {
                yield return(new KeyValuePair <string, string>("revs", "true"));
            }

            if ((additionalProperties & AdditionalDocumentProperty.RevisionInfo) == AdditionalDocumentProperty.RevisionInfo)
            {
                yield return(new KeyValuePair <string, string>("revs_info", "true"));
            }
        }
Ejemplo n.º 7
0
        public string GetDocumentUriString(
            string documentId,
            string revision,
            AdditionalDocumentProperty additionalProperties = default(AdditionalDocumentProperty))
        {
            var uriStringBuilder = new StringBuilder();

            AppendDocId(uriStringBuilder, documentId);

            var delimiter = '?';

            foreach (var documentQueryParam in GetDocumentQueryParams(revision, additionalProperties))
            {
                uriStringBuilder.Append(delimiter);
                if (delimiter == '?')
                {
                    delimiter = '&';
                }
                uriStringBuilder.Append(documentQueryParam.Key).Append('=').Append(documentQueryParam.Value);
            }

            return(uriStringBuilder.ToString());
        }
Ejemplo n.º 8
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);
			}
		}
 public Task<Document> RequestDocument(string documentId, string revision = null, AdditionalDocumentProperty additionalProperties = (AdditionalDocumentProperty) 0)
 {
     return Task.Factory.StartNew(() =>
     {
         Thread.Sleep(50); //preventing returning already done task
         return (Document) null;
     });
 }
Ejemplo n.º 10
0
 public Task <Document> RequestDocument(string documentId, string revision = null, AdditionalDocumentProperty additionalProperties = (AdditionalDocumentProperty)0)
 {
     return(Task.Factory.StartNew(() =>
     {
         Thread.Sleep(50);                     //preventing returning already done task
         return (Document)null;
     }));
 }
Ejemplo n.º 11
0
        private static IEnumerable<KeyValuePair<string, string>> GetDocumentQueryParams(
            string revision, AdditionalDocumentProperty additionalProperties)
        {
            if (!string.IsNullOrEmpty(revision))
                yield return new KeyValuePair<string, string>("rev", revision);

            if(additionalProperties == default(AdditionalDocumentProperty))
                yield break;

            if((additionalProperties & AdditionalDocumentProperty.Attachments) == AdditionalDocumentProperty.Attachments)
                yield return new KeyValuePair<string, string>("attachments", "true");

            if ((additionalProperties & AdditionalDocumentProperty.Conflicts) == AdditionalDocumentProperty.Conflicts)
                yield return new KeyValuePair<string, string>("conflicts", "true");

            if ((additionalProperties & AdditionalDocumentProperty.DeletedConflicts) == AdditionalDocumentProperty.DeletedConflicts)
                yield return new KeyValuePair<string, string>("deleted_conflicts", "true");

            if ((additionalProperties & AdditionalDocumentProperty.RevisionHistory) == AdditionalDocumentProperty.RevisionHistory)
                yield return new KeyValuePair<string, string>("revs", "true");

            if ((additionalProperties & AdditionalDocumentProperty.RevisionInfo) == AdditionalDocumentProperty.RevisionInfo)
                yield return new KeyValuePair<string, string>("revs_info", "true");
        }
Ejemplo n.º 12
0
 public Uri GetFullDocumentUri(
     string docId,
     string revision = null,
     AdditionalDocumentProperty additionalProperties = default(AdditionalDocumentProperty))
 {
     var documentUriString = GetDocumentUriString(docId, revision, additionalProperties);
     return new Uri(databaseUriWithSlash, documentUriString).LeaveDotsAndSlashesEscaped();
 }
Ejemplo n.º 13
0
		public Task<Document> RequestDocument(
			string documentId, string revision, AdditionalDocumentProperty additionalProperties = default(AdditionalDocumentProperty))
		{
			if (string.IsNullOrEmpty(documentId)) throw new ArgumentNullException("documentId");
			using (SyncContext.SwitchToDefault())
				return DocumentRequestTask.Start(uriConstructor, this, parent, documentId, revision, additionalProperties);
		}
Ejemplo n.º 14
0
 public Document RequestDocument(
     string documentId,
     string revision = null,
     AdditionalDocumentProperty additionalProperties = default(AdditionalDocumentProperty))
 {
     return databaseApi.RequestDocument(documentId, revision, additionalProperties).WaitForResult();
 }