Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added StatusCode to the Response

  • Loading branch information...
commit 44c2334fe043f685d8463d1eb58e52e173b19c75 1 parent d177d35
@soitgoes authored
View
18 LoveSeat/CouchDatabase.cs
@@ -30,7 +30,7 @@ internal CouchDatabase(string baseUri, string databaseName, string username, str
/// <param name="id">Id of Document</param>
/// <param name="jsonForDocument"></param>
/// <returns></returns>
- public JObject CreateDocument(string id, string jsonForDocument)
+ public CouchResponse CreateDocument(string id, string jsonForDocument)
{
var jobj = JObject.Parse(jsonForDocument);
if (jobj.Value<object>("_rev") == null)
@@ -43,7 +43,7 @@ public JObject CreateDocument(string id, string jsonForDocument)
resp.GetJObject();
}
- public JObject CreateDocument(IBaseObject doc)
+ public CouchResponse CreateDocument(IBaseObject doc)
{
var serialized = ObjectSerializer.Serialize(doc);
if (doc.Id != null)
@@ -57,14 +57,14 @@ public JObject CreateDocument(IBaseObject doc)
/// </summary>
/// <param name="jsonForDocument">Json for creating the document</param>
/// <returns>The response as a JObject</returns>
- public JObject CreateDocument(string jsonForDocument)
+ public CouchResponse CreateDocument(string jsonForDocument)
{
var json = JObject.Parse(jsonForDocument); //to make sure it's valid json
var jobj =
GetRequest(databaseBaseUri + "/").Post().Json().Data(jsonForDocument).GetResponse().GetJObject();
return jobj;
}
- public JObject DeleteDocument(string id, string rev)
+ public CouchResponse DeleteDocument(string id, string rev)
{
if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(rev))
throw new Exception("Both id and rev must have a value that is not empty");
@@ -168,7 +168,7 @@ public BulkDocumentResponses SaveDocuments(Documents docs, bool all_or_nothing)
/// <param name="id">id of the couch Document</param>
/// <param name="attachment">byte[] of of the attachment. Use File.ReadAllBytes()</param>
/// <param name="contentType">Content Type must be specifed</param>
- public JObject AddAttachment(string id, byte[] attachment, string filename, string contentType)
+ public CouchResponse AddAttachment(string id, byte[] attachment, string filename, string contentType)
{
var doc = GetDocument(id);
return AddAttachment(id, doc.Rev, attachment, filename, contentType);
@@ -182,7 +182,7 @@ public JObject AddAttachment(string id, byte[] attachment, string filename, stri
/// <param name="filename">filename of the attachment</param>
/// <param name="contentType">Content Type must be specifed</param>
/// <returns></returns>
- public JObject AddAttachment(string id, string rev, byte[] attachment, string filename, string contentType)
+ public CouchResponse AddAttachment(string id, string rev, byte[] attachment, string filename, string contentType)
{
return
GetRequest(databaseBaseUri + "/" + id + "/" + filename + "?rev=" + rev).Put().ContentType(contentType).Data(attachment).GetResponse().GetJObject();
@@ -202,17 +202,17 @@ public Stream GetAttachmentStream(string docId, string attachmentName)
if (doc == null) return null;
return GetAttachmentStream(docId, doc.Rev, attachmentName);
}
- public JObject DeleteAttachment(string id, string rev, string attachmentName)
+ public CouchResponse DeleteAttachment(string id, string rev, string attachmentName)
{
return GetRequest(databaseBaseUri + "/" + id + "/" + attachmentName + "?rev=" + rev).Json().Delete().GetResponse().GetJObject();
}
- public JObject DeleteAttachment(string id, string attachmentName)
+ public CouchResponse DeleteAttachment(string id, string attachmentName)
{
var doc = GetDocument(id);
return DeleteAttachment(doc.Id, doc.Rev, attachmentName);
}
- public JObject SaveDocument(Document document)
+ public CouchResponse SaveDocument(Document document)
{
if (document.Rev == null)
return CreateDocument(document);
View
14 LoveSeat/CouchResponse.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Newtonsoft.Json.Linq;
+
+namespace LoveSeat {
+ public class CouchResponse : JObject {
+ public CouchResponse(JObject obj) : base(obj)
+ {
+ }
+ public int StatusCode { get; set; }
+ }
+}
View
18 LoveSeat/Interfaces/IDocumentDatabase.cs
@@ -13,18 +13,18 @@ public interface IDocumentDatabase
/// <param name="id">Id of Document</param>
/// <param name="jsonForDocument"></param>
/// <returns>The status from CouchDb as a JObject</returns>
- JObject CreateDocument(string id, string jsonForDocument);
+ CouchResponse CreateDocument(string id, string jsonForDocument);
- JObject CreateDocument(IBaseObject doc);
+ CouchResponse CreateDocument(IBaseObject doc);
/// <summary>
/// Creates a document when you intend for Couch to generate the id for you.
/// </summary>
/// <param name="jsonForDocument">Json for creating the document</param>
/// <returns>Returns the status from Couchdb as a JObject</returns>
- JObject CreateDocument(string jsonForDocument);
+ CouchResponse CreateDocument(string jsonForDocument);
- JObject DeleteDocument(string id, string rev);
+ CouchResponse DeleteDocument(string id, string rev);
/// <summary>
/// Returns null if document is not found
@@ -40,7 +40,7 @@ public interface IDocumentDatabase
/// <param name="id">id of the couch Document</param>
/// <param name="attachment">byte[] of of the attachment. Use File.ReadAllBytes()</param>
/// <param name="contentType">Content Type must be specifed</param>
- JObject AddAttachment(string id, byte[] attachment, string filename, string contentType);
+ CouchResponse AddAttachment(string id, byte[] attachment, string filename, string contentType);
/// <summary>
/// Adds an attachment to the documnet. Rev must be specified on this signature. If you want to attach no matter what then use the method without the rev param
@@ -51,14 +51,14 @@ public interface IDocumentDatabase
/// <param name="filename">filename of the attachment</param>
/// <param name="contentType">Content Type must be specifed</param>
/// <returns></returns>
- JObject AddAttachment(string id, string rev, byte[] attachment, string filename, string contentType);
+ CouchResponse AddAttachment(string id, string rev, byte[] attachment, string filename, string contentType);
Stream GetAttachmentStream(Document doc, string attachmentName);
Stream GetAttachmentStream(string docId, string rev, string attachmentName);
Stream GetAttachmentStream(string docId, string attachmentName);
- JObject DeleteAttachment(string id, string rev, string attachmentName);
- JObject DeleteAttachment(string id, string attachmentName);
- JObject SaveDocument(Document document);
+ CouchResponse DeleteAttachment(string id, string rev, string attachmentName);
+ CouchResponse DeleteAttachment(string id, string attachmentName);
+ CouchResponse SaveDocument(Document document);
/// <summary>
/// Gets the results of a view with no view parameters. Use the overload to pass parameters
View
1  LoveSeat/LoveSeat.csproj
@@ -67,6 +67,7 @@
<ItemGroup>
<Compile Include="CouchConfiguration.cs" />
<Compile Include="CouchException.cs" />
+ <Compile Include="CouchResponse.cs" />
<Compile Include="Document.cs" />
<Compile Include="CouchUser.cs" />
<Compile Include="Interfaces\IDocumentDatabase.cs" />
View
6 LoveSeat/Support/ResponseExtensionMethods.cs
@@ -24,9 +24,11 @@ public static string GetResponseString(this HttpWebResponse response)
}
}
}
- public static JObject GetJObject(this HttpWebResponse response)
+ public static CouchResponse GetJObject(this HttpWebResponse response)
{
- return JObject.Parse(response.GetResponseString());
+ var resp = new CouchResponse(JObject.Parse(response.GetResponseString()));
+ resp.StatusCode = (int)response.StatusCode;
+ return resp;
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.