private HttpWebRequest GetRequest() { var requestUri = new UriBuilder("http", server.Host, server.Port, ((db != null) ? db.Name + "/" : "") + path, query).Uri; var request = WebRequest.Create(requestUri) as HttpWebRequest; if (request == null) { throw CouchException.Create("Failed to create request"); } request.Timeout = 3600000; // 1 hour. May use System.Threading.Timeout.Infinite; request.Method = method; if (mimeType != null) { request.ContentType = mimeType; } foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } if (!string.IsNullOrEmpty(server.EncodedCredentials)) { request.Headers.Add("Authorization", server.EncodedCredentials); } if (postStream != null) { WriteData(request); } Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Request: {0} Method: {1}", requestUri, method)); return(request); }
/// <summary> /// Create or update a list of ICouchDocuments in CouchDB. Uses POST and CouchDB will /// allocate new ids if the documents lack them. /// </summary> /// <param name="documents">List of documents to store.</param> /// <remarks>POST may be problematic in some environments.</remarks> public void SaveDocuments(IEnumerable <ICouchDocument> documents, bool allOrNothing) { var bulk = new CouchBulkDocuments(documents); try { var result = Request("_bulk_docs") .Data(CouchDocument.WriteJson(bulk)) .Query("?all_or_nothing=" + allOrNothing.ToString().ToLower()) .PostJson() .Parse <JArray>(); int index = 0; foreach (var document in documents) { document.Id = (result[index])["id"].Value <string>(); document.Rev = (result[index])["rev"].Value <string>(); ++index; } } catch (WebException e) { throw CouchException.Create("Failed to create bulk documents", e); } }
public CouchViewResultStream <T> StreamResult <T>() where T : ICanJson, new() { try { return(new CouchViewResultStream <T>(Request().Stream())); } catch (WebException e) { throw CouchException.Create("Query failed", e); } }
public CouchGenericViewResult GetResult() { try { return(GetResult <CouchGenericViewResult>()); } catch (WebException e) { throw CouchException.Create("Query failed", e); } }
/// <summary> /// Read a couch attachment given a document id, this method does not have enough information to do caching. /// </summary> /// <param name="documentId">Document identifier</param> /// <returns>Document attachment</returns> public WebResponse ReadAttachment(string documentId, string attachmentName) { try { return(Request(documentId + "/" + attachmentName).Response()); } catch (WebException e) { throw CouchException.Create("Failed to read document: " + e.Message, e); } }
/// <summary> /// Read a couch document given an id, this method does not have enough information to do caching. /// </summary> /// <param name="documentId">Document identifier</param> /// <returns>Document Json as string</returns> public string ReadDocumentString(string documentId) { try { return(Request(documentId).String()); } catch (WebException e) { throw CouchException.Create("Failed to read document: " + e.Message, e); } }
/// <summary> /// Read a couch document given an id, this method does not have enough information to do caching. /// </summary> /// <param name="documentId">Document identifier</param> /// <returns>Document Json as JObject</returns> public JObject ReadDocument(string documentId) { try { return(Request(documentId).Parse()); } catch (WebException e) { throw CouchException.Create("Failed to read document", e); } }
/// <summary> /// Typically only used from CouchServer. /// </summary> public void CreateDatabase(string name) { try { Request().Path(name).Put().Check("Failed to create database"); } catch (WebException e) { throw CouchException.Create("Failed to create database", e); } }
/// <summary> /// Create a given ICouchDocument in CouchDB. Uses POST and CouchDB will allocate a new id and overwrite any existing id. /// </summary> /// <param name="document">Document to store.</param> /// <returns>Document with Id and Rev set.</returns> /// <remarks>POST which may be problematic in some environments.</remarks> public ICouchDocument CreateDocument(ICouchDocument document) { try { JObject result = Request().Data(CouchDocument.WriteJson(document)).PostJson().Check("Failed to create document").Result(); document.Id = result["id"].Value <string>(); document.Rev = result["rev"].Value <string>(); return(document); } catch (WebException e) { throw CouchException.Create("Failed to create document", e); } }
new public string GetResult() { try { var sb = new StringBuilder(); var reader = new StreamReader(Request().Response().GetResponseStream()); sb.Append(reader.ReadLine()); return(sb.ToString()); } catch (WebException e) { throw CouchException.Create("Query failed", e); } }
/// <summary> /// Write a CouchDocument or ICouchDocument, it may already exist in db and will then be overwritten. /// </summary> /// <param name="document">Couch document</param> /// <param name="batch">True if we don't want to wait for flush (commit).</param> /// <returns>Couch Document with new Rev set.</returns> /// <remarks>This relies on the document to already have an id.</remarks> public ICouchDocument WriteDocument(ICouchDocument document, bool batch) { if (document.Id == null) { throw CouchException.Create( "Failed to write document using PUT because it lacks an id, use CreateDocument instead to let CouchDB generate an id"); } JObject result = Request(document.Id).Query(batch ? "?batch=ok" : null).Data(CouchDocument.WriteJson(document)).Put().Check("Failed to write document").Result(); document.Id = result["id"].Value <string>(); // Not really needed document.Rev = result["rev"].Value <string>(); return(document); }
/// <summary> /// Writes the attachment. /// </summary> /// <param name="document">The document.</param> /// <param name="attachmentName">Name of the attachment.</param> /// <param name="attachmentData">The attachment data.</param> /// <param name="mimeType">Type of the MIME.</param> /// <returns>The document.</returns> /// <remarks>This relies on the document to already have an id.</remarks> public ICouchDocument WriteAttachment(ICouchDocument document, string attachmentName, Stream attachmentData, string mimeType) { if (document.Id == null) { throw CouchException.Create( "Failed to add attachment to document using PUT because it lacks an id"); } JObject result = Request(document.Id + "/" + attachmentName).Query("?rev=" + document.Rev).Data(attachmentData).MimeType(mimeType).Put().Check("Failed to write attachment") .Result(); document.Id = result["id"].Value <string>(); // Not really neeed document.Rev = result["rev"].Value <string>(); return(document); }
public CouchJsonDocument GetDocument(string documentId) { try { try { return(new CouchJsonDocument(Request(documentId).Parse())); } catch (WebException e) { throw CouchException.Create("Failed to get document", e); } } catch (CouchNotFoundException) { return(null); } }
public ICouchRequest Check(string message) { try { if (result == null) { Parse(); } if (!result["ok"].Value <bool>()) { throw CouchException.Create(string.Format(CultureInfo.InvariantCulture, message + ": {0}", result)); } return(this); } catch (WebException e) { throw CouchException.Create(message, e); } }
public T GetResult <T>() where T : CouchViewResult, new() { var req = Request(); if (Result == null) { Result = new T(); } else { // Tell the request what we already have req.Etag(Result.etag); if (checkETagUsingHead) { // Make a HEAD request to avoid transfer of data if (req.Head().Send().IsETagValid()) { return((T)Result); } // Set back to GET before proceeding below req.Get(); } } JObject json; try { json = req.Parse(); } catch (WebException e) { throw CouchException.Create("Query failed", e); } if (json != null) // ETag did not match, view has changed { Result.Result(json); Result.etag = req.Etag(); } return((T)Result); }
/// <summary> /// Delete documents in bulk fashion. /// </summary> public void DeleteDocuments(ICanJson bulk) { try { var result = Request("_bulk_docs").Data(CouchDocument.WriteJson(bulk)).PostJson().Parse <JArray>(); for (int i = 0; i < result.Count(); i++) { //documents[i].id = (result[i])["id"].Value<string>(); //documents[i].rev = (result[i])["rev"].Value<string>(); if ((result[i])["error"] != null) { throw CouchException.Create(string.Format(CultureInfo.InvariantCulture, "Document with id {0} was not deleted: {1}: {2}", (result[i])["id"].Value <string>(), (result[i])["error"], (result[i])["reason"])); } } } catch (WebException e) { throw CouchException.Create("Failed to bulk delete documents", e); } }