如何使用 Python 在 Autentique API 中上传文件?

How to upload a file in Autentique API using Python?

Autentique API 是 Graphql。文档:https://docs.autentique.com.br/api/integracao/criando-um-documento

您必须先在 Autentique 上创建一个帐户并创建一个 API 密钥。

正在上传沙箱中的文件并将其发送到电子邮件进行签名。它 returns 文档的 ID 和名称。

使用 curl

  curl -H "Authorization: Bearer <TOKEN>" https://api.autentique.com.br/v2/graphql \
  -F operations='{"query": "mutation CreateDocumentMutation($document: DocumentInput! $signers: [SignerInput!]! $file: Upload!) {createDocument(sandbox: true, document: $document, signers: $signers, file: $file) {id name }}", "variables": { "document": {"name": "<DOCUMENT_NAME>"}, "signers": [{"email": "<FROM_EMAIL>","action": "SIGN"}], "file": null } }' \
  -F map='{ "0": ["variables.file"] }' \
  -F 0=@<FULL_PATH_FILE>

使用aiogqlc

https://github.com/DoctorJohn/aiogqlc

import asyncio
from aiogqlc import GraphQLClient

endpoint = "https://api.autentique.com.br/v2/graphql"

headers = {
    "Authorization": "Bearer <TOKEN>"
}

client = GraphQLClient(endpoint, headers=headers)

async def create_document():
    query = """
      mutation CreateDocumentMutation(
        $document: DocumentInput!
        $signers: [SignerInput!]!
        $file: Upload!
        ) {
        createDocument(
        sandbox: true,
        document: $document,
        signers: $signers,
        file: $file) 
        {
          id
          name
          }
        }"""

    variables = {
        "document": {
            "name": "<DOCUMENT_NAME>"
        },
        "signers": [{
            "email": "<FROM_EMAIL>",
            "action": "SIGN"
        }],
        "file": open('<FULL_PATH_FILE>', 'rb'),
    }

    response = await client.execute(query, variables=variables)
    print(await response.json())


if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(create_document())

更多语言实现:https://github.com/jaydenseric/graphql-multipart-request-spec#implementations