#' @export
post_request_fast <- function(path, data){
# tictoc::tic()
url <- paste(bi_params$api_url, bi_params$private_api_version, path, sep = "/")
# tictoc::toc()
# tictoc::tic()
data$timestamp <- round(as.numeric(lubridate::now("UTC"))*1000)
data$signature <- generate_signature(data = data)
par <- order_params(data)
# tictoc::toc()
# tictoc::tic()
if(is.null(get_proxy())){
req <- httr::POST(url,
httr::add_headers(.headers = c(accept = "application/json",
`User-Agent` = 'binance/python',
`X-MBX-APIKEY` = api_key())),
body = par, encode = "form")
} else {
req <- httr::POST(url,
httr::add_headers(.headers = c(accept = "application/json",
`User-Agent` = 'binance/python',
`X-MBX-APIKEY` = api_key())),
add_proxy(),
body = par, encode = "form")
}
if(Sys.getenv("BINANCE_SIZE") == "TRUE"){
try(cat(as.character(format(nanotime::nanotime(lubridate::now("UTC")), format = "%Y-%m-%d %H:%M:%E2S")), "\t\t",
format(object.size(req$content), units = "kB", standard = "SI"), "\t\t",
stringr::str_extract(req$url, ".{0,70}"), "\n"))
}
jsonlite::fromJSON(rawToChar(req$content))
# tictoc::toc()
}
#' @export
bi_request <- function(url, method, signed, force_params = FALSE, requests_params = NULL, ...){
timeout <- 10
dots <- list(...)
#dots <- list()
#dots <- list(data = data)
dots$a <- ""
dots$requests_params <- requests_params
has_data <- "data" %in% names(dots)
if(!has_data){
dots$data <- tibble::tibble(a = "")
}
# Merge request param if request param in dots$data
# remove it form dots$data
if(signed){
dots$data$timestamp <- round(as.numeric(lubridate::now("UTC"))*1000)
if(!has_data){
dots$data$a <- NULL
}
# print(timestamp)
dots$data$signature <- generate_signature(data = dots$data)
# print(dots$data$signature)
}
if(!has_data){
if(signed){
dots$data <- dots$data %>%
order_params() %>%
#remove all null arguments
purrr::compact()
}else {
dots$data <- NULL
}
} else {
dots$data <- dots$data %>%
order_params() %>%
#remove all null arguments
purrr::compact()
}
# print(force_params)
if(!is.null(dots$data) & (method == "get"| force_params)){
dots$params <- collapse_query(data = dots$data)
}
if(identical(dots, list(a = ""))){
dots <- NULL
}
if(api_key() == ""){
stop("No user found. Please register a coinr-user using 'register_account(api_key = '<your_api_key>', api_secret = '<your_api_secret>')'")
}
if(method == "get"){
if(!is.null(dots)){
if(stringr::str_detect(url, "\\?"))stop("Url already has query parameters")
url <- paste0(url, dots$params)
}
# print(url)
if(is.null(get_proxy())){
tmp_res <- httr::GET(url,
httr::add_headers(.headers = account_headers()))
res <- tmp_res %>%
httr::content()
} else {
tmp_res <- httr::GET(url,
add_proxy(),
httr::add_headers(.headers = account_headers()))
res <- tmp_res %>%
httr::content()
}
} else if( method == "post"){
# print(dots$data)
# print(account)
if(is.null(get_proxy())){
tmp_res <- httr::POST(url,
httr::add_headers(.headers = account_headers()),
body = dots$data,
encode = "form") %>%
httr::content()
res <- tmp_res %>%
httr::content()
} else {
tmp_res <- httr::POST(url,
httr::add_headers(.headers = account_headers()),
add_proxy(),
body = dots$data,
encode = "form")
res <- tmp_res %>%
httr::content()
}
} else if( method == "delete"){
# print(dots$data)
# print(account)
if(is.null(get_proxy())){
tmp_res <- httr::DELETE(url,
httr::add_headers(.headers = account_headers()),
body = dots$data,
encode = "form")
res <- tmp_res %>%
httr::content()
} else {
tmp_res <- httr::DELETE(url,
httr::add_headers(.headers = account_headers()),
body = dots$data,
encode = "form")
res <- tmp_res %>%
httr::content()
}
}
msg <- paste(
as.character(format(nanotime::nanotime(lubridate::now("UTC")), format = "%Y-%m-%d %H:%M:%E2S")), "\t\t",
format(object.size(tmp_res$content), units = "kB", standard = "SI"), "\t\t",
stringr::str_extract(tmp_res$url, ".{0,70}"),
collapse = "")
if(Sys.getenv("BINANCE_SIZE") == "TRUE"){
try(cat(msg, "\n"))
}
if(identical(names(res), c("code", "msg"))){
warning("\n\n", msg, glue::glue("\n\n{res$code}/{res$msg}"))
}
return(res)
}
#' @export
delete_order <- function(symbol, order_id = NULL){
del_data <- list(symbol = symbol)
if(!is.null(order_id)){
del_data$orderId <- order_id
out <- bi_delete(path = "order", signed = T, data = del_data)
} else {
out <- bi_delete(path = "openOrders", signed = T, data = del_data)
}
if(length(setdiff(c("code", "msg"), names(out))) == 0) stop(glue::glue("Order could not be deleted.\n{out$code}/{out$msg}"))
return(dplyr::mutate(tibble::as_tibble(del_data), type = "delete", resp = list(out)))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.