Beispiel #1
0
    def test_basic(self):
        b = get_browser()
        query = Data()
        query.cmd = 'articles_length'

        url = config.url_root + 'api.py?' + query.urlencode()
        b.go(url)
        result = b.get_html()
        # For Secure key. server is using secure key?
        self.assertNotEqual(result.find('Error'), -1)

        # For articles_length
        query.secure_key = config.SECURE_KEY
        url = config.url_root + 'api.py?' + query.urlencode()

        b.go(url)
        result = b.get_html()
        self.assertEqual(libs.removeBlank(result), '2')

        # For comments_length
        query.cmd = 'comments_length'
        query.doc_id = Var.dummy_id

        url = config.url_root + 'api.py?' + query.urlencode()
        b.go(url)
        result = b.get_html()
        self.assertEqual(libs.removeBlank(result), '1')

        # For article_json
        query.cmd = 'article_json'
        url = config.url_root + 'api.py?' + query.urlencode()
        b.go(url)
        result = b.get_html()
        # Get the index of the local dummy
        article = Article()
        article.set(Var.dummy_id)
        result = json.loads(libs.removeBlank(result))
        self.assertEqual(result['doc_id'], unicode(article.__dict__['doc_id']))
Beispiel #2
0
class API(object):

    """
    The class is used like urllib2.urlopen function with the arguments.
    The arguments specifies the data argument of urllib2.Request class. To
    create the arguments we use the class Data. The returned result is the
    returned result of urllib2.urlopen.

    """
    url = config.url_api

    def __init__(self, data):
        self.required_data = Data()
        # Set secure key
        self.required_data.secure_key = config.SECURE_KEY
        self.data = data
        self.cmd = self._getCmd()
        self._setArgs()
        #req = Request(self.url, self.required_data)
        #return urlopen(req)

    def _getCmd(self):
        try:
            return getattr(api, self.data.cmd)
        except AttributeError:
            raise AttributeError("'%s' requires data.cmd" % self.__class__.__name__)

    def _setArgs(self):
        # get the argument variable of self.cmd
        spec = inspect.getargspec(self.cmd)
        setattr(self.required_data, "cmd", self.data.cmd)
        for arg in spec.args:
            value = getattr(self.data, arg)
            setattr(self.required_data, arg, value)

    def send(self):
        data = self.required_data.urlencode()
        req = Request(self.url, data)
        #libs.log(req)
        return urlopen(req)