Skip to content

Commit

Permalink
VadeCloud: Additional custom header to platform requests
Browse files Browse the repository at this point in the history
  • Loading branch information
vladyslav-huriev committed Nov 4, 2023
1 parent c4737f2 commit 0d71636
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions VadeCloud/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## 2023-10-31 - 1.1.1

### Fixed

- Fixed bug with a first start

## 2023-10-12 - 1.0.5

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion VadeCloud/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
"name": "Vade Cloud",
"slug": "vade-cloud",
"uuid": "203d043e-a1ea-4b5c-81ef-6fc0f8a6a9d3",
"version": "1.1"
"version": "1.1.1"
}
4 changes: 4 additions & 0 deletions VadeCloud/vadecloud_modules/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ def __init__(
password: str,
nb_retries: int = 5,
ratelimit_per_minute: int = 20,
default_headers: dict[str, str] | None = None,
):
super().__init__()
self.auth: ApiKeyAuthentication = ApiKeyAuthentication(hostname, login, password)
self.auth.authenticate()

if default_headers is not None:
self.headers.update(default_headers)

self.mount(
"https://",
LimiterAdapter(
Expand Down
10 changes: 9 additions & 1 deletion VadeCloud/vadecloud_modules/client/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@


class ApiKeyAuthentication(AuthBase):
def __init__(self, hostname: str, login: str, password: str):
def __init__(self, hostname: str, login: str, password: str, default_headers: dict[str, str] | None = None):
self.__hostname = hostname
self.__login = login
self.__password = password
self.__http_session = requests.Session()

self.default_headers: dict[str, str] = default_headers or {}

if default_headers is not None:
self.__http_session.headers.update(default_headers)

Check warning on line 16 in VadeCloud/vadecloud_modules/client/auth.py

View check run for this annotation

Codecov / codecov/patch

VadeCloud/vadecloud_modules/client/auth.py#L16

Added line #L16 was not covered by tests

self.__http_session.mount(
"https://",
HTTPAdapter(
Expand All @@ -28,6 +34,7 @@ def authenticate(self):
response = self.__http_session.post(
url=f"{self.__hostname}/rest/v3.0/login/login",
headers={
**self.default_headers,
"Content-type": "application/json",
"Accept": "application/json",
},
Expand Down Expand Up @@ -55,4 +62,5 @@ def account_id(self):

def __call__(self, request):
request.headers["x-vrc-authorization"] = self.get_authorization_header()

return request
15 changes: 15 additions & 0 deletions VadeCloud/vadecloud_modules/trigger_vade_cloud_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def client(self):
login=self.connector.module.configuration.login,
password=self.connector.module.configuration.password,
ratelimit_per_minute=self.connector.configuration.ratelimit_per_minute,
default_headers=self.connector._http_default_headers,
)
return client

Expand Down Expand Up @@ -248,6 +249,20 @@ def __init__(self, *args, **kwargs):
self.context_lock = Lock()
self.consumers = {}

@cached_property
def _http_default_headers(self) -> dict[str, str]:
"""
Return the default headers for the HTTP requests used in this connector.
Returns:
dict[str, str]:
"""
return {
"User-Agent": "sekoiaio-connector/{0}-{1}".format(
self.module.manifest.get("slug"), self.module.manifest.get("version")
),
}

def start_consumers(self):
consumers = {}

Expand Down

0 comments on commit 0d71636

Please sign in to comment.