createAiinApiKey function

Future<AiinApiKey> createAiinApiKey({
  1. required String accessToken,
  2. Client? client,
  3. String apiBaseUrl = aiinApiBaseUrl,
})

Registers a new AIIN API key for the authenticated user.

Requires the user's llm product; a 403 arrives as AiinAuthException with code llm_access_denied — surface the message (top up / ask the admin to grant LLM access).

Implementation

Future<AiinApiKey> createAiinApiKey({
  required String accessToken,
  http.Client? client,
  String apiBaseUrl = aiinApiBaseUrl,
}) async {
  final response = await _postOrGet(
    method: 'POST',
    url: Uri.parse('$apiBaseUrl/v1/keys'),
    headers: {'Authorization': 'Bearer $accessToken'},
    client: client,
  );
  final parsed = _decode(response);
  final key = parsed['key'];
  if (key is! String || key.isEmpty) {
    throw AiinAuthException(
      'AIIN key registration returned no key',
      code: 'invalid_response',
      statusCode: response.statusCode,
    );
  }
  return AiinApiKey(
    raw: key,
    id: switch (parsed['id']) {
      String value => value,
      _ => '',
    },
    prefix: switch (parsed['prefix']) {
      String value => value,
      _ => '',
    },
    createdAt: switch (parsed['created_at']) {
      String value => value,
      _ => '',
    },
  );
}