Type annotations for aiobotocore CognitoIdentityProvider

3.4.0 · active · verified Fri Apr 17

This package provides PEP 561-compatible type annotations for the `aiobotocore` library's CognitoIdentityProvider service client. It is generated by `mypy-boto3-builder` and ensures type safety for `aiobotocore` calls, enhancing developer experience with static analysis tools like MyPy. The current version is 3.4.0, aligning with a specific `aiobotocore` service version, and it releases frequently to match upstream AWS API changes and builder updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `aiobotocore` with `types-aiobotocore-cognito-idp` for type-safe interaction with AWS Cognito Identity Provider. It creates a client, then calls `list_users` to retrieve user information, leveraging the provided type hints for client methods and response structures. Remember to configure your AWS credentials and a Cognito User Pool ID.

import asyncio
import os
import aiobotocore
from types_aiobotocore_cognito_idp.client import CognitoIdentityProviderClient
from types_aiobotocore_cognito_idp.type_defs import ListUsersResponseTypeDef

async def main():
    # Ensure AWS credentials are set up (e.g., via environment variables, ~/.aws/credentials)
    # This example assumes you have valid credentials for Cognito User Pools.

    # Using aiobotocore.get_session() is the recommended way for aiobotocore
    session = aiobotocore.get_session()

    async with session.create_client(
        "cognito-idp",
        region_name=os.environ.get("AWS_REGION", "us-east-1"),
        aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID", ""), # Provide actual credentials or rely on env/config
        aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY", ""), # Provide actual credentials or rely on env/config
    ) as client:
        # The client object will be correctly typed as CognitoIdentityProviderClient
        # by type checkers like MyPy due to the installed stubs.
        typed_client: CognitoIdentityProviderClient = client

        user_pool_id = os.environ.get("COGNITO_USER_POOL_ID", "") # Replace with your User Pool ID

        if not user_pool_id:
            print("Error: COGNITO_USER_POOL_ID environment variable not set.")
            return

        try:
            response: ListUsersResponseTypeDef = await typed_client.list_users(
                UserPoolId=user_pool_id,
                Limit=2
            )
            print(f"Successfully listed users in User Pool '{user_pool_id}':")
            for user in response.get("Users", []):
                print(f"  - Username: {user.get('Username')}, Status: {user.get('UserStatus')}")
            if "PaginationToken" in response:
                print(f"  (Next token: {response['PaginationToken']})")

        except Exception as e:
            print(f"Error listing users: {e}")
            print("Ensure COGNITO_USER_POOL_ID and AWS credentials are correctly set and have permissions.")

if __name__ == "__main__":
    # To run this example, set environment variables:
    # export AWS_REGION="your-aws-region"
    # export AWS_ACCESS_KEY_ID="your-access-key"
    # export AWS_SECRET_ACCESS_KEY="your-secret-key"
    # export COGNITO_USER_POOL_ID="your-cognito-user-pool-id"
    asyncio.run(main())

view raw JSON →