Skip to content

Commit

Permalink
Use dev pkgsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Sep 24, 2024
1 parent 3a59d3e commit edf766a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 34 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Config/needs/dependencies:
pkgbuild,
r-lib/pkgcache,
r-lib/pkgdepends,
pkgsearch,
r-hub/pkgsearch,
processx,
ps,
Config/Needs/website:
Expand Down
9 changes: 4 additions & 5 deletions src/library/pkgsearch/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: pkgsearch
Title: Search and Query CRAN R Packages
Version: 3.1.3
Version: 3.1.3.9000
Authors@R: c(
person("Gábor", "Csárdi", , "[email protected]", role = c("aut", "cre")),
person("Maëlle", "Salmon", role = "aut",
Expand All @@ -22,12 +22,11 @@ Suggests: covr, memoise, mockery, pillar, pingr (>= 2.0.0), rstudioapi,
withr
Config/testthat/edition: 3
Encoding: UTF-8
RoxygenNote: 7.2.0.9000
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1.9000
NeedsCompilation: no
Packaged: 2023-12-10 13:34:02 UTC; gaborcsardi
Packaged: 2024-09-24 07:38:50 UTC; gaborcsardi
Author: Gábor Csárdi [aut, cre],
Maëlle Salmon [aut] (<https://orcid.org/0000-0002-2815-0399>),
R Consortium [fnd]
Maintainer: Gábor Csárdi <[email protected]>
Repository: CRAN
Date/Publication: 2023-12-10 14:00:03 UTC
5 changes: 5 additions & 0 deletions src/library/pkgsearch/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# pkgsearch (development version)

* pkgsearch now uses the `timeout` option to set the limit for the total
time of each HTTP request (#125, @gladkia).

# pkgsearch 3.1.3

* No user visible changes.
Expand Down
6 changes: 2 additions & 4 deletions src/library/pkgsearch/R/advanced_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ advanced_search <- function(..., json = NULL, format = c("short", "long"),
qstr <- json
}

server <- Sys.getenv("R_PKG_SEARCH_SERVER", "search.r-pkg.org")
port <- as.integer(Sys.getenv("R_PKG_SEARCH_PORT", "80"))
server <- Sys.getenv("R_PKG_SEARCH_SERVER", "https://search.r-pkg.org")

resp <- do_query(qstr, server, port, from, size)
resp <- do_query(qstr, server, from, size)

result <- format_result(
resp,
Expand All @@ -104,7 +103,6 @@ advanced_search <- function(..., json = NULL, format = c("short", "long"),
from = from,
size = size,
server = server,
port = port,
qstr = qstr
)

Expand Down
32 changes: 15 additions & 17 deletions src/library/pkgsearch/R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,22 @@ pkg_search <- function(query = NULL, format = c("short", "long"),

if (is.null(query)) return(pkg_search_again())
format <- match.arg(format)
server <- Sys.getenv("R_PKG_SEARCH_SERVER", "search.r-pkg.org")
port <- as.integer(Sys.getenv("R_PKG_SEARCH_PORT", "80"))
server <- Sys.getenv("R_PKG_SEARCH_SERVER", "https://search.r-pkg.org")

make_pkg_search(query, format, from, size, server, port)
make_pkg_search(query, format, from, size, server)
}

#' @rdname pkg_search
#' @export

ps <- pkg_search

make_pkg_search <- function(query, format, from, size, server, port) {
make_pkg_search <- function(query, format, from, size, server) {

qry <- make_query(query = query)
rsp <- do_query(qry, server = server, port = port, from = from,
size = size)
rsp <- do_query(qry, server = server, from = from, size = size)
rst <- format_result(rsp, query = query, format = format, from = from,
size = size, server = server, port = port)
size = size, server = server)

s_data$prev_q <- list(type = "simple", result = rst)

Expand All @@ -108,8 +106,7 @@ more <- function(format = NULL, size = NULL) {
format = format %||% meta(rst)$format,
from = meta(rst)$from + meta(rst)$size,
size = size %||% meta(rst)$size,
server = meta(rst)$server,
port = meta(rst)$port
server = meta(rst)$server
)

} else if (s_data$prev_q$type == "advanced") {
Expand Down Expand Up @@ -183,17 +180,20 @@ make_query <- function(query) {
)
}

do_query <- function(query, server, port, from, size) {
do_query <- function(query, server, from, size) {

check_count(from)
check_count(size)

url <- "http://" %+% server %+% ":" %+% as.character(port) %+%
"/package/_search?from=" %+% as.character(from - 1) %+%
"&size=" %+% as.character(size)
# timeout for the curl's connect phase (in seconds)
timeout <- getOption("timeout", 60)

url <- server %+% "/package/_search?from=" %+%
as.character(from - 1) %+% "&size=" %+% as.character(size)
result <- http_post(
url, body = query,
headers = c("Content-Type" = "application/json"))
headers = c("Content-Type" = "application/json"),
options = list(timeout = timeout))
chain_error(
http_stop_for_status(result),
new_query_error(result, "search server failure")
Expand Down Expand Up @@ -236,8 +236,7 @@ print.pkgsearch_query_error <- function(x, ...) {
invisible(x)
}

format_result <- function(result, query, format, from, size, server,
port, ...) {
format_result <- function(result, query, format, from, size, server, ...) {
result <- jsonlite::fromJSON(result, simplifyVector = FALSE)

meta <- list(
Expand All @@ -246,7 +245,6 @@ format_result <- function(result, query, format, from, size, server,
from = from,
size = size,
server = server,
port = port,
total = result$hits$total,
max_score = result$hits$max_score,
took = result$took,
Expand Down
18 changes: 18 additions & 0 deletions src/library/pkgsearch/R/http.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
get_default_curl_options <- function(options) {
getopt <- function(nm) {
if (!is.null(v <- options[[nm]])) return(v)
anm <- paste0("async_http_", nm)
if (!is.null(v <- getOption(anm))) return(v)
if (!is.na(v <- Sys.getenv(toupper(anm), NA_character_))) return (v)
}
modifyList(
options,
drop_nulls(list(
timeout = as.integer(getopt("timeout") %||% 0),
connecttimeout = as.integer(getopt("connecttimeout") %||% 300),
low_speed_time = as.integer(getopt("low_speed_time") %||% 0),
low_speed_limit = as.integer(getopt("low_speed_limit") %||% 0),
cainfo = getopt("cainfo")
))
)
}

http_get <- function(url) {
curl::curl_fetch_memory(url)
Expand Down
18 changes: 11 additions & 7 deletions src/library/pkgsearch/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ contains <- function(x, y) y %in% x
isin <- function(x, y) x %in% y

remove_special <- function(list, level = 1) {

assert_that(is_positive_count(level))

if (level == 1) {
replace(
grepl(pattern = "^_", names(list)),
Expand All @@ -92,7 +92,7 @@ remove_special <- function(list, level = 1) {
} else {
lapply(list, remove_special, level = level - 1)
}

}

pluck <- function(list, idx) list[[idx]]
Expand All @@ -104,25 +104,25 @@ needs_packages <- function(pkgs) {

if (!all(has)) {
not_installed_pkgs <- pkgs[!has]

if (length(not_installed_pkgs) == 1) {

throw(new_error(
"The ",
sQuote(not_installed_pkgs),
" package is needed for this addin.",
call. = FALSE
))
} else {

throw(new_error(
"The ",
paste(sQuote(not_installed_pkgs), collapse = ", "),
" packages are needed for this addin.",
call. = FALSE
))
}

}
}

Expand All @@ -133,3 +133,7 @@ clean_description <- function(txt) {
zap_null <- function(x) {
x[! map_lgl(x, is.null)]
}

drop_nulls <- function (x) {
x[!map_lgl(x, is.null)]
}

0 comments on commit edf766a

Please sign in to comment.