finalsa-common-http-client 0.0.4__py3-none-any.whl → 0.0.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
finalsa/http/_shared.py CHANGED
@@ -2,8 +2,10 @@ from __future__ import annotations
2
2
 
3
3
  from collections.abc import Mapping
4
4
  from importlib import metadata
5
+ from typing import Any, Dict
5
6
  from urllib.parse import urljoin
6
7
 
8
+ from finalsa.common.models import BaseDomainException
7
9
  from finalsa.traceability import get_w3c_traceparent, get_w3c_tracestate
8
10
  from finalsa.traceability.functions import (
9
11
  HTTP_HEADER_TRACEPARENT,
@@ -13,6 +15,18 @@ from finalsa.traceability.functions import (
13
15
  Headers = Mapping[str, str]
14
16
 
15
17
 
18
+ class InternalHttpError(BaseDomainException):
19
+ """Exception raised when a service is unavailable."""
20
+ response_code = 500
21
+
22
+ def __init__(self, response_code: int | None = None):
23
+ super().__init__(
24
+ message="Internal HTTP error",
25
+ response_code=response_code or self.response_code,
26
+ name="InternalHttpError"
27
+ )
28
+
29
+
16
30
  def normalize_base_url(
17
31
  base_url: str | None,
18
32
  default_scheme: str,
@@ -92,3 +106,11 @@ def get_package_version(package_name: str = "finalsa-http-client") -> str:
92
106
  except metadata.PackageNotFoundError:
93
107
  return "0.0.0"
94
108
 
109
+
110
+ def raise_for_response(response: Dict[str, Any], status: int) -> None:
111
+ if response.get("message") and response.get("name"):
112
+ raise BaseDomainException(
113
+ message=response.get("message"),
114
+ response_code=status,
115
+ name=response.get("name")
116
+ )
@@ -4,8 +4,7 @@ from collections.abc import Mapping
4
4
  from typing import Any
5
5
 
6
6
  import aiohttp
7
- from aiohttp import ClientResponseError, ClientSession, ClientTimeout
8
- from finalsa.common.models import BaseDomainException
7
+ from aiohttp import ClientSession, ClientTimeout
9
8
 
10
9
  from finalsa.http import _shared as shared
11
10
 
@@ -27,14 +26,12 @@ class BaseAsyncHttpClient:
27
26
  default_scheme: str = "http",
28
27
  service_name: str = "finalsa-http-client",
29
28
  trust_env: bool = False,
30
- raise_for_status: bool = True,
31
29
  session: ClientSession | None = None,
32
30
  ) -> None:
33
31
  self._default_scheme = default_scheme
34
32
  self.base_url = shared.normalize_base_url(base_url, default_scheme)
35
33
  self._timeout = self._normalize_timeout(timeout)
36
34
  self._trust_env = trust_env
37
- self._raise_for_status = raise_for_status
38
35
  self._session = session
39
36
  self._owns_session = session is None
40
37
  self._default_headers = shared.build_default_headers(
@@ -76,6 +73,16 @@ class BaseAsyncHttpClient:
76
73
  default_scheme=self._default_scheme,
77
74
  )
78
75
 
76
+ async def raise_for_status(self, response: aiohttp.ClientResponse) -> None:
77
+ headers = response.headers if isinstance(
78
+ response.headers, Mapping) else response['headers']
79
+ if response.status < 400:
80
+ return
81
+ if "Content-Type" in headers and headers["Content-Type"] == "application/json":
82
+ data = await response.json()
83
+ shared.raise_for_response(data, response.status)
84
+ raise shared.InternalHttpError(response.status)
85
+
79
86
  async def request(
80
87
  self,
81
88
  method: str,
@@ -102,31 +109,17 @@ class BaseAsyncHttpClient:
102
109
  )
103
110
  merged_headers = shared.merge_headers(self._default_headers, headers)
104
111
  request_timeout = self._normalize_timeout(timeout) or self._timeout
105
- response = None
106
- try:
107
-
108
- response = await session.request(
109
- method.upper(),
110
- resolved_url,
111
- headers=merged_headers,
112
- params=params,
113
- json=json,
114
- data=data,
115
- timeout=request_timeout,
116
- **kwargs,
117
- )
118
- response.raise_for_status()
119
- return response
120
- except Exception as e:
121
- if response is not None and "Content-Type" in response.headers and response.headers["Content-Type"] == "application/json":
122
- data = await response.json()
123
- if data.get("message") and data.get("name"):
124
- raise BaseDomainException(
125
- message=data.get("message"),
126
- response_code=response.status_code,
127
- name=data.get("name")
128
- ) from e
129
- raise e
112
+ response = await session.request(
113
+ method.upper(),
114
+ resolved_url,
115
+ headers=merged_headers,
116
+ params=params,
117
+ json=json,
118
+ data=data,
119
+ timeout=request_timeout,
120
+ **kwargs,
121
+ )
122
+ return response
130
123
 
131
124
  async def get(self, path: str, **kwargs: Any) -> aiohttp.ClientResponse:
132
125
  return await self.request("GET", path, **kwargs)
@@ -155,6 +148,7 @@ class BaseAsyncHttpClient:
155
148
  self._session = aiohttp.ClientSession(
156
149
  timeout=self._timeout,
157
150
  trust_env=self._trust_env,
151
+ raise_for_status=self.raise_for_status,
158
152
  )
159
153
  return self._session
160
154
 
@@ -4,7 +4,6 @@ from collections.abc import Mapping
4
4
  from typing import Any
5
5
 
6
6
  import requests
7
- from finalsa.common.models import BaseDomainException
8
7
 
9
8
  from finalsa.http import _shared as shared
10
9
 
@@ -117,12 +116,7 @@ class BaseSyncHttpClient:
117
116
  except requests.RequestException as e:
118
117
  if e.response is not None and "Content-Type" in e.response.headers and e.response.headers["Content-Type"] == "application/json":
119
118
  data = e.response.json()
120
- if data.get("message") and data.get("name"):
121
- raise BaseDomainException(
122
- message=data.get("message"),
123
- response_code=e.response.status_code,
124
- name=data.get("name")
125
- ) from e
119
+ shared.raise_for_response(data, e.response.status)
126
120
  raise e
127
121
 
128
122
  def get(self, path: str, **kwargs: Any) -> requests.Response:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: finalsa-common-http-client
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Summary: HTTP client library for common data types used in business applications
5
5
  Project-URL: Homepage, https://github.com/finalsa/finalsa-http-client
6
6
  Project-URL: Documentation, https://github.com/finalsa/finalsa-http-client#readme
@@ -10,7 +10,7 @@ Project-URL: Changelog, https://github.com/finalsa/finalsa-http-client/blob/main
10
10
  Author-email: Luis Jimenez <luis@finalsa.com>
11
11
  License: MIT License
12
12
 
13
- Copyright (c) 2021 Luis Diego Jiménez Delgado
13
+ Copyright (c) 2025 Luis Diego Jiménez Delgado
14
14
 
15
15
  Permission is hereby granted, free of charge, to any person obtaining a copy
16
16
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,10 @@
1
+ finalsa/http/__init__.py,sha256=tTeA_AP5gfFiXF783AxX5ZtSiQXxR1jkQ196J9YxXNk,419
2
+ finalsa/http/_shared.py,sha256=qrgleO4fbDZC3J1UNfdzMsl43yRItjAZa1I3XcRXT38,3471
3
+ finalsa/http/async_client/__init__.py,sha256=L25WfVpUpPZlMv6-cgeWlNvmK5UJOhLZ1NOndcIsA9w,73
4
+ finalsa/http/async_client/base.py,sha256=22L6QLfjAF4H8z9CcE8PhNuI2lJFbUkRLE405YXUFWw,5735
5
+ finalsa/http/sync_client/__init__.py,sha256=ocLE6dofwYCD9_rJpIP1KhlIujRFcaJfhoaeXVrSCVE,59
6
+ finalsa/http/sync_client/base.py,sha256=EWdfk4T5ggDMFppCO9xIRn6QKeiW9NqL-e5et3DFvGI,5121
7
+ finalsa_common_http_client-0.0.6.dist-info/METADATA,sha256=ZOaVl2JCLS3wnGlZcZiKZpjj38x2RBOHZ9fTVnpZFwo,9102
8
+ finalsa_common_http_client-0.0.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
+ finalsa_common_http_client-0.0.6.dist-info/licenses/LICENSE.md,sha256=yqzhfnTBr2S4lUBx-yibVPOIXRUDPrSUN9-_7AsC6OU,1084
10
+ finalsa_common_http_client-0.0.6.dist-info/RECORD,,
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 Luis Diego Jiménez Delgado
3
+ Copyright (c) 2025 Luis Diego Jiménez Delgado
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,10 +0,0 @@
1
- finalsa/http/__init__.py,sha256=tTeA_AP5gfFiXF783AxX5ZtSiQXxR1jkQ196J9YxXNk,419
2
- finalsa/http/_shared.py,sha256=cYrac3Tjai7fv8KiyKi7Y5M5u88Q-lcTFS3XsEgy-IQ,2729
3
- finalsa/http/async_client/__init__.py,sha256=L25WfVpUpPZlMv6-cgeWlNvmK5UJOhLZ1NOndcIsA9w,73
4
- finalsa/http/async_client/base.py,sha256=W9UEe_74ie82QmrdUx58dsYVskRmPPbnEaym3CicX0k,5998
5
- finalsa/http/sync_client/__init__.py,sha256=ocLE6dofwYCD9_rJpIP1KhlIujRFcaJfhoaeXVrSCVE,59
6
- finalsa/http/sync_client/base.py,sha256=9hGvci9USjmp9gG_uKAdcj-hsF79NJ3pvmNVlA5K6u8,5406
7
- finalsa_common_http_client-0.0.4.dist-info/METADATA,sha256=m2xYoFTcYN5bL6ZIKDoPrjtrdsdXL-S0du69X7uycsc,9102
8
- finalsa_common_http_client-0.0.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
- finalsa_common_http_client-0.0.4.dist-info/licenses/LICENSE.md,sha256=_lu-V-f2tGID1BS2V_W6D2XWppBsylFF1J2KEpfIXN0,1084
10
- finalsa_common_http_client-0.0.4.dist-info/RECORD,,