11# Generated by actant-sdk-codegen. Edit the template, not this file.
2- import urllib .request , json
2+ import json
3+ import urllib .parse
4+ import urllib .request
5+
6+ _ALLOWED_URL_SCHEMES = {"http" , "https" }
7+
8+
9+ def _validate_url_scheme (url ):
10+ scheme = urllib .parse .urlparse (url ).scheme
11+ if scheme not in _ALLOWED_URL_SCHEMES :
12+ allowed = ", " .join (sorted (_ALLOWED_URL_SCHEMES ))
13+ raise ValueError (f"ActantClient only supports these URL schemes: { allowed } " )
14+
15+
16+ def _build_http_opener ():
17+ opener = urllib .request .OpenerDirector ()
18+ opener .add_handler (urllib .request .HTTPHandler ())
19+ opener .add_handler (urllib .request .HTTPSHandler ())
20+ opener .add_handler (urllib .request .HTTPDefaultErrorHandler ())
21+ opener .add_handler (urllib .request .HTTPRedirectHandler ())
22+ opener .add_handler (urllib .request .HTTPErrorProcessor ())
23+ return opener
24+
25+
26+ _HTTP_OPENER = _build_http_opener ()
27+
28+
29+ def _open_http_request (req ):
30+ _validate_url_scheme (req .full_url )
31+ return _HTTP_OPENER .open (req )
32+
333
434class ActantClient :
535 def __init__ (self , base = "http://127.0.0.1:4555" ):
36+ _validate_url_scheme (base )
637 self .base = base
738
839 def command (self , workspace_id , actor_id , command_type , input_ , idempotency_key = None ):
@@ -19,5 +50,5 @@ def command(self, workspace_id, actor_id, command_type, input_, idempotency_key=
1950 data = json .dumps (body ).encode ("utf-8" ),
2051 headers = {"content-type" : "application/json" },
2152 )
22- with urllib . request . urlopen (req ) as r :
53+ with _open_http_request (req ) as r :
2354 return json .loads (r .read ())
0 commit comments