Skip to content

Commit

Permalink
implement an api parameter to choose the scope of the application
Browse files Browse the repository at this point in the history
  • Loading branch information
RLesur committed Oct 31, 2019
1 parent 2966348 commit bf35085
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 29 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: apinsee
Title: Manage Access to Insee APIs
Version: 0.0.0.9031
Version: 0.0.0.9040
Authors@R: c(
person("Romain", "Lesur", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0002-0721-5595")),
person()
Expand All @@ -17,6 +17,7 @@ RoxygenNote: 6.1.1
Collate:
'endpoint.R'
'utils.R'
'scope.R'
'token.R'
'insee_auth.R'
'zzz.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export(TokenInsee)
export(insee_auth)
export(insee_deauth)
export(insee_endpoint)
export(insee_scopes)
export(insee_token)
importFrom(rlang,":=")
15 changes: 0 additions & 15 deletions R/endpoint.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,3 @@ insee_endpoint <- function(insee_url = getOption("apinsee.url")) {
authorize = NULL
)
}

insee_scope <- function(insee_url = getOption("apinsee.url")) {
modify_insee_url <- function(path) {
httr::modify_url(url = insee_url, path = path)
}

paths <- list(
c("metadonnees", "nomenclatures", "v1"),
c("entreprises", "sirene", "V3"),
c("entreprises", "sirene")
)

vapply(paths, modify_insee_url, character(1))

}
5 changes: 4 additions & 1 deletion R/insee_auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ NULL
#' @param cache booléen indiquant si `apinsee` doit sauvegarder les jetons
#' d'accès dans un fichier cache, par défaut `.httr-oauth`.
#' @param verbose booléen; souhaitez-vous des messages d'information ?
#' @inheritParams insee_token
#' @inheritParams insee_endpoint
#' @inheritSection insee_endpoint Utilisation interne à l'Insee
#' @encoding UTF-8
Expand Down Expand Up @@ -58,6 +59,7 @@ insee_auth <- function(
key = Sys.getenv("INSEE_APP_KEY"),
secret = Sys.getenv("INSEE_APP_SECRET"),
validity_period = 86400,
api = c("Sirene V3", "Nomenclatures v1"),
cache = FALSE,
verbose = TRUE,
insee_url = getOption("apinsee.url")
Expand All @@ -76,7 +78,8 @@ insee_auth <- function(
app = app,
cache = cache,
validity_period = validity_period,
insee_url = insee_url
insee_url = insee_url,
api = api
)

stopifnot(is_legit_token(token, verbose = TRUE))
Expand Down
43 changes: 43 additions & 0 deletions R/scope.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' Addresses of the Insee APIs
#'
#' Cette fonction renvoie les adresses des API de l'Insee.
#'
#' @param api Un vecteur de chaînes de caractères dont chaque élément comprend
#' le nom d'une API. La correspondance partielle est acceptée.
#' @inheritParams insee_endpoint
#' @inheritSection insee_endpoint Utilisation interne à l'Insee
#'
#' @return Un vecteur de chaînes de caractères comprenant les adresses des API
#' de l'Insee.
#' @keywords internal
#' @export
#'
#' @examples
#' insee_scopes()
#' insee_scopes("Sirene")
#' insee_scopes("Nomenclatures")
#' insee_scopes(c("Sirene", "Nomenclatures"))
insee_scopes <- function(
api = c("Sirene V3", "Nomenclatures v1"),
insee_url = getOption("apinsee.url")
) {
api <- match.arg(api, several.ok = TRUE)

paths <- list(
`Nomenclatures v1` = list(
c("metadonnees", "nomenclatures", "v1")
),
`Sirene V3` = list(
c("entreprises", "sirene", "V3"),
c("entreprises", "sirene")
)
)

modify_insee_url <- function(path) {
vapply(path, function(x) httr::modify_url(url = insee_url, path = x), character(1))
}

urls <- lapply(paths, modify_insee_url)

unlist(urls[api], use.names = FALSE)
}
15 changes: 11 additions & 4 deletions R/token.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @include endpoint.R
#' @include endpoint.R scope.R
NULL

#' Generate a valid token for an Insee application
Expand All @@ -9,7 +9,12 @@ NULL
#' @inheritParams httr::oauth2.0_token
#' @inheritParams insee_endpoint
#' @inheritSection insee_endpoint Utilisation interne à l'Insee
#' @param validity_period A positive integer; token validity period in seconds.
#' @param validity_period Un entier; durée de validité du jeton d'accès. Cette
#' valeur n'est utilisée que lorsque le dernier jeton d'accès a expiré ou a
#' été révoqué.
#' @param api Un vecteur de chaînes de caractères dont chaque élément comprend
#' le ou les noms des API accessibles par l'application. La correspondance
#' partielle est acceptée.
#'
#' @return Un objet de classe [TokenInsee].
#' @keywords internal
Expand All @@ -18,15 +23,17 @@ NULL
insee_token <- function(
app, cache = getOption("httr_oauth_cache"),
config_init = list(), credentials = NULL,
validity_period = 86400, insee_url = getOption("apinsee.url")
validity_period = 86400, insee_url = getOption("apinsee.url"),
api = c("Sirene V3", "Nomenclatures v1")
) {

stopifnot(
rlang::is_scalar_integerish(validity_period, finite = TRUE),
validity_period > 0
)

scope <- insee_scope()
api <- match.arg(api, several.ok = TRUE)
scope <- insee_scopes(api = api, insee_url = insee_url)

user_params <- list(
grant_type = "client_credentials",
Expand Down
8 changes: 4 additions & 4 deletions man/TokenInsee.Rd

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

8 changes: 6 additions & 2 deletions man/insee_auth.Rd

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

42 changes: 42 additions & 0 deletions man/insee_scopes.Rd

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

11 changes: 9 additions & 2 deletions man/insee_token.Rd

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

0 comments on commit bf35085

Please sign in to comment.