# Path: aredis/connection.py
# class Connection(BaseConnection):
#     description = 'Connection<host={host},port={port},db={db}>'
# 
#     def __init__(self, host='127.0.0.1', port=6379, password=None,
#                  db=0, retry_on_timeout=False, stream_timeout=None, connect_timeout=None,
#                  ssl_context=None, parser_class=DefaultParser, reader_read_size=65535,
#                  encoding='utf-8', decode_responses=False, socket_keepalive=None,
#                  socket_keepalive_options=None, *, loop=None):
#         super(Connection, self).__init__(retry_on_timeout, stream_timeout,
#                                          parser_class, reader_read_size,
#                                          encoding, decode_responses,
#                                          loop=loop)
#         self.host = host
#         self.port = port
#         self.password = password
#         self.db = db
#         self.ssl_context = ssl_context
#         self._connect_timeout = connect_timeout
#         self._description_args = {
#             'host': self.host,
#             'port': self.port,
#             'db': self.db
#         }
#         self.socket_keepalive = socket_keepalive
#         self.socket_keepalive_options = socket_keepalive_options or {}
# 
#     async def _connect(self):
#         reader, writer = await exec_with_timeout(
#             asyncio.open_connection(host=self.host,
#                                     port=self.port,
#                                     ssl=self.ssl_context,
#                                     loop=self.loop),
#             self._connect_timeout,
#             loop=self.loop
#         )
#         self._reader = reader
#         self._writer = writer
#         sock = writer.transport.get_extra_info('socket')
#         if sock is not None:
#             sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
#             try:
#                 # TCP_KEEPALIVE
#                 if self.socket_keepalive:
#                     sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
#                     for k, v in self.socket_keepalive_options.items():
#                         sock.setsockopt(socket.SOL_TCP, k, v)
#             except (socket.error, TypeError):
#                 # `socket_keepalive_options` might contain invalid options
#                 # causing an error. Do not leave the connection open.
#                 writer.close()
#                 raise
#         await self.on_connect()


from aredis import Connection
import asyncio
"""
`client reply off | on | skip` is hard to be supported by aredis gracefully because of the client pool usage.
The client is supposed to read response from server and release connection after the command being sent.
But the connection is needed to be always reused if you need to turn on | off | skip the reply,
it should always be the connection by which you send `client reply` command to server you use to send the rest commands.

However, you can use the connection by your self like the example below~
"""



async def skip():
    print('skip response example::')
    conn = Connection(host='127.0.0.1', port=6379)
    await conn.send_command('flushdb')
    print(await conn.read_response())
    await conn.send_command('CLIENT REPLY', 'SKIP')
    await conn.send_command('SET', 'lalala', 1)
    await conn.send_command('SET', 'lalala', 2)
    print(await conn.read_response())

async def off_and_on():
    print('turn off response and then turn it ')

# Path: aredis/connection.py
# class Connection(BaseConnection):
#     description = 'Connection<host={host},port={port},db={db}>'
# 
#     def __init__(self, host='127.0.0.1', port=6379, password=None,
#                  db=0, retry_on_timeout=False, stream_timeout=None, connect_timeout=None,
#                  ssl_context=None, parser_class=DefaultParser, reader_read_size=65535,
#                  encoding='utf-8', decode_responses=False, socket_keepalive=None,
#                  socket_keepalive_options=None, *, loop=None):
#         super(Connection, self).__init__(retry_on_timeout, stream_timeout,
#                                          parser_class, reader_read_size,
#                                          encoding, decode_responses,
#                                          loop=loop)
#         self.host = host
#         self.port = port
#         self.password = password
#         self.db = db
#         self.ssl_context = ssl_context
#         self._connect_timeout = connect_timeout
#         self._description_args = {
#             'host': self.host,
#             'port': self.port,
#             'db': self.db
#         }
#         self.socket_keepalive = socket_keepalive
#         self.socket_keepalive_options = socket_keepalive_options or {}
# 
#     async def _connect(self):
#         reader, writer = await exec_with_timeout(
#             asyncio.open_connection(host=self.host,
#                                     port=self.port,
#                                     ssl=self.ssl_context,
#                                     loop=self.loop),
#             self._connect_timeout,
#             loop=self.loop
#         )
#         self._reader = reader
#         self._writer = writer
#         sock = writer.transport.get_extra_info('socket')
#         if sock is not None:
#             sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
#             try:
#                 # TCP_KEEPALIVE
#                 if self.socket_keepalive:
#                     sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
#                     for k, v in self.socket_keepalive_options.items():
#                         sock.setsockopt(socket.SOL_TCP, k, v)
#             except (socket.error, TypeError):
#                 # `socket_keepalive_options` might contain invalid options
#                 # causing an error. Do not leave the connection open.
#                 writer.close()
#                 raise
#         await self.on_connect()


# Path: examples/client_reply.py
from aredis import Connection
import asyncio

"""
`client reply off | on | skip` is hard to be supported by aredis gracefully because of the client pool usage.
The client is supposed to read response from server and release connection after the command being sent.
But the connection is needed to be always reused if you need to turn on | off | skip the reply,
it should always be the connection by which you send `client reply` command to server you use to send the rest commands.

However, you can use the connection by your self like the example below~
"""



async def skip():
    print('skip response example::')
    conn = Connection(host='127.0.0.1', port=6379)
    await conn.send_command('flushdb')
    print(await conn.read_response())
    await conn.send_command('CLIENT REPLY', 'SKIP')
    await conn.send_command('SET', 'lalala', 1)
    await conn.send_command('SET', 'lalala', 2)
    print(await conn.read_response())

async def off_and_on():
    print('turn off response and then turn it ')

    conn = Connection()