Send Signed Request
Description
The send_signed_request function sends an HTTP request to a specified URL with signed headers for authentication using HMAC-SHA256. It supports various HTTP methods (GET, POST, PUT, PATCH, DELETE) and returns the HTTP status code and response string.
Function Signature:
def send_signed_request(ws_config: WebServiceConfig, method: str, url: str, path: str, headers: dict = None, parameters: str = None, body: str = None) -> tuple[int, str]:
Parameters
- ws_config (
WebServiceConfig): Configuration object containing the base URL, credentials, and SSL settings for the web service. - method (
str): The HTTP method (GET, POST, PUT, PATCH, DELETE). - url (
str): The full URL of the web service endpoint. - path (
str): The API path, used for generating the HMAC signature. - headers (
dict, optional): The HTTP headers for the request. Defaults to{"Content-Type": "application/json"}. - parameters (
str, optional): The query parameters for the request (used with GET or DELETE methods). - body (
str, optional): The request body (used with POST, PUT, or PATCH methods).
Returns
- tuple[int, str]: The status code and response string from the web service.
Example Usage
ws_config = WebServiceConfig('https://api.actionstreamer.com', 'your-access-key', 'your-secret-key', 30)
status_code, response = send_signed_request(ws_config, 'POST', 'https://api.actionstreamer.com/data', '/v1/data', body='{"key":"value"}')
print(status_code, response)
Notes
- This function uses HMAC-SHA256 to generate a signature for secure authentication. The
get_hmac_signaturefunction is used to generate the signature. - The function supports multiple HTTP methods (GET, POST, PUT, PATCH, DELETE) based on the
methodparameter. - SSL verification is controlled by the
ws_config.ignore_sslsetting. Ifignore_sslisTrue, SSL verification is disabled. - The function uses the
requestslibrary to send the HTTP request and handle responses. - If any exception occurs during the request, the exception is caught, and an error message is printed.
Error Handling
- If an error occurs during the request (e.g., a connection issue or invalid response), an exception will be caught and printed. The function will return a status code of
-1and an error message.