Commits

Anonymous committed 0410276

Authentication buffering fix

  • Participants
  • Parent commits ba08483

Comments (0)

Files changed (2)

+
+try:
+    import json
+except ImportError:
+    import simplejson as json
+
+import urllib
 import pycurl
-import simplejson as json
-import urllib
 
 class TausException(Exception):
     def __init__(self, data, message=None):
-        if message is None:
-            if data is not None and data.has_key('message'):
+        if not message:
+            if data and data.has_key('message'):
                 message = data['message']
             else:
                 message = "Generic error"
         self.data = data
 
 class Taus:
+
     URL = "http://www.tausdata.org/api/"
     POST = 1
     GET = 2
 
     def __init__(self):
+
         self.conn = pycurl.Curl()
         self.json_decoder = json.JSONDecoder()
-        pass
 
     def authenticate(self, login, password):
+
         self.auth_key = None
         self.conn.setopt(pycurl.USERPWD, "%s:%s" % (login, password))
         self._perform_action(Taus.POST, 'auth_key', {'action': 'login'}, self._on_receive_authentication)
+        data = self._parse_result(self._temp_data, allowed_codes=[201])
+        self.auth_key = data['auth_key']['id']
 
     def _on_receive_authentication(self, data):
-        data = self._parse_result(data, [201])
-        self.auth_key = data['auth_key']['id']
-
+        self._temp_data += data  
         
     def set_lang(self, source_lang, target_lang):
+
         self.source_lang = source_lang
         self.target_lang = target_lang
 
     def segment(self, q):
+
         action = {
             'q': q,
             'action': 'segment',
             'source_lang': self.source_lang,
             'target_lang': self.target_lang}
         self._perform_action(Taus.GET, 'segment', action, self._on_receive_segment)
+
         return self._parse_result(self._temp_data, allowed_codes = [200])
 
     def _on_receive_segment(self, data):
         self._temp_data += data        
 
     def _parse_result(self, data, allowed_codes = [200]):
+
         decoded = self.json_decoder.decode(data)
         if allowed_codes.count(decoded["status"]) == 0:
             raise TausException(decoded)
         return decoded
 
     def _perform_action(self, method, action, params, write_function):
+
         self._temp_data = ''
         action += '.json'
         if self.auth_key:

tests/taustest.py

-# -*- coding: utf-8 -*-
+# -*- codecs: utf-8 -*-
 
 import unittest
 import pytaus
 
-
-class  TausTestCase(unittest.TestCase):
+class TausTestCase(unittest.TestCase):
     LOGIN = 'login@domain'
-    PASSWORD = 'password'
+    PASSWORD = 'pass'
 
     def setUp(self):
         self.tested = pytaus.Taus()
-    
 
     def tearDown(self):
         self.tested = None
 
     def test_authentication_correct(self):
         self.tested.authenticate(TausTestCase.LOGIN, TausTestCase.PASSWORD)
-        self.assertTrue(self.tested.auth_key is not None, 'auth_key not found')
+        print self.tested.auth_key
+        self.assertTrue(self.tested.auth_key, 'auth_key not found')
 
     def test_parse_result(self):
         self.assertRaises(pytaus.TausException, self.tested._parse_result, 
-            '{"status":401,"reason":"bad_username_password","message":"no such user (%s) or incorrect password"}' % TausTestCase.PASSWORD)
+            '{"status":401,"reason":"bad_username_password","message":"no such user (%s) or incorrect password"}' % TausTestCase.LOGIN)
 
     def test_segment(self):
         self.tested.authenticate(TausTestCase.LOGIN, TausTestCase.PASSWORD)
         self.tested.set_lang('pl-pl', 'en-us')
         tested = self.tested.segment('Analiza')
         self.assertTrue(tested is not None, "Segment result is null")
-        print tested
 
 class TausExceptionTestCase(unittest.TestCase):
     def test_ctor1(self):
         tested = pytaus.TausException(data)
         self.assertEquals(tested.message, "Some message", "Some message message not found")
 
-
-
 if __name__ == '__main__':
     unittest.main()