1- from typing import Any , Optional
1+ from typing import Optional
22
33import httpx
4- from pydantic import BaseModel
4+ from pydantic import BaseModel , HttpUrl
55
6- from models .com .nokia .eda .siteinfo .v1alpha1 import Banner
76from models .core import (
87 Transaction ,
98 TransactionContent ,
2221API_CLIENT_ID = "eda"
2322
2423
25- class Client (httpx .Client ):
24+ class EDAClient (httpx .Client ):
2625 def __init__ (self , base_url : str ):
27- super ().__init__ ()
28- self .base_url = base_url
29- self .kc_url = f"{ self .base_url } /core/httpproxy/v1/keycloak/"
30- self .token = ""
26+ self .base_url : str = base_url
27+ self .kc_url : str = self .base_url .join ("/core/httpproxy/v1/keycloak" )
28+
29+ self .headers : dict [str , str ] = {}
30+ self .token : str = ""
3131 self .transaction : Optional [Transaction ] = None
32+ self .transaction_endpoint : str = self .base_url .join ("/core/transaction/v1" )
33+
34+ # acquire the token during initialization
35+ self .auth ()
36+ super ().__init__ (headers = self .headers , verify = False )
3237
3338 def auth (self ) -> None :
3439 """Authenticate and get access token"""
3540 self .token = _get_access_token (self )
41+ self .headers = {
42+ "Authorization" : f"Bearer { self .token } " ,
43+ "Content-Type" : "application/json" ,
44+ }
3645
3746 def add_to_transaction_create (self , resource : BaseModel ) -> None :
3847 """Add resource to transaction"""
48+ # convert the resource instance of whatever actual type to a TransactionContent
3949 content = TransactionContent (
4050 ** resource .model_dump (exclude_unset = True , exclude_defaults = True )
4151 )
@@ -50,8 +60,19 @@ def add_to_transaction_create(self, resource: BaseModel) -> None:
5060 description = "" ,
5161 dryRun = False ,
5262 )
53- # else:
54- # self.transaction.resources.append(resource)
63+ else :
64+ self .transaction .crs .append (
65+ TransactionCr (
66+ type = TransactionType (create = TransactionValue (value = content ))
67+ )
68+ )
69+
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+ # )
5576
5677
5778def _get_client_secret (kc_url : str ) -> str :
@@ -100,7 +121,7 @@ def _get_client_secret(kc_url: str) -> str:
100121 return client_secret
101122
102123
103- def _get_access_token (self : Client ) -> str :
124+ def _get_access_token (self : EDAClient ) -> str :
104125 client_secret = _get_client_secret (self .kc_url )
105126 token_endpoint = f"{ self .kc_url } /realms/{ EDA_REALM } /protocol/openid-connect/token"
106127
0 commit comments