Closed Btibert3 closed 6 years ago
Modified zzz.R
and shown below to account for the datacenter and add as a parameter in chmp_GET
to pass to chmp_base
.
With those tweaks, the following code now works:
options(stringsAsFactors = FALSE)
## load the package
library(chimpr)
## API key
Sys.setenv(MAILCHIMP_KEY = "mykeyhere-us17")
## test
chmp_ping(dc="us17")
The zzz.R file on my local machine:
ct <- function(l) Filter(Negate(is.null), l)
chimpr_ua <- function() {
versions <- c(
paste0("r-curl/", utils::packageVersion("curl")),
paste0("crul/", utils::packageVersion("crul")),
sprintf("rOpenSci(chimpr/%s)", utils::packageVersion("chimpr"))
)
paste0(versions, collapse = " ")
}
chmp_GET <- function(dc = "us7", path, key, query = list(), ...){
cli <- crul::HttpClient$new(
url = chmp_base(dc),
opts = c(list(useragent = chimpr_ua()), ...),
auth = crul::auth(user = "anystring", pwd = check_key(key))
)
temp <- cli$get(
path = file.path("3.0", path),
query = query)
err_catcher(temp)
#temp$raise_for_status()
x <- temp$parse("UTF-8")
return(x)
}
err_catcher <- function(x) {
if (x$status_code > 201) {
if (x$response_headers$`content-type` ==
"application/problem+json; charset=utf-8") {
xx <- jsonlite::fromJSON(x$parse("UTF-8"))
xx <- paste0("\n ", paste(names(xx), unname(xx), sep = ": ",
collapse = "\n "))
stop(xx, call. = FALSE)
} else {
x$raise_for_status()
}
}
}
chmp_parse <- function(x, parse) {
jsonlite::fromJSON(x, parse)
}
check_key <- function(x){
tmp <- if (is.null(x)) Sys.getenv("MAILCHIMP_KEY", "") else x
if (tmp == "") {
stop("need an API key for the Mailchimp API", call. = FALSE)
} else {
tmp
}
}
chmp_base <- function(x="us7") sprintf("https://%s.api.mailchimp.com", x)
space <- function(x) gsub("\\s", "%20", x)
assert_is <- function(x, y) {
if (!is.null(x)) {
if (!class(x) %in% y) {
stop(deparse(substitute(x)), " must be of class ",
paste0(y, collapse = ", "), call. = FALSE)
}
}
}
assert_n <- function(x, n) {
if (!is.null(x)) {
if (!length(x) == n) {
stop(deparse(substitute(x)), " must be length ", n, call. = FALSE)
}
}
}
`%||%` <- function(x, y) {
if (is.null(x) || length(x) == 0) y else x
}
thanks @Btibert3
a question for you: In a single R session are you going to use the same datacenter the whole time, or do you switch between data centers? Because if the main use case is a single person only interacts with one datacenter, I think it makes more sense to set datacenter as an env var or similar at the beginning of a session instead of as a parameter to each fxn call. thoguhts?
It's a good question. In my current use case it is a singular data center, but if I were managing multiple client accounts, its possible to envision a scenario where changing it on the fly makes sense. I probably could go either way TBH.
thanks. can you install from the rework
branch remotes::install_github("sckott/chimpr@rework")
and try again? now there's a ChmpClient
R6 object to create first, then pass on to all other classes (and root and ping moved inside ChmpClient
), now chmp_lists()
is only method that's a simple function and not an R6
Looks good!
glad it works, will merge to master now
This issue is to address the hardcoded datacenter found in
chmp_base
inzzz.R
. With the hardcoded datacenter, a call to my account fails.