Skip to content

Commit fd7398c

Browse files
committed
feat(api): Introduce comprehensive authentication and request management features
- Added various authentication methods including API Key, Basic, Bearer, and OAuth2. - Implemented request configuration options such as rate limiting, circuit breakers, and caching strategies. - Enhanced API metrics tracking for better performance monitoring. - Introduced file upload support and request/response hooks for improved endpoint functionality. - Reorganized API module for better structure and maintainability.
1 parent c343444 commit fd7398c

27 files changed

+914
-648
lines changed

agentle/agents/apis/__init__.py

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,90 @@
1-
# yourpkg/__init__.py
2-
from .object_schema import ObjectSchema
3-
from .array_schema import ArraySchema
4-
from .primitive_schema import PrimitiveSchema
5-
from .endpoint_parameter import EndpointParameter
1+
# Import all classes for public API
2+
from agentle.agents.apis.api import API
3+
from agentle.agents.apis.api_metrics import APIMetrics
4+
from agentle.agents.apis.authentication import (
5+
ApiKeyLocation,
6+
AuthType,
7+
AuthenticationConfig,
8+
AuthenticationBase,
9+
NoAuthentication,
10+
BearerAuthentication,
11+
BasicAuthentication,
12+
ApiKeyAuthentication,
13+
OAuth2Authentication,
14+
HMACAuthentication,
15+
OAuth2GrantType,
16+
)
17+
from agentle.agents.apis.endpoint import Endpoint
18+
from agentle.agents.apis.endpoint_parameter import EndpointParameter
19+
from agentle.agents.apis.file_upload import FileUpload
20+
from agentle.agents.apis.http_method import HTTPMethod
21+
from agentle.agents.apis.object_serialization_style import ObjectSerializationStyle
22+
from agentle.agents.apis.parameter_location import ParameterLocation
23+
from agentle.agents.apis.request_config import (
24+
CacheStrategy,
25+
CircuitBreaker,
26+
CircuitBreakerError,
27+
RateLimiter,
28+
RequestConfig,
29+
ResponseCache,
30+
RetryStrategy,
31+
RateLimitError,
32+
)
33+
from agentle.agents.apis.request_hook import RequestHook
34+
from agentle.agents.apis.object_schema import ObjectSchema
35+
from agentle.agents.apis.array_schema import ArraySchema
36+
from agentle.agents.apis.primitive_schema import PrimitiveSchema
37+
from agentle.agents.apis.endpoints_to_tools import endpoints_to_tools
638

739
_types = {
840
"ObjectSchema": ObjectSchema,
941
"ArraySchema": ArraySchema,
1042
"PrimitiveSchema": PrimitiveSchema,
1143
}
1244

13-
45+
# Rebuild models for proper type resolution
1446
ObjectSchema.model_rebuild(_types_namespace=_types)
1547
ArraySchema.model_rebuild(_types_namespace=_types)
1648
PrimitiveSchema.model_rebuild(_types_namespace=_types)
1749
EndpointParameter.model_rebuild(_types_namespace=_types)
50+
51+
__all__ = [
52+
# API classes
53+
"API",
54+
"APIMetrics",
55+
# Endpoint classes
56+
"Endpoint",
57+
"EndpointParameter",
58+
"FileUpload",
59+
"RequestHook",
60+
"HTTPMethod",
61+
"ParameterLocation",
62+
"ObjectSerializationStyle",
63+
# Authentication classes
64+
"AuthType",
65+
"ApiKeyLocation",
66+
"OAuth2GrantType",
67+
"AuthenticationBase",
68+
"NoAuthentication",
69+
"BearerAuthentication",
70+
"BasicAuthentication",
71+
"ApiKeyAuthentication",
72+
"OAuth2Authentication",
73+
"HMACAuthentication",
74+
"AuthenticationConfig",
75+
# Request configuration classes
76+
"RequestConfig",
77+
"RetryStrategy",
78+
"CacheStrategy",
79+
"CircuitBreaker",
80+
"RateLimiter",
81+
"ResponseCache",
82+
"CircuitBreakerError",
83+
"RateLimitError",
84+
# Schema classes
85+
"ObjectSchema",
86+
"ArraySchema",
87+
"PrimitiveSchema",
88+
# Utilities
89+
"endpoints_to_tools",
90+
]

agentle/agents/apis/api.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
from __future__ import annotations
1515

1616
import logging
17-
from collections.abc import Coroutine, Mapping, MutableMapping, MutableSequence, Sequence
17+
from collections.abc import (
18+
Coroutine,
19+
Mapping,
20+
MutableMapping,
21+
MutableSequence,
22+
Sequence,
23+
)
1824
from pathlib import Path
1925
from typing import Any, Literal, cast
2026

@@ -24,6 +30,7 @@
2430
from rsb.models.base_model import BaseModel
2531
from rsb.models.field import Field
2632

33+
from agentle.agents.apis.api_metrics import APIMetrics
2734
from agentle.agents.apis.array_schema import ArraySchema
2835
from agentle.agents.apis.authentication import (
2936
ApiKeyLocation,
@@ -43,17 +50,6 @@
4350
logger = logging.getLogger(__name__)
4451

4552

46-
class APIMetrics(BaseModel):
47-
"""Metrics for API usage."""
48-
49-
total_requests: int = 0
50-
successful_requests: int = 0
51-
failed_requests: int = 0
52-
total_latency_ms: float = 0.0
53-
average_latency_ms: float = 0.0
54-
requests_by_endpoint: dict[str, int] = {}
55-
56-
5753
class API(BaseModel):
5854
"""
5955
Enhanced API collection with comprehensive features.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""API key authentication."""
2+
3+
from __future__ import annotations
4+
5+
from collections.abc import MutableMapping
6+
from typing import Any
7+
8+
import aiohttp
9+
from agentle.agents.apis.api_key_location import ApiKeyLocation
10+
from agentle.agents.apis.authentication_base import AuthenticationBase
11+
12+
13+
class ApiKeyAuthentication(AuthenticationBase):
14+
"""API Key authentication."""
15+
16+
def __init__(
17+
self,
18+
api_key: str,
19+
location: ApiKeyLocation = ApiKeyLocation.HEADER,
20+
key_name: str = "X-API-Key",
21+
):
22+
self.api_key = api_key
23+
self.location = location
24+
self.key_name = key_name
25+
26+
async def apply_auth(
27+
self,
28+
session: aiohttp.ClientSession,
29+
url: str,
30+
headers: MutableMapping[str, str],
31+
params: MutableMapping[str, Any],
32+
) -> None:
33+
"""Add API key to the appropriate location."""
34+
if self.location == ApiKeyLocation.HEADER:
35+
headers[self.key_name] = self.api_key
36+
elif self.location == ApiKeyLocation.QUERY:
37+
params[self.key_name] = self.api_key
38+
elif self.location == ApiKeyLocation.COOKIE:
39+
headers["Cookie"] = f"{self.key_name}={self.api_key}"
40+
41+
async def refresh_if_needed(self) -> bool:
42+
"""No refresh needed for API key."""
43+
return False
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""API key location types."""
2+
3+
from enum import StrEnum
4+
5+
6+
class ApiKeyLocation(StrEnum):
7+
"""Where to place API key."""
8+
9+
HEADER = "header"
10+
QUERY = "query"
11+
COOKIE = "cookie"

agentle/agents/apis/api_metrics.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""API metrics tracking."""
2+
3+
from __future__ import annotations
4+
5+
from rsb.models.base_model import BaseModel
6+
7+
8+
class APIMetrics(BaseModel):
9+
"""Metrics for API usage."""
10+
11+
total_requests: int = 0
12+
successful_requests: int = 0
13+
failed_requests: int = 0
14+
total_latency_ms: float = 0.0
15+
average_latency_ms: float = 0.0
16+
requests_by_endpoint: dict[str, int] = {}

agentle/agents/apis/auth_type.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Authentication types."""
2+
3+
from enum import StrEnum
4+
5+
6+
class AuthType(StrEnum):
7+
"""Types of authentication supported."""
8+
9+
NONE = "none"
10+
BEARER = "bearer"
11+
BASIC = "basic"
12+
API_KEY = "api_key"
13+
OAUTH2 = "oauth2"
14+
JWT = "jwt"
15+
CUSTOM = "custom"
16+
AWS_SIGNATURE = "aws_signature"
17+
HMAC = "hmac"

0 commit comments

Comments
 (0)