Commits

Branko Vukelic committed fab8eb3

Basic design of the client class

This still misses any mission-specific methods which will be worked
out next.

Comments (0)

Files changed (3)

mstranslator/__init__.py

+from client import MSTranslator

mstranslator/client.py

+from __future__ import unicode_literals
+
+from urllib2 import Request
+from urllib import urlencode
+
+
+class MSTranslator(object):
+    api_protocol = 'http'
+    api_host = 'api.microsofttranslator.com'
+    api_version = 'v2'
+    api_endpoint_base = 'Http.svc'
+    default_endpoint = 'Translate'
+    valid_endpoints = {
+        'AddTranslation': 'GET',
+        'AddTranslationArray': 'POST',
+        'BreakSentences': 'GET',
+        'Detect': 'GET',
+        'DetectArray': 'POST',
+        'GetLanguageNames': 'POST',
+        'GetLanguagesForSpeak': 'GET',
+        'GetLanguagesForTranslate': 'GET',
+        'GetTranslations': 'POST',
+        'GetTranslationsArray': 'POST',
+        'Speak': 'GET',
+        'Translate': 'GET',
+        'TranslateArray': 'POST',
+    }
+
+    _cache = dict()
+
+    def __init__(self, api_key):
+        self.api_key = api_key
+
+    def get_quoted_data(self, **kwargs):
+        return urlencode(kwargs)
+
+    def get_endpoint_url(self, method=None):
+        method = method or self.default_endpoint
+
+        if method not in self.valid_endpoints:
+            raise ValueError('%s is an invalid endpoint' % method)
+
+        return '%s://%s/%s/%s/%s' % (
+            self.api_protocol,
+            self.api_host,
+            self.api_version,
+            self.api_endpoint_base,
+            method
+        )
+
+    def get_full_url(self, method, data=None, **kwargs):
+        return '%s?%s' % (self.get_endpoint_url(),
+                          self.get_quoted_data(**kwargs))
+
+    def get_authorization_string(self):
+        if not self.api_key:
+            raise RuntimeError('Cannot use Translator API without API key.')
+
+        return "Bearer %s" % self.api_key
+
+    def get_authorization_header(self):
+        return {'Authorization': self.get_authorization_string()}
+
+
+
+
+

tests/test_client.py

+from unittest import TestCase
+from os import path
+
+from mstranslator import MSTranslator
+
+f = open(path.join(path.dirname(path.realpath(__file__)), 'apikey.txt'))
+api_key = f.read().strip()
+f.close()
+
+class ClientUnitTestCase(TestCase):
+    def setUp(self):
+        self.t = MSTranslator(api_key)
+
+    def test_get_full_url(self):
+        self.assertEqual(
+            self.t.get_endpoint_url('BreakSentences'),
+            'http://api.microsofttranslator.com/v2/Http.svc/BreakSentences'
+        )
+
+    def test_get_default_url(self):
+        self.assertEqual(
+            self.t.get_endpoint_url(),
+            'http://api.microsofttranslator.com/v2/Http.svc/Translate'
+        )
+
+    def test_get_quoted_data(self):
+        self.assertEqual(
+            self.t.get_quoted_data(foo='bar', bar=1),
+            'foo=bar&bar=1'
+        )
+
+    def test_get_full_url(self):
+        self.assertEqual(
+            self.t.get_full_url('Translate', text='foo'),
+            'http://api.microsofttranslator.com/v2/Http.svc/Translate?text=foo'
+        )
+
+    def test_cannot_use_invalid_endpoint(self):
+        with self.assertRaises(ValueError):
+            self.t.get_endpoint_url('Bogus')
+
+    def test_get_auth_string(self):
+        self.assertEqual(
+            self.t.get_authorization_string(),
+            'Bearer %s' % api_key
+        )
+
+    def test_get_auth_header(self):
+        self.assertEqual(
+            self.t.get_authorization_header(),
+            dict(Authorization='Bearer %s' % api_key)
+        )
+
+
+