# Path: yams_api/plugins/dev/api_utils/http_methods.py
# def http_requests_get_to_dict(url, headers={}):
#     """Fetch content at a URL, returning a namedtuple of python-request module attributes
# 
#     :param url: url to scrape
#     :param headers: dict of headers to use for the request
#     :return: a namedtuple containing ('headers', 'text', 'status') for Pythonic scripting
#     """
# 
#     # Keep these outside of the try/except so we can potentially get metadata even on exceptions.
#     # Defines some placeholders to avoid a potential NameError
#     _headers = dict()
#     _status = 200
#     _status_major_code = 0
# 
#     try:
#         r = requests.get(url, headers=headers)
#         # r.content is a bytes obj and probably won't be json serializable.  could return a tuple
#         _status = r.status_code
#         _str_status = str(_status)
#         _status_major_code = int(_str_status[0]) # '200' -> '2' ->2
# 
#         rh = r.headers
#         rt = r.text
# 
#         # common headers
#         _headers["status_code"] = _status
#         _headers["etag"] = rh.get("etag", "")
#         _headers["date"] = rh.get("date", "")
#         _headers["last-modified"] = rh.get("last-modified", "")
#         _headers["content-type"] = rh.get("content-type", "")
#         _headers["raw_headers"] = dict(rh)
# 
#         _http_namedtuple = HTTPResponse(
#             _headers,
#             rt,
#             _status,
#             (_status_major_code == 2),
#             _status_major_code,
#             False
#         )
# 
#     except ConnectionError as e:
#         _headers["response"] = str(e)
# 
#         _http_namedtuple = HTTPResponse(
#             _headers,
#             str(e),
#             _status,
#             (_status_major_code == 2),
#             _status_major_code,
#             True)
# 
#     return _http_namedtuple


from lxml.html import fromstring as html_string
from yams_api.plugins.dev.api_utils.http_methods import http_requests_get_to_dict
    # response dict - response is data blob to return (e.g. 'Site is normal!'), headers are headers, status is ok/nok
    _resp = {
        "response": dict(),
        "headers": dict(),
        "status": "ok"
    }

    _filtered_headers = {
        "date": r.headers.get("date", ""),
        "etag": r.headers.get("etag", ""),
        "status_code": r.headers.get("status_code", 200)
    }

    _resp["headers"] = _filtered_headers

    if not r.error and r.http_2xx:

        _rtxml = html_string(r.text)
        _body = _rtxml.xpath("//html/body")[0]

        _content_div = _body.xpath("div[contains(@class, 'layout-content')]/div[@class='container']")[0]

        general_page_status_raw_text = _content_div.xpath("div[contains(@class, 'page-status')]/span[contains(@class, 'status')]")[0].text
        general_page_status = general_page_status_raw_text.strip()


        by_service_status_divs = _content_div.xpath("div[contains(@class, 'components-section')]/div[contains(@class, 'components-container')]")[0]

        _serv_status = {}


# Path: yams_api/plugins/dev/api_utils/http_methods.py
# def http_requests_get_to_dict(url, headers={}):
#     """Fetch content at a URL, returning a namedtuple of python-request module attributes
# 
#     :param url: url to scrape
#     :param headers: dict of headers to use for the request
#     :return: a namedtuple containing ('headers', 'text', 'status') for Pythonic scripting
#     """
# 
#     # Keep these outside of the try/except so we can potentially get metadata even on exceptions.
#     # Defines some placeholders to avoid a potential NameError
#     _headers = dict()
#     _status = 200
#     _status_major_code = 0
# 
#     try:
#         r = requests.get(url, headers=headers)
#         # r.content is a bytes obj and probably won't be json serializable.  could return a tuple
#         _status = r.status_code
#         _str_status = str(_status)
#         _status_major_code = int(_str_status[0]) # '200' -> '2' ->2
# 
#         rh = r.headers
#         rt = r.text
# 
#         # common headers
#         _headers["status_code"] = _status
#         _headers["etag"] = rh.get("etag", "")
#         _headers["date"] = rh.get("date", "")
#         _headers["last-modified"] = rh.get("last-modified", "")
#         _headers["content-type"] = rh.get("content-type", "")
#         _headers["raw_headers"] = dict(rh)
# 
#         _http_namedtuple = HTTPResponse(
#             _headers,
#             rt,
#             _status,
#             (_status_major_code == 2),
#             _status_major_code,
#             False
#         )
# 
#     except ConnectionError as e:
#         _headers["response"] = str(e)
# 
#         _http_namedtuple = HTTPResponse(
#             _headers,
#             str(e),
#             _status,
#             (_status_major_code == 2),
#             _status_major_code,
#             True)
# 
#     return _http_namedtuple


# Path: yams_api/plugins/dev/cloudinary/methods.py
from lxml.html import fromstring as html_string
from yams_api.plugins.dev.api_utils.http_methods import http_requests_get_to_dict

    # response dict - response is data blob to return (e.g. 'Site is normal!'), headers are headers, status is ok/nok
    _resp = {
        "response": dict(),
        "headers": dict(),
        "status": "ok"
    }

    _filtered_headers = {
        "date": r.headers.get("date", ""),
        "etag": r.headers.get("etag", ""),
        "status_code": r.headers.get("status_code", 200)
    }

    _resp["headers"] = _filtered_headers

    if not r.error and r.http_2xx:

        _rtxml = html_string(r.text)
        _body = _rtxml.xpath("//html/body")[0]

        _content_div = _body.xpath("div[contains(@class, 'layout-content')]/div[@class='container']")[0]

        general_page_status_raw_text = _content_div.xpath("div[contains(@class, 'page-status')]/span[contains(@class, 'status')]")[0].text
        general_page_status = general_page_status_raw_text.strip()


        by_service_status_divs = _content_div.xpath("div[contains(@class, 'components-section')]/div[contains(@class, 'components-container')]")[0]

        _serv_status = {}


        for _service in by_service_status_divs: