Permalink
Browse files

Refactor name of CouchResponse and CouchWebResponse to restore consis…

…tency.

CouchResponse is now the counter-part of CouchRequest.
CouchResponseObject is a thin wrapper around the JObject returned from CouchDB. It also contain the StatusCode of the response.
  • Loading branch information...
1 parent ce78d8d commit 5cfdd9083d8d90b6f8309d2d3fd73acc5fb40b1a @ido-ran ido-ran committed Oct 7, 2013
View
12 LoveSeat/CouchClient.cs
@@ -76,7 +76,7 @@ public CouchClient(CouchConfiguration config)
/// <param name="target">Uri or database name of database to replicate to</param>
/// <param name="continuous">Whether or not CouchDB should continue to replicate going forward on it's own</param>
/// <returns></returns>
- public CouchResponse TriggerReplication(string source, string target, bool continuous)
+ public CouchResponseObject TriggerReplication(string source, string target, bool continuous)
{
var request = GetRequest(baseUri + "_replicate");
@@ -88,7 +88,7 @@ public CouchResponse TriggerReplication(string source, string target, bool conti
return response.GetJObject();
}
- public CouchResponse TriggerReplication(string source, string target)
+ public CouchResponseObject TriggerReplication(string source, string target)
{
return TriggerReplication(source, target, false);
}
@@ -98,7 +98,7 @@ public CouchResponse TriggerReplication(string source, string target)
/// </summary>
/// <param name="databaseName">Name of new database</param>
/// <returns></returns>
- public CouchResponse CreateDatabase(string databaseName)
+ public CouchResponseObject CreateDatabase(string databaseName)
{
return GetRequest(baseUri + databaseName).Put().GetCouchResponse().GetJObject();
}
@@ -107,7 +107,7 @@ public CouchResponse CreateDatabase(string databaseName)
/// </summary>
/// <param name="databaseName">Database to delete</param>
/// <returns></returns>
- public CouchResponse DeleteDatabase(string databaseName)
+ public CouchResponseObject DeleteDatabase(string databaseName)
{
return GetRequest(baseUri + databaseName).Delete().GetCouchResponse().GetJObject();
}
@@ -128,7 +128,7 @@ public CouchDatabase GetDatabase(string databaseName)
/// <param name="usernameToCreate"></param>
/// <param name="passwordToCreate"></param>
/// <returns></returns>
- public CouchResponse CreateAdminUser(string usernameToCreate, string passwordToCreate)
+ public CouchResponseObject CreateAdminUser(string usernameToCreate, string passwordToCreate)
{
//Creates the user in the local.ini
var iniResult = GetRequest(baseUri + "_config/admins/" + HttpUtility.UrlEncode(usernameToCreate))
@@ -210,7 +210,7 @@ public Document GetUser(string userId)
/// <param name="usernameToCreate"></param>
/// <param name="passwordToCreate"></param>
/// <returns></returns>
- public CouchResponse CreateUser(string usernameToCreate, string passwordToCreate)
+ public CouchResponseObject CreateUser(string usernameToCreate, string passwordToCreate)
{
var user = "";
View
32 LoveSeat/CouchDatabase.cs
@@ -31,7 +31,7 @@ internal CouchDatabase(string baseUri, string databaseName, string username, str
/// <param name="id">Id of Document</param>
/// <param name="jsonForDocument"></param>
/// <returns></returns>
- public CouchResponse CreateDocument(string id, string jsonForDocument)
+ public CouchResponseObject CreateDocument(string id, string jsonForDocument)
{
var jobj = JObject.Parse(jsonForDocument);
if (jobj.Value<object>("_rev") == null)
@@ -44,7 +44,7 @@ public CouchResponse CreateDocument(string id, string jsonForDocument)
resp.GetJObject();
}
- public CouchResponse CreateDocument(IBaseObject doc)
+ public CouchResponseObject CreateDocument(IBaseObject doc)
{
var serialized = ObjectSerializer.Serialize(doc);
if (doc.Id != null)
@@ -58,14 +58,14 @@ public CouchResponse CreateDocument(IBaseObject doc)
/// </summary>
/// <param name="jsonForDocument">Json for creating the document</param>
/// <returns>The response as a JObject</returns>
- public CouchResponse CreateDocument(string jsonForDocument)
+ public CouchResponseObject CreateDocument(string jsonForDocument)
{
var json = JObject.Parse(jsonForDocument); //to make sure it's valid json
var jobj =
GetRequest(databaseBaseUri + "/").Post().Json().Data(jsonForDocument).GetCouchResponse().GetJObject();
return jobj;
}
- public CouchResponse DeleteDocument(string id, string rev)
+ public CouchResponseObject 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");
@@ -138,7 +138,7 @@ public ViewResult GetDocuments(Keys keyLst)
Keys = keyLst.Values.Select(x => new KeyOptions(x)).ToArray()
};
- CouchWebResponse resp = GetRequest(viewOptions, databaseBaseUri + "/_all_docs").GetCouchResponse();
+ CouchResponse resp = GetRequest(viewOptions, databaseBaseUri + "/_all_docs").GetCouchResponse();
if (resp == null) return null;
@@ -167,7 +167,7 @@ public BulkDocumentResponses SaveDocuments(Documents docs, bool all_or_nothing)
uri = uri + "?all_or_nothing=true";
}
- CouchWebResponse resp = GetRequest(uri).Post().Json().Data(data).GetCouchResponse();
+ CouchResponse resp = GetRequest(uri).Post().Json().Data(data).GetCouchResponse();
if (resp == null)
{
@@ -196,7 +196,7 @@ public BulkDocumentResponses SaveDocuments(Documents docs, bool all_or_nothing)
/// <param name="attachment">byte[] of of the attachment. Use File.ReadAllBytes()</param>
/// <param name="filename">filename of the attachment</param>
/// <param name="contentType">Content Type must be specifed</param>
- public CouchResponse AddAttachment(string id, byte[] attachment, string filename, string contentType)
+ public CouchResponseObject AddAttachment(string id, byte[] attachment, string filename, string contentType)
{
var doc = GetDocument(id);
return AddAttachment(id, doc.Rev, attachment, filename, contentType);
@@ -210,7 +210,7 @@ public CouchResponse AddAttachment(string id, byte[] attachment, string filename
/// <param name="filename">filename of the attachment</param>
/// <param name="contentType">Content Type must be specifed</param>
/// <returns></returns>
- public CouchResponse AddAttachment(string id, string rev, byte[] attachment, string filename, string contentType)
+ public CouchResponseObject AddAttachment(string id, string rev, byte[] attachment, string filename, string contentType)
{
return
GetRequest(string.Format("{0}/{1}/{2}?rev={3}", databaseBaseUri, id, filename, rev)).Put().ContentType(contentType).Data(attachment).GetCouchResponse().GetJObject();
@@ -221,7 +221,7 @@ public CouchResponse AddAttachment(string id, string rev, byte[] attachment, str
/// <param name="id">id of the couch Document</param>
/// <param name="attachmentStream">Stream of the attachment.</param>
/// <param name="contentType">Content Type must be specifed</param>
- public CouchResponse AddAttachment(string id, Stream attachmentStream, string filename, string contentType)
+ public CouchResponseObject AddAttachment(string id, Stream attachmentStream, string filename, string contentType)
{
var doc = GetDocument(id);
return AddAttachment(id, doc.Rev, attachmentStream, filename, contentType);
@@ -235,7 +235,7 @@ public CouchResponse AddAttachment(string id, Stream attachmentStream, string fi
/// <param name="filename">filename of the attachment</param>
/// <param name="contentType">Content Type must be specifed</param>
/// <returns></returns>
- public CouchResponse AddAttachment(string id, string rev, Stream attachmentStream, string filename, string contentType)
+ public CouchResponseObject AddAttachment(string id, string rev, Stream attachmentStream, string filename, string contentType)
{
return
GetRequest(string.Format("{0}/{1}/{2}?rev={3}", databaseBaseUri, id, filename, rev)).Put().ContentType(contentType).Data(attachmentStream).GetCouchResponse().GetJObject();
@@ -255,17 +255,17 @@ public Stream GetAttachmentStream(string docId, string attachmentName)
if (doc == null) return null;
return GetAttachmentStream(docId, doc.Rev, attachmentName);
}
- public CouchResponse DeleteAttachment(string id, string rev, string attachmentName)
+ public CouchResponseObject DeleteAttachment(string id, string rev, string attachmentName)
{
return GetRequest(string.Format("{0}/{1}/{2}?rev={3}", databaseBaseUri, id, attachmentName, rev)).Json().Delete().GetCouchResponse().GetJObject();
}
- public CouchResponse DeleteAttachment(string id, string attachmentName)
+ public CouchResponseObject DeleteAttachment(string id, string attachmentName)
{
var doc = GetDocument(id);
return DeleteAttachment(doc.Id, doc.Rev, attachmentName);
}
- public CouchResponse SaveDocument(Document document)
+ public CouchResponseObject SaveDocument(Document document)
{
if (document.Rev == null)
return CreateDocument(document);
@@ -331,7 +331,7 @@ public JObject Compact(string designDoc)
return CheckAccepted(GetRequest(databaseBaseUri + "/_compact/" + designDoc).Post().Json().GetCouchResponse());
}
- private static JObject CheckAccepted(CouchWebResponse resp)
+ private static JObject CheckAccepted(CouchResponse resp)
{
if (resp == null)
{
@@ -388,7 +388,7 @@ public void SetDefaultDesignDoc(string designDoc)
private ViewResult<T> ProcessGenericResults<T>(string uri, ViewOptions options)
{
CouchRequest req = GetRequest(options, uri);
- CouchWebResponse resp = req.GetCouchResponse();
+ CouchResponse resp = req.GetCouchResponse();
bool includeDocs = false;
if (options != null)
@@ -437,7 +437,7 @@ public ViewResult View(string viewName, ViewOptions options)
private ViewResult ProcessResults(string uri, ViewOptions options)
{
CouchRequest req = GetRequest(options, uri);
- CouchWebResponse resp = req.GetCouchResponse();
+ CouchResponse resp = req.GetCouchResponse();
return new ViewResult(resp, req.GetRequest());
}
View
2 LoveSeat/CouchException.cs
@@ -14,7 +14,7 @@ public CouchException(HttpWebRequest request, HttpWebResponse response, string m
this.response = response;
}
- public CouchException(HttpWebRequest request, CouchWebResponse response, string mesg)
+ public CouchException(HttpWebRequest request, CouchResponse response, string mesg)
: base(mesg)
{
}
View
16 LoveSeat/CouchResponse.cs
@@ -1,16 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Net;
-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
20 LoveSeat/CouchResponseObject.cs
@@ -0,0 +1,20 @@
+using Newtonsoft.Json.Linq;
+
+namespace LoveSeat
+{
+ /// <summary>
+ /// Repersent response object from CouchDB as JSON.
+ /// </summary>
+ public class CouchResponseObject : JObject
+ {
+ public CouchResponseObject(JObject obj)
+ : base(obj)
+ {
+ }
+
+ /// <summary>
+ /// Get the HTTP status code of the response.
+ /// </summary>
+ 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>
- CouchResponse CreateDocument(string id, string jsonForDocument);
+ CouchResponseObject CreateDocument(string id, string jsonForDocument);
- CouchResponse CreateDocument(IBaseObject doc);
+ CouchResponseObject 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>
- CouchResponse CreateDocument(string jsonForDocument);
+ CouchResponseObject CreateDocument(string jsonForDocument);
- CouchResponse DeleteDocument(string id, string rev);
+ CouchResponseObject 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>
- CouchResponse AddAttachment(string id, byte[] attachment, string filename, string contentType);
+ CouchResponseObject 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>
- CouchResponse AddAttachment(string id, string rev, byte[] attachment, string filename, string contentType);
+ CouchResponseObject 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);
- CouchResponse DeleteAttachment(string id, string rev, string attachmentName);
- CouchResponse DeleteAttachment(string id, string attachmentName);
- CouchResponse SaveDocument(Document document);
+ CouchResponseObject DeleteAttachment(string id, string rev, string attachmentName);
+ CouchResponseObject DeleteAttachment(string id, string attachmentName);
+ CouchResponseObject SaveDocument(Document document);
BulkDocumentResponses SaveDocuments(Documents docs, bool all_or_nothing);
/// <summary>
View
4 LoveSeat/ListResult.cs
@@ -8,9 +8,9 @@ namespace LoveSeat
public class ListResult : IListResult
{
private readonly HttpWebRequest request;
- private readonly CouchWebResponse response;
+ private readonly CouchResponse response;
- public ListResult(HttpWebRequest request, CouchWebResponse response)
+ public ListResult(HttpWebRequest request, CouchResponse response)
{
this.request = request;
this.response = response;
View
4 LoveSeat/LoveSeat.csproj
@@ -67,7 +67,7 @@
<ItemGroup>
<Compile Include="CouchConfiguration.cs" />
<Compile Include="CouchException.cs" />
- <Compile Include="CouchResponse.cs" />
+ <Compile Include="CouchResponseObject.cs" />
<Compile Include="Document.cs" />
<Compile Include="CouchUser.cs" />
<Compile Include="Interfaces\IDocumentDatabase.cs" />
@@ -84,7 +84,7 @@
<Compile Include="CouchDatabase.cs" />
<Compile Include="Support\CouchRequest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
- <Compile Include="Support\CouchWebResponse.cs" />
+ <Compile Include="Support\CouchResponse.cs" />
<Compile Include="Support\NullableDictionary.cs" />
<Compile Include="Support\ReplicationOptions.cs" />
<Compile Include="Support\ResponseExtensionMethods.cs" />
View
13 LoveSeat/Support/CouchRequest.cs
@@ -6,6 +6,9 @@
namespace LoveSeat.Support
{
+ /// <summary>
+ /// Repersent a web request for CouchDB database.
+ /// </summary>
public class CouchRequest
{
private const string INVALID_USERNAME_OR_PASSWORD = "reason=Name or password is incorrect";
@@ -160,7 +163,11 @@ public HttpWebRequest GetRequest()
return request;
}
- public CouchWebResponse GetCouchResponse()
+ /// <summary>
+ /// Get the response from CouchDB.
+ /// </summary>
+ /// <returns></returns>
+ public CouchResponse GetCouchResponse()
{
bool failedAuth = false;
try
@@ -179,7 +186,7 @@ public CouchWebResponse GetCouchResponse()
throw new CouchException(request, response, response.GetResponseString() + "\n" + request.RequestUri);
}
- return new CouchWebResponse(response);
+ return new CouchResponse(response);
}
}
catch (WebException webEx)
@@ -191,7 +198,7 @@ public CouchWebResponse GetCouchResponse()
var response = (HttpWebResponse)webEx.Response;
if (response == null)
throw new HttpException("Request failed to receive a response", webEx);
- return new CouchWebResponse(response);
+ return new CouchResponse(response);
}
throw new HttpException("Request failed to receive a response");
}
View
16 LoveSeat/Support/CouchWebResponse.cs → LoveSeat/Support/CouchResponse.cs
@@ -6,14 +6,14 @@ namespace LoveSeat.Support
/// <summary>
/// Repersent a web response from CouchDB database.
/// </summary>
- public class CouchWebResponse
+ public class CouchResponse
{
private readonly string responseString;
private readonly HttpStatusCode statusCode;
private readonly string statusDescription;
private readonly string etag;
- public CouchWebResponse(HttpWebResponse response)
+ public CouchResponse(HttpWebResponse response)
{
responseString = response.GetResponseString();
statusCode = response.StatusCode;
@@ -29,13 +29,21 @@ public CouchWebResponse(HttpWebResponse response)
public string ETag { get { return etag; } }
- public CouchResponse GetJObject()
+ /// <summary>
+ /// Get the response string as JSON object.
+ /// </summary>
+ /// <returns></returns>
+ public CouchResponseObject GetJObject()
{
- var resp = new CouchResponse(JObject.Parse(responseString));
+ var resp = new CouchResponseObject(JObject.Parse(responseString));
resp.StatusCode = (int)statusCode;
return resp;
}
+ /// <summary>
+ /// Get the response as raw document.
+ /// </summary>
+ /// <returns></returns>
public Document GetCouchDocument()
{
var jobj = JObject.Parse(responseString);
View
4 LoveSeat/Support/ResponseExtensionMethods.cs
@@ -24,9 +24,9 @@ public static string GetResponseString(this HttpWebResponse response)
}
}
}
- public static CouchResponse GetJObject(this HttpWebResponse response)
+ public static CouchResponseObject GetJObject(this HttpWebResponse response)
{
- var resp = new CouchResponse(JObject.Parse(response.GetResponseString()));
+ var resp = new CouchResponseObject(JObject.Parse(response.GetResponseString()));
resp.StatusCode = (int)response.StatusCode;
return resp;
}
View
6 LoveSeat/ViewResult.cs
@@ -14,7 +14,7 @@ public class ViewResult<T> : ViewResult
{
private readonly IObjectSerializer objectSerializer = null;
private CouchDictionary<T> dict = null;
- public ViewResult(CouchWebResponse response, HttpWebRequest request, IObjectSerializer objectSerializer, bool includeDocs = false)
+ public ViewResult(CouchResponse response, HttpWebRequest request, IObjectSerializer objectSerializer, bool includeDocs = false)
: base(response, request, includeDocs)
{
this.objectSerializer = objectSerializer;
@@ -52,12 +52,12 @@ public IEnumerable<T> Items
public class ViewResult : IViewResult
{
- private readonly CouchWebResponse response;
+ private readonly CouchResponse response;
private readonly HttpWebRequest request;
private JObject json = null;
public JObject Json { get { return json ?? (json = JObject.Parse(response.ResponseString)); } }
- public ViewResult(CouchWebResponse response, HttpWebRequest request, bool includeDocs = false)
+ public ViewResult(CouchResponse response, HttpWebRequest request, bool includeDocs = false)
{
this.response = response;
this.request = request;

0 comments on commit 5cfdd90

Please sign in to comment.