Skip to content

Commit

Permalink
Merge pull request #27 from avantcredit/suppress_readr_warnings
Browse files Browse the repository at this point in the history
add silent_read_csv param to run_inline_query
  • Loading branch information
abelcastilloavant authored Jun 9, 2016
2 parents 7b16b9c + 6202a55 commit 10ab731
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: looker3
Type: Package
Title: looker3 (http://github.com/abelcastilloavant/avant-looker3)
Description: Pull data from Looker using the fancy new 3.0 API.
Version: 0.1.9
Version: 0.1.10
Author: Abel Castillo <[email protected]>
Maintainer: Abel Castillo <[email protected]>
Authors@R: c(person("Abel", "Castillo",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Version 0.1.10
- Added a param `silent_csv_read`, to switch off/on warnings while reading the stream into a data frame.

# Version 0.1.9
- Added an extra validation in `extract_query_response` to catch silent errors in the body of httr response objects.

Expand Down
8 changes: 6 additions & 2 deletions R/looker3.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#' @param filters list. Each element of the list is a length 2 character vector,
#' each vector describing one of the \code{filters} of the query.
#' @param limit numeric. The \code{limit} parameter of the query.
#' @param silent_read_csv logical. Whether or not to suppress warnings
#' when reading the streamed data into a data frame.
#'
#' @return a data.frame containing the data returned by the query
#'
Expand All @@ -24,11 +26,13 @@ looker3 <- checkr::ensure(pre = list( # model, view, and fields are
filters %is% simple_string ||
((filters %is% list || filters %is% vector) &&
(filters %contains_only% simple_string || filters %is% empty)),
limit %is% numeric && limit > 0 && limit %% 1 == 0
limit %is% numeric && limit > 0 && limit %% 1 == 0,
silent_read_csv %is% logical
),

function(model, view, fields,
filters = list(), limit = 1000) {
filters = list(), limit = 1000,
silent_read_csv = TRUE) {

env_var_descriptions <- list(
LOOKER_URL = "API url",
Expand Down
8 changes: 6 additions & 2 deletions R/response_handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ handle_logout_response <- function(logout_response) {
validate_response(logout_response)
}

extract_query_result <- function(query_response) {
extract_query_result <- function(query_response, silent_read_csv = TRUE) {
validate_response(query_response)
data_from_query <- httr::content(query_response)
if (grepl("^Error:", data_from_query)) {
# assume that the query errored quietly and that data_from_query is an error message.
stop("Looker returned the following error message:\n", as.character(data_from_query))
}
readr::read_csv(data_from_query)
if (silent_read_csv) {
suppressWarnings(readr::read_csv(data_from_query))
} else {
readr::read_csv(data_from_query)
}
}

4 changes: 2 additions & 2 deletions R/run_inline_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#'
run_inline_query <- function(base_url, client_id, client_secret,
model, view, fields, filters,
limit = 1000) {
limit = 1000, silent_read_csv) {


# The API requires you to "log in" and obtain a session token
Expand All @@ -29,5 +29,5 @@ run_inline_query <- function(base_url, client_id, client_secret,
inline_query_response <- query_api_call(base_url,
model, view, fields, filters, limit)

extract_query_result(inline_query_response)
extract_query_result(inline_query_response, silent_read_csv)
}
6 changes: 5 additions & 1 deletion man/looker3.Rd

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

2 changes: 1 addition & 1 deletion man/run_inline_query.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-run_inline_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("run_inline_query helpers called with the corresponding inputs", {
test_that("extract_query_result receives the output of query_api_call", {
with_mock(
`looker3:::query_api_call` = function(...) "response received",
`looker3:::extract_query_result` = function(inline_query_response) {
`looker3:::extract_query_result` = function(inline_query_response, ...) {
stop(paste0("extract_query_result called: "), inline_query_response)
}, {
expect_error(do.call(run_inline_query, args),
Expand Down

0 comments on commit 10ab731

Please sign in to comment.