Skip to content

Commit 5a7ea21

Browse files
committed
handle transaction failures
1 parent 58f7d9a commit 5a7ea21

2 files changed

Lines changed: 40 additions & 31 deletions

File tree

main.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22

3-
from pydantic import HttpUrl
43
from rich import print
54
from rich.logging import RichHandler
65

@@ -23,12 +22,11 @@
2322

2423

2524
def main():
26-
with EDAClient(base_url="https://devbox.panda-cobra.ts.net") as eda:
27-
my_banner = banner("This is a test banner")
28-
eda.add_to_transaction_create(my_banner)
29-
resp = eda.commit_transaction()
25+
eda = EDAClient(base_url="https://devbox.panda-cobra.ts.net")
3026

31-
print(resp)
27+
my_banner = banner("This is a test banner")
28+
eda.add_to_transaction_create(my_banner)
29+
_ = eda.commit_transaction()
3230

3331
# iface = Interface(
3432
# apiVersion="interfaces.eda.nokia.com/v1alpha1",

src/client.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Any, Optional
2+
from typing import Any, List, Optional
33

44
import httpx
55
from pydantic import BaseModel
@@ -9,6 +9,7 @@
99
Transaction,
1010
TransactionContent,
1111
TransactionCr,
12+
TransactionDetails,
1213
TransactionType,
1314
TransactionValue,
1415
)
@@ -35,13 +36,14 @@ def __init__(self, base_url: str):
3536
self.transaction: Optional[Transaction] = None
3637
self.transaction_endpoint: str = self.base_url.join("/core/transaction/v1")
3738

39+
super().__init__(headers=self.headers, verify=False)
40+
3841
# acquire the token during initialization
3942
self.auth()
40-
super().__init__(headers=self.headers, verify=False)
4143

4244
def auth(self) -> None:
4345
"""Authenticate and get access token"""
44-
self.token = _get_access_token(self)
46+
self.token = self._get_access_token()
4547
self.headers = {
4648
"Authorization": f"Bearer {self.token}",
4749
"Content-Type": "application/json",
@@ -97,9 +99,12 @@ def commit_transaction(self) -> Any:
9799
tx_id: int = response.json()["id"]
98100
logger.info(f"Transaction {tx_id} committed")
99101

100-
self.get_transaction(tx_id)
102+
tx_details: TransactionDetails = self.get_transaction_details(tx_id)
103+
errs = self.tx_must_succeed(tx_details)
104+
if errs:
105+
logger.error(f"Transaction {tx_id} errors:\n - " + "\n - ".join(errs))
101106

102-
def get_transaction(self, tx_id: int) -> Any:
107+
def get_transaction_details(self, tx_id: int) -> TransactionDetails:
103108
"""Get transaction"""
104109

105110
params = {
@@ -113,12 +118,13 @@ def get_transaction(self, tx_id: int) -> Any:
113118
if response.status_code != 200:
114119
raise ValueError(response.text)
115120

116-
logger.info(f"Transaction {tx_id} details:\n{response.json()}")
121+
# logger.info(f"Transaction {tx_id} details:\n{response.json()}")
117122

123+
return TransactionDetails(**response.json())
118124

119-
def _get_client_secret(kc_url: str) -> str:
120-
with httpx.Client(verify=False) as client:
121-
token_url = f"{kc_url}/realms/{KC_REALM}/protocol/openid-connect/token"
125+
def _get_client_secret(self) -> str:
126+
client = self
127+
token_url = f"{self.kc_url}/realms/{KC_REALM}/protocol/openid-connect/token"
122128
token_data = {
123129
"grant_type": "password",
124130
"client_id": KC_CLIENT_ID,
@@ -133,7 +139,7 @@ def _get_client_secret(kc_url: str) -> str:
133139
access_token = token_response.json()["access_token"]
134140

135141
# Step 2: Fetch the `eda` client ID and secret
136-
admin_api_url = f"{kc_url}/admin/realms/{EDA_REALM}/clients"
142+
admin_api_url = f"{self.kc_url}/admin/realms/{EDA_REALM}/clients"
137143
auth_headers = {
138144
"Authorization": f"Bearer {access_token}",
139145
"Content-Type": "application/json",
@@ -161,23 +167,28 @@ def _get_client_secret(kc_url: str) -> str:
161167

162168
return client_secret
163169

170+
def _get_access_token(self) -> str:
171+
client_secret = self._get_client_secret()
172+
token_endpoint = (
173+
f"{self.kc_url}/realms/{EDA_REALM}/protocol/openid-connect/token"
174+
)
164175

165-
def _get_access_token(self: EDAClient) -> str:
166-
client_secret = _get_client_secret(self.kc_url)
167-
token_endpoint = f"{self.kc_url}/realms/{EDA_REALM}/protocol/openid-connect/token"
168-
169-
token_data = {
170-
"client_id": API_CLIENT_ID,
171-
"grant_type": "password",
172-
"scope": "openid",
173-
"username": "admin",
174-
"password": "admin",
175-
"client_secret": client_secret,
176-
}
176+
token_data = {
177+
"client_id": API_CLIENT_ID,
178+
"grant_type": "password",
179+
"scope": "openid",
180+
"username": "admin",
181+
"password": "admin",
182+
"client_secret": client_secret,
183+
}
177184

178-
headers = {"Content-Type": "application/x-www-form-urlencoded"}
185+
headers = {"Content-Type": "application/x-www-form-urlencoded"}
179186

180-
with httpx.Client(verify=False) as client:
181-
response = client.post(token_endpoint, data=token_data, headers=headers)
187+
response = self.post(token_endpoint, data=token_data, headers=headers)
182188
response.raise_for_status()
183189
return response.json()["access_token"]
190+
191+
def tx_must_succeed(self, tx_details: TransactionDetails) -> List[str] | None:
192+
"""Check if transaction succeeded"""
193+
if not tx_details.success:
194+
return tx_details.generalErrors

0 commit comments

Comments
 (0)