Commits

Anonymous committed 01ee5a2

follow_redirect flag for append and the like

Comments (0)

Files changed (1)

whoops/__init__.py

         return self._op('GET', path, 'GETFILESTATUS')
 
     @jsonpath(['FileChecksum'])
-    def checksum(self, path):
+    def checksum(self, path, follow_redirects=False):
         resp = self._op('GET', path, 'GETFILECHECKSUM')
-        url = self._get_redirect(resp)
+        url = self._get_redirect(resp, follow_redirects=follow_redirects)
         return requests.get(url)
 
     @jsonpath(['Path'])
 
         self._op('PUT', path, 'SETOWNER', query=query)
 
-    def read(self, path, offset=0, length=0, buffersize=0):
+    def read(self, path, offset=0, length=0, buffersize=0, follow_redirects=False):
         # FIXME: Find a way to unite handling of optional parameters
         query = {}
         if offset:
             query['buffersize'] = intparam(buffersize)
 
         resp = self._op('GET', path, 'OPEN', query=query)
-        url = self._get_redirect(resp)
+        url = self._get_redirect(resp, follow_redirects=follow_redirects)
         return requests.get(url).content
 
     def put(self, local, path, overwrite=False, blocksize=0, replication=0,
 
         self._put('CREATE', 'PUT', local, path, query)
 
-    def append(self, local, path, buffersize=0):
+    def append(self, local, path, buffersize=0, follow_redirects=False):
         query = {}
         if buffersize:
             query['buffersize'] = intparam(buffersize)
-        self._put('APPEND', 'POST', local, path, query)
+        self._put('APPEND', 'POST', local, path, query, follow_redirects=follow_redirects)
 
     @jsonpath(['boolean'])
     def mkdir(self, path, permission=0):
 
 
     # Below here are some utility functions
-    def _put(self, op, method, local, path, query):
+    def _put(self, op, method, local, path, query, follow_redirects=False):
         if not isfile(local):
             raise WebHDFSError('put error: {0} not found'.format(local))
 
         resp = self._op(method, path, op, query)
-        url = self._get_redirect(resp)
+        url = self._get_redirect(resp, follow_redirects=follow_redirects)
 
         with open(local) as fo:
             data = fo.read()
         resp = requests.request(method, url, data=data)
         self._check_resp(resp)
 
-    def _get_redirect(self, resp):
-        # The host in the redirect URL is *internal* one, so we need to fix
-        # the url. Otherwise we'd just follow the redirects
+    def _get_redirect(self, resp, follow_redirects=False):
         url = urlparse(resp.headers['Location'])
-        host, port = url.netloc.split(':')
-        url = url._replace(netloc='{0}:{1}'.format(self.host, port))
+        if not follow_redirects:
+            # The host in the redirect URL is *internal* one, so we need to fix
+            # the url. Otherwise we'd just follow the redirects            
+            host, port = url.netloc.split(':')
+            url = url._replace(netloc='{0}:{1}'.format(self.host, port))
         return url.geturl()
 
     def _check_resp(self, resp):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.