# Path: aioconsul/common/addr.py
# def parse_addr(addr, *, proto=None, host=None):
#     """Parses an address
# 
#     Returns:
#         Address: the parsed address
#     """
#     port = None
# 
#     if isinstance(addr, Address):
#         return addr
#     elif isinstance(addr, str):
#         if addr.startswith('http://'):
#             proto, addr = 'http', addr[7:]
#         if addr.startswith('udp://'):
#             proto, addr = 'udp', addr[6:]
#         elif addr.startswith('tcp://'):
#             proto, addr = 'tcp', addr[6:]
#         elif addr.startswith('unix://'):
#             proto, addr = 'unix', addr[7:]
#         a, _, b = addr.partition(':')
#         host = a or host
#         port = b or port
#     elif isinstance(addr, (tuple, list)):
#         # list is not good
#         a, b = addr
#         host = a or host
#         port = b or port
#     elif isinstance(addr, int):
#         port = addr
#     else:
#         raise ValueError('bad value')
# 
#     if port is not None:
#         port = int(port)
#     return Address(proto, host, port)
# 
# Path: aioconsul/common/objs.py
# class Response:
# 
#     def __init__(self, path, status, body, headers, method):
#         self.path = path
#         self.status = status
#         self.body = body
#         self.headers = headers
#         self.method = method
# 
#     def __repr__(self):
#         return "<%s(method=%r, path=%r, status=%r, body=%r, headers=%r)>" % (
#             self.__class__.__name__,
#             self.method,
#             self.path,
#             self.status,
#             self.body,
#             self.headers
#         )


import aiohttp
import asyncio
import json
from .addr import parse_addr
from .objs import Response


class RequestHandler:

    def __init__(self, address, *, loop=None):
        self.address = parse_addr(address, proto="http", host="localhost")
        self.loop = loop or asyncio.get_event_loop()
        self.session = aiohttp.ClientSession(loop=self.loop)
        self.json_loader = json.loads

    async def request(self, method, path, **kwargs):
        url = "%s/%s" % (self.address.__str__(), path.lstrip("/"))
        async with self.session.request(method, url, **kwargs) as response:
            if response.headers.get("Content-Type") == "application/json":
                body = await response.json(encoding="utf-8",
                                           loads=self.json_loader)
            else:
                body = await response.read()

# Path: aioconsul/common/addr.py
# def parse_addr(addr, *, proto=None, host=None):
#     """Parses an address
# 
#     Returns:
#         Address: the parsed address
#     """
#     port = None
# 
#     if isinstance(addr, Address):
#         return addr
#     elif isinstance(addr, str):
#         if addr.startswith('http://'):
#             proto, addr = 'http', addr[7:]
#         if addr.startswith('udp://'):
#             proto, addr = 'udp', addr[6:]
#         elif addr.startswith('tcp://'):
#             proto, addr = 'tcp', addr[6:]
#         elif addr.startswith('unix://'):
#             proto, addr = 'unix', addr[7:]
#         a, _, b = addr.partition(':')
#         host = a or host
#         port = b or port
#     elif isinstance(addr, (tuple, list)):
#         # list is not good
#         a, b = addr
#         host = a or host
#         port = b or port
#     elif isinstance(addr, int):
#         port = addr
#     else:
#         raise ValueError('bad value')
# 
#     if port is not None:
#         port = int(port)
#     return Address(proto, host, port)
# 
# Path: aioconsul/common/objs.py
# class Response:
# 
#     def __init__(self, path, status, body, headers, method):
#         self.path = path
#         self.status = status
#         self.body = body
#         self.headers = headers
#         self.method = method
# 
#     def __repr__(self):
#         return "<%s(method=%r, path=%r, status=%r, body=%r, headers=%r)>" % (
#             self.__class__.__name__,
#             self.method,
#             self.path,
#             self.status,
#             self.body,
#             self.headers
#         )


# Path: aioconsul/common/req.py
import aiohttp
import asyncio
import json
from .addr import parse_addr
from .objs import Response



class RequestHandler:

    def __init__(self, address, *, loop=None):
        self.address = parse_addr(address, proto="http", host="localhost")
        self.loop = loop or asyncio.get_event_loop()
        self.session = aiohttp.ClientSession(loop=self.loop)
        self.json_loader = json.loads

    async def request(self, method, path, **kwargs):
        url = "%s/%s" % (self.address.__str__(), path.lstrip("/"))
        async with self.session.request(method, url, **kwargs) as response:
            if response.headers.get("Content-Type") == "application/json":
                body = await response.json(encoding="utf-8",
                                           loads=self.json_loader)
            else:
                body = await response.read()

            return Response(path=path,