Skip to content

whatsapie/schema_generator/__init__.py

SchemaGenerator

Generates api schema from native python object classes.

Schema generator class, that generated api body schemas according to provided arguments of type Message.

Source code in whatsapie/schema_generator/__init__.py
class SchemaGenerator:
    """Generates api schema from native python object classes.

    Schema generator class, that generated api body schemas according to
    provided arguments of type Message.
    """

    def __init__(self):
        pass

    def generate(
        self,
        message: Message,
        dump_json_str: bool = True,  # If set to True returns a stringified version of the dict object
    ):
        """Generates schema for the provided message argument.

        Args:
            message: Must be a type of Message Instance supports **Text, Location, Media**.
            dump_json_str: When set to false, will return native python dict.

        Returns:
            body: if dump_json_str is True, returns stringified json, else returns native python dict.
        """
        body = {"messaging_product": "whatsapp", "recipient_type": "individual"}
        body["to"] = message.to

        if isinstance(message, Text):
            body = generate_text_schema(body, message)

        if isinstance(message, Location):
            body = generate_location_schema(body, message)

        if isinstance(message, Media):
            body = generate_media_schema(body, message)

        if isinstance(message, Template):
            body = generate_template_schema(body, message)

        if isinstance(message, ContactGroup):
            body = generate_contact_group_schema(body, message)

        if dump_json_str:
            return self.dump_json(body)

        return body

    def dump_json(self, body):
        return json.dumps(body)

generate(message, dump_json_str=True)

Generates schema for the provided message argument.

Parameters:

Name Type Description Default
message Message

Must be a type of Message Instance supports Text, Location, Media.

required
dump_json_str bool

When set to false, will return native python dict.

True

Returns:

Name Type Description
body

if dump_json_str is True, returns stringified json, else returns native python dict.

Source code in whatsapie/schema_generator/__init__.py
def generate(
    self,
    message: Message,
    dump_json_str: bool = True,  # If set to True returns a stringified version of the dict object
):
    """Generates schema for the provided message argument.

    Args:
        message: Must be a type of Message Instance supports **Text, Location, Media**.
        dump_json_str: When set to false, will return native python dict.

    Returns:
        body: if dump_json_str is True, returns stringified json, else returns native python dict.
    """
    body = {"messaging_product": "whatsapp", "recipient_type": "individual"}
    body["to"] = message.to

    if isinstance(message, Text):
        body = generate_text_schema(body, message)

    if isinstance(message, Location):
        body = generate_location_schema(body, message)

    if isinstance(message, Media):
        body = generate_media_schema(body, message)

    if isinstance(message, Template):
        body = generate_template_schema(body, message)

    if isinstance(message, ContactGroup):
        body = generate_contact_group_schema(body, message)

    if dump_json_str:
        return self.dump_json(body)

    return body