# Path: netman/api/switch_api_base.py
# class SwitchApiBase(object):
#     def __init__(self, switch_factory, sessions_manager):
#         self.switch_factory = switch_factory
#         self.sessions_manager = sessions_manager
# 
#     def hook_to(self, server):
#         raise NotImplemented()
# 
#     @property
#     def logger(self):
#         return logging.getLogger(__name__)
# 
#     def _get_switch_descriptor_from_request_headers(self, hostname):
#         headers_present = [h in request.headers for h in ['Netman-Model', 'Netman-Username', 'Netman-Password']]
# 
#         if not any(headers_present):
#             return None
# 
#         if not all(headers_present):
#             raise BadRequest('For anonymous switch usage, please specify headers: Netman-Model, Netman-Username and Netman-Password.')
# 
#         port = None
#         if "Netman-Port" in request.headers:
#             try:
#                 port = int(request.headers["Netman-Port"])
#             except ValueError:
#                 raise BadRequest('Netman-Port optional header should be an integer')
# 
#         netman_server = request.headers.get("Netman-Proxy-Server", None)
#         if netman_server is not None and "," in netman_server:
#             netman_server = [e.strip() for e in netman_server.split(",")]
# 
#         self.logger.info("Anonymous Switch Access ({}) {}@{}".format(request.headers['Netman-Model'], request.headers['Netman-Username'], hostname))
#         return SwitchDescriptor(
#             hostname=hostname,
#             model=request.headers['Netman-Model'],
#             username=request.headers['Netman-Username'],
#             password=request.headers['Netman-Password'],
#             port=port,
#             netman_server=netman_server
#         )
# 
#     def resolve_switch(self, hostname):
#         switch_descriptor = self._get_switch_descriptor_from_request_headers(hostname)
# 
#         if switch_descriptor:
#             return self.switch_factory.get_switch_by_descriptor(switch_descriptor)
#         return self.switch_factory.get_switch(hostname)
# 
#     def resolve_session(self, session_id):
#         return self.sessions_manager.get_switch_for_session(session_id)
# 
# Path: netman/api/validators.py
# def resource(*validators):
# 
#     def resource_decorator(fn):
#         @wraps(fn)
#         def wrapper(self, **kwargs):
#             with MultiContext(self, kwargs, *validators) as ctxs:
#                 return fn(self, *ctxs, **kwargs)
# 
#         return wrapper
# 
#     return resource_decorator
# 
# def content(validator_fn):
# 
#     def content_decorator(fn):
#         @wraps(fn)
#         def wrapper(*args, **kwargs):
#             kwargs.update(validator_fn(request.data))
#             return fn(*args, **kwargs)
# 
#         return wrapper
# 
#     return content_decorator
# 
# class Session:
#     def __init__(self, switch_api):
#         self.switch_api = switch_api
#         self.session = None
# 
#     def process(self, parameters):
#         self.session = parameters.pop('session_id')
#         self.switch_api.resolve_session(self.session)
# 
#     def __enter__(self):
#         return self.session
# 
#     def __exit__(self, *_):
#         pass
# 
# class Resource:
#     def __init__(self, switch_api):
#         self.switch_api = switch_api
#         self.resource = None
# 
#     def process(self, parameters):
#         self.resource = parameters.pop('resource')
# 
#     def __enter__(self):
#         return self.resource
# 
#     def __exit__(self, *_):
#         pass
# 
# def is_session(data, **_):
#     try:
#         json_data = json.loads(data)
#     except ValueError:
#         raise BadRequest("Malformed content, should be a JSON object")
# 
#     if "hostname" not in json_data:
#         raise MalformedSwitchSessionRequest()
#     return {
#         'hostname': json_data["hostname"]
#     }


from flask import request
from netman.api.api_utils import BadRequest, to_response
from netman.api.switch_api_base import SwitchApiBase
from netman.api.validators import resource, content, Session, \
    Resource, is_session

        :code 201 CREATED:

        Example output:

        .. literalinclude:: ../doc_config/api_samples/post_switch_session_result.json
            :language: json

        """

        switch = self.resolve_switch(hostname)
        session_id = self.sessions_manager.open_session(switch, session_id)

        return 201, {'session_id': session_id}

    @to_response
    @resource(Session)
    def close_session(self, session_id):
        """
        Close a session on a switch

        :arg str session: ID of the session

        """

        self.sessions_manager.close_session(session_id)

        return 204, None

    @to_response

# Path: netman/api/switch_api_base.py
# class SwitchApiBase(object):
#     def __init__(self, switch_factory, sessions_manager):
#         self.switch_factory = switch_factory
#         self.sessions_manager = sessions_manager
# 
#     def hook_to(self, server):
#         raise NotImplemented()
# 
#     @property
#     def logger(self):
#         return logging.getLogger(__name__)
# 
#     def _get_switch_descriptor_from_request_headers(self, hostname):
#         headers_present = [h in request.headers for h in ['Netman-Model', 'Netman-Username', 'Netman-Password']]
# 
#         if not any(headers_present):
#             return None
# 
#         if not all(headers_present):
#             raise BadRequest('For anonymous switch usage, please specify headers: Netman-Model, Netman-Username and Netman-Password.')
# 
#         port = None
#         if "Netman-Port" in request.headers:
#             try:
#                 port = int(request.headers["Netman-Port"])
#             except ValueError:
#                 raise BadRequest('Netman-Port optional header should be an integer')
# 
#         netman_server = request.headers.get("Netman-Proxy-Server", None)
#         if netman_server is not None and "," in netman_server:
#             netman_server = [e.strip() for e in netman_server.split(",")]
# 
#         self.logger.info("Anonymous Switch Access ({}) {}@{}".format(request.headers['Netman-Model'], request.headers['Netman-Username'], hostname))
#         return SwitchDescriptor(
#             hostname=hostname,
#             model=request.headers['Netman-Model'],
#             username=request.headers['Netman-Username'],
#             password=request.headers['Netman-Password'],
#             port=port,
#             netman_server=netman_server
#         )
# 
#     def resolve_switch(self, hostname):
#         switch_descriptor = self._get_switch_descriptor_from_request_headers(hostname)
# 
#         if switch_descriptor:
#             return self.switch_factory.get_switch_by_descriptor(switch_descriptor)
#         return self.switch_factory.get_switch(hostname)
# 
#     def resolve_session(self, session_id):
#         return self.sessions_manager.get_switch_for_session(session_id)
# 
# Path: netman/api/validators.py
# def resource(*validators):
# 
#     def resource_decorator(fn):
#         @wraps(fn)
#         def wrapper(self, **kwargs):
#             with MultiContext(self, kwargs, *validators) as ctxs:
#                 return fn(self, *ctxs, **kwargs)
# 
#         return wrapper
# 
#     return resource_decorator
# 
# def content(validator_fn):
# 
#     def content_decorator(fn):
#         @wraps(fn)
#         def wrapper(*args, **kwargs):
#             kwargs.update(validator_fn(request.data))
#             return fn(*args, **kwargs)
# 
#         return wrapper
# 
#     return content_decorator
# 
# class Session:
#     def __init__(self, switch_api):
#         self.switch_api = switch_api
#         self.session = None
# 
#     def process(self, parameters):
#         self.session = parameters.pop('session_id')
#         self.switch_api.resolve_session(self.session)
# 
#     def __enter__(self):
#         return self.session
# 
#     def __exit__(self, *_):
#         pass
# 
# class Resource:
#     def __init__(self, switch_api):
#         self.switch_api = switch_api
#         self.resource = None
# 
#     def process(self, parameters):
#         self.resource = parameters.pop('resource')
# 
#     def __enter__(self):
#         return self.resource
# 
#     def __exit__(self, *_):
#         pass
# 
# def is_session(data, **_):
#     try:
#         json_data = json.loads(data)
#     except ValueError:
#         raise BadRequest("Malformed content, should be a JSON object")
# 
#     if "hostname" not in json_data:
#         raise MalformedSwitchSessionRequest()
#     return {
#         'hostname': json_data["hostname"]
#     }


# Path: netman/api/switch_session_api.py
from flask import request
from netman.api.api_utils import BadRequest, to_response
from netman.api.switch_api_base import SwitchApiBase
from netman.api.validators import resource, content, Session, \
    Resource, is_session


        :code 201 CREATED:

        Example output:

        .. literalinclude:: ../doc_config/api_samples/post_switch_session_result.json
            :language: json

        """

        switch = self.resolve_switch(hostname)
        session_id = self.sessions_manager.open_session(switch, session_id)

        return 201, {'session_id': session_id}

    @to_response
    @resource(Session)
    def close_session(self, session_id):
        """
        Close a session on a switch

        :arg str session: ID of the session

        """

        self.sessions_manager.close_session(session_id)

        return 204, None

    @to_response

    @resource(Session, Resource)