1- from typing import Optional
1+ import logging
2+ from typing import Any , Optional
23
34import httpx
4- from pydantic import BaseModel , HttpUrl
5+ from pydantic import BaseModel
6+ from rich import print
57
68from models .core import (
79 Transaction ,
1113 TransactionValue ,
1214)
1315
16+ logger = logging .getLogger (__name__ )
17+
1418EDA_VERSION = "v24.12.1"
1519
1620KC_REALM = "master"
@@ -47,7 +51,9 @@ def add_to_transaction_create(self, resource: BaseModel) -> None:
4751 """Add resource to transaction"""
4852 # convert the resource instance of whatever actual type to a TransactionContent
4953 content = TransactionContent (
50- ** resource .model_dump (exclude_unset = True , exclude_defaults = True )
54+ ** resource .model_dump (
55+ exclude_unset = True , exclude_none = True , exclude_defaults = True
56+ )
5157 )
5258
5359 if self .transaction is None :
@@ -67,12 +73,47 @@ def add_to_transaction_create(self, resource: BaseModel) -> None:
6773 )
6874 )
6975
70- # def run_transaction(self) -> Any:
71- # """Run transaction"""
72- # # convert the transaction instance to a dict
73- # transaction_dict = self.transaction.model_dump(
74- # exclude_unset=True, exclude_defaults=True
75- # )
76+ def commit_transaction (self ) -> Any :
77+ """Commit transaction"""
78+
79+ # convert the transaction instance to a dict
80+ if self .transaction is None :
81+ raise ValueError ("No transaction to commit" )
82+
83+ self .transaction .retain = True
84+ self .transaction .resultType = "normal"
85+
86+ content = self .transaction .model_dump_json (
87+ exclude_unset = True , exclude_none = True , exclude_defaults = True
88+ )
89+
90+ response = self .post (
91+ url = self .transaction_endpoint ,
92+ content = content ,
93+ )
94+ if response .status_code != 200 :
95+ raise ValueError (response .text )
96+
97+ tx_id : int = response .json ()["id" ]
98+ logger .info (f"Transaction { tx_id } committed" )
99+
100+ self .get_transaction (tx_id )
101+
102+ def get_transaction (self , tx_id : int ) -> Any :
103+ """Get transaction"""
104+
105+ params = {
106+ "waitForComplete" : "true" ,
107+ # "failOnErrors": "true"
108+ }
109+ response = self .get (
110+ url = f"{ self .transaction_endpoint } /details/{ tx_id } " ,
111+ params = params ,
112+ )
113+ if response .status_code != 200 :
114+ raise ValueError (response .text )
115+
116+ logger .info (f"Transaction { tx_id } details:\n { response .json ()} " )
76117
77118
78119def _get_client_secret (kc_url : str ) -> str :
0 commit comments