import json

from go.vumitools.conversation.definition import (
    ConversationDefinitionBase, ConversationAction)


class ViewLogsAction(ConversationAction):
    action_name = 'view_logs'
    action_display_name = 'View Sandbox Logs'
    redirect_to = 'jsbox_logs'


class ConversationDefinition(ConversationDefinitionBase):
    conversation_type = 'jsbox'
    conversation_display_name = 'Javascript App'

    actions = (ViewLogsAction,)

    def configured_endpoints(self, config):
        # TODO: make jsbox apps define these explicitly and
        #       update the outbound resource to check and
        #       complain if a jsbox app sends on an endpoint
        #       it hasn't defined.
        app_config = config.get("jsbox_app_config", {})
        raw_js_config = app_config.get("config", {}).get("value", {})
        try:
            js_config = json.loads(raw_js_config)
        except Exception:
            return []

        endpoints = set()
        # vumi-jssandbox-toolkit v2 endpoints
        try:
            endpoints.update(js_config["endpoints"].keys())
        except Exception:
            pass
        # vumi-jssandbox-toolkit v1 endpoints
        try:
            pool, tag = js_config["sms_tag"]
            endpoints.add("%s:%s" % (pool, tag))
        except Exception:
            pass
        return sorted(endpoints)

import json

from go.vumitools.conversation.definition import (
    ConversationDefinitionBase, ConversationAction)


class ViewLogsAction(ConversationAction):
    action_name = 'view_logs'
    action_display_name = 'View Sandbox Logs'
    redirect_to = 'jsbox_logs'


class ConversationDefinition(ConversationDefinitionBase):
    conversation_type = 'jsbox'
    conversation_display_name = 'Javascript App'

    actions = (ViewLogsAction,)

    def configured_endpoints(self, config):
        app_config = config.get("jsbox_app_config", {})
        raw_js_config = app_config.get("config", {}).get("value", {})
        try:
            js_config = json.loads(raw_js_config)
        except Exception:
            return []

        endpoints = set()
        # vumi-jssandbox-toolkit v2 endpoints
        try:
            endpoints.update(js_config["endpoints"].keys())
        except Exception:
            pass
        # vumi-jssandbox-toolkit v1 endpoints
        try:
            pool, tag = js_config["sms_tag"]
            endpoints.add("%s:%s" % (pool, tag))
        except Exception:
            pass
        return sorted(endpoints)

Remove ancient TODO that was resolved a long time ago.
Remove ancient TODO that was resolved a long time ago.

  import json
  
  from go.vumitools.conversation.definition import (
      ConversationDefinitionBase, ConversationAction)
  
  
  class ViewLogsAction(ConversationAction):
      action_name = 'view_logs'
      action_display_name = 'View Sandbox Logs'
      redirect_to = 'jsbox_logs'
  
  
  class ConversationDefinition(ConversationDefinitionBase):
      conversation_type = 'jsbox'
      conversation_display_name = 'Javascript App'
  
      actions = (ViewLogsAction,)
  
      def configured_endpoints(self, config):
-         # TODO: make jsbox apps define these explicitly and
-         #       update the outbound resource to check and
-         #       complain if a jsbox app sends on an endpoint
-         #       it hasn't defined.
          app_config = config.get("jsbox_app_config", {})
          raw_js_config = app_config.get("config", {}).get("value", {})
          try:
              js_config = json.loads(raw_js_config)
          except Exception:
              return []
  
          endpoints = set()
          # vumi-jssandbox-toolkit v2 endpoints
          try:
              endpoints.update(js_config["endpoints"].keys())
          except Exception:
              pass
          # vumi-jssandbox-toolkit v1 endpoints
          try:
              pool, tag = js_config["sms_tag"]
              endpoints.add("%s:%s" % (pool, tag))
          except Exception:
              pass
          return sorted(endpoints)
  
Remove ancient TODO that was resolved a long time ago.
## Code Before:
import json

from go.vumitools.conversation.definition import (
    ConversationDefinitionBase, ConversationAction)


class ViewLogsAction(ConversationAction):
    action_name = 'view_logs'
    action_display_name = 'View Sandbox Logs'
    redirect_to = 'jsbox_logs'


class ConversationDefinition(ConversationDefinitionBase):
    conversation_type = 'jsbox'
    conversation_display_name = 'Javascript App'

    actions = (ViewLogsAction,)

    def configured_endpoints(self, config):
        # TODO: make jsbox apps define these explicitly and
        #       update the outbound resource to check and
        #       complain if a jsbox app sends on an endpoint
        #       it hasn't defined.
        app_config = config.get("jsbox_app_config", {})
        raw_js_config = app_config.get("config", {}).get("value", {})
        try:
            js_config = json.loads(raw_js_config)
        except Exception:
            return []

        endpoints = set()
        # vumi-jssandbox-toolkit v2 endpoints
        try:
            endpoints.update(js_config["endpoints"].keys())
        except Exception:
            pass
        # vumi-jssandbox-toolkit v1 endpoints
        try:
            pool, tag = js_config["sms_tag"]
            endpoints.add("%s:%s" % (pool, tag))
        except Exception:
            pass
        return sorted(endpoints)

## Instruction:
Remove ancient TODO that was resolved a long time ago.
## Code After:
import json

from go.vumitools.conversation.definition import (
    ConversationDefinitionBase, ConversationAction)


class ViewLogsAction(ConversationAction):
    action_name = 'view_logs'
    action_display_name = 'View Sandbox Logs'
    redirect_to = 'jsbox_logs'


class ConversationDefinition(ConversationDefinitionBase):
    conversation_type = 'jsbox'
    conversation_display_name = 'Javascript App'

    actions = (ViewLogsAction,)

    def configured_endpoints(self, config):
        app_config = config.get("jsbox_app_config", {})
        raw_js_config = app_config.get("config", {}).get("value", {})
        try:
            js_config = json.loads(raw_js_config)
        except Exception:
            return []

        endpoints = set()
        # vumi-jssandbox-toolkit v2 endpoints
        try:
            endpoints.update(js_config["endpoints"].keys())
        except Exception:
            pass
        # vumi-jssandbox-toolkit v1 endpoints
        try:
            pool, tag = js_config["sms_tag"]
            endpoints.add("%s:%s" % (pool, tag))
        except Exception:
            pass
        return sorted(endpoints)
