Exemple #1
0
        /// <summary>
        /// Update a document with a new revision on the server.
        /// The revision must have the id and revId of the document being updated.
        ///
        /// </summary>
        /// <param name="revision">Revision to update.</param>
        public async Task <DocumentRevision> UpdateAsync(DocumentRevision revision)
        {
            Debug.WriteLine("==== enter Database::update(Object)");
            if (revision == null)
            {
                throw new ArgumentNullException("revision");
            }

            if (string.IsNullOrEmpty(revision.revId))
            {
                throw new ArgumentException("The document revision must contain a revId to perform an update");
            }


            if (string.IsNullOrEmpty(revision.docId))
            {
                throw new ArgumentException("The document revision parameter docId must not be null or empty ");
            }

            var uri = new Uri(dbNameUrlEncoded + "/" + Uri.EscapeDataString(revision.docId), UriKind.Relative);

            Debug.WriteLine("relative URI is: " + uri.ToString());

            if (revision.body == null)
            {
                var empty = new Dictionary <String, Object>();
                revision.body = empty;
            }

            var payload = Database.SerializeObject(revision);

            Debug.WriteLine("Update document payload is: " + Database.SerializeObject(payload));
            // Send the HTTP request
            var response = await client.httpHelper.PutAsync(uri, null, payload).ConfigureAwait(continueOnCapturedContext: false);

            // Check the HTTP status code
            int httpStatus = (int)response.StatusCode;

            if (httpStatus < 200 || httpStatus > 300)
            {
                var errorMessage = String.Format("Failed to update document.\nHTTP_Status: {0}\nErrorMessage: {1}",
                                                 httpStatus, response.ReasonPhrase);
                Debug.WriteLine(errorMessage);
                throw new DataException(DataException.Database_CreateDocumentRevisionFailure, errorMessage);
            }

            // Read in the response JSON into a Dictionary
            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false);

            var document = Database.DeserializeObject <DocumentRevision>(content);

            document.body = revision.body; //response doesn't contain document content
            return(document);
        }
Exemple #2
0
        /// <summary>
        /// Create the specified document in the database.
        /// </summary>
        /// <param name="revision">Revision to create.</param>
        public async Task <DocumentRevision> CreateAsync(DocumentRevision revision)
        {
            Debug.WriteLine("==== enter Database::Create(Object)");
            if (revision == null)
            {
                throw new ArgumentNullException("revision");
            }

            string encodedDocId = null;

            if (!string.IsNullOrEmpty(revision.docId))
            {
                encodedDocId = Uri.EscapeDataString(revision.docId);
            }
            var uri = new Uri(dbNameUrlEncoded + "/" + encodedDocId, UriKind.Relative);

            Debug.WriteLine("reltive URL is: " + uri.ToString());

            var payload = Database.SerializeObject(revision);

            Debug.WriteLine("Create document payload is: " + Database.SerializeObject(payload));

            // Send the HTTP request
            HttpResponseMessage response;

            if (!string.IsNullOrEmpty(revision.docId))
            {
                response = await client.httpHelper.PutAsync(uri, null, payload).ConfigureAwait(continueOnCapturedContext: false);
            }
            else
            {
                response = await client.httpHelper.PostAsync(uri, null, payload).ConfigureAwait(continueOnCapturedContext: false);
            }

            // Check the HTTP status code
            var httpStatus = (int)response.StatusCode;

            if (httpStatus < 200 || httpStatus > 300)
            {
                var errorMessage = String.Format("Failed to create a new document.\nHTTP_Status: {0}\nErrorMessage: {1}",
                                                 httpStatus, response.ReasonPhrase);
                Debug.WriteLine(errorMessage);
                throw new DataException(DataException.Database_CreateDocumentRevisionFailure, errorMessage);
            }

            // Read in the response JSON into a Dictionary
            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false);

            var document = Database.DeserializeObject <DocumentRevision>(content);

            document.body = revision.body; //response doesn't contain document content
            return(document);
        }
Exemple #3
0
        /// <summary>
        /// Delete the specified Document Revision.
        /// </summary>
        /// <param name="revision">Document revision to delete.</param>
        public async Task <String> DeleteAsync(DocumentRevision revision)
        {
            Debug.WriteLine("==== enter Database::delete(Object)");

            try
            {
                if (revision == null)
                {
                    throw new ArgumentNullException("revision");
                }

                var requestUrl = new Uri(
                    dbNameUrlEncoded + "/"
                    + Uri.EscapeDataString(revision.docId) + "?rev=" + Uri.EscapeDataString(revision.revId),
                    UriKind.Relative);
                // Send the HTTP request
                var response = await client.httpHelper.DeleteAsync(requestUrl, null).ConfigureAwait(continueOnCapturedContext: false);

                // Read in the response JSON into a Dictionary
                var content = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false);

                Dictionary <string, object> responseJSON = Database.DeserializeObject <Dictionary <string, object> >(content);

                Debug.WriteLine("Cloudant delete document response.  Relative URI: " + requestUrl.ToString() + ". " +
                                "Document: " + revision.docId + "\nResponse content:" + content);

                // Check the HTTP status code
                int httpStatus = (int)response.StatusCode;
                if (httpStatus < 200 || httpStatus >= 300)
                {
                    string errorMessage = String.Format("Failed to delete document revision.\nRequest URL: {0}\nHTTP_Status: {1}\nJSONBody: {2}",
                                                        requestUrl.ToString(), httpStatus, content);
                    Debug.WriteLine(errorMessage);
                    throw new DataException(DataException.Database_DeleteDocumentRevisionFailure, errorMessage);
                }

                Object responseRevisionId;
                responseJSON.TryGetValue(DOC_RESPONSE_REV, out responseRevisionId);
                if (responseRevisionId == null || responseRevisionId.Equals(""))
                {
                    string errorMessage = "\ndocument delete JSON Response didn't contain a revisionId.";
                    Debug.WriteLine(errorMessage);
                    throw new DataException(DataException.Database_DeleteDocumentRevisionFailure, errorMessage);
                }
                return((string)responseRevisionId);
            }
            catch (Exception e)
            {
                throw new DataException(DataException.Database_DeleteDocumentRevisionFailure, e.Message, e);
            }
        }
Exemple #4
0
        public override object ReadJson(JsonReader reader,
                                        Type objectType,
                                        object existingValue,
                                        JsonSerializer serializer)
        {
            var jsonObject = JObject.Load(reader);
            var document   = new DocumentRevision();

            document.body = new Dictionary <string, object>();

            foreach (var kv in jsonObject)
            {
                switch (kv.Key)
                {
                case "_id":
                case "id":
                    document.docId = kv.Value.ToObject <string>();
                    break;

                case "_rev":
                case "rev":
                    document.revId = kv.Value.ToObject <string>();
                    break;

                case "_deleted":
                    document.isDeleted = kv.Value.ToObject <bool>();
                    break;

                default:
                    // Other values go in the body
                    document.body.Add(kv.Key, kv.Value.ToObject <dynamic>());
                    break;
                }
            }


            return(document);
        }