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("}","") %}
def test_{{ api_name | lower }}({{ api_info.get("params",[])|join(", ") }}):
    data = {}
    {% for param in api_info.get("params",[]) %}data["{{ param }}"] = {{ param }}
    {% 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")
    url = Template("{{ api_info.get("url") }}").render(**data)
    resp = _trigger_requests("{{ api_info.get("method") }}", url,
                      header=req["headers"],
                      data=json.dumps(data))
    print(resp.status_code)
    print(resp.text)
    assertions.assert_for_{{ api_name | lower}}(req,resp)


{% for validations in custom_validations %}
{% set param_name = validations[1].split("=")[0] %}
{% set params_list = api_info.get("params",[]) %}
def test_{{ api_name | lower }}_for_{{ validations[0] | lower }}({{ params_list|join(", ") }}):
    data = {}
    {{ validations[1] }}
    {% for param in api_info.get("params",[]) %}data["{{ param }}"] = {{ param }}
    {% endfor %}
    req = {
             "data": data,
             "headers": {{ api_info.get("header") }}
          }
    if "Authorization" in req["headers"]:
        req["headers"]["Authorization"] = os.environ.get("CVDAST_ACCESS_TOKEN")
    url = Template("{{ api_info.get("url") }}").render(**data)
    resp = _trigger_requests(HOST_URL[-1]+"{{ api_info.get("method") }}", url,
                      header=req["headers"],
                      data=json.dumps(data))
    print(resp.status_code)
    print(resp.text)
    assertions.assert_for_custom_response(req,resp,{{ validations[2] }})
{% endfor %}