Pular para conteúdo

change_files

build_metadata_and_files(tipo, items)

Constrói o JSON serializado de metadata e o mapa de arquivos multipart esperado pelo ApiClient.param_serialize().

O nome físico local não é validado nem interpretado no SDK. A API recebe qualquer nome de arquivo e realiza a renomeação interna.

Source code in pldpro_sdk/core/change_files.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def build_metadata_and_files(
    tipo: str,
    items: Iterable[ChangeFileItem],
) -> tuple[str, dict]:
    """
    Constrói o JSON serializado de metadata e o mapa de arquivos multipart
    esperado pelo ApiClient.param_serialize().

    O nome físico local não é validado nem interpretado no SDK.
    A API recebe qualquer nome de arquivo e realiza a renomeação interna.
    """
    sorted_items = sort_items(items)

    metadata_items: list[dict] = []
    files: dict = {}

    for index, item in enumerate(sorted_items, start=1):
        if tipo == TIPO_NEWAVE and item.revisao is not None:
            raise ValueError("revisao não é permitida para trocas do NEWAVE.")

        if tipo not in {TIPO_NEWAVE, TIPO_DECOMP}:
            raise ValueError(f"Tipo de troca inválido: {tipo}")

        file_key = item.file_key or default_file_key(index)

        metadata_item = {
            "fileKey": file_key,
            "mes": item.mes,
            "ano": item.ano,
            "nomeArquivo": normalize(item.nome_arquivo),
            "extensao": normalize(item.extensao),
        }

        if tipo == TIPO_DECOMP and item.revisao is not None:
            metadata_item["revisao"] = item.revisao

        metadata_items.append(metadata_item)

        files[file_key] = (item.file_path.name, item.file_path.read_bytes())

    metadata_json = json.dumps({"items": metadata_items})
    return metadata_json, files