23
23
from src .utils .user_agent import get_user_agent
24
24
25
25
T = TypeVar ("T" )
26
+
27
+
26
28
class PortClient :
27
29
"""Client for interacting with the Port API."""
28
30
@@ -46,9 +48,9 @@ def __init__(
46
48
client_secret = client_secret ,
47
49
us_region = (region == "US" ),
48
50
)
49
-
51
+
50
52
self ._setup_custom_headers ()
51
-
53
+
52
54
self .agent = PortAgentClient (self ._client )
53
55
self .blueprints = PortBlueprintClient (self ._client )
54
56
self .entities = PortEntityClient (self ._client )
@@ -61,25 +63,23 @@ def _setup_custom_headers(self):
61
63
"""Setup custom headers for all HTTP requests."""
62
64
user_agent = get_user_agent ()
63
65
logger .debug (f"Setting User-Agent header: { user_agent } " )
64
-
66
+
65
67
original_make_request = self ._client .make_request
66
-
68
+
67
69
def make_request_with_headers (* args , ** kwargs ):
68
70
"""Wrapper to add custom headers to all requests."""
69
- if ' headers' not in kwargs :
70
- kwargs [' headers' ] = {}
71
-
72
- kwargs [' headers' ][ ' User-Agent' ] = user_agent
73
-
71
+ if " headers" not in kwargs :
72
+ kwargs [" headers" ] = {}
73
+
74
+ kwargs [" headers" ][ " User-Agent" ] = user_agent
75
+
74
76
return original_make_request (* args , ** kwargs )
75
-
77
+
76
78
self ._client .make_request = make_request_with_headers
77
79
78
80
def handle_http_error (self , e : requests .exceptions .HTTPError ) -> PortError :
79
81
result = e .response .json ()
80
- message = (
81
- f"Error in { e .request .method } { e .request .url } - { e .response .status_code } : { result } "
82
- )
82
+ message = f"Error in { e .request .method } { e .request .url } - { e .response .status_code } : { result } "
83
83
logger .error (message )
84
84
raise PortError (message )
85
85
@@ -110,99 +110,63 @@ async def update_blueprint(self, blueprint_data: dict[str, Any]) -> Blueprint:
110
110
return await self .wrap_request (lambda : self .blueprints .update_blueprint (blueprint_data ))
111
111
112
112
async def delete_blueprint (self , blueprint_identifier : str ) -> bool :
113
- return await self .wrap_request (
114
- lambda : self .blueprints .delete_blueprint (blueprint_identifier )
115
- )
113
+ return await self .wrap_request (lambda : self .blueprints .delete_blueprint (blueprint_identifier ))
116
114
117
115
async def get_entity (self , blueprint_identifier : str , entity_identifier : str ) -> EntityResult :
118
- return await self .wrap_request (
119
- lambda : self .entities .get_entity (blueprint_identifier , entity_identifier )
120
- )
116
+ return await self .wrap_request (lambda : self .entities .get_entity (blueprint_identifier , entity_identifier ))
121
117
122
118
async def get_entities (self , blueprint_identifier : str ) -> list [EntityResult ]:
123
119
return await self .wrap_request (lambda : self .entities .get_entities (blueprint_identifier ))
124
120
125
121
async def search_entities (self , blueprint_identifier : str , search_query : dict [str , Any ]) -> list [EntityResult ]:
126
122
return await self .wrap_request (lambda : self .entities .search_entities (blueprint_identifier , search_query ))
127
123
128
- async def create_entity (
129
- self , blueprint_identifier : str , entity_data : dict [str , Any ], query : dict [str , Any ]
130
- ) -> EntityResult :
131
- return await self .wrap_request (
132
- lambda : self .entities .create_entity (blueprint_identifier , entity_data , query )
133
- )
124
+ async def create_entity (self , blueprint_identifier : str , entity_data : dict [str , Any ], query : dict [str , Any ]) -> EntityResult :
125
+ return await self .wrap_request (lambda : self .entities .create_entity (blueprint_identifier , entity_data , query ))
134
126
135
- async def update_entity (
136
- self , blueprint_identifier : str , entity_identifier : str , entity_data : dict [str , Any ]
137
- ) -> EntityResult :
138
- return await self .wrap_request (
139
- lambda : self .entities .update_entity (
140
- blueprint_identifier , entity_identifier , entity_data
141
- )
142
- )
127
+ async def update_entity (self , blueprint_identifier : str , entity_identifier : str , entity_data : dict [str , Any ]) -> EntityResult :
128
+ return await self .wrap_request (lambda : self .entities .update_entity (blueprint_identifier , entity_identifier , entity_data ))
143
129
144
- async def delete_entity (
145
- self , blueprint_identifier : str , entity_identifier : str , delete_dependents : bool = False
146
- ) -> bool :
130
+ async def delete_entity (self , blueprint_identifier : str , entity_identifier : str , delete_dependents : bool = False ) -> bool :
147
131
return await self .wrap_request (
148
- lambda : self .entities .delete_entity (
149
- blueprint_identifier , entity_identifier , delete_dependents
150
- )
132
+ lambda : self .entities .delete_entity (blueprint_identifier , entity_identifier , delete_dependents )
151
133
)
152
134
153
135
async def get_scorecard (self , blueprint_id : str , scorecard_id : str ) -> Scorecard :
154
- return await self .wrap_request (
155
- lambda : self .scorecards .get_scorecard (blueprint_id , scorecard_id )
156
- )
136
+ return await self .wrap_request (lambda : self .scorecards .get_scorecard (blueprint_id , scorecard_id ))
157
137
158
138
async def get_scorecards (self , blueprint_identifier : str ) -> list [Scorecard ]:
159
139
return await self .wrap_request (lambda : self .scorecards .get_scorecards (blueprint_identifier ))
160
140
161
- async def create_scorecard (
162
- self , blueprint_id : str , scorecard_data : dict [str , Any ]
163
- ) -> Scorecard :
164
- return await self .wrap_request (
165
- lambda : self .scorecards .create_scorecard (blueprint_id , scorecard_data )
166
- )
141
+ async def create_scorecard (self , blueprint_id : str , scorecard_data : dict [str , Any ]) -> Scorecard :
142
+ return await self .wrap_request (lambda : self .scorecards .create_scorecard (blueprint_id , scorecard_data ))
167
143
168
- async def update_scorecard (
169
- self , blueprint_id : str , scorecard_id : str , scorecard_data : dict [str , Any ]
170
- ) -> Scorecard :
171
- return await self .wrap_request (
172
- lambda : self .scorecards .update_scorecard (blueprint_id , scorecard_id , scorecard_data )
173
- )
144
+ async def update_scorecard (self , blueprint_id : str , scorecard_id : str , scorecard_data : dict [str , Any ]) -> Scorecard :
145
+ return await self .wrap_request (lambda : self .scorecards .update_scorecard (blueprint_id , scorecard_id , scorecard_data ))
174
146
175
147
async def delete_scorecard (self , scorecard_id : str , blueprint_id : str ) -> bool :
176
- return await self .wrap_request (
177
- lambda : self .scorecards .delete_scorecard (scorecard_id , blueprint_id )
178
- )
148
+ return await self .wrap_request (lambda : self .scorecards .delete_scorecard (scorecard_id , blueprint_id ))
179
149
180
150
async def get_all_actions (self , trigger_type : str = "self-service" ) -> list [Action ]:
181
151
return await self .wrap_request (lambda : self .actions .get_all_actions (trigger_type ))
182
152
183
153
async def get_action (self , action_identifier : str ) -> Action :
184
154
return await self .wrap_request (lambda : self .actions .get_action (action_identifier ))
185
-
155
+
186
156
async def create_action (self , action_data : dict [str , Any ]) -> Action :
187
157
return await self .wrap_request (lambda : self .actions .create_action (action_data ))
188
158
189
159
async def update_action (self , action_identifier : str , action_data : dict [str , Any ]) -> Action :
190
- return await self .wrap_request (
191
- lambda : self .actions .update_action (action_identifier , action_data )
192
- )
160
+ return await self .wrap_request (lambda : self .actions .update_action (action_identifier , action_data ))
193
161
194
162
async def delete_action (self , action_identifier : str ) -> bool :
195
163
return await self .wrap_request (lambda : self .actions .delete_action (action_identifier ))
196
-
164
+
197
165
async def create_global_action_run (self , action_identifier : str , ** kwargs ) -> ActionRun :
198
- return await self .wrap_request (
199
- lambda : self .action_runs .create_global_action_run (action_identifier , ** kwargs )
200
- )
166
+ return await self .wrap_request (lambda : self .action_runs .create_global_action_run (action_identifier , ** kwargs ))
201
167
202
168
async def create_entity_action_run (self , action_identifier : str , ** kwargs ) -> ActionRun :
203
- return await self .wrap_request (
204
- lambda : self .action_runs .create_entity_action_run (action_identifier , ** kwargs )
205
- )
169
+ return await self .wrap_request (lambda : self .action_runs .create_entity_action_run (action_identifier , ** kwargs ))
206
170
207
171
async def get_action_run (self , run_id : str ) -> ActionRun :
208
172
return await self .wrap_request (lambda : self .action_runs .get_action_run (run_id ))
0 commit comments