Completed
Pull Request — master (#543)
by
unknown
03:20 queued 51s
created

TextQueryAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 6
1
from st2actions.runners.pythonrunner import Action
2
import wit
3
import json
4
5
6
class TextQueryAction(Action):
7
    def __init__(self, config):
8
        super(TextQueryAction, self).__init__(config)
9
        self._applications = self.config.get('applications', {})
10
        self._default_application = self.config.get('default_application',
11
                                                    None)
12
13
    def run(self, text, application=None):
14
        if not application and self._default_application:
15
            app = self._default_application
16
        elif application:
17
            app = application
18
        else:
19
            raise Exception('Unknown application: %s' % application)
20
21
        access_token = self._applications.get(app, None)
22
        if not access_token:
23
            raise Exception('Missing API key for application %s' % application)
24
25
        # pylint: disable=no-member
26
        wit.init()
27
        response = wit.text_query(text, access_token)
28
        wit.close()
29
30
        return json.loads(response)
31