Skip to content

Commit 515e275

Browse files
committed
sync api
1 parent dca54be commit 515e275

2 files changed

Lines changed: 19 additions & 25 deletions

File tree

hello.py renamed to main.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,10 @@
99
from src import auth
1010

1111

12-
async def get_api_token():
13-
client_secret = await auth.get_client_secret()
14-
access_token = await auth.get_access_token(client_secret)
15-
print(f"Access Token: {access_token}")
16-
17-
1812
def main():
19-
# Run the asynchronous function
20-
asyncio.run(get_api_token())
13+
client_secret = auth.get_client_secret()
14+
access_token = auth.get_access_token(client_secret)
15+
print(f"Access Token: {access_token}")
2116

2217
# iface = Interface(
2318
# apiVersion="interfaces.eda.nokia.com/v1alpha1",

src/auth.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@
1111
EDA_REALM = "eda"
1212
API_CLIENT_ID = "eda"
1313

14-
# Step 1: Get an access token
15-
token_url = f"{KC_KEYCLOAK_URL}/realms/{KC_REALM}/protocol/openid-connect/token"
16-
token_data = {
17-
"grant_type": "password",
18-
"client_id": KC_CLIENT_ID,
19-
"username": KC_USERNAME,
20-
"password": KC_PASSWORD,
21-
}
22-
headers = {"Content-Type": "application/x-www-form-urlencoded"}
2314

15+
def get_client_secret() -> str:
16+
with httpx.Client(verify=False) as client:
17+
token_url = f"{KC_KEYCLOAK_URL}/realms/{KC_REALM}/protocol/openid-connect/token"
18+
token_data = {
19+
"grant_type": "password",
20+
"client_id": KC_CLIENT_ID,
21+
"username": KC_USERNAME,
22+
"password": KC_PASSWORD,
23+
}
24+
headers = {"Content-Type": "application/x-www-form-urlencoded"}
2425

25-
async def get_client_secret() -> str:
26-
async with httpx.AsyncClient(verify=False) as client:
2726
# Fetch access token
28-
token_response = await client.post(token_url, data=token_data, headers=headers)
27+
token_response = client.post(token_url, data=token_data, headers=headers)
2928
token_response.raise_for_status()
3029
access_token = token_response.json()["access_token"]
3130

@@ -37,7 +36,7 @@ async def get_client_secret() -> str:
3736
}
3837

3938
# Fetch clients
40-
clients_response = await client.get(admin_api_url, headers=auth_headers)
39+
clients_response = client.get(admin_api_url, headers=auth_headers)
4140
clients_response.raise_for_status()
4241
clients = clients_response.json()
4342

@@ -52,14 +51,14 @@ async def get_client_secret() -> str:
5251
# Fetch the client secret
5352
client_id = eda_client["id"]
5453
client_secret_url = f"{admin_api_url}/{client_id}/client-secret"
55-
secret_response = await client.get(client_secret_url, headers=auth_headers)
54+
secret_response = client.get(client_secret_url, headers=auth_headers)
5655
secret_response.raise_for_status()
5756
client_secret = secret_response.json()["value"]
5857

5958
return client_secret
6059

6160

62-
async def get_access_token(client_secret: str) -> str:
61+
def get_access_token(client_secret: str) -> str:
6362
token_endpoint = (
6463
f"{KC_KEYCLOAK_URL}/realms/{EDA_REALM}/protocol/openid-connect/token"
6564
)
@@ -75,7 +74,7 @@ async def get_access_token(client_secret: str) -> str:
7574

7675
headers = {"Content-Type": "application/x-www-form-urlencoded"}
7776

78-
async with httpx.AsyncClient(verify=False) as client:
79-
response = await client.post(token_endpoint, data=token_data, headers=headers)
77+
with httpx.Client(verify=False) as client:
78+
response = client.post(token_endpoint, data=token_data, headers=headers)
8079
response.raise_for_status()
8180
return response.json()["access_token"]

0 commit comments

Comments
 (0)