-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestScript.py
More file actions
93 lines (72 loc) · 2.79 KB
/
Copy pathRequestScript.py
File metadata and controls
93 lines (72 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 9 14:19:33 2024
@author: Basem
"""
import requests
class KeycloakClient:
def __init__(self, base_url, realm, client_id, client_secret, username, password):
self.base_url = base_url
self.realm = realm
self.client_id = client_id
self.client_secret = client_secret
self.username = username
self.password = password
self.token = None
def request_token(self):
url = f"{self.base_url}/realms/{self.realm}/protocol/openid-connect/token"
data = {
'client_id': self.client_id,
'client_secret': self.client_secret,
'grant_type': 'password',
'username': self.username,
'password': self.password
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.post(url, data=data, headers=headers)
if response.status_code == 200:
self.token = response.json().get('access_token')
print("Token retrieved successfully")
else:
raise Exception(f"Failed to retrieve token: {response.status_code} - {response.text}")
def send_request(self, method, api_url, json_data=None):
if self.token is None:
raise Exception("Token not found. Call 'get_token()' first.")
headers = {
'Authorization': f'Bearer {self.token}',
'Content-Type': 'application/json'
}
response = requests.request(method, api_url, headers=headers, json=json_data)
if response.status_code == 200:
print("Request successful:", response.json())
elif response.status_code == 201:
print("Resource created:", response.json())
elif response.status_code == 403:
print("Unauthorized Access: 403")
else:
print(f"Request failed: {response.status_code} - {response.text}")
return response
if __name__ == "__main__" :
# Keycloak settings
base_url = "http://localhost:8080"
realm = "todorealm"
client_id = "todo-client"
client_secret = "****"
username = "foo"
password = "password"
if int(input("0 for authorised, 1 for unauthorised")):
username = "bar"
print(username)
kc_client = KeycloakClient(base_url, realm, client_id, client_secret, username, password)
kc_client.request_token()
todo_api_url = "http://localhost:5272/api/TodoItems"
for i in range(1,10):
todo_record = {
"name": f"Item {i}",
"isComplete": i%2==0
}
kc_client.send_request("POST", todo_api_url, json_data=todo_record)
print('\n\n\n')
kc_client.send_request("GET", todo_api_url)