Skip to content

Update python SDK version: 1.62.0 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Crowdsec
Copyright (c) 2025 Crowdsec

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
57 changes: 48 additions & 9 deletions crowdsec_service_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
from enum import Enum
from .base_model import Page
from .models import *
from .base_model import Page
from .services.allowlists import Allowlists
from .services.blocklists import Blocklists
from .services.integrations import Integrations
from .services.info import Info
from .services.metrics import Metrics
from .services.hub import Hub
from .http_client import ApiKeyAuth

class Server(Enum):
production_server = 'https://admin.api.crowdsec.net/v1/'

__all__ = [
'Allowlists',
'Blocklists',
'Integrations',
'Info',
'Metrics',
'Hub',
'AllowlistCreateRequest',
'AllowlistCreateResponse',
'AllowlistGetItemsResponse',
'AllowlistGetResponse',
'AllowlistItemUpdateRequest',
'AllowlistItemUpdateResponse',
'AllowlistItemsCreateRequest',
'AllowlistScope',
'AllowlistSubscriberEntity',
'AllowlistSubscribersCount',
'AllowlistSubscriptionRequest',
'AllowlistSubscriptionResponse',
'AllowlistUpdateRequest',
'AllowlistUpdateResponse',
'ApiKeyCredentials',
'AttacksMetrics',
'BasicAuthCredentials',
'BlocklistAddIPsRequest',
'BlocklistCategory',
'BlocklistContentStats',
'BlocklistCreateRequest',
'BlocklistCreateResponse',
'BlocklistDeleteIPsRequest',
'BlocklistGetResponse',
'BlocklistIncludeFilters',
'BlocklistResponse',
'BlocklistOrigin',
'BlocklistSearchRequest',
'BlocklistShareRequest',
'BlocklistSources',
Expand All @@ -37,13 +55,16 @@ class Server(Enum):
'BlocklistUpdateRequest',
'BlocklistUsageStats',
'Body_uploadBlocklistContent',
'ComputedMetrics',
'ComputedSavedMetrics',
'CtiAs',
'CtiBehavior',
'CtiCategory',
'CtiCountry',
'CtiIp',
'CtiScenario',
'EntityType',
'GetRemediationMetricsResponse',
'HTTPValidationError',
'InfoResponse',
'IntegrationCreateRequest',
Expand All @@ -53,18 +74,36 @@ class Server(Enum):
'IntegrationUpdateRequest',
'IntegrationUpdateResponse',
'Links',
'MetricUnits',
'OriginMetrics',
'OutputFormat',
'Page_BlocklistResponse_',
'Page_IntegrationGetResponse_',
'PaginatedBlocklistResponse',
'PageAllowlistGetItemsResponse',
'PageAllowlistGetResponse',
'PageAllowlistSubscriberEntity',
'PageIntegrationGetResponse',
'PagePublicBlocklistResponse',
'Permission',
'PricingTiers',
'PublicBlocklistResponse',
'PublicPaginatedBlocklistResponse',
'RawMetrics',
'RemediationMetrics',
'RemediationMetricsData',
'Share',
'SourceInfo',
'SourceType',
'Stats',
'SubscriberEntityType',
'ValidationError',
'HubItem',
'HubType',
'AppsecConfigIndex',
'AppsecRuleIndex',
'CollectionIndex',
'ContextIndex',
'Index',
'ParserIndex',
'PostoverflowIndex',
'ScenarioIndex',
'VersionDetail',
'ApiKeyAuth',
'Server',
'Page'
Expand Down
Binary file modified crowdsec_service_api/__pycache__/base_model.cpython-311.pyc
Binary file not shown.
Binary file modified crowdsec_service_api/__pycache__/http_client.cpython-311.pyc
Binary file not shown.
13 changes: 9 additions & 4 deletions crowdsec_service_api/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ class BaseModelSdk(BaseModel):


class Page(BaseModelSdk, Generic[T]):
_client: "Service"
items: Sequence[T]
total: Optional[int]
page: Optional[int]
size: Optional[int]
pages: Optional[int] = None
links: Optional[dict] = None

def next(self, client: "Service") -> "Page[T]":
return client.next_page(self)
def __init__(self, _client: "Service", **data):
super().__init__(**data)
self._client = _client

def next(self, client: "Service" = None) -> "Page[T]":
return (client if client is not None else self._client).next_page(self)


class Service:
Expand All @@ -39,7 +44,7 @@ def next_page(self, page: Page[T]) -> Page[T]:
# links are relative to host not to full base url. We need to pass a full formatted url here
parsed_url = urlparse(self.http_client.base_url)
response = self.http_client.get(
f"{parsed_url.scheme}://{parsed_url.netloc}{page.links['next']}"
f"{parsed_url.scheme}://{parsed_url.netloc}{page.links['next']}", path_params=None, params=None, headers=None
)
return Page[T](**response.json())
return page.__class__(_client=self, **response.json())
return None
19 changes: 10 additions & 9 deletions crowdsec_service_api/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ def __init__(self, base_url: str, auth: httpx.Auth, aws_region="eu-west-1") -> N
self.timeout = 30

def _replace_path_params(self, url: str, path_params: dict):
for param, value in path_params.items():
if not value:
raise ValueError(
f"Parameter {param} is required, cannot be empty or blank."
)
url = url.replace(f"{{{param}}}", quote(str(value)))
if path_params:
for param, value in path_params.items():
if not value:
raise ValueError(
f"Parameter {param} is required, cannot be empty or blank."
)
url = url.replace(f"{{{param}}}", quote(str(value)))
return url

def _normalize_url(self, url: str):
Expand All @@ -70,9 +71,9 @@ def _normalize_url(self, url: str):
def get(
self,
url: str,
path_params: dict = {},
params: dict = {},
headers: dict = {},
path_params: dict = None,
params: dict = None,
headers: dict = None,
):
url = self._replace_path_params(
url=self._normalize_url(url), path_params=path_params
Expand Down
Loading