Skip to content

[Python] API token Authentication not stored #33

@paulgrainger85

Description

@paulgrainger85

Describe the issue

When the API client is built using an API token, this token needs to be passed in as a header in subsequent steps as a header, otherwise Unauthorized errors are thrown

Specifically in the example below, I should not need to add

_headers={'Authorization': f'Bearer {os.environ.get("KESTRA_API_TOKEN")}'}

if I have already stored the api_key in the client at start up

import kestra_api_client
from dotenv import load_dotenv
import os
import json
load_dotenv()

configuration = kestra_api_client.Configuration(
    host = os.environ.get("KESTRA_HOST"),
    api_key = os.environ.get("KESTRA_API_TOKEN"),
    api_key_prefix = "Bearer",
)

api_client = kestra_api_client.ApiClient(configuration)
api_instance = kestra_api_client.FlowsApi(api_client)


tenant = 'demo'
flow_id = 'sdk-test-flow2'
namespace = 'demo'
body = f"""
id: {flow_id}
namespace: {namespace}
tasks:
  - id: hello
    type: io.kestra.plugin.core.log.Log
    message: Hello from the SDK! 👋
"""

try:
    api_response = api_instance.create_flow(
        tenant,
        body,
        _headers={'Authorization': f'Bearer {os.environ.get("KESTRA_API_TOKEN")}'}

    )
    print(api_response)
except kestra_api_client.rest.ApiException as e:
    if e.status == 422 and "Flow id already exists" in json.loads(e.body).get("message", ""):
        try:
            api_response = api_instance.update_flow(flow_id, namespace, tenant, body,_headers={'Authorization': f'Bearer {os.environ.get("KESTRA_API_TOKEN")}'})
            print(api_response)
        except ValueError:
            print("Flow updated successfully")
    else:
        print(e)

Environment

  • Kestra Version: develop

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/pluginPlugin-related issue or feature requestbugSomething isn't working

    Type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions