ropensci / aRxiv
1
#' Check for connection to arXiv API
2
#'
3
#' Check for connection to arXiv API
4
#'
5
#' @param max_time Maximum wait time in seconds
6
#'
7
#' @return Returns TRUE if connection is established and FALSE
8
#' otherwise.
9
#'
10
#' @examples
11
#' \donttest{
12
#' can_arxiv_connect(2)
13
#' }
14
#'
15
#' @export
16
can_arxiv_connect <-
17
    function(max_time=5) # maximum wait time in seconds
18
{
19 0
    query_url <- "http://export.arxiv.org/api/query"
20

21 0
    result <- tryCatch(z <- httr::POST(query_url, body=list(search_query="all:electron", max_results=0),
22 0
                                       httr::timeout(max_time)),
23 0
                       error=function(e) paste("Failure to connect in arxiv_check"))
24

25
    # check for error in httr::POST
26 0
    if(!is.null(result) && length(result)==1 &&
27 0
       result == "Failure to connect in arxiv_check") {
28 0
        warning("Failed to connect to ", query_url, " in ", max_time, " sec")
29 0
        return(FALSE)
30
    }
31

32
    # check for arXiv error
33 0
    listresult <- result2list(z)
34 0
    error_message <- arxiv_error_message(listresult)
35 0
    if(!is.null(error_message)) {
36 0
        warning("arXiv error: ", error_message)
37 0
        return(FALSE)
38
    }
39

40
    # check for general http error
41 0
    status <- httr::http_status(z)
42 0
    if(status$category != "success" && status$category != "Success") {
43 0
        httr::warn_for_status(z)
44 0
        return(FALSE)
45
    }
46

47
    # seems okay...return TRUE
48 0
    TRUE
49
}

Read our documentation on viewing source code .

Loading