Coverage for src\baobab_web_api_caller\auth\api_key_header_authentication_strategy.py: 92%
18 statements
« prev ^ index » next coverage.py v7.10.3, created at 2026-03-21 12:10 +0100
« prev ^ index » next coverage.py v7.10.3, created at 2026-03-21 12:10 +0100
1"""Stratégie API key via en-tête HTTP."""
3from __future__ import annotations
5from dataclasses import dataclass
7from baobab_web_api_caller.auth.authentication_strategy import AuthenticationStrategy
8from baobab_web_api_caller.core.baobab_request import BaobabRequest
9from baobab_web_api_caller.exceptions.configuration_exception import ConfigurationException
12@dataclass(frozen=True, slots=True)
13class ApiKeyHeaderAuthenticationStrategy(AuthenticationStrategy):
14 """Ajoute une API key dans un header.
16 :param header_name: Nom du header.
17 :type header_name: str
18 :param api_key: Valeur de la clé.
19 :type api_key: str
20 :raises ConfigurationException: Si les paramètres sont invalides.
21 """
23 header_name: str
24 api_key: str
26 def __post_init__(self) -> None:
27 if not isinstance(self.header_name, str) or self.header_name.strip() == "":
28 raise ConfigurationException("header_name must be a non-empty string")
29 if not isinstance(self.api_key, str) or self.api_key.strip() == "":
30 raise ConfigurationException("api_key must be a non-empty string")
31 if ":" in self.header_name or " " in self.header_name: 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true
32 raise ConfigurationException("header_name must not contain spaces or ':'")
34 def apply(self, request: BaobabRequest) -> BaobabRequest:
35 """Ajoute/écrase le header API key."""
37 return request.with_header(self.header_name, self.api_key)