import requests
import json
import os
from jinja2 import Template
import pytest
import assertions

#HOST_URL = {{ host_url }}


def _trigger_requests(req_method, url, header, data, proxies=None):
    print("\n\nRegenerating traffic from CloudVector events....")
    return requests.request(method=req_method, url=url, proxies=proxies, headers=header, data=data, verify=False)

{% set api_name = api_name | replace("{","") | replace("}","") %}
{% for method, api_info in apis_metadata.items() %}
def test_{{ method | lower }}_{{ api_name | lower }}({{ api_info.get("params",[])|join(", ") }}, update_headers):
    data = {}
    {% for param in api_info.get("params",[]) %}{% if (param not in ('host', 'access_token','url_prefix')) %}data["{{ param }}"] = {{ param }}{% endif %}
    {% endfor %}
    req = {
             "data": data,
             "headers": {{ api_info.get("header") }}
          }
    req["headers"] = update_headers(headers=req["headers"], api="{{ api_info.get("url") }}")
    if "Authorization" in req["headers"] and req["headers"]["Authorization"] is None:
        req["headers"]["Authorization"] = os.environ.get("CVDAST_ACCESS_TOKEN")
    if access_token:
        req["headers"]["Authorization"] = access_token
    print("for debugging:"+str(req))
    url = Template(host+url_prefix+"{{ api_info.get("url") }}").render(**data)
    resp = _trigger_requests("{{ method }}", url,
                      header=req["headers"],
                      data=json.dumps(data))
    print(resp.status_code)
    print(resp.text)
    assertions.assert_for_{{ api_name | lower}}(req,resp)
{% endfor %}

{% if STATUS_CODES %}
{% set api_name = api_name | replace("{","") | replace("}","") %}
{% for method, api_info in apis_metadata.items() %}
{% for status_code in STATUS_CODES.get(method) %}
{% if status_code not in [ "200", "201"] %}
def test_{{ method | lower }}_{{ api_name | lower }}_for_{{ status_code }}_code({{ api_info.get("params",[])|join(", ") }}, update_headers):
    data = {}
    {% for param in api_info.get("params",[]) %}{% if (param not in ('host', 'access_token','url_prefix')) %}data["{{ param }}"] = {{ param }}{% endif %}
    {% endfor %}
    req = {
             "data": data,
             "headers": {{ api_info.get("header") }}
          }
    if "Authorization" in req["headers"] and req["headers"]["Authorization"] is None:
        req["headers"]["Authorization"] = os.environ.get("CVDAST_ACCESS_TOKEN")
    if access_token:
        req["headers"]["Authorization"] = access_token
    req["headers"] = update_headers(headers=req["headers"], api="{{ api_info.get("url").split("?")[0] }}", status_code="{{ status_code }}")
    print("for debugging:"+str(req))
    url = Template(host+url_prefix+"{{ api_info.get("url") }}").render(**data)
    resp = _trigger_requests("{{ method }}", url,
                      header=req["headers"],
                      data=json.dumps(data))
    print(resp.status_code)
    print(resp.text)
    assertions.assert_for_respcodes(req, resp, {{ status_code }}, None)
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
