Skip to content

Commit 342536c

Browse files
committed
Merge dev into main
2 parents 6e5a444 + 6afa711 commit 342536c

24 files changed

Lines changed: 1852 additions & 398 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OSBot-Fast-API
22

3-
![Current Release](https://img.shields.io/badge/release-v0.18.0-blue)
3+
![Current Release](https://img.shields.io/badge/release-v0.18.2-blue)
44
![Python](https://img.shields.io/badge/python-3.8+-green)
55
![FastAPI](https://img.shields.io/badge/FastAPI-0.100+-red)
66
![Type-Safe](https://img.shields.io/badge/Type--Safe-✓-brightgreen)

osbot_fast_api/admin_ui/api/routes/Routes__Admin__Config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ def api__routes_grouped(self) -> Dict[str, List[Dict[str, Any]]]:
4242
def api__middlewares(self) -> List[Dict[str, Any]]: # Get middleware information
4343
if not self.parent_app:
4444
return []
45-
46-
return self.parent_app.user_middlewares()
45+
return self.parent_app.user_middlewares(include_params=False)
4746

4847
def api__openapi_spec(self) -> Dict[str, Any]: # Get OpenAPI specification
4948
if not self.parent_app:

osbot_fast_api/admin_ui/api/routes/Routes__Admin__Cookies.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ class Cookie__Template(Type_Safe): # Template for common cookie
3030
description : str
3131
cookies : List[Cookie__Config]
3232

33-
class Cookies__Templates(Type_Safe):
34-
cookies_templates : List[Cookie__Template]
33+
class Cookie__Bulk_Request(Type_Safe):
34+
name : str
35+
value : str
36+
expires_in : Optional[int] = None
37+
38+
class Cookies__Bulk_Request(Type_Safe):
39+
cookies : List[Cookie__Bulk_Request]
3540

3641
class Routes__Admin__Cookies(Fast_API__Routes): # API routes for cookie management
3742
tag = 'admin-cookies'
@@ -144,7 +149,7 @@ def api__cookies_list(self, request: Request) -> List[Dict[str, Any]]:
144149
def api__cookies_templates(self) -> List[Dict[str, Any]]: # Get available cookie templates
145150
return self.COOKIE_TEMPLATES # todo: see note above in COOKIE_TEMPLATES
146151

147-
def api__cookie_get(self, cookie_name: str, request: Request) -> Dict[str, Any]: # Get a specific cookie value
152+
def api__cookie_get__cookie_name(self, cookie_name: str, request: Request) -> Dict[str, Any]: # Get a specific cookie value
148153
value = request.cookies.get(cookie_name)
149154

150155
config = None # Find cookie config
@@ -193,33 +198,33 @@ def api__cookie_set__cookie_name(self, cookie_name : str ,
193198
"name" : cookie_name , # we also need to standardise the return object/class
194199
"value_set": len(cookie_value.value) > 0 }
195200

196-
def api__cookie_delete(self, cookie_name: str, response: Response) -> Dict[str, Any]: # Delete a cookie
201+
def api__cookie_delete__cookie_name(self, cookie_name: str, response: Response) -> Dict[str, Any]: # Delete a cookie
197202
response.delete_cookie(key=cookie_name)
198203

199204
return { "success" : True , # todo: convert this to a Schema_* class
200205
"name" : cookie_name,
201206
"deleted" : True }
202207

203-
def api__cookies_bulk_set(self, cookies_templates : Cookies__Templates, #cookies: List[Dict[str, str]],
204-
request : Request ,
205-
response : Response
208+
def api__cookies_bulk_set(self, bulk_request : Cookies__Bulk_Request, #cookies: List[Dict[str, str]],
209+
request : Request ,
210+
response : Response
206211
) -> Dict[str, Any]: # Set multiple cookies at once"""
207212
results = []
208213

209-
for cookie_data in cookies_templates:
210-
cookie_name = cookie_data.get('name')
211-
cookie_value = Cookie__Value(value=cookie_data.get('value', ''))
214+
for cookie_data in bulk_request.cookies:
215+
cookie_name = cookie_data.name
216+
cookie_value = Cookie__Value(value=cookie_data.value , expires_in=cookie_data.expires_in)
212217

213218
if cookie_name:
214-
result = self.api__cookie_set(cookie_name, cookie_value, request, response)
219+
result = self.api__cookie_set__cookie_name(cookie_name, cookie_value, request, response)
215220
results.append(result)
216221

217222
return {
218223
"success": all(r.get('success') for r in results),
219224
"results": results
220225
}
221226

222-
def api__generate_value(self, value_type: str = "uuid") -> Dict[str, str]:
227+
def api__generate_value__value_type(self, value_type: str = "uuid") -> Dict[str, str]:
223228
"""Generate a value for cookies (UUID, random string, etc.)"""
224229
if value_type == "uuid":
225230
return {"value": random_guid(), "type": "uuid"}
@@ -245,10 +250,10 @@ def _validate_cookie_value(self, value: Optional[str], pattern: Optional[str]) -
245250
return True
246251

247252
def setup_routes(self):
248-
self.add_route_get (self.api__cookies_list )
249-
self.add_route_get (self.api__cookies_templates )
250-
self.add_route_get (self.api__cookie_get )
251-
self.add_route_post (self.api__cookie_set__cookie_name )
252-
self.add_route_delete(self.api__cookie_delete )
253-
self.add_route_post (self.api__cookies_bulk_set )
254-
self.add_route_get (self.api__generate_value )
253+
self.add_route_get (self.api__cookies_list )
254+
self.add_route_get (self.api__cookies_templates )
255+
self.add_route_get (self.api__cookie_get__cookie_name )
256+
self.add_route_post (self.api__cookie_set__cookie_name )
257+
self.add_route_delete(self.api__cookie_delete__cookie_name)
258+
self.add_route_post (self.api__cookies_bulk_set )
259+
self.add_route_get (self.api__generate_value__value_type )

osbot_fast_api/admin_ui/api/routes/Routes__Admin__Docs.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ def api__client_examples(self) -> Dict[str, Any]: # Get client code ex
123123
return examples
124124

125125
def api__api_info(self) -> Dict[str, Any]: # Get API metadata and information
126-
if not self.parent_app:
127-
return {"error": "Parent app not configured"}
128126

129127
openapi = self.parent_app.open_api_json()
130128

osbot_fast_api/admin_ui/api/routes/Routes__Admin__Info.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ class Routes__Admin__Info(Fast_API__Routes): # API routes for admin dashb
1010
parent_app = None # Will be set by Admin_UI__Fast_API
1111

1212
def api__server_info(self) -> Dict[str, Any]: # Get server information # todo: convert this object to Schema__Fast_API__Admin__Server_Info
13-
return { "server_id" : str(fast_api__server_info.server_id),
14-
"server_name" : str(fast_api__server_info.server_name) if fast_api__server_info.server_name else "unnamed",
15-
"server_instance_id": str(fast_api__server_info.server_instance_id),
13+
return { "server_id" : fast_api__server_info.server_id or '' ,
14+
"server_name" : fast_api__server_info.server_name or "unnamed",
15+
"server_instance_id": fast_api__server_info.server_instance_id,
1616
"server_boot_time" : int(fast_api__server_info.server_boot_time),
1717
"current_time" : timestamp_utc_now(),
1818
"uptime_ms" : timestamp_utc_now() - int(fast_api__server_info.server_boot_time) # double check this calculation
1919
}
2020

2121
def api__app_info(self) -> Dict[str, Any]: # Get FastAPI application information # todo: convert this object to Schema__Fast_API__Admin__App_Info
22-
if not self.parent_app:
23-
return {"error": "Parent app not configured"}
2422

2523
return { "name" : self.parent_app.name ,
2624
"version" : self.parent_app.version ,
@@ -31,8 +29,6 @@ def api__app_info(self) -> Dict[str, Any]: # Get FastAPI applicatio
3129
"enable_api_key": self.parent_app.enable_api_key }
3230

3331
def api__stats(self) -> Dict[str, Any]: # Get application statistics
34-
if not self.parent_app:
35-
return {"error": "Parent app not configured"}
3632

3733
routes = self.parent_app.routes(include_default=False, expand_mounts=True)
3834

osbot_fast_api/admin_ui/static/js/admin_ui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export class AdminUI {
236236
}
237237

238238
async generateValue(type = 'uuid') {
239-
return await this.apiCall(`/admin-cookies/api/generate-value?value_type=${type}`);
239+
return await this.apiCall(`/admin-cookies/api/generate-value/${type}`);
240240
}
241241

242242
// Utility Methods

osbot_fast_api/admin_ui/static/js/components/cookie_editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ class CookieEditor extends HTMLElement {
948948

949949
async generateValue(type) {
950950
try {
951-
const response = await fetch(`${this.apiBase}/generate-value?value_type=${type}`);
951+
const response = await fetch(`${this.apiBase}/generate-value/${type}`);
952952
const result = await response.json();
953953

954954
if (result.value) {

osbot_fast_api/api/Fast_API.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def setup_middleware__http_events(self):
247247
return self
248248

249249

250-
def user_middlewares(self):
250+
def user_middlewares(self, include_params=True):
251251
import types
252252

253253
middlewares = []
@@ -261,8 +261,9 @@ def user_middlewares(self):
261261
else:
262262
function_name = None
263263
middleware = { 'type' : type_name ,
264-
'function_name': function_name ,
265-
'params' : options }
264+
'function_name': function_name }
265+
if include_params:
266+
middleware['params'] = options
266267
middlewares.append(middleware)
267268
return middlewares
268269

osbot_fast_api/utils/Fast_API_Server.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,24 @@ def stop(self):
6666
self.running = False
6767
return result
6868

69+
def requests_delete(self, path='', **kwargs):
70+
url = url_join_safe(self.url(), path)
71+
return requests.delete(url, **kwargs)
72+
6973
def requests_get(self, path='', **kwargs):
7074
url = url_join_safe(self.url(), path)
7175
return requests.get(url, **kwargs)
7276

73-
def requests_post(self, path='', data=None):
74-
if Type_Safe in base_types(data):
75-
json_data = data.json()
76-
elif type(data) is dict:
77-
json_data = data
78-
else:
79-
raise ValueError("data must be a Type_Safe or a dict")
77+
def requests_post(self, path='', data=None, json=None, **kwargs):
78+
if json is None:
79+
if Type_Safe in base_types(data):
80+
json = data.json()
81+
elif type(data) is dict:
82+
json = data
83+
else:
84+
raise ValueError("data must be a Type_Safe or a dict")
8085
url = urljoin(self.url(), path)
81-
return requests.post(url, json=json_data)
86+
return requests.post(url, json=json, **kwargs)
8287

8388
def url(self):
8489
return f'http://{FAST_API__HOST}:{self.port}/'

osbot_fast_api/utils/Fast_API__Server_Info.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ class Fast_API__Server_Info(Type_Safe):
1414
server_instance_id: Random_Guid
1515
server_boot_time : Timestamp_Now
1616

17+
1718
fast_api__server_info = Fast_API__Server_Info()

0 commit comments

Comments
 (0)