1
- from typing import Any , Optional
1
+ from typing import Optional
2
2
3
3
import httpx
4
- from pydantic import BaseModel
4
+ from pydantic import BaseModel , HttpUrl
5
5
6
- from models .com .nokia .eda .siteinfo .v1alpha1 import Banner
7
6
from models .core import (
8
7
Transaction ,
9
8
TransactionContent ,
22
21
API_CLIENT_ID = "eda"
23
22
24
23
25
- class Client (httpx .Client ):
24
+ class EDAClient (httpx .Client ):
26
25
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 = ""
31
31
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 )
32
37
33
38
def auth (self ) -> None :
34
39
"""Authenticate and get access token"""
35
40
self .token = _get_access_token (self )
41
+ self .headers = {
42
+ "Authorization" : f"Bearer { self .token } " ,
43
+ "Content-Type" : "application/json" ,
44
+ }
36
45
37
46
def add_to_transaction_create (self , resource : BaseModel ) -> None :
38
47
"""Add resource to transaction"""
48
+ # convert the resource instance of whatever actual type to a TransactionContent
39
49
content = TransactionContent (
40
50
** resource .model_dump (exclude_unset = True , exclude_defaults = True )
41
51
)
@@ -50,8 +60,19 @@ def add_to_transaction_create(self, resource: BaseModel) -> None:
50
60
description = "" ,
51
61
dryRun = False ,
52
62
)
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
+ # )
55
76
56
77
57
78
def _get_client_secret (kc_url : str ) -> str :
@@ -100,7 +121,7 @@ def _get_client_secret(kc_url: str) -> str:
100
121
return client_secret
101
122
102
123
103
- def _get_access_token (self : Client ) -> str :
124
+ def _get_access_token (self : EDAClient ) -> str :
104
125
client_secret = _get_client_secret (self .kc_url )
105
126
token_endpoint = f"{ self .kc_url } /realms/{ EDA_REALM } /protocol/openid-connect/token"
106
127
0 commit comments