import hashlib
import json
import requests


def get_bearer():

    api_token = "f5204a00-6f31-4de2-96d8-ed998e0d230c"
    api_secret = "8c11320781a5b8f4f327b6937e6f8241"

    url = "https://carapi.app/api/auth/login"
    headers = {
        "accept": "text/plain",
        "Content-Type": "application/json"
    }
    data = {
        "api_token": api_token,
        "api_secret": api_secret
    }

    response = requests.post(url, headers=headers, json=data)

    if response.status_code == 200:
        return response.text
    else:
        print("the api key or secret is not valid")
        return None


def get_car_data(vin):

    url = f"https://carapi.app/api/vin/{vin}?verbose=no&all_trims=no"
    headers = {
        "Authorization": f"Bearer {get_bearer()}"
    }

    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()  # Raises an HTTPError for bad responses (4XX, 5XX)

        response = response.json()
        print(response)

    except requests.exceptions.HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
    except requests.exceptions.RequestException as err:
        print(f"Error occurred: {err}")
    except Exception as e:
        print(f"An error occurred: {e}")


def get_from_cardatabase(vin):
    vin = vin
    url = "https://api.vehicledatabases.com/premium/vin-decode/{vin}"

    payload = {}
    headers = {
        'x-AuthKey': '3cefdfd4272445f1929b5801c55d8fa5'
    }

    response = requests.request("GET", url, headers=headers, data=payload)

    print(response.text)