# -*- coding: utf-8 -*-

from requests import Session
from requests.adapters import HTTPAdapter
from functools import partial

from blackgate.executor_pools  import ExecutorPools
from blackgate.circuit_beaker import NoCircuitBeaker, InProcessCircuitBeaker, get_circuit_beaker

class Component(object):

    def __init__(self):
        self.session = Session()
        self.urls = []
        self.pools = ExecutorPools()
        self.circuit_beakers = {}
        self.circuit_beaker_impl = NoCircuitBeaker
        self.circuit_beaker_options = {}
        self.get_circuit_beaker = partial(
            get_circuit_beaker,
            self.circuit_beakers,
        )
        self.configurations = {}
        self.configurations.setdefault('urls', [])

    @property
    def config(self):
        return self.configurations

    def set(self, key, value):
        self.configurations[key] = value

    def add(self, key, value):
        self.configurations.setdefault(key, [])
        if key in self.configurations:
            assert isinstance(self.configurations[key], list)
        self.configurations[key].append(value)

    def delete(self, key):
        del self.configurations[key]

    def install(self):
        self.install_executor_pool()
        self.install_circuit_beaker()
        self.install_session()
        self.install_tornado_urls()

    def install_executor_pool(self):
        for proxy in self.configurations.get('proxies', []):
            max_workers = proxy.get('pool_max_workers') or 10
            self.pools.register_pool(proxy['name'], max_workers)

    def install_circuit_beaker(self):
        if 'circuit_beaker_enabled' in self.configurations:
            self.circuit_beaker_impl = NoCircuitBeaker
            self.circuit_beaker_options = {}
        elif 'circuit_beaker_impl' not in self.configurations:
            self.circuit_beaker_impl = InProcessCircuitBeaker
            self.circuit_beaker_options = {'metrics': None} # FIXME
        else:
            # FIXME: add definition of import_string
            self.circuit_beaker_impl = import_string(self.configurations['circuit_beaker_impl'])
            self.circuit_beaker_options = self.configurations.get('circuit_beaker_options') or {}

    def install_session(self):
        for proxy in self.configurations.get('proxies', []):
            pool_connections = proxy.get('pool_max_workers') or 10
            pool_maxsize = proxy.get('pool_max_queue_size') or 10
            max_retries = proxy.get('pool_max_retries') or 0
            pool_block = proxy.get('pool_block') or False
            prefix = proxy['upstream_url']
            self.session.mount(prefix, HTTPAdapter(
                pool_connections=pool_connections,
                pool_maxsize=pool_maxsize,
                max_retries=max_retries,
                pool_block=pool_block,
            ))

    def install_tornado_urls(self):
        from blackgate.http_proxy import HTTPProxy
        from blackgate.command import Command

        for proxy in self.configurations.get('proxies', []):
            route = [
                proxy['request_path_regex'],
                HTTPProxy,
                dict(proxy=proxy),
            ]
            self.urls.append(route)

@@ -22,79 +22,8 @@
 *-%0A%0A
-from requests import Session%0Afrom requests.adapters import HTTPAdapter%0A
 from
@@ -48,16 +48,16 @@
 partial%0A
+
 %0Afrom bl
@@ -253,41 +253,8 @@
 f):%0A
-        self.session = Session()%0A
     
@@ -1179,39 +1179,8 @@
 r()%0A
-        self.install_session()%0A
     
@@ -1993,16 +1993,16 @@
 impl'%5D)%0A
+
         
@@ -2096,637 +2096,8 @@
 %7B%7D%0A%0A
-    def install_session(self):%0A        for proxy in self.configurations.get('proxies', %5B%5D):%0A            pool_connections = proxy.get('pool_max_workers') or 10%0A            pool_maxsize = proxy.get('pool_max_queue_size') or 10%0A            max_retries = proxy.get('pool_max_retries') or 0%0A            pool_block = proxy.get('pool_block') or False%0A            prefix = proxy%5B'upstream_url'%5D%0A            self.session.mount(prefix, HTTPAdapter(%0A                pool_connections=pool_connections,%0A                pool_maxsize=pool_maxsize,%0A                max_retries=max_retries,%0A                pool_block=pool_block,%0A            ))%0A%0A
     

Remove requests in component.