Retrieves the current server time as a Unix timestamp in milliseconds from the KuCoin Futures API asynchronously. This helper function is essential for authenticated requests, ensuring timestamps align with server time within a 5-second window.
Usage
get_server_time(base_url = get_base_url())Arguments
- base_url
Character string specifying the base URL for the KuCoin Futures API. Defaults to
get_base_url().
Value
Promise resolving to a numeric Unix timestamp in milliseconds or rejecting with an error if the request fails.
Details
Workflow Overview
URL Construction: Combines the base URL (default from
get_base_url()) with the endpoint/api/v1/timestamp.API Request: Sends an asynchronous GET request with a 3-second timeout using
httr::GET().Status Check: Validates the HTTP status code is 200, rejecting the promise if not.
Response Parsing: Extracts the response text and parses it as JSON with
jsonlite::fromJSON().Structure Validation: Ensures
"code"and"data"fields exist, rejecting if missing.Success Check: Confirms the API code is
"200000", rejecting if not.Result Resolution: Resolves the promise with the timestamp from the
"data"field.
Usage
Utilised to synchronise request timestamps for signature generation and to prevent replay attacks in authenticated API calls.
Examples
if (FALSE) { # \dontrun{
get_server_time()$
then(function(timestamp) {
cat("KuCoin Server Time:", timestamp, "\n")
})$
catch(function(e) {
message("Error retrieving server time:", conditionMessage(e))
})
while (!later::loop_empty()) later::run_now(timeoutSecs = Inf, all = TRUE)
} # }