Commits

Anonymous committed 09917e5

add possibility to stream fetch_attachment

Comments (0)

Files changed (3)

couchdbkit/client.py

         return View(self, '_all_docs', wrapper=wrapper, **params)
     iter_documents = documents    
 
-    def put_attachment(self, doc, content, name=None, 
-            content_type=None, content_length=None, chunked=False):
+    def put_attachment(self, doc, content, name=None, content_type=None, 
+        content_length=None):
         """ Add attachement to a document.
 
         @param doc: dict, document object
             headers['Content-Type'] = content_type
             
         # add appropriate headers    
-        if chunked:
-            headers.setdefault("Transfer-Encoding", "chunked")
-            if 'Content-Length' in headers:
-                del headers['Content-Length']
-        elif content_length and content_length is not None:
+        if content_length and content_length is not None:
             headers['Content-Length'] = content_length
 
         if hasattr(doc, 'to_json'):
         
         return self.res(docid).delete(name, rev=doc['_rev'])
 
-    def fetch_attachment(self, id_or_doc, name):
+    def fetch_attachment(self, id_or_doc, name, stream=False, 
+            stream_size=16384):
         """ get attachment in a document
         
         @param id_or_doc: str or dict, doc id or document dict
         @param name: name of attachment default: default result
-
+        @param stream: boolean, response return a ResponseStream object
+        @param stream_size: int, size in bytes of response stream block
+        
         @return: str, attachment
         """
 
         docid = self.escape_docid(docid)
         name = url_quote(name, safe="")
         try:
-            data = self.res(docid).get(name)
+            data = self.res(docid).get(name, _stream=stream, 
+                _stream_size=stream_size)
         except ResourceNotFound:
             return None
         return data

couchdbkit/resource.py

         """ add copy to HTTP verbs """
         return self.request('COPY', path=path, headers=headers, **params)
         
-    def request(self, method, path=None, payload=None, headers=None, **params):
+    def request(self, method, path=None, payload=None, headers=None, 
+         _stream=False, _stream_size=16384, **params):
         """ Perform HTTP call to the couchdb server and manage 
         JSON conversions, support GET, POST, PUT and DELETE.
         
             converted to JSON.
         @param headers: dict, optionnal headers that will
             be added to HTTP request.
+        @param _stream: boolean, response return a ResponseStream object
+        @param _stream_size: int, size in bytes of response stream block
         @param params: Optionnal parameterss added to the request. 
             Parameterss are for example the parameters for a view. See 
             `CouchDB View API reference 
         def _make_request(retry=1):
             try:
                 return restkit.Resource.request(self, method, path=path,
-                        payload=body, headers=headers, **params)
+                        payload=body, headers=headers, _stream=_stream, 
+                        _stream_size=_stream_size, **params)
             except (socket.error, httplib.BadStatusLine), e:
                 if retry > 0:
                     time.sleep(0.4)

tests/test_schema.py

         
         b1  = B()
         b1.ls = [u'hello', u'123']
-        self._assert(b1.ls == [u'hello', u'123'])
-        self._assert(b1._doc['ls'] == [u'hello', u'123'])
+        self.assert_(b1.ls == [u'hello', u'123'])
+        self.assert_(b1._doc['ls'] == [u'hello', u'123'])
 
          
     def testDictProperty(self):