# Path: sevenbridges/http/error_handlers.py
# @repeatable_handler
# def rate_limit_sleeper(api, response):
#     """
#     Pauses the execution if rate limit is breached.
#     :param api: Api instance.
#     :param response: requests.Response object
# 
#     """
#     while response.status_code == 429:
#         headers = response.headers
#         remaining_time = headers.get('X-RateLimit-Reset')
#         sleep = max(int(remaining_time) - int(time.time()), 0)
# 
#         logger.warning('Rate limit reached! Waiting for [%s]s', sleep)
#         time.sleep(sleep)
# 
#         response = api.session.send(response.request)
#     return response
# 
# @repeatable_handler
# def maintenance_sleeper(api, response, sleep=300):
#     """
#     Pauses the execution if sevenbridges api is under maintenance.
#     :param api: Api instance.
#     :param response: requests.Response object.
#     :param sleep: Time to sleep in between the requests.
#     """
#     while response.status_code == 503:
#         logger.info(
#             'Service unavailable: Response=[%s]',
#             response.__dict__
#         )
#         response_body = response.json()
#         if 'code' in response_body:
#             if response_body['code'] == 0:
#                 logger.warning(
#                     'API Maintenance in progress! Waiting for [%s]s',
#                     sleep
#                 )
#                 time.sleep(sleep)
#                 response = api.session.send(response.request)
#             else:
#                 return response
#         else:
#             return response
#     return response
# 
# @repeatable_handler
# def general_error_sleeper(api, response, sleep=300):
#     """
#     Pauses the execution if response status code is > 500.
#     :param api: Api instance.
#     :param response: requests.Response object
#     :param sleep: Time to sleep in between the requests.
# 
#     """
#     while response.status_code >= 500:
#         logger.warning(
#             'Caught [%s] status code! Waiting for [%s]s',
#             response.status_code,
#             sleep
#         )
#         time.sleep(sleep)
#         response = api.session.send(response.request)
#     return response


import time
import faker
import pytest
import requests
from json import JSONDecodeError
from sevenbridges.http.error_handlers import (
    rate_limit_sleeper, maintenance_sleeper, general_error_sleeper)
    resp429.headers = {
        'X-RateLimit-Reset': time.time() + 1
    }
    resp429.status_code = 429
    resp200 = requests.Response()
    resp200.status_code = 200

    api._session = MockSession([resp429, resp200])
    resp = rate_limit_sleeper(api, resp429)

    assert resp.status_code == resp200.status_code


def test_maintenance_sleeper_invalid_json(api):
    resp503 = requests.Response()
    resp503.status_code = 503

    api._session = MockSession([resp503])
    with pytest.raises(JSONDecodeError):
        maintenance_sleeper(api, resp503, 1)


def test_maintenance_sleeper(api):
    resp503 = requests.Response()
    resp503.status_code = 503
    resp503._content = b'{"code": 0}'
    resp200 = requests.Response()
    resp200.status_code = 200

    api._session = MockSession([resp503, resp200])

# Path: sevenbridges/http/error_handlers.py
# @repeatable_handler
# def rate_limit_sleeper(api, response):
#     """
#     Pauses the execution if rate limit is breached.
#     :param api: Api instance.
#     :param response: requests.Response object
# 
#     """
#     while response.status_code == 429:
#         headers = response.headers
#         remaining_time = headers.get('X-RateLimit-Reset')
#         sleep = max(int(remaining_time) - int(time.time()), 0)
# 
#         logger.warning('Rate limit reached! Waiting for [%s]s', sleep)
#         time.sleep(sleep)
# 
#         response = api.session.send(response.request)
#     return response
# 
# @repeatable_handler
# def maintenance_sleeper(api, response, sleep=300):
#     """
#     Pauses the execution if sevenbridges api is under maintenance.
#     :param api: Api instance.
#     :param response: requests.Response object.
#     :param sleep: Time to sleep in between the requests.
#     """
#     while response.status_code == 503:
#         logger.info(
#             'Service unavailable: Response=[%s]',
#             response.__dict__
#         )
#         response_body = response.json()
#         if 'code' in response_body:
#             if response_body['code'] == 0:
#                 logger.warning(
#                     'API Maintenance in progress! Waiting for [%s]s',
#                     sleep
#                 )
#                 time.sleep(sleep)
#                 response = api.session.send(response.request)
#             else:
#                 return response
#         else:
#             return response
#     return response
# 
# @repeatable_handler
# def general_error_sleeper(api, response, sleep=300):
#     """
#     Pauses the execution if response status code is > 500.
#     :param api: Api instance.
#     :param response: requests.Response object
#     :param sleep: Time to sleep in between the requests.
# 
#     """
#     while response.status_code >= 500:
#         logger.warning(
#             'Caught [%s] status code! Waiting for [%s]s',
#             response.status_code,
#             sleep
#         )
#         time.sleep(sleep)
#         response = api.session.send(response.request)
#     return response


# Path: tests/test_error_handlers.py
import time
import faker
import pytest
import requests
from json import JSONDecodeError
from sevenbridges.http.error_handlers import (
    rate_limit_sleeper, maintenance_sleeper, general_error_sleeper)

    resp429.headers = {
        'X-RateLimit-Reset': time.time() + 1
    }
    resp429.status_code = 429
    resp200 = requests.Response()
    resp200.status_code = 200

    api._session = MockSession([resp429, resp200])
    resp = rate_limit_sleeper(api, resp429)

    assert resp.status_code == resp200.status_code


def test_maintenance_sleeper_invalid_json(api):
    resp503 = requests.Response()
    resp503.status_code = 503

    api._session = MockSession([resp503])
    with pytest.raises(JSONDecodeError):
        maintenance_sleeper(api, resp503, 1)


def test_maintenance_sleeper(api):
    resp503 = requests.Response()
    resp503.status_code = 503
    resp503._content = b'{"code": 0}'
    resp200 = requests.Response()
    resp200.status_code = 200

    api._session = MockSession([resp503, resp200])

    resp = maintenance_sleeper(api, resp503, 1)