Skip to content

Commit e0ebd97

Browse files
authored
updated endpoint list (#37)
* updated endpoint list * endpoints are now singular * removed to_singular and to_plural * simplified get_endpoints() * put back to_plural() * generated files * restored group handling * generated files * endpoint name changes * endpoint name changes * generated files * endpoint name changes * generated files * removed to_plural() again * back to regular group checking * bumped endpoint count and RoxygenNote version * generated files
1 parent e1ca36b commit e0ebd97

19 files changed

+622
-812
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Authors@R: c(
1010
Encoding: UTF-8
1111
Description: Provides functions to simplify the 'PatentsView' API
1212
(<https://patentsview.org/apis/purpose>) query language,
13-
send GET and POST requests to the API's seven endpoints, and parse the data
13+
send GET and POST requests to the API's twenty seven endpoints, and parse the data
1414
that comes back.
1515
URL: https://docs.ropensci.org/patentsview/index.html
1616
BugReports: https://github.com/ropensci/patentsview/issues
@@ -28,5 +28,5 @@ Suggests:
2828
rmarkdown,
2929
testthat,
3030
tidyr
31-
RoxygenNote: 7.1.2
31+
RoxygenNote: 7.3.2
3232
Roxygen: list(markdown = TRUE)

R/get-fields.R

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
#' possible fields for each endpoint).
88
#'
99
#' @param endpoint The API endpoint whose field list you want to get. See
10-
#' \code{\link{get_endpoints}} for a list of the 7 endpoints.
10+
#' \code{\link{get_endpoints}} for a list of the 27 endpoints.
1111
#' @param groups A character vector giving the group(s) whose fields you want
1212
#' returned. A value of \code{NULL} indicates that you want all of the
1313
#' endpoint's fields (i.e., do not filter the field list based on group
1414
#' membership). See the field tables located online to see which groups you
1515
#' can specify for a given endpoint (e.g., the
16-
#' \href{https://patentsview.org/apis/api-endpoints/patents}{patents
16+
#' \href{https://search.patentsview.org/docs/docs/Search%20API/SearchAPIReference/#patent}{patent
1717
#' endpoint table}), or use the \code{fieldsdf} table
18-
#' (e.g., \code{unique(fieldsdf[fieldsdf$endpoint == "patents", "group"])}).
18+
#' (e.g., \code{unique(fieldsdf[fieldsdf$endpoint == "patent", "group"])}).
1919
#'
2020
#' @return A character vector with field names.
2121
#'
2222
#' @examples
23-
#' # Get all assignee-level fields for the patents endpoint:
24-
#' fields <- get_fields(endpoint = "patents", groups = "assignees_at_grant")
23+
#' # Get all assignee-level fields for the patent endpoint:
24+
#' fields <- get_fields(endpoint = "patent", groups = "assignees")
2525
#'
2626
#' # ...Then pass to search_pv:
2727
#' \dontrun{
@@ -31,8 +31,8 @@
3131
#' fields = fields
3232
#' )
3333
#' }
34-
#' # Get all patent and assignee-level fields for the patents endpoint:
35-
#' fields <- get_fields(endpoint = "patents", groups = c("assignees_at_grant", "patents"))
34+
#' # Get all patent and assignee-level fields for the patent endpoint:
35+
#' fields <- get_fields(endpoint = "patent", groups = c("assignees", "patents"))
3636
#'
3737
#' \dontrun{
3838
#' # ...Then pass to search_pv:
@@ -48,7 +48,7 @@ get_fields <- function(endpoint, groups = NULL) {
4848
if (is.null(groups)) {
4949
fieldsdf[fieldsdf$endpoint == endpoint, "field"]
5050
} else {
51-
validate_groups(groups = groups)
51+
validate_groups(endpoint, groups = groups)
5252
fieldsdf[fieldsdf$endpoint == endpoint & fieldsdf$group %in% groups, "field"]
5353
}
5454
}
@@ -61,10 +61,5 @@ get_fields <- function(endpoint, groups = NULL) {
6161
#' @return A character vector with the names of each endpoint.
6262
#' @export
6363
get_endpoints <- function() {
64-
c(
65-
"application_citations", "assignees", "cpc_groups", "cpc_subgroups",
66-
"cpc_subsections", "inventors", "nber_categories",
67-
"nber_subcategories", "patent_citations", "patents",
68-
"uspc_subclasses", "uspc_mainclasses"
69-
)
64+
unique(fieldsdf$endpoint)
7065
}

R/process-error.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ throw_if_loc_error <- function(resp) {
1111
if (num_grps > 2) {
1212
stop2(
1313
"Your request resulted in a 500 error, likely because you have ",
14-
"requested too many fields in your request (the locations endpoint ",
14+
"requested too many fields in your request (the location endpoint ",
1515
"currently has restrictions on the number of fields/groups you can ",
1616
"request). Try slimming down your field list and trying again."
1717
)
1818
}
1919
}
2020
}
2121

22+
# Not sure this is still applicable
2223
#' @noRd
2324
hit_locations_ep <- function(url) {
2425
grepl(
25-
"^https://api.patentsview.org/locations/",
26+
"^https://search.patentsview.org/api/v1/location/",
2627
url,
2728
ignore.case = TRUE
2829
)
@@ -32,7 +33,7 @@ hit_locations_ep <- function(url) {
3233
get_num_groups <- function(url) {
3334
prsd_json_filds <- gsub(".*&f=([^&]*).*", "\\1", utils::URLdecode(url))
3435
fields <- jsonlite::fromJSON(prsd_json_filds)
35-
grps <- fieldsdf[fieldsdf$endpoint == "locations" &
36+
grps <- fieldsdf[fieldsdf$endpoint == "location" &
3637
fieldsdf$field %in% fields, "group"]
3738
length(unique(grps))
3839
}
@@ -52,5 +53,5 @@ xheader_er_or_status <- function(resp) {
5253
#' @noRd
5354
get_x_status <- function(resp) {
5455
headers <- httr::headers(resp)
55-
headers[grepl("x-status-reason", names(headers), ignore.case = TRUE)]
56+
headers[grepl("x-status-reason$", names(headers), ignore.case = TRUE)]
5657
}

R/search-pv.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' @noRd
22
get_base <- function(endpoint) {
3-
sprintf("https://search.patentsview.org/api/v1/%s/", to_singular(endpoint))
3+
sprintf("https://search.patentsview.org/api/v1/%s/", endpoint)
44
}
55

66
#' @noRd
@@ -201,7 +201,7 @@ request_apply <- function(ex_res, method, query, base_url, arg_list, api_key, ..
201201
#'
202202
#' search_pv(
203203
#' query = qry_funs$gt(patent_year = 2010),
204-
#' fields = get_fields("patents", c("patents", "assignees_at_grant"))
204+
#' fields = get_fields("patent", c("patents", "assignees"))
205205
#' )
206206
#'
207207
#' search_pv(
@@ -212,27 +212,27 @@ request_apply <- function(ex_res, method, query, base_url, arg_list, api_key, ..
212212
#' )
213213
#'
214214
#' search_pv(
215-
#' query = qry_funs$eq(name_last = "crew"),
216-
#' endpoint = "inventors",
215+
#' query = qry_funs$eq(inventor_name_last = "Crew"),
216+
#' endpoint = "inventor",
217217
#' all_pages = TRUE
218218
#' )
219219
#'
220220
#' search_pv(
221-
#' query = qry_funs$contains(name_last = "smith"),
222-
#' endpoint = "assignees"
221+
#' query = qry_funs$contains(assignee_individual_name_last = "Smith"),
222+
#' endpoint = "assignee"
223223
#' )
224224
#'
225225
#' search_pv(
226-
#' query = qry_funs$contains(inventors_at_grant.name_last = "smith"),
227-
#' endpoint = "patents",
226+
#' query = qry_funs$contains(inventors_at_grant.name_last = "Smith"),
227+
#' endpoint = "patent",
228228
#' config = httr::timeout(40)
229229
#' )
230230
#' }
231231
#'
232232
#' @export
233233
search_pv <- function(query,
234234
fields = NULL,
235-
endpoint = "patents",
235+
endpoint = "patent",
236236
subent_cnts = FALSE,
237237
mtchd_subent_only = lifecycle::deprecated(),
238238
page = 1,

R/unnest-pv-data.R

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#' This function suggests a value that you could use for the \code{pk} argument
44
#' in \code{\link{unnest_pv_data}}, based on the endpoint you searched.
55
#' It will return a potential unique identifier for a given entity (i.e., a
6-
#' given endpoint). For example, it will return "patent_number" when
7-
#' \code{endpoint = "patents"}.
6+
#' given endpoint). For example, it will return "patent_id" when
7+
#' \code{endpoint = "patent"}.
88
#'
99
#' @param endpoint The endpoint which you would like to know a potential primary
1010
#' key for.
@@ -13,21 +13,15 @@
1313
#' \code{\link{unnest_pv_data}}.
1414
#'
1515
#' @examples
16-
#' get_ok_pk(endpoint = "inventors") # Returns "inventor_id"
17-
#' get_ok_pk(endpoint = "cpc_subsections") # Returns "cpc_subsection_id"
16+
#' get_ok_pk(endpoint = "inventor")
17+
#' get_ok_pk(endpoint = "cpc_subclass")
18+
#' get_ok_pk("publication/rel_app_text")
1819
#'
1920
#' @export
2021
get_ok_pk <- function(endpoint) {
21-
es_eps <- c(
22-
"uspc_mainclasses" = "uspc_mainclass_id",
23-
"nber_subcategories" = "nber_subcategory_id",
24-
"patents" = "patent_number"
25-
)
26-
ifelse(
27-
endpoint %in% names(es_eps),
28-
es_eps[[endpoint]],
29-
gsub("s$", "_id", endpoint)
30-
)
22+
unnested_endpoint <- sub("^(patent|publication)/", "", endpoint)
23+
possible_pks <- c("patent_id", "document_number", paste0(unnested_endpoint, "_id"))
24+
fieldsdf[fieldsdf$endpoint == endpoint & fieldsdf$field %in% possible_pks, "field"]
3125
}
3226

3327
#' Unnest PatentsView data
@@ -58,9 +52,9 @@ get_ok_pk <- function(endpoint) {
5852
#' @examples
5953
#' \dontrun{
6054
#'
61-
#' fields <- c("patent_number", "patent_title", "inventor_city", "inventor_country")
55+
#' fields <- c("patent_id", "patent_title", "inventors.inventor_city", "inventors.inventor_country")
6256
#' res <- search_pv(query = '{"_gte":{"patent_year":2015}}', fields = fields)
63-
#' unnest_pv_data(data = res$data, pk = "patent_number")
57+
#' unnest_pv_data(data = res$data, pk = "patent_id")
6458
#' }
6559
#'
6660
#' @export

R/utils.R

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,3 @@ format_num <- function(x) {
1111
big.mark = ",", scientific = FALSE, trim = TRUE
1212
)
1313
}
14-
15-
#' @noRd
16-
to_singular <- function(plural) {
17-
if (endsWith(plural, "ees")) {
18-
sub("ees$", "ee", plural)
19-
} else if (endsWith(plural, "ies")) {
20-
sub("ies$", "y", plural)
21-
} else if (endsWith(plural, "es")) {
22-
sub("es$", "", plural)
23-
} else if (endsWith(plural, "s")) {
24-
sub("s$", "", plural)
25-
} else {
26-
plural
27-
}
28-
}
29-
30-
#' @noRd
31-
to_plural <- function(singular) {
32-
if (endsWith(singular, "y")) {
33-
sub("y$", "ies", singular)
34-
} else if (endsWith(singular, "s")) {
35-
paste0(singular, "es")
36-
} else {
37-
paste0(singular, "s")
38-
}
39-
}

R/validate-args.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ validate_args <- function(api_key, fields, endpoint, method, page, per_page,
4646
}
4747

4848
#' @noRd
49-
validate_groups <- function(groups) {
50-
ok_grps <- unique(fieldsdf$group)
49+
validate_groups <- function(endpoint, groups) {
50+
ok_grps <- unique(fieldsdf[fieldsdf$endpoint == endpoint, "group"])
5151
asrt(
5252
all(groups %in% ok_grps),
53-
"group must be one of the following: ", paste(ok_grps, collapse = ", ")
53+
"for the ", endpoint, " endpoint, group must be one of the following: ",
54+
paste(ok_grps, collapse = ", ")
5455
)
5556
}
5657

0 commit comments

Comments
 (0)