Add return-value checking to AssuanClient response handling.
authorW. Trevor King <wking@drexel.edu>
Sat, 24 Mar 2012 20:07:11 +0000 (16:07 -0400)
committerW. Trevor King <wking@drexel.edu>
Sat, 24 Mar 2012 20:07:11 +0000 (16:07 -0400)
This makes calling code more Pythonic (catch is better than check ;).

pyassuan/client.py

index 13382881242e76e62d9db76825fefa7108318f82..7abe61779432acc254e030a2e01dec57ecbc5f9c 100644 (file)
@@ -60,7 +60,7 @@ class AssuanClient (object):
         self.logger.info('S: {}'.format(response))
         return response
 
-    def make_request(self, request, response=True):
+    def make_request(self, request, response=True, expect=['OK']):
         rstring = str(request)
         self.logger.info('C: {}'.format(rstring))
         self.output.write(rstring)
@@ -70,9 +70,9 @@ class AssuanClient (object):
         except IOError:
             raise
         if response:
-            return self.get_responses(request=request)
+            return self.get_responses(request=request, expect=expect)
 
-    def get_responses(self, request=None):
+    def get_responses(self, request=None, expect=['OK']):
         responses = list(self.responses())
         if responses[-1].type == 'ERR':
             eresponse = responses[-1]
@@ -87,6 +87,8 @@ class AssuanClient (object):
                 error.request = request
             error.responses = responses
             raise error
+        if expect:
+            assert responses[-1].type in expect, [str(r) for r in responses]
         data = []
         for response in responses:
             if response.type == 'D':