-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from matthiasgomolka/api_v3
API v3
- Loading branch information
Showing
67 changed files
with
1,134 additions
and
2,468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Type: Package | ||
Package: simfinapi | ||
Title: Accessing 'SimFin' Data | ||
Version: 0.2.4 | ||
Version: 1.0.0 | ||
Authors@R: | ||
person("Matthias", "Gomolka", , "[email protected]", role = c("aut", "cre")) | ||
Description: Through simfinapi, you can intuitively access the 'SimFin' | ||
|
@@ -14,13 +14,11 @@ BugReports: https://github.com/matthiasgomolka/simfinapi/issues | |
Depends: | ||
R (>= 3.5) | ||
Imports: | ||
bit64, | ||
checkmate (>= 2.0.0), | ||
data.table (>= 1.12.8), | ||
future.apply (>= 1.4.0), | ||
httr, | ||
httr2, | ||
lifecycle, | ||
memoise (>= 1.1.0), | ||
progressr, | ||
RcppSimdJson (>= 0.1.1), | ||
utils | ||
Suggests: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,34 @@ | ||
#' @importFrom httr content | ||
#' @importFrom RcppSimdJson fparse | ||
#' @importFrom utils hasName | ||
#' @importFrom memoise memoise cache_filesystem | ||
#' @importFrom httr GET | ||
call_api <- function(..., cache_dir) { | ||
# check for cache setup | ||
#' @importFrom httr2 request req_url_path_append req_headers req_user_agent req_url_query | ||
#' req_perform last_response resp_is_error resp_body_string | ||
#' @noRd | ||
call_api <- function(url, api_key, cache_dir, ...) { | ||
# check for cache setup | ||
|
||
if (is.null(cache_dir)) { | ||
warning( | ||
"'cache_dir' not set. Defaulting to 'tempdir()'. Thus, API results will ", | ||
"only be cached during this session. To learn why and how to cache ", | ||
"results over the end of this session, see `?sfa_set_cache_dir`.\n\n", | ||
"[This warning appears only once per session.]", | ||
call. = FALSE | ||
) | ||
sfa_set_cache_dir(tempdir(), create = TRUE) | ||
cache_dir <- getOption("sfa_cache_dir") | ||
} | ||
if (is.null(cache_dir)) { | ||
warning("'cache_dir' not set. Defaulting to 'tempdir()'. Thus, API results will ", "only be cached during this session. To learn why and how to cache ", | ||
"results over the end of this session, see `?sfa_set_cache_dir`.\n\n", "[This warning appears only once per session.]", | ||
call. = FALSE) | ||
sfa_set_cache_dir(tempdir(), create = TRUE) | ||
cache_dir <- getOption("sfa_cache_dir") | ||
} | ||
|
||
checkmate::assert_directory(cache_dir, access = "rw") | ||
checkmate::assert_directory(cache_dir, access = "rw") | ||
|
||
mem_GET <- memoise::memoise( | ||
httr::GET, | ||
cache = memoise::cache_filesystem(cache_dir) | ||
) | ||
req <- httr2::request("https://prod.simfin.com/api/v3") |> | ||
httr2::req_url_path_append(url) |> | ||
httr2::req_headers(Authorization = api_key, accept = "application/json") |> | ||
httr2::req_user_agent("simfinapi (https://github.com/matthiasgomolka/simfinapi)") |> | ||
httr2::req_url_query(...) | ||
|
||
# call API and transform result to list | ||
response <- mem_GET( | ||
url = "https://legacy.simfin.com", | ||
... | ||
) | ||
request <- response[["request"]][["url"]] | ||
content <- RcppSimdJson::fparse( | ||
response[["content"]], | ||
max_simplify_lvl = "vector", | ||
int64_policy = "integer64" | ||
) | ||
mem_req_perform <- memoise::memoise(httr2::req_perform, cache = memoise::cache_filesystem(cache_dir)) | ||
|
||
if (utils::hasName(content, "error")) { | ||
warning("From 'SimFin' API: '", content[["error"]], "'", call. = FALSE) | ||
return(NULL) | ||
} | ||
resp <- tryCatch(mem_req_perform(req), error = \(error) httr2::last_response()) | ||
|
||
if (httr2::resp_is_error(resp)) { | ||
handle_api_error(resp) | ||
} | ||
|
||
return(list(request = request, content = content)) | ||
return(resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.