from .api_errors import *


def __make_invalid_argument_type_error(name, value, except_type):
    related = {"param_name": name, "excepted_type": "int", "value": value}
    raise Exception(API_ERROR_INVALID_ARGUMENT_TYPE, related)


def api_make_response(response):
    return {"response": API_OK_OBJ | response}


def api_get_param_int(params: dict, name: str, required=True, default=0):
    if name in params:
        try:
            return int(params[name])
        except:
            __make_invalid_argument_type_error(name, params[name], "int")

    if required:
        raise Exception(API_ERROR_MISSING_ARGUMENT, name)

    return default


def api_get_param_str(params: dict, name: str, required=True, default=""):
    if name in params:
        return params[name]

    if required:
        raise Exception(API_ERROR_MISSING_ARGUMENT, name)

    return default
