Skip to content

Commit

Permalink
HATEOAS feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mustberuss committed Sep 18, 2022
1 parent 406e571 commit 7372fc1
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export(get_endpoints)
export(get_fields)
export(get_ok_pk)
export(qry_funs)
export(retrieve_linked_data)
export(search_pv)
export(unnest_pv_data)
export(with_qfuns)
Expand Down
38 changes: 38 additions & 0 deletions R/search-pv.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,41 @@ search_pv <- function(query,

res
}

#' Get Linked Data
#'
#' Some of the endpoints now returns HATEOAS style links to get more data.
#' ex "inventor": "https://search.patentsview.org/api/v1/inventor/252373/"
#'
#' @param url The link that was returned by the API on a previous call
#'
#' @param api_key API key. See \href{https://patentsview.org/apis/keyrequest}{
#' Here} for info on creating a key.
#'
#' @return A character vector with field names, same as search_pv()
#'
#' @examples
#' \dontrun{
#'
#' retrieve_linked_data(
#' "https://search.patentsview.org/api/v1/cpc_subgroup/G01S7:4811/"
#' )
#' }
#'
#' @export
retrieve_linked_data <- function(url,
api_key = Sys.getenv("PATENTSVIEW_API_KEY")
) {

# Don't sent the API key to any domain other than patentsview.org
if (!grepl("^https://[^/]*\\.*patentsview.org/", url)) {
stop2("retrieve_linked_data is only for patentsview.org urls - sends API key")
}

# Go through one_request, it would resend on 429 too many requests
# The API doesn't seeem to mind ?q=&f=&o=&s= appended to the url
res = one_request("GET", "", url, list(), api_key)
res <- process_resp(res)
res
}

5 changes: 4 additions & 1 deletion man/patentsview-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/retrieve_linked_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions tests/testthat/test-search-pv.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,61 @@ test_that("Throttled requests are automatically retried", {

expect_identical(built_singly, result_all)
})

test_that("We won't expose the user's patentsview API key to random websites", {
skip_on_cran()
skip_on_ci()

# We will try to call the api that tells us who is currently in space
in_space_now_url <- "http://api.open-notify.org/astros.json"

expect_error(
retrieve_linked_data(in_space_now_url)
)
})


test_that("We can call all the legitimate HATEOAS endpoints", {
skip_on_cran()
skip_on_ci()

# Call the new, Get only endpoints that don't accept q: s:, o:, f:, parameters
# The links are returned fully qualified, like below, from some of the endpoints
# These queries retrieve one specific row
single_item_queries <- c(
"https://search.patentsview.org/api/v1/assignee/10/",
"https://search.patentsview.org/api/v1/cpc_group/A01B/",
"https://search.patentsview.org/api/v1/cpc_subgroup/G01S7:4811/",
"https://search.patentsview.org/api/v1/cpc_subsection/A01/",
"https://search.patentsview.org/api/v1/inventor/10/",
"https://search.patentsview.org/api/v1/nber_category/1/",
"https://search.patentsview.org/api/v1/nber_subcategory/11/",
"https://search.patentsview.org/api/v1/patent/10757852/",
"https://search.patentsview.org/api/v1/uspc_mainclass/30/",
"https://search.patentsview.org/api/v1/uspc_subclass/30:100/"
)

# These queries can return more than a single row
multi_item_queries <- c(
"https://search.patentsview.org/api/v1/application_citation/10966293/",
"https://search.patentsview.org/api/v1/patent_citation/10966293/"
)

x <- lapply(single_item_queries, function(q) {
Sys.sleep(2)
print(q)
j <- retrieve_linked_data(q)

# here all the total hits should be 1
expect_equal(j$query_results$total_hits, 1)
})

x <- lapply(multi_item_queries, function(q) {
Sys.sleep(2)
print(q)
j <- retrieve_linked_data(q)

# here all the total hits should be 1 or more rows
expect_true(j$query_results$total_hits >= 1)
})
})

0 comments on commit 7372fc1

Please sign in to comment.