Asset Connector Documentation
Loading...
Searching...
No Matches
response_body.py
Go to the documentation of this file.
1"""Defines the ResponseBody model and related utilities for HTTP response handling.
2
3This module provides:
4- ResponseBody: a Pydantic model for HTTP responses;
5- create_response: a helper function to create ResponseBody instances.
6"""
7
8import json
9from typing import Any, ClassVar
10
11from basyx.aas.adapter.json import AASToJsonEncoder
12from basyx.aas.model import ModelReference as Reference
13from basyx.aas.model import Submodel
14from pydantic import BaseModel, Field
15
16
17class ResponseBody(BaseModel): # noqa: D101
18 status_code: int = Field(
19 default=200,
20 description="The HTTP status code of the response.",
21 alias="StatusCode",
22 example=200,
23 )
24
25 message: str = Field(
26 default="Successfully",
27 description="A message providing additional information about the response.",
28 alias="Message",
29 example="Successfully invoked `/set-config` with raw JSON in payload",
30 )
31
32 payload: Any = Field(
33 default={},
34 description="Json content of the response.",
35 alias="Payload",
36 example="",
37 )
38
39 value: str = Field(
40 default="",
41 description="The value returned by the operation, if applicable.",
42 alias="Value",
43 example="myResult",
44 )
45
46 class Config: # noqa: D106
47 arbitrary_types_allowed = True
48 json_encoders: ClassVar = {Reference: lambda v: json.dumps(v, cls=AASToJsonEncoder), Submodel: lambda v: json.dumps(v, cls=AASToJsonEncoder)}
49
50
51def create_response(status_code: int, message: str, payload: str = "{}", value: str = "") -> ResponseBody:
52 """
53 Create a ResponseBody instance with the given parameters.
54
55 :param status_code: The HTTP status code of the response.
56 :param message: A message providing additional information about the response.
57 :param payload: Json content of the response.
58 :param value: The value returned by the operation, if applicable.
59 :return: An instance of ResponseBody.
60 """
61 body = ResponseBody()
62 body.status_code = status_code
63 body.message = message
64 body.payload = payload
65 body.value = value
66
67 return body