Skip to content

Commit

Permalink
Merge pull request #4 from hleegwater/main
Browse files Browse the repository at this point in the history
Add function that replaces an Rspace document by the new html file.
  • Loading branch information
burgerga authored Jun 5, 2024
2 parents 777add0 + dd47012 commit 68330c3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 15 additions & 0 deletions R/documents.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ document_post <- function(body, api_key = get_api_key()) {
json
}

document_replace <- function(body, existing_document_id, api_key = get_api_key()) {
body$tags <- paste0(c("rspacer", body$tags), collapse= ",")
request() |>
httr2::req_url_path_append("documents", parse_rspace_id(existing_document_id)) |>
httr2::req_headers(`apiKey` = api_key) |>
httr2::req_body_json(body) |>
httr2::req_method("PUT") |>
httr2::req_perform() |>
httr2::resp_body_json() -> json

cli::cli_inform("Document replaced: {.url {create_global_id_link(json$globalId)}}")

return(json)
}

#'
#' Global search for a term
#'
Expand Down
10 changes: 8 additions & 2 deletions R/mdtodoc.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ doc_get_fields <- function(doc_id, api_key = get_api_key()) {
#' @param folder_id folder_id in which the document will be created (can be a notebook)
#' @param tags vector of tags to apply to the document
#' @param attachment attachment to attach to one of the fields, e.g., `list(field = 7, path = "file.txt")`
#' @param existing_document_id if you want to replace a document by a new one, specify the current identifier here.
#' @inheritParams api_status
#' @export
document_create_from_html <- function(path, template_id = NULL, folder_id = NULL, tags = NULL, attachment = NULL, api_key = get_api_key()) {
document_create_from_html <- function(path, template_id = NULL, folder_id = NULL, tags = NULL, attachment = NULL, api_key = get_api_key(), existing_document_id = NULL) {
doc_body <- html_to_doc_body(path, verbose = F)
if(!is.null(template_id)) {
template_fields <- doc_get_fields(template_id)
Expand Down Expand Up @@ -86,7 +87,12 @@ document_create_from_html <- function(path, template_id = NULL, folder_id = NULL
# The API wants a plain array -> remove the names
names(doc_body$fields) <- NULL

json <- document_post(doc_body)
# Create or replace the document
if(is.null(existing_document_id)){
json <- document_post(doc_body)
} else {
json <- document_replace(doc_body, existing_document_id)
}

return(invisible(json))
}

0 comments on commit 68330c3

Please sign in to comment.