# Code generated by Lark OpenAPI.

import lark_oapi as lark
from lark_oapi.api.task.v2 import *


def main():
    # 创建client
    client = lark.Client.builder() \
        .app_id(lark.APP_ID) \
        .app_secret(lark.APP_SECRET) \
        .log_level(lark.LogLevel.DEBUG) \
        .build()

    # 构造请求对象
    request: CreateTaskRequest = CreateTaskRequest.builder() \
        .user_id_type("open_id") \
        .request_body(InputTask.builder()
                      .summary("针对全年销售进行一次复盘")
                      .description("需要事先阅读复盘总结文档")
                      .due(Due.builder().build())
                      .origin(Origin.builder().build())
                      .extra("dGVzdA==")
                      .completed_at("0")
                      .members([])
                      .repeat_rule("FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR")
                      .custom_complete(CustomComplete.builder().build())
                      .tasklists([])
                      .client_token("daa2237f-8310-4707-a83b-52c8a81e0fb7")
                      .start(Start.builder().build())
                      .reminders([])
                      .mode(2)
                      .is_milestone(False)
                      .custom_fields([])
                      .docx_source(DocxSource.builder().build())
                      .positive_reminders([])
                      .build()) \
        .build()

    # 发起请求
    response: CreateTaskResponse = client.task.v2.task.create(request)

    # 处理失败返回
    if not response.success():
        lark.logger.error(
            f"client.task.v2.task.create failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}")
        return

    # 处理业务结果
    lark.logger.info(lark.JSON.marshal(response.data, indent=4))


# 异步方式
async def amain():
    # 创建client
    client = lark.Client.builder() \
        .app_id(lark.APP_ID) \
        .app_secret(lark.APP_SECRET) \
        .log_level(lark.LogLevel.DEBUG) \
        .build()

    # 构造请求对象
    request: CreateTaskRequest = CreateTaskRequest.builder() \
        .user_id_type("open_id") \
        .request_body(InputTask.builder()
                      .summary("针对全年销售进行一次复盘")
                      .description("需要事先阅读复盘总结文档")
                      .due(Due.builder().build())
                      .origin(Origin.builder().build())
                      .extra("dGVzdA==")
                      .completed_at("0")
                      .members([])
                      .repeat_rule("FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR")
                      .custom_complete(CustomComplete.builder().build())
                      .tasklists([])
                      .client_token("daa2237f-8310-4707-a83b-52c8a81e0fb7")
                      .start(Start.builder().build())
                      .reminders([])
                      .mode(2)
                      .is_milestone(False)
                      .custom_fields([])
                      .docx_source(DocxSource.builder().build())
                      .positive_reminders([])
                      .build()) \
        .build()

    # 发起请求
    response: CreateTaskResponse = await client.task.v2.task.acreate(request)

    # 处理失败返回
    if not response.success():
        lark.logger.error(
            f"client.task.v2.task.acreate failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}")
        return

    # 处理业务结果
    lark.logger.info(lark.JSON.marshal(response.data, indent=4))


if __name__ == "__main__":
    # asyncio.run(amain()) 异步方式
    main()
