# Path: scraper/http.py
# class Http:
# 
#     def __init__(self):
#         pass
# 
#     def get(self, url, number_attempts=_STD_NUMBER_ATTEMPTS, initial_sleep_period=_STD_INITIAL_SLEEP_PERIOD):
#         attempt = 1
#         sleep_period = initial_sleep_period
#         while attempt <= number_attempts:
#             gevent.sleep(sleep_period)
#             try:
#                 request = grequests.get(url)
#                 grequests.map([request])
#                 if request.response is not None:
#                     if request.response.status_code == requests.codes.ok:
#                         return True, request.response.text
#                 else:
#                     return False, BAD_URL_NETWORK_PROBLEM
#             except requests.exceptions.RequestException:
#                 return False, BAD_URL_NETWORK_PROBLEM
#             sleep_period = _get_next_sleep_period(sleep_period, attempt)
#             attempt += 1
#         return False, {'status-code': request.response.status_code}
# 
# COOK_COUNTY_JAIL_INMATE_DETAILS_URL = \
#     'http://www2.cookcountysheriff.org/search2/details.asp?jailnumber='
# 
# BAD_URL_NETWORK_PROBLEM = 'Bad url or network problem.'


import httpretty
from random import randint
from scraper.http import Http, COOK_COUNTY_JAIL_INMATE_DETAILS_URL, BAD_URL_NETWORK_PROBLEM
        ccj_api_requests = {'succeed-attempt': randint(1, number_of_attempts), 'current-attempt': 0}

        def fulfill_ccj_api_request(_, uri, headers):
            assert uri == INMATE_URL
            ccj_api_requests['current-attempt'] += 1
            if ccj_api_requests['current-attempt'] == ccj_api_requests['succeed-attempt']:
                return 200, headers, expected_text
            return 500, headers, 'did not work'

        httpretty.register_uri(httpretty.GET, COOK_COUNTY_JAIL_INMATE_DETAILS_URL,
                               body=fulfill_ccj_api_request)

        http = Http()
        okay, fetched_contents = http.get(INMATE_URL, number_of_attempts)

        assert okay
        assert ccj_api_requests['current-attempt'] == ccj_api_requests['succeed-attempt']
        assert fetched_contents == expected_text

    @httpretty.activate
    def test_get_fails_500(self):
        number_of_attempts = 2
        expected_text = 'did not work'
        ccj_api_requests = {'succeed-attempt': number_of_attempts, 'current-attempt': 0}

        def fulfill_ccj_api_request(_, uri, headers):
            assert uri == INMATE_URL
            ccj_api_requests['current-attempt'] += 1
            return 500, headers, expected_text


# Path: scraper/http.py
# class Http:
# 
#     def __init__(self):
#         pass
# 
#     def get(self, url, number_attempts=_STD_NUMBER_ATTEMPTS, initial_sleep_period=_STD_INITIAL_SLEEP_PERIOD):
#         attempt = 1
#         sleep_period = initial_sleep_period
#         while attempt <= number_attempts:
#             gevent.sleep(sleep_period)
#             try:
#                 request = grequests.get(url)
#                 grequests.map([request])
#                 if request.response is not None:
#                     if request.response.status_code == requests.codes.ok:
#                         return True, request.response.text
#                 else:
#                     return False, BAD_URL_NETWORK_PROBLEM
#             except requests.exceptions.RequestException:
#                 return False, BAD_URL_NETWORK_PROBLEM
#             sleep_period = _get_next_sleep_period(sleep_period, attempt)
#             attempt += 1
#         return False, {'status-code': request.response.status_code}
# 
# COOK_COUNTY_JAIL_INMATE_DETAILS_URL = \
#     'http://www2.cookcountysheriff.org/search2/details.asp?jailnumber='
# 
# BAD_URL_NETWORK_PROBLEM = 'Bad url or network problem.'


# Path: tests/scraper/test_http.py
import httpretty
from random import randint
from scraper.http import Http, COOK_COUNTY_JAIL_INMATE_DETAILS_URL, BAD_URL_NETWORK_PROBLEM

        ccj_api_requests = {'succeed-attempt': randint(1, number_of_attempts), 'current-attempt': 0}

        def fulfill_ccj_api_request(_, uri, headers):
            assert uri == INMATE_URL
            ccj_api_requests['current-attempt'] += 1
            if ccj_api_requests['current-attempt'] == ccj_api_requests['succeed-attempt']:
                return 200, headers, expected_text
            return 500, headers, 'did not work'

        httpretty.register_uri(httpretty.GET, COOK_COUNTY_JAIL_INMATE_DETAILS_URL,
                               body=fulfill_ccj_api_request)

        http = Http()
        okay, fetched_contents = http.get(INMATE_URL, number_of_attempts)

        assert okay
        assert ccj_api_requests['current-attempt'] == ccj_api_requests['succeed-attempt']
        assert fetched_contents == expected_text

    @httpretty.activate
    def test_get_fails_500(self):
        number_of_attempts = 2
        expected_text = 'did not work'
        ccj_api_requests = {'succeed-attempt': number_of_attempts, 'current-attempt': 0}

        def fulfill_ccj_api_request(_, uri, headers):
            assert uri == INMATE_URL
            ccj_api_requests['current-attempt'] += 1
            return 500, headers, expected_text


        httpretty.register_uri(httpretty.GET, COOK_COUNTY_JAIL_INMATE_DETAILS_URL,