HTTP Clients
The DID Spaces SDK integrates powerful HTTP clients, Axios and Got, configured with agentkeepalive
for efficient and resilient communication with DID Spaces services. This setup ensures persistent connections, optimizes performance by reusing sockets, and provides robust retry mechanisms for enhanced stability.
For a broader understanding of how the SDK interacts with DID Spaces, you might want to review the Core Concepts section.
Persistent Connection Agents#
The SDK utilizes agentkeepalive
to manage HTTP and HTTPS connections, providing persistent agents (httpAgent
and httpsAgent
). These agents maintain a pool of sockets, which reduces the overhead of establishing new connections for every request, leading to improved performance and reduced latency, especially during high-volume operations.
Both agents are configured with a TIMEOUT
of 6 hours (1000 * 60 * 60 * 6
milliseconds) for maxSockets
, maxFreeSockets
, timeout
, freeSocketTimeout
, and keepAliveMsecs
to ensure long-lived connections for intensive tasks like large data synchronizations or backups. The keepAlive
option is set to true
to enable connection pooling.
Axios Client#
The api
export provides a pre-configured axios
instance for making HTTP requests. It's set up to use the persistent httpAgent
and httpsAgent
for all its network operations, benefiting from connection pooling and the extended timeout settings.
This client is suitable for general-purpose HTTP requests, offering a familiar and widely adopted interface for developers.
Example: Using the Axios client to make a GET request
import { api } from '@arcblock/did-spaces-client';
async function fetchData(url) {
try {
const response = await api.get(url);
console.log('Data fetched:', response.data);
return response.data;
} catch (error) {
console.error('Error fetching data:', error.message);
throw error;
}
}
// Example usage: Replace with your DID Space endpoint and object path
// fetchData('https://your-did-space-endpoint.com/api/objects/your-object-path');
This example demonstrates how to use the api
client to perform a GET request to a specified URL, handling both successful responses and potential errors.
Got Client#
The gotApi
export provides a got
instance, a more feature-rich HTTP request library. Similar to the Axios client, gotApi
is also configured with the persistent httpAgent
and httpsAgent
and the extensive TIMEOUT
.
Additionally, gotApi
includes a retry mechanism, configured to automatically retry failed requests up to 3 times. This enhances the reliability of operations, especially in environments where network transient issues might occur.
Example: Using the Got client to make a POST request with retries
import { gotApi } from '@arcblock/did-spaces-client';
async function postData(url, data) {
try {
const response = await gotApi.post(url, { json: data });
console.log('Data posted:', response.body);
return response.body;
} catch (error) {
console.error('Error posting data:', error.message);
throw error;
}
}
// Example usage: Replace with your DID Space endpoint and data
// postData('https://your-did-space-endpoint.com/api/some-endpoint', { key: 'value' });
This example illustrates how to use the gotApi
client to send a POST request with JSON data. The built-in retry logic will automatically attempt to resend the request if it fails due to transient network issues.
TIMEOUT Constant#
The TIMEOUT
constant is defined as 1000 * 60 * 60 * 6
, which equates to 6 hours (21,600,000 milliseconds). This extended timeout is applied across both agentkeepalive
instances and the gotApi
client, ensuring that long-running operations, such as large file uploads, downloads, or extensive synchronization processes, are not prematurely terminated due to short timeout settings.
Conclusion#
By leveraging axios
and got
with carefully configured persistent connection agents and robust timeout settings, the DID Spaces SDK provides a highly efficient and reliable foundation for all network communications. These utilities abstract away the complexities of HTTP requests, allowing developers to focus on integrating DID Spaces into their applications with confidence.
To understand how files and data are handled within the SDK, proceed to the File System utilities section.