Skip to content

Conversation

@nated0g
Copy link
Contributor

@nated0g nated0g commented Sep 8, 2025

Issue #, if available:

Description of changes:

self.api_key_identity_resolver was never being set on the generated Config, only a local api_key_identity_resolver was being set (and then never being used).

This was leading to an error when trying to make requests to a service with api key auth.

'Config' object has no attribute 'api_key_identity_resolver'


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@nated0g nated0g requested a review from a team as a code owner September 8, 2025 17:25
@jonathan343
Copy link
Contributor

This change looks good to me! Gonna drop some testing notes below:

We will create a test directory called test_dir with the following structure:

.
├── models
│   └── model.smithy
└── smithy-build.json

Generate A Test Service w/ httpApiKeyAuth Trait

$version: "2.0"

namespace com.example
use aws.protocols#restJson1

/// Echoes input
@restJson1
@httpApiKeyAuth(name: "X-Api-Key", in: "header")
service EchoService {
    version: "2006-03-01"
    operations: [EchoMessage]
}

@http(uri: "/echo", method: "POST")
operation EchoMessage {
    input := {
        @httpHeader("x-echo-message")
        message: String
    }
    output := {
        message: String
    }
}

Create smithy-build.json File

{
    "version": "1.0",
    "sources": ["models"],
    "maven": {
        "dependencies": [
            "software.amazon.smithy:smithy-model:[1.54.0,2.0)",
            "software.amazon.smithy:smithy-aws-traits:[1.54.0,2.0)",
            "software.amazon.smithy.python.codegen.aws:core:0.0.1"
        ]
    },
    "projections": {
        "client": {
            "plugins": {
                "python-client-codegen": {
                    "service": "com.example#EchoService",
                    "module": "echo",
                    "moduleVersion": "0.0.1"
                }
            }
        }
    }
}

Build Code Generator

uv venv 
. .venv/bin/activate

make install
make build-java
./gradlew publishToMavenLocal

cd ../test_dir
smithy build

Install Echo Client

cd build/smithy/client/python-client-codegen
uv pip install .

Run the following script

import asyncio

from echo.client import EchoService
from echo.config import Config
from echo.models import EchoMessageInput


import logging 
logging.basicConfig(level=logging.DEBUG)

async def main() -> None:
    client = EchoService(Config(endpoint_uri="https://example.com/", api_key="testing123123"))
    response = await client.echo_message(EchoMessageInput(message="spam"))
    print(response.message)


if __name__ == "__main__":
    asyncio.run(main())

Verify Auth Header Sent

DEBUG:smithy_core.aio.client:Sending request HTTPRequest(destination=URI(scheme='https', username=None, password=None, host='example.com', port=None, path='/echo', query='', fragment=None), method='POST', fields=Fields(OrderedDict({'x-echo-message': Field(name='x-echo-message', value=['spam'], kind=<FieldPosition.HEADER: 0>), 'user-agent': Field(name='User-Agent', value=['aws-sdk-python/0.0.3 md/smithy-core#0.0.2 md/awscrt#0.26.1 ua/2.1 api/EchoService#0.0.1 os/macos#24.6.0 md/arch#arm64 lang/python#3.12.11 md/pyimpl#CPython'], kind=<FieldPosition.HEADER: 0>), 'x-api-key': Field(name='X-Api-Key', value=['testing123123'], kind=<FieldPosition.HEADER: 0>)})))

Copy link
Contributor

@jonathan343 jonathan343 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jonathan343 jonathan343 merged commit a5d1b2e into smithy-lang:develop Sep 9, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants