Skip to content

Commit 7872e7b

Browse files
committed
useless commit message to save state
1 parent 8ee429f commit 7872e7b

File tree

4 files changed

+94
-18
lines changed

4 files changed

+94
-18
lines changed

main.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
import logging
22

3+
from pydantic import HttpUrl
4+
from rich import print
5+
from rich.logging import RichHandler
6+
37
from models.com.nokia.eda.interfaces.v1alpha1 import (
48
Interface,
59
Member,
610
Metadata,
711
SpecModel,
812
)
913
from src.banner import banner
10-
from src.client import Client
14+
from src.client import EDAClient
1115

16+
# Replace the basic logging config with Rich handler
1217
logging.basicConfig(
13-
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
18+
level=logging.INFO,
19+
format="%(message)s",
20+
handlers=[RichHandler(rich_tracebacks=True, markup=True)],
1421
)
1522
logger = logging.getLogger(__name__)
1623

1724

1825
def main():
19-
c = Client(base_url="https://devbox.panda-cobra.ts.net")
20-
c.auth()
21-
logger.info(f"Access Token: {c.token}")
26+
eda = EDAClient(base_url="https://devbox.panda-cobra.ts.net")
27+
logger.info(f"Access Token: {eda.token}")
2228

2329
my_banner = banner("This is a test banner")
2430
# print(my_banner.model_dump())
25-
c.add_to_transaction_create(my_banner)
31+
eda.add_to_transaction_create(my_banner)
2632

2733
# iface = Interface(
2834
# apiVersion="interfaces.eda.nokia.com/v1alpha1",

pyproject.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ version = "0.1.0"
44
description = "Working with EDA OpenAPI specification"
55
readme = "README.md"
66
requires-python = "~=3.12"
7-
dependencies = ["httpx==0.28.1", "pydantic==2.10.5"]
7+
dependencies = [
8+
"httpx==0.28.1",
9+
"pydantic==2.10.5",
10+
"rich>=13.9.4",
11+
]

src/client.py

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import Any, Optional
1+
from typing import Optional
22

33
import httpx
4-
from pydantic import BaseModel
4+
from pydantic import BaseModel, HttpUrl
55

6-
from models.com.nokia.eda.siteinfo.v1alpha1 import Banner
76
from models.core import (
87
Transaction,
98
TransactionContent,
@@ -22,20 +21,31 @@
2221
API_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

5778
def _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

uv.lock

+45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)