Esempio n. 1
0
class OpenChatBotCollection(object):
    def __init__(self):
        self._openchatbots = {}
        self._domain_handler = None

    def load_from_configuration(self,
                                configuration: BrainOpenChatBotsConfiguration):

        names = configuration.openchatbots()
        for name in names:
            openchatbot_config = configuration.openchatbot(name)
            self._openchatbots[name.upper()] = OpenChatBot(
                openchatbot_config.section_name, openchatbot_config.url,
                openchatbot_config.method, openchatbot_config.authorization,
                openchatbot_config.api_key)

        self._domain_handler = OpenChatBotDomainHandler(configuration)

        return True

    def exists(self, name):
        return bool(name.upper() in self._openchatbots)

    def openchatbot(self, name):
        if self.exists(name):
            return self._openchatbots[name.upper()]

        return self._domain_handler.get_endpoint(name)
Esempio n. 2
0
    def test_construction_with_no_scan_alternatives(self):

        mock_requests = MockHTTPRequests(response="""{
                       "openchatbot": {
                           "endpoint": "api/mybot/v1.0/ask",
                           "host": "https://website1.com",
                           "port": 80,
                           "methods": ["GET", "POST"]
                       }
                    }""")

        handler = OpenChatBotDomainHandler(scan_alternatives=False, http_requests=mock_requests)
        self.assertIsNotNone(handler)

        domaincb = handler.get_endpoint("website1", https=False)
        self.assertIsNotNone(domaincb)

        self.assertEquals("https://website1.com:80/api/mybot/v1.0/ask", domaincb.url)
Esempio n. 3
0
    def test_construction_with_scan_alternatives_org(self):

        mock_requests = MockHTTPRequests(response="""{
                       "openchatbot": {
                           "endpoint": "api/mybot/v1.0/ask",
                           "host": "https://website1.com",
                           "port": 80,
                           "methods": ["GET", "POST"]
                       }
                    }""",
                    valid_domain=".org")

        config = BrainOpenChatBotsConfiguration()
        config._domains = ['org']
        handler = OpenChatBotDomainHandler(config, http_requests=mock_requests)
        self.assertIsNotNone(handler)

        domaincb = handler.get_endpoint("website1")
        self.assertIsNotNone(domaincb)

        self.assertEquals("https://website1.com:80/api/mybot/v1.0/ask", domaincb.url)