Browse files

Implement attachments parameter for GetDocument method in

CouchDatabase.cs. This allows you to fetch a document and all its
attachments in one request, by adding an ?attachments=true URL parameter
to the GET request. Although asking for a MIME multipart/related is more
efficient, it can sometimes be useful to just do it in one request.

Read
http://wiki.apache.org/couchdb/HTTP_Document_API#Getting_Attachments_With_a_Document
for more information about this subject.
  • Loading branch information...
1 parent b9c3a58 commit 31ae025756f2d3df8a126ffa864a6769da7b143e @jvdgeest jvdgeest committed Jan 30, 2013
Showing with 33 additions and 13 deletions.
  1. +33 −13 LoveSeat/CouchDatabase.cs
View
46 LoveSeat/CouchDatabase.cs
@@ -75,29 +75,49 @@ public CouchResponse DeleteDocument(string id, string rev)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
- public Document GetDocument(string id)
+ public T GetDocument<T>(string id, bool attachments, IObjectSerializer objectSerializer)
{
- var resp = GetRequest(databaseBaseUri + "/" + id).Get().Json().GetResponse();
- if (resp.StatusCode==HttpStatusCode.NotFound) return null;
- return resp.GetCouchDocument();
+ var resp = GetRequest(String.Format("{0}/{1}{2}", databaseBaseUri, id, attachments ? "?attachments=true" : string.Empty)).Get().Json().GetResponse();
+ if (resp.StatusCode == HttpStatusCode.NotFound) return default(T);
+ return objectSerializer.Deserialize<T>(resp.GetResponseString());
}
- public T GetDocument<T>(Guid id , IObjectSerializer objectSerializer)
+ public T GetDocument<T>(string id, IObjectSerializer objectSerializer)
{
- return GetDocument<T>(id.ToString(), objectSerializer);
+ return GetDocument<T>(id, false, objectSerializer);
}
- public T GetDocument<T>(Guid id)
+ public T GetDocument<T>(string id, bool attachments)
{
- return GetDocument<T>(id.ToString());
+ return GetDocument<T>(id, attachments, ObjectSerializer);
}
public T GetDocument<T>(string id)
{
- return GetDocument<T>(id, ObjectSerializer);
+ return GetDocument<T>(id, false);
}
- public T GetDocument<T>(string id, IObjectSerializer objectSerializer)
+ public T GetDocument<T>(Guid id, bool attachments, IObjectSerializer objectSerializer)
{
- var resp = GetRequest(databaseBaseUri + "/" + id).Get().Json().GetResponse();
- if (resp.StatusCode == HttpStatusCode.NotFound) return default(T);
- return objectSerializer.Deserialize<T>(resp.GetResponseString());
+ return GetDocument<T>(id.ToString(), attachments, objectSerializer);
+ }
+ public T GetDocument<T>(Guid id, IObjectSerializer objectSerializer)
+ {
+ return GetDocument<T>(id, false, objectSerializer);
+ }
+ public T GetDocument<T>(Guid id, bool attachments)
+ {
+ return GetDocument<T>(id.ToString(), attachments);
+ }
+ public T GetDocument<T>(Guid id)
+ {
+ return GetDocument<T>(id, false);
+ }
+ public Document GetDocument(string id, bool attachments)
+ {
+ var resp = GetRequest(String.Format("{0}/{1}{2}", databaseBaseUri, id, attachments ? "?attachments=true" : string.Empty)).Get().Json().GetResponse();
+ if (resp.StatusCode == HttpStatusCode.NotFound) return null;
+ return resp.GetCouchDocument();
+ }
+ public Document GetDocument(string id)
+ {
+ return GetDocument(id, false);
}
/// <summary>

0 comments on commit 31ae025

Please sign in to comment.