@@ -302,9 +302,87 @@ def _parse_json_safely(self, content: str) -> Union[Dict[str, Any], str]:
302302 except json .JSONDecodeError :
303303 return content
304304
305+ def _resolve_variables_in_text (
306+ self , text : str , variable_context : Optional [Dict [str , Any ]] = None
307+ ) -> str :
308+ """
309+ Utility method to resolve variables in text content.
310+
311+ Args:
312+ text: Text containing variable placeholders
313+ variable_context: Dictionary of variables for resolution
314+
315+ Returns:
316+ Text with resolved variables
317+ """
318+ if not text or not variable_context :
319+ return text
320+
321+ import re
322+
323+ def replace_placeholder (match ):
324+ var_name = match .group (1 ).strip ()
325+ if var_name in variable_context :
326+ return str (variable_context [var_name ])
327+ return match .group (0 ) # Keep placeholder if variable not found
328+
329+ return re .sub (r'\{\{([^}]+)\}\}' , replace_placeholder , text )
330+
331+ def _get_raw_content (
332+ self , variable_context : Optional [Dict [str , Any ]] = None
333+ ) -> Optional [str ]:
334+ """Get content for RAW mode."""
335+ return self ._resolve_variables_in_text (self .raw , variable_context )
336+
337+ def _get_urlencoded_content (
338+ self , variable_context : Optional [Dict [str , Any ]] = None
339+ ) -> Dict [str , str ]:
340+ """Get content for URLENCODED mode."""
341+ result = {}
342+ for param in self .urlencoded :
343+ if param .is_active ():
344+ value = param .get_effective_value (variable_context )
345+ if value is not None :
346+ result [param .key ] = value
347+ return result
348+
349+ def _get_formdata_content (
350+ self , variable_context : Optional [Dict [str , Any ]] = None
351+ ) -> List [tuple ]:
352+ """Get content for FORMDATA mode."""
353+ result = []
354+ for param in self .formdata :
355+ if param .is_active ():
356+ if param .is_file ():
357+ # File parameter - use src if available, otherwise value
358+ file_value = param .src or param .value or ""
359+ result .append ((param .key , file_value , "file" ))
360+ else :
361+ # Text parameter
362+ value = param .get_effective_value (variable_context )
363+ if value is not None :
364+ result .append ((param .key , value , "text" ))
365+ return result
366+
367+ def _get_graphql_content (
368+ self , variable_context : Optional [Dict [str , Any ]] = None
369+ ) -> Optional [Union [Dict [str , Any ], str ]]:
370+ """Get content for GRAPHQL mode."""
371+ if not self .raw :
372+ return None
373+
374+ resolved_content = self ._resolve_variables_in_text (self .raw , variable_context )
375+ return self ._parse_json_safely (resolved_content )
376+
377+ def _get_file_content (
378+ self , variable_context : Optional [Dict [str , Any ]] = None
379+ ) -> Optional [str ]:
380+ """Get content for FILE mode."""
381+ return self .file .get ("src" )
382+
305383 def get_content (
306384 self , variable_context : Optional [Dict [str , Any ]] = None
307- ) -> Optional [Union [str , bytes , Dict [str , Any ]]]:
385+ ) -> Optional [Union [str , Dict [str , Any ], List [ tuple ]]]:
308386 """
309387 Get the body content in appropriate format.
310388
@@ -321,60 +399,19 @@ def get_content(
321399 if not mode :
322400 return None
323401
324- if mode == BodyMode .RAW :
325- content = self .raw
326- if content and variable_context :
327- # Resolve variables in raw content
328- for var_name , var_value in variable_context .items ():
329- placeholder = f"{{{{{ var_name } }}}}"
330- if placeholder in content :
331- content = content .replace (placeholder , str (var_value ))
332- return content
333-
334- elif mode == BodyMode .URLENCODED :
335- # Return as dictionary for URL encoding
336- result = {}
337- for param in self .urlencoded :
338- if param .is_active ():
339- value = param .get_effective_value (variable_context )
340- if value is not None :
341- result [param .key ] = value
342- return result
343-
344- elif mode == BodyMode .FORMDATA :
345- # Return as list of tuples for multipart form data
346- result = []
347- for param in self .formdata :
348- if param .is_active ():
349- if param .is_file ():
350- # File parameter - use src if available, otherwise value
351- file_value = param .src or param .value or ""
352- result .append ((param .key , file_value , "file" ))
353- else :
354- # Text parameter
355- value = param .get_effective_value (variable_context )
356- if value is not None :
357- result .append ((param .key , value , "text" ))
358- return result
359-
360- elif mode == BodyMode .GRAPHQL :
361- # GraphQL body is typically JSON with query/variables
362- if self .raw :
363- if variable_context :
364- # Resolve variables in the JSON string
365- content = self .raw
366- for var_name , var_value in variable_context .items ():
367- placeholder = f"{{{{{ var_name } }}}}"
368- if placeholder in content :
369- content = content .replace (placeholder , str (var_value ))
370- return self ._parse_json_safely (content )
371- else :
372- return self ._parse_json_safely (self .raw )
373- return None
374-
375- elif mode == BodyMode .FILE :
376- # Return file path
377- return self .file .get ("src" )
402+ # Dispatch to mode-specific handlers
403+ content_handlers = {
404+ BodyMode .RAW : self ._get_raw_content ,
405+ BodyMode .URLENCODED : self ._get_urlencoded_content ,
406+ BodyMode .FORMDATA : self ._get_formdata_content ,
407+ BodyMode .GRAPHQL : self ._get_graphql_content ,
408+ BodyMode .FILE : self ._get_file_content ,
409+ BodyMode .BINARY : self ._get_raw_content , # Binary uses same as raw
410+ }
411+
412+ handler = content_handlers .get (mode )
413+ if handler :
414+ return handler (variable_context )
378415
379416 return None
380417
0 commit comments