# Path: performanceplatform/collector/logging_setup.py
# def extra_fields_from_exception(exception):
#     """Exception into a dict to be passed to logger
# 
#     Standardise the way we send exception messages so they can be filtered in
#     kibana, also make it easy to add extra information in future.
#     """
#     return {
#         'exception_class': type(exception).__name__,
#         'exception_message': exception.message
#     }


import logging
import time
from performanceplatform.collector.logging_setup import (
    extra_fields_from_exception)
from requests import *

# import everything from requests so that other code can import this package
# instead of requests

_MAX_RETRIES = 5


def get(url, *args, **kwargs):
    return __request_with_backoff('GET', url, *args, **kwargs)


def post(url, *args, **kwargs):
    return __request_with_backoff('POST', url, *args, **kwargs)


def __request_with_backoff(method, url, *args, **kwargs):
    delay = 10

    for n in range(_MAX_RETRIES):
        response = request(method, url, *args, **kwargs)
        code = response.status_code
        if code in [403, 502, 503]:
            retry_info = ('{} request for {} failed with code {} '
                          '(Attempt {} of {}).'.format(method, url, code,
                                                       n + 1, _MAX_RETRIES))
            if n + 1 < _MAX_RETRIES:
                retry_info += ' Retrying in {} seconds...'.format(delay)
            logging.info(retry_info)

# Path: performanceplatform/collector/logging_setup.py
# def extra_fields_from_exception(exception):
#     """Exception into a dict to be passed to logger
# 
#     Standardise the way we send exception messages so they can be filtered in
#     kibana, also make it easy to add extra information in future.
#     """
#     return {
#         'exception_class': type(exception).__name__,
#         'exception_message': exception.message
#     }


# Path: performanceplatform/utils/requests_with_backoff.py
import logging
import time
from performanceplatform.collector.logging_setup import (
    extra_fields_from_exception)
from requests import *


# import everything from requests so that other code can import this package
# instead of requests

_MAX_RETRIES = 5


def get(url, *args, **kwargs):
    return __request_with_backoff('GET', url, *args, **kwargs)


def post(url, *args, **kwargs):
    return __request_with_backoff('POST', url, *args, **kwargs)


def __request_with_backoff(method, url, *args, **kwargs):
    delay = 10

    for n in range(_MAX_RETRIES):
        response = request(method, url, *args, **kwargs)
        code = response.status_code
        if code in [403, 502, 503]:
            retry_info = ('{} request for {} failed with code {} '
                          '(Attempt {} of {}).'.format(method, url, code,
                                                       n + 1, _MAX_RETRIES))
            if n + 1 < _MAX_RETRIES:
                retry_info += ' Retrying in {} seconds...'.format(delay)
            logging.info(retry_info)

            time.sleep(delay)