Commits

Anonymous committed 97da64c

new way to get status_code. now when you get result, this a string +
some attributes.

from restclient import RestClient
client = RestClient()
page = resource.request('GET', 'http://friendpaste.com';
print page.http_code

Comments (0)

Files changed (4)

             response_headers = self._parseHeaders(header)
             code = c.getinfo(pycurl.RESPONSE_CODE)
             
-            return self._make_response(url, code, response_headers,
-                data.getvalue())
+            return self._make_response(final_url=url, status=code,
+                    headers=response_headers, body=data.getvalue())
         finally:
             c.close()
 
     probably an HTML error page) is e.response.body.
     """
 
+class ResourceResult(str):
+    """ result returned by `restclient.rest.RestClient`.
+    
+    you can get result like any string and  status code by result.http_code, 
+    or see anything about the response via e.response. For example, the entire 
+    result body is resulte.response.body.
+
+    .. code-block:: python
+
+            from restclient import RestClient
+            client = RestClient()
+            page = resource.request('GET', 'http://friendpaste.com'
+            print page.http_code
+
+    """
+    def __new__(cls, s, http_code, response):
+        self = str.__new__(cls, s)
+        self.http_code = http_code
+        self.response = response
+        return self
+
+
 
 class Resource(object):
     """A class that can be instantiated for access to a RESTful resource, 
         """
         return self.client.put(self.uri, path=path, body=payload, headers=headers, **params)
 
-    def get_status_code(self):
-        """ get status code of the last request """
-        return self.client.status_code
-    status_code = property(get_status_code)
 
     def update_uri(self, path):
         """
                 body=body, headers=headers)
 
         status_code = int(resp.status)
-        self.status_code = status_code
 
         if status_code >= 400:
             if type(data) is dict:
 
             if status_code == 404:
                 raise ResourceNotFound(error, http_code=404, response=resp)
-            elif self.status_code == 401 or self.status_code == 403:
+            elif status_code == 401 or status_code == 403:
                 raise Unauthorized(error, http_code=status_code,
                         response=resp)
             else:
                 raise RequestFailed(error, http_code=status_code,
                         response=resp)
 
-        return data
+        return ResourceResult(data, status_code, resp)
 
 
 def make_uri(base, *path, **query):

tests/clients_test.py

 
     def testGetWithContentType(self):
         result = self.res.get('/json', headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
         def bad_get():
             result = self.res.get('/json', headers={'Content-Type': 'text/plain'})
         self.assertRaises(RequestFailed, bad_get) 
 
     def testGetWithQuery(self):
         result = self.res.get('/query', test="testing")
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
 
     def testSimplePost(self):
     def testPostWithContentType(self):
         result = self.res.post('/json', payload="test",
                 headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
         def bad_post():
             result = self.res.post('/json', payload="test",
                     headers={'Content-Type': 'text/plain'})
     def testEmptyPost(self):
         result = self.res.post('/empty', payload="",
                 headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
         result = self.res.post('/empty',headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
 
     def testPostWithQuery(self):
         result = self.res.post('/query', test="testing")
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
     def testSimplePut(self):
         result = self.res.put(payload="test")
     def testPutWithContentType(self):
         result = self.res.put('/json', payload="test",
                 headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
         def bad_put():
             result = self.res.put('/json', payload="test",
                     headers={'Content-Type': 'text/plain'})
     def testEmptyPut(self):
         result = self.res.put('/empty', payload="",
                 headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
         result = self.res.put('/empty',headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
 
     def testPuWithQuery(self):
         result = self.res.put('/query', test="testing")
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
     def testHead(self):
         result = self.res.head('/ok')
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
     def testDelete(self):
         result = self.res.delete('/delete')
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
 
 
         
         res = Resource(self.url, httpclient)
         result = res.get('/auth')
-        self.assert_(res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
         httpclient.add_credentials("test", "test2")
         

tests/resource_test.py

     def testGet(self):
         result = self.res.get()
         self.assert_(result == "welcome")
+        self.assert_(result.http_code == 200)
 
     def testGetWithContentType(self):
         result = self.res.get('/json', headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
         def bad_get():
             result = self.res.get('/json', headers={'Content-Type': 'text/plain'})
         self.assertRaises(RequestFailed, bad_get) 
 
     def testGetWithQuery(self):
         result = self.res.get('/query', test="testing")
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
 
     def testSimplePost(self):
     def testPostWithContentType(self):
         result = self.res.post('/json', payload="test",
                 headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
         def bad_post():
             result = self.res.post('/json', payload="test",
                     headers={'Content-Type': 'text/plain'})
     def testEmptyPost(self):
         result = self.res.post('/empty', payload="",
                 headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
         result = self.res.post('/empty',headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
 
     def testPostWithQuery(self):
         result = self.res.post('/query', test="testing")
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
     def testSimplePut(self):
         result = self.res.put(payload="test")
     def testPutWithContentType(self):
         result = self.res.put('/json', payload="test",
                 headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
         def bad_put():
             result = self.res.put('/json', payload="test",
                     headers={'Content-Type': 'text/plain'})
     def testEmptyPut(self):
         result = self.res.put('/empty', payload="",
                 headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
         result = self.res.put('/empty',headers={'Content-Type': 'application/json'})
-        self.assert_(self.res.status_code == 200 )
+        self.assert_(result.http_code == 200 )
 
     def testPuWithQuery(self):
         result = self.res.put('/query', test="testing")
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
     def testHead(self):
         result = self.res.head('/ok')
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
     def testDelete(self):
         result = self.res.delete('/delete')
-        self.assert_(self.res.status_code == 200)
+        self.assert_(result.http_code == 200)
 
 
     def testAuth(self):
         
         res = Resource(self.url, httpclient)
         result = res.get('/auth')
-        self.assert_(res.status_code == 200) 
+        self.assert_(result.http_code == 200) 
     
 if __name__ == '__main__':
     from _server_test import run_server_test