invoke.py


Overview

The invoke.py file defines a reusable component within the InfiniFlow system that enables making HTTP requests to external endpoints. It provides a flexible interface for invoking web services using various HTTP methods (GET, POST, PUT), supports proxy configurations, custom headers, and flexible data formats (json or formdata). Additionally, it can optionally clean and parse HTML responses to extract meaningful content.

This component is designed to interact with remote APIs or web services as part of a larger workflow, allowing dynamic variable substitution and robust error handling with retry mechanisms.


Classes and Their Functionality

InvokeParam

Purpose:
Defines and validates configuration parameters for the Invoke component.

Inheritance:
Inherits from ComponentParamBase.

Attributes:

Attribute

Type

Default

Description

proxy

str/None

None

Proxy URL to route HTTP requests through (e.g., "http://proxy")

headers

str

""

JSON string representing HTTP headers to attach to requests

method

str

"get"

HTTP method to use: "get", "post", or "put"

variables

list

[]

List of key-value/ref dictionaries for request parameters

url

str

""

Target URL to which the HTTP request will be sent

timeout

int

60

Timeout in seconds for the HTTP request

clean_html

bool

False

Whether to parse and clean HTML response content

datatype

str

"json"

Data format for posting: "json" or "formdata"

max_retries

int

Inherited

Number of retries on failure (inherited from base class)

delay_after_error

int

Inherited

Delay in seconds after an error before retrying (inherited)

Methods:

Usage Example:

param = InvokeParam()
param.url = "https://api.example.com/data"
param.method = "post"
param.headers = '{"Authorization": "Bearer token"}'
param.variables = [{"key": "param1", "value": "value1"}]
param.timeout = 30
param.clean_html = False
param.datatype = "json"
param.check()

Invoke

Purpose:
Main component class responsible for executing HTTP requests based on parameters and returning processed results.

Inheritance:
Inherits from ComponentBase and ABC (Abstract Base Class).

Class Attribute:

Key Method:

_invoke(self, **kwargs)

invoke_component = Invoke()
invoke_component._param = InvokeParam()
invoke_component._param.url = "https://jsonplaceholder.typicode.com/posts"
invoke_component._param.method = "get"
invoke_component._param.variables = [{"key": "userId", "value": "1"}]
invoke_component._param.headers = '{"Accept": "application/json"}'
invoke_component._param.timeout = 10

result = invoke_component._invoke()
print(result)  # JSON response from the API

thoughts(self) -> str


Important Implementation Details


Interaction with Other System Components


Visual Diagram: Class Structure of invoke.py

classDiagram
    class InvokeParam {
        +proxy: str or None
        +headers: str
        +method: str
        +variables: list
        +url: str
        +timeout: int
        +clean_html: bool
        +datatype: str
        +check()
    }

    class Invoke {
        +component_name: str
        +_invoke(**kwargs) str
        +thoughts() str
    }

    ComponentParamBase <|-- InvokeParam
    ComponentBase <|-- Invoke
    Invoke ..|> ABC

Summary

The invoke.py file provides a robust, configurable HTTP request component for the InfiniFlow framework, supporting dynamic inputs, error handling, proxying, and flexible data posting formats. Its design enables easy integration into complex workflows requiring external API interactions or web crawling functionality with optional HTML content parsing.