Skip to content

Sending a request

To send a request to the miners using the Fiber network, you need to follow a series of steps to ensure secure and authenticated communication. Below is a detailed guide on how to achieve this.

Steps to Send a Request

  1. Encrypt the Payload: Encrypt the payload using the symmetric key.
  2. Add Headers: Add the symmetric key UUID and validator SS58 address to the headers. This acts as an API key to help miners prevent DDoS attacks.
  3. Send the Request: Send the request to the miner.

Streamed Requests

For streamed requests, you can use the following function:

async def make_streamed_post(
httpx_client: httpx.AsyncClient,
server_address: str,
validator_ss58_address: str,
fernet: Fernet,
symmetric_key_uuid: str,
endpoint: str,
payload: dict[str, Any],
timeout: int = 10,
) -> AsyncGenerator[bytes, None]:
headers = _get_headers(symmetric_key_uuid, validator_ss58_address)
encrypted_payload = fernet.encrypt(json.dumps(payload).encode())
async with httpx_client.stream(
method="POST",
url=server_address + endpoint,
content=encrypted_payload,
headers=headers,
timeout=timeout,
) as response:
response.raise_for_status()
async for line in response.aiter_raw():
yield line

This function encrypts the payload, adds the necessary headers, and sends the request to the server. It then streams the response back to the client.

Non-Streamed Requests

For non-streamed requests, you can use the following function:

async def make_non_streamed_post(
httpx_client: httpx.AsyncClient,
server_address: str,
validator_ss58_address: str,
fernet: Fernet,
symmetric_key_uuid: str,
endpoint: str,
payload: dict[str, Any],
timeout: int = 10,
) -> httpx.Response:
headers = _get_headers(symmetric_key_uuid, validator_ss58_address)
encrypted_payload = fernet.encrypt(json.dumps(payload).encode())
response = await httpx_client.post(
content=encrypted_payload,
timeout=timeout,
headers=headers,
url=server_address + endpoint,
)
return response

This function works similarly to the streamed version but returns the entire response at once instead of streaming it.