Ejemplo n.º 1
0
def problem(
    status: int = 400,
    title: str = "A problem occurred.",
    detail: Optional[str] = None,
    type_: Optional[str] = None,
    fields: Optional[dict[str, list[str]]] = None,
    ext: Optional[dict[str, Any]] = None,
) -> Response:
    problem_dict = {
        "title": title,
        "status": status,
    }
    if detail is not None:
        problem_dict["detail"] = detail
    if type_ is not None:
        problem_dict["type"] = type_

    if fields is not None:
        problem_dict["fields"] = fields

    if ext is not None:
        problem_dict["ext"] = ext

    response = Response()
    response.status_code = status
    response.set_content_type("application/problem+json")
    response.set_data(json.dumps(problem_dict))
    return response
Ejemplo n.º 2
0
def _serve_time_period(time_period):
    response = Response()
    response.set_data(json.dumps(time_period))
    response.set_content_type('application/json')
    response.headers.add('ETag',
                         constructors.etag_of_dict(time_period).to_header())
    return response
Ejemplo n.º 3
0
def problem(
    status: int = 400,
    title: str = "A problem occured.",
    detail: Optional[str] = None,
    type_: Optional[str] = None,
    ext: Optional[Dict[str, Any]] = None,
):
    problem_dict = {
        "title": title,
        "status": status,
    }
    if detail is not None:
        problem_dict["detail"] = detail
    if type_ is not None:
        problem_dict["type"] = type_

    if isinstance(ext, dict):
        problem_dict.update(ext)
    else:
        if ext:
            problem_dict["ext"] = ext

    response = Response()
    response.status_code = status
    response.set_content_type("application/problem+json")
    response.set_data(json.dumps(problem_dict))
    return response
Ejemplo n.º 4
0
def _serve_host(host, effective_attributes=False):
    response = Response()
    response.set_data(json.dumps(serialize_host(host, effective_attributes)))
    response.set_content_type("application/json")
    etag = constructors.etag_of_dict(_host_etag_values(host))
    response.headers.add("ETag", etag.to_header())
    return response
Ejemplo n.º 5
0
def _serve_host(host, effective_attributes=False):
    response = Response()
    response.set_data(json.dumps(serialize_host(host, effective_attributes)))
    response.set_content_type('application/json')
    etag = etag_of_host(host)
    response.headers.add('ETag', etag.to_header())
    return response
Ejemplo n.º 6
0
def _serve_host_tag_group(tag_details: Dict[str, Any]) -> Response:
    response = Response()
    response.set_data(json.dumps(serialize_host_tag_group(tag_details)))
    response.set_content_type('application/json')
    response.headers.add('ETag',
                         constructors.etag_of_dict(tag_details).to_header())
    return response
Ejemplo n.º 7
0
def serve_group(group, serializer):
    response = Response()
    response.set_data(json.dumps(serializer(group)))
    if response.status_code != 204:
        response.set_content_type('application/json')
    response.headers.add('ETag', constructors.etag_of_dict(group).to_header())
    return response
Ejemplo n.º 8
0
def serve_user(user_id):
    response = Response()
    user_attributes = user_config_attributes(user_id)
    response.set_data(json.dumps(serialize_user(user_id, user_attributes)))
    response.set_content_type('application/json')
    response.headers.add('ETag', constructors.etag_of_dict(user_attributes).to_header())
    return response
Ejemplo n.º 9
0
def serve_user(user_id):
    response = Response()
    user_attributes_internal = _load_user(user_id)
    user_attributes = _internal_to_api_format(user_attributes_internal)
    response.set_data(json.dumps(serialize_user(user_id, complement_customer(user_attributes))))
    response.set_content_type("application/json")
    response.headers.add("ETag", constructors.etag_of_dict(user_attributes).to_header())
    return response
Ejemplo n.º 10
0
def _serve_password(ident, password_details):
    response = Response()
    response.set_data(json.dumps(serialize_password(ident, password_details)))
    response.set_content_type('application/json')
    response.headers.add(
        'ETag',
        constructors.etag_of_dict(password_details).to_header())
    return response
Ejemplo n.º 11
0
def _serve_host_tag_group(tag_details: TaggroupSpec) -> Response:
    response = Response()
    response.set_data(json.dumps(serialize_host_tag_group(dict(tag_details))))
    response.set_content_type("application/json")
    response.headers.add(
        "ETag",
        constructors.etag_of_dict(dict(tag_details)).to_header())
    return response
Ejemplo n.º 12
0
def _serve_services(host, discovered_services, discovery_state):
    response = Response()
    response.set_data(
        json.dumps(
            serialize_service_discovery(host, discovered_services,
                                        discovery_state)))
    response.set_content_type('application/json')
    return response
Ejemplo n.º 13
0
def serve_json(data: Serializable,
               profile: Optional[Dict[str, str]] = None) -> Response:
    content_type = "application/json"
    if profile is not None:
        content_type += ';profile="%s"' % (profile, )
    response = Response()
    response.set_content_type(content_type)
    response.set_data(json.dumps(data))
    return response
Ejemplo n.º 14
0
def serve_json(data, profile=None):
    content_type = 'application/json'
    if profile is not None:
        content_type += ';profile="%s"' % (profile, )
    response = Response()
    response.set_content_type(content_type)
    response.set_data(json.dumps(data))
    # HACK: See wrap_with_validation.
    response.original_data = data  # type: ignore[attr-defined]
    return response
Ejemplo n.º 15
0
def _serve_services(
    host: watolib.CREHost,
    discovered_services: CheckTable,
    discovery_phases: List[str],
):
    response = Response()
    response.set_data(
        json.dumps(serialize_service_discovery(host, discovered_services, discovery_phases)))

    response.set_content_type('application/json')
    return response
Ejemplo n.º 16
0
def _serve_services(
    host: CREHost,
    discovered_services: Sequence[CheckPreviewEntry],
    discovery_phases: List[str],
) -> Response:
    response = Response()
    response.set_data(
        json.dumps(
            serialize_service_discovery(host, discovered_services,
                                        discovery_phases)))

    response.set_content_type("application/json")
    return response
Ejemplo n.º 17
0
def _serve_downtimes(downtimes):
    response = Response()
    response.set_data(json.dumps(_serialize_downtimes(downtimes)))
    response.set_content_type('application/json')
    return response
Ejemplo n.º 18
0
def _serve_downtime(downtime_details):
    response = Response()
    response.set_data(json.dumps(_serialize_single_downtime(downtime_details)))
    response.set_content_type("application/json")
    return response
Ejemplo n.º 19
0
def _serve_password(ident, password_details):
    response = Response()
    response.set_data(json.dumps(serialize_password(ident, password_details)))
    response.set_content_type('application/json')
    return response
Ejemplo n.º 20
0
def _serve_time_period(time_period):
    response = Response()
    response.set_data(json.dumps(time_period))
    response.set_content_type('application/json')
    return response
Ejemplo n.º 21
0
def _serve_host(host):
    response = Response()
    response.set_data(json.dumps(serialize_host(host)))
    response.set_content_type('application/json')
    response.headers.add('ETag', constructors.etag_of_obj(host).to_header())
    return response