Skip to content

Commit 202d1ac

Browse files
Refactor R examples for pdfRest API
- Standardized `tryCatch` usage across all R examples for consistent error handling. - Improved file extension handling in "Complex Flow" script for better readability. - Added `.lintr` configuration for styling and removed redundant code. - Updated `.gitignore` to include `.cache/` directory for local styler cache.
1 parent 6c95a75 commit 202d1ac

7 files changed

Lines changed: 218 additions & 198 deletions

File tree

R/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,7 @@ rsconnect/
5656
# R package: bookdown caching files
5757
/*_files/
5858

59-
# End of https://www.toptal.com/developers/gitignore/api/r
59+
# Local cache for styler
60+
.cache/
61+
62+
# End of https://www.toptal.com/developers/gitignore/api/r

R/.lintr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
linters: lintr::linters_with_defaults(
2+
line_length_linter = lintr::line_length_linter(100),
3+
commented_code_linter = NULL
4+
)

R/Complex Flow Examples/merge-different-file-types.R

Lines changed: 70 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ content_type_for <- function(path) {
4242
switch(ext,
4343
pdf = "application/pdf",
4444
png = "image/png",
45-
jpg = "image/jpeg", jpeg = "image/jpeg",
45+
jpg = "image/jpeg",
46+
jpeg = "image/jpeg",
4647
gif = "image/gif",
47-
tif = "image/tiff", tiff = "image/tiff",
48+
tif = "image/tiff",
49+
tiff = "image/tiff",
4850
bmp = "image/bmp",
4951
webp = "image/webp",
5052
doc = "application/msword",
@@ -55,77 +57,80 @@ content_type_for <- function(path) {
5557
xlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
5658
txt = "text/plain",
5759
rtf = "application/rtf",
58-
html = "text/html", htm = "text/html",
60+
html = "text/html",
61+
htm = "text/html",
5962
"application/octet-stream"
6063
)
6164
}
6265

63-
tryCatch({
64-
collected_ids <- character(0)
66+
tryCatch(
67+
{
68+
collected_ids <- character(0)
6569

66-
for (i in seq_along(args)) {
67-
p <- args[[i]]
68-
ext <- tolower(tools::file_ext(p))
69-
if (ext == "pdf") {
70-
# Upload PDF to get id
71-
upload_url <- paste0(api_base, "/upload")
72-
upload_resp <- httr::POST(
73-
upload_url,
74-
httr::add_headers(
75-
"api-key" = api_key,
76-
"content-filename" = basename(p),
77-
"Content-Type" = "application/octet-stream"
78-
),
79-
body = readBin(p, what = "raw", n = file.info(p)$size)
80-
)
81-
txt <- httr::content(upload_resp, as = "text", encoding = "UTF-8")
82-
message(txt)
83-
if (httr::http_error(upload_resp)) {
84-
stop(sprintf("Upload failed for input #%d with status %s", i, httr::status_code(upload_resp)))
70+
for (i in seq_along(args)) {
71+
p <- args[[i]]
72+
ext <- tolower(tools::file_ext(p))
73+
if (ext == "pdf") {
74+
# Upload PDF to get id
75+
upload_url <- paste0(api_base, "/upload")
76+
upload_resp <- httr::POST(
77+
upload_url,
78+
httr::add_headers(
79+
"api-key" = api_key,
80+
"content-filename" = basename(p),
81+
"Content-Type" = "application/octet-stream"
82+
),
83+
body = readBin(p, what = "raw", n = file.info(p)$size)
84+
)
85+
txt <- httr::content(upload_resp, as = "text", encoding = "UTF-8")
86+
message(txt)
87+
if (httr::http_error(upload_resp)) {
88+
stop(sprintf("Upload failed for input #%d with status %s", i, httr::status_code(upload_resp)))
89+
}
90+
up_json <- jsonlite::fromJSON(txt)
91+
collected_ids <- c(collected_ids, if (is.data.frame(up_json$files)) up_json$files$id[[1]] else up_json$files[[1]]$id)
92+
message(sprintf("Uploaded PDF (#%d); id=%s", i, tail(collected_ids, 1)))
93+
} else {
94+
# Convert to PDF via /pdf to get outputId
95+
conv_url <- paste0(api_base, "/pdf")
96+
body <- list(file = httr::upload_file(p, type = content_type_for(p)))
97+
conv_resp <- httr::POST(conv_url, httr::add_headers("api-key" = api_key), body = body, encode = "multipart")
98+
txt <- httr::content(conv_resp, as = "text", encoding = "UTF-8")
99+
message(txt)
100+
if (httr::http_error(conv_resp)) {
101+
stop(sprintf("Conversion failed for input #%d with status %s", i, httr::status_code(conv_resp)))
102+
}
103+
cv_json <- jsonlite::fromJSON(txt)
104+
collected_ids <- c(collected_ids, cv_json$outputId)
105+
message(sprintf("Converted non-PDF (#%d); outputId=%s", i, tail(collected_ids, 1)))
85106
}
86-
up_json <- jsonlite::fromJSON(txt)
87-
collected_ids <- c(collected_ids, if (is.data.frame(up_json$files)) up_json$files$id[[1]] else up_json$files[[1]]$id)
88-
message(sprintf("Uploaded PDF (#%d); id=%s", i, tail(collected_ids, 1)))
89-
} else {
90-
# Convert to PDF via /pdf to get outputId
91-
conv_url <- paste0(api_base, "/pdf")
92-
body <- list(file = httr::upload_file(p, type = content_type_for(p)))
93-
conv_resp <- httr::POST(conv_url, httr::add_headers("api-key" = api_key), body = body, encode = "multipart")
94-
txt <- httr::content(conv_resp, as = "text", encoding = "UTF-8")
95-
message(txt)
96-
if (httr::http_error(conv_resp)) {
97-
stop(sprintf("Conversion failed for input #%d with status %s", i, httr::status_code(conv_resp)))
98-
}
99-
cv_json <- jsonlite::fromJSON(txt)
100-
collected_ids <- c(collected_ids, cv_json$outputId)
101-
message(sprintf("Converted non-PDF (#%d); outputId=%s", i, tail(collected_ids, 1)))
102107
}
103-
}
104108

105-
# Build x-www-form-urlencoded merge body
106-
enc <- function(x) utils::URLencode(x, reserved = TRUE)
107-
parts <- character(0)
108-
for (id in collected_ids) {
109-
parts <- c(parts, paste0("id[]=", enc(id)))
110-
parts <- c(parts, paste0("pages[]=", enc("1-last")))
111-
parts <- c(parts, paste0("type[]=", enc("id")))
112-
}
113-
merge_body <- paste(parts, collapse = "&")
109+
# Build x-www-form-urlencoded merge body
110+
enc <- function(x) utils::URLencode(x, reserved = TRUE)
111+
parts <- character(0)
112+
for (id in collected_ids) {
113+
parts <- c(parts, paste0("id[]=", enc(id)))
114+
parts <- c(parts, paste0("pages[]=", enc("1-last")))
115+
parts <- c(parts, paste0("type[]=", enc("id")))
116+
}
117+
merge_body <- paste(parts, collapse = "&")
114118

115-
merge_url <- paste0(api_base, "/merged-pdf")
116-
merge_resp <- httr::POST(
117-
merge_url,
118-
httr::add_headers("api-key" = api_key, "Content-Type" = "application/x-www-form-urlencoded"),
119-
body = merge_body
120-
)
119+
merge_url <- paste0(api_base, "/merged-pdf")
120+
merge_resp <- httr::POST(
121+
merge_url,
122+
httr::add_headers("api-key" = api_key, "Content-Type" = "application/x-www-form-urlencoded"),
123+
body = merge_body
124+
)
121125

122-
merge_text <- httr::content(merge_resp, as = "text", encoding = "UTF-8")
123-
cat(merge_text)
124-
if (httr::http_error(merge_resp)) {
125-
stop(sprintf("Merge failed with status %s", httr::status_code(merge_resp)))
126+
merge_text <- httr::content(merge_resp, as = "text", encoding = "UTF-8")
127+
cat(merge_text)
128+
if (httr::http_error(merge_resp)) {
129+
stop(sprintf("Merge failed with status %s", httr::status_code(merge_resp)))
130+
}
131+
},
132+
error = function(e) {
133+
stderrf("Error: %s: %s\n", class(e)[1], conditionMessage(e))
134+
quit(status = 1)
126135
}
127-
128-
}, error = function(e) {
129-
stderrf("Error: %s: %s\n", class(e)[1], conditionMessage(e))
130-
quit(status = 1)
131-
})
136+
)

R/Endpoint Examples/JSON Payload/markdown.R

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,51 @@ if (is.null(pdf_path) || !file.exists(pdf_path)) {
4141
filename <- basename(pdf_path)
4242
file_bytes <- readBin(pdf_path, what = "raw", n = file.info(pdf_path)$size)
4343

44-
tryCatch({
45-
# Step 1: Upload the file to receive a reusable id
46-
upload_url <- paste0(api_base, "/upload")
47-
upload_resp <- httr::POST(
48-
upload_url,
49-
httr::add_headers(
50-
"api-key" = api_key,
51-
"content-filename" = filename,
52-
"Content-Type" = "application/octet-stream"
53-
),
54-
body = file_bytes
55-
)
44+
tryCatch(
45+
{
46+
# Step 1: Upload the file to receive a reusable id
47+
upload_url <- paste0(api_base, "/upload")
48+
upload_resp <- httr::POST(
49+
upload_url,
50+
httr::add_headers(
51+
"api-key" = api_key,
52+
"content-filename" = filename,
53+
"Content-Type" = "application/octet-stream"
54+
),
55+
body = file_bytes
56+
)
5657

57-
upload_text <- httr::content(upload_resp, as = "text", encoding = "UTF-8")
58-
message(upload_text)
59-
if (httr::http_error(upload_resp)) {
60-
stop(sprintf("Upload failed with status %s", httr::status_code(upload_resp)))
61-
}
58+
upload_text <- httr::content(upload_resp, as = "text", encoding = "UTF-8")
59+
message(upload_text)
60+
if (httr::http_error(upload_resp)) {
61+
stop(sprintf("Upload failed with status %s", httr::status_code(upload_resp)))
62+
}
6263

63-
upload_json <- jsonlite::fromJSON(upload_text)
64-
# jsonlite parses arrays of objects as a data.frame; extract first id safely
65-
uploaded_id <- if (is.data.frame(upload_json$files)) upload_json$files$id[[1]] else upload_json$files[[1]]$id
66-
message(sprintf("Successfully uploaded with an id of: %s", uploaded_id))
64+
upload_json <- jsonlite::fromJSON(upload_text)
65+
# jsonlite parses arrays of objects as a data.frame; extract first id safely
66+
uploaded_id <- if (is.data.frame(upload_json$files)) upload_json$files$id[[1]] else upload_json$files[[1]]$id
67+
message(sprintf("Successfully uploaded with an id of: %s", uploaded_id))
6768

68-
# Step 2: Request Markdown output using the uploaded id
69-
md_url <- paste0(api_base, "/markdown")
70-
body <- jsonlite::toJSON(list(id = uploaded_id, page_break_comments = "on"), auto_unbox = TRUE)
71-
md_resp <- httr::POST(
72-
md_url,
73-
httr::add_headers(
74-
"api-key" = api_key,
75-
"Content-Type" = "application/json"
76-
),
77-
body = body
78-
)
69+
# Step 2: Request Markdown output using the uploaded id
70+
md_url <- paste0(api_base, "/markdown")
71+
body <- jsonlite::toJSON(list(id = uploaded_id, page_break_comments = "on"), auto_unbox = TRUE)
72+
md_resp <- httr::POST(
73+
md_url,
74+
httr::add_headers(
75+
"api-key" = api_key,
76+
"Content-Type" = "application/json"
77+
),
78+
body = body
79+
)
7980

80-
md_text <- httr::content(md_resp, as = "text", encoding = "UTF-8")
81-
cat(md_text)
82-
if (httr::http_error(md_resp)) {
83-
stop(sprintf("Markdown conversion failed with status %s", httr::status_code(md_resp)))
81+
md_text <- httr::content(md_resp, as = "text", encoding = "UTF-8")
82+
cat(md_text)
83+
if (httr::http_error(md_resp)) {
84+
stop(sprintf("Markdown conversion failed with status %s", httr::status_code(md_resp)))
85+
}
86+
},
87+
error = function(e) {
88+
stderrf("Error: %s: %s\n", class(e)[1], conditionMessage(e))
89+
quit(status = 1)
8490
}
85-
86-
}, error = function(e) {
87-
stderrf("Error: %s: %s\n", class(e)[1], conditionMessage(e))
88-
quit(status = 1)
89-
})
91+
)

R/Endpoint Examples/JSON Payload/rasterized-pdf.R

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,48 +41,50 @@ if (is.null(pdf_path) || !file.exists(pdf_path)) {
4141
filename <- basename(pdf_path)
4242
file_bytes <- readBin(pdf_path, what = "raw", n = file.info(pdf_path)$size)
4343

44-
tryCatch({
45-
# Step 1: Upload the file to receive a reusable id
46-
upload_url <- paste0(api_base, "/upload")
47-
upload_resp <- httr::POST(
48-
upload_url,
49-
httr::add_headers(
50-
"api-key" = api_key,
51-
"content-filename" = filename,
52-
"Content-Type" = "application/octet-stream"
53-
),
54-
body = file_bytes
55-
)
44+
tryCatch(
45+
{
46+
# Step 1: Upload the file to receive a reusable id
47+
upload_url <- paste0(api_base, "/upload")
48+
upload_resp <- httr::POST(
49+
upload_url,
50+
httr::add_headers(
51+
"api-key" = api_key,
52+
"content-filename" = filename,
53+
"Content-Type" = "application/octet-stream"
54+
),
55+
body = file_bytes
56+
)
5657

57-
upload_text <- httr::content(upload_resp, as = "text", encoding = "UTF-8")
58-
message(upload_text)
59-
if (httr::http_error(upload_resp)) {
60-
stop(sprintf("Upload failed with status %s", httr::status_code(upload_resp)))
61-
}
58+
upload_text <- httr::content(upload_resp, as = "text", encoding = "UTF-8")
59+
message(upload_text)
60+
if (httr::http_error(upload_resp)) {
61+
stop(sprintf("Upload failed with status %s", httr::status_code(upload_resp)))
62+
}
6263

63-
upload_json <- jsonlite::fromJSON(upload_text)
64-
uploaded_id <- if (is.data.frame(upload_json$files)) upload_json$files$id[[1]] else upload_json$files[[1]]$id
65-
message(sprintf("Successfully uploaded with an id of: %s", uploaded_id))
64+
upload_json <- jsonlite::fromJSON(upload_text)
65+
uploaded_id <- if (is.data.frame(upload_json$files)) upload_json$files$id[[1]] else upload_json$files[[1]]$id
66+
message(sprintf("Successfully uploaded with an id of: %s", uploaded_id))
6667

67-
# Step 2: Request a rasterized PDF using the uploaded id
68-
rast_url <- paste0(api_base, "/rasterized-pdf")
69-
body <- jsonlite::toJSON(list(id = uploaded_id), auto_unbox = TRUE)
70-
rast_resp <- httr::POST(
71-
rast_url,
72-
httr::add_headers(
73-
"api-key" = api_key,
74-
"Content-Type" = "application/json"
75-
),
76-
body = body
77-
)
68+
# Step 2: Request a rasterized PDF using the uploaded id
69+
rast_url <- paste0(api_base, "/rasterized-pdf")
70+
body <- jsonlite::toJSON(list(id = uploaded_id), auto_unbox = TRUE)
71+
rast_resp <- httr::POST(
72+
rast_url,
73+
httr::add_headers(
74+
"api-key" = api_key,
75+
"Content-Type" = "application/json"
76+
),
77+
body = body
78+
)
7879

79-
rast_text <- httr::content(rast_resp, as = "text", encoding = "UTF-8")
80-
cat(rast_text)
81-
if (httr::http_error(rast_resp)) {
82-
stop(sprintf("Rasterization failed with status %s", httr::status_code(rast_resp)))
80+
rast_text <- httr::content(rast_resp, as = "text", encoding = "UTF-8")
81+
cat(rast_text)
82+
if (httr::http_error(rast_resp)) {
83+
stop(sprintf("Rasterization failed with status %s", httr::status_code(rast_resp)))
84+
}
85+
},
86+
error = function(e) {
87+
stderrf("Error: %s: %s\n", class(e)[1], conditionMessage(e))
88+
quit(status = 1)
8389
}
84-
85-
}, error = function(e) {
86-
stderrf("Error: %s: %s\n", class(e)[1], conditionMessage(e))
87-
quit(status = 1)
88-
})
90+
)

0 commit comments

Comments
 (0)