| Total Complexity | 6 |
| Total Lines | 25 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | from st2actions.runners.pythonrunner import Action |
||
| 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 |