Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httr:::print.response when there are no headers #587

Closed
krose opened this issue Apr 9, 2019 · 2 comments
Closed

httr:::print.response when there are no headers #587

krose opened this issue Apr 9, 2019 · 2 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@krose
Copy link

krose commented Apr 9, 2019

It is likely out of scope for the package to fully support sftp, but I have a small request for a change in the print statement that will help me. In the new_print_statement I check if all_headers in the response object is NULL, before assigning content_type. All tests (except for a test that needs an interactive session) are passed. I'll be happy to create a pull request.

library(httr)
#> Warning: package 'httr' was built under R version 3.4.4

# simplyfied response from entsoe
x <- httr:::response(url = "sftp://sftp-transparency.entsoe.eu/TP_export/", 
  status_code = 0L, headers = raw(0), all_headers = NULL)
httr:::print.response(x)
#> Error in x$headers$`content-type`: $ operator is invalid for atomic vectors

`%||%` <- httr:::`%||%`

new_print_response <- function(x, ..., max.lines = 10, width = getOption("width")) {
  if (!is.null(x$all_headers)) {
    content_type <- x$headers$`content-type`
  } else {
    content_type <- NULL
  }
  cat("Response [", x$url, "]\n", sep = "")
  cat("  Date: ", format(x$date, "%Y-%m-%d %H:%M"), "\n", sep = "")
  cat("  Status: ", x$status_code, "\n", sep = "")
  cat("  Content-Type: ", content_type %||% "<unknown>", "\n", sep = "")
  size <- length(x$content)
  if (size == 0) {
    cat("<EMPTY BODY>\n")
    return()
  }
  cat("  Size: ", bytes(size), "\n", sep = "")
  if (is.path(x$content)) {
    cat("<ON DISK> ", x$content)
    return()
  }
  if (!is_text(content_type)) {
    cat("<BINARY BODY>\n")
    return()
  }
  suppressMessages(text <- content(x, "text"))
  breaks <- gregexpr("\n", text, fixed = TRUE)[[1]]
  last_line <- breaks[min(length(breaks), max.lines)]
  lines <- strsplit(substr(text, 1, last_line), "\n")[[1]]
  too_wide <- nchar(lines) > width
  lines[too_wide] <- paste0(substr(lines[too_wide], 1, width - 3), "...")
  cat(lines, sep = "\n")
  if (max.lines < length(breaks)) 
    cat("...\n")
  invisible(x)
}
new_print_response(x)
#> Response [sftp://sftp-transparency.entsoe.eu/TP_export/]
#>   Date: NULL
#>   Status: 0
#>   Content-Type: <unknown>
#> <EMPTY BODY>
#> NULL

Here are the devtools::test() results.

> devtools::test()
Loading httr
Testing httr
√ | OK F W S | Context
√ | 22       | Body [2.8 s]
√ |  4       | Callback
√ |  6       | Config [1.4 s]
√ |  8       | test-content-parse
√ |  4       | Content [0.7 s]
√ | 13       | Headers [1.1 s]
√ |  5       | http_condition
√ |  6       | http_error [0.5 s]
√ |  3       | test-http-head [0.8 s]
√ | 17       | OAuth cache [0.1 s]
√ |  2       | OAuth: server side
x | 16 1     | OAuth [1.0 s]
--------------------------------------------------------------------------------
test-oauth.R:114: failure: can not use oob in non-interactive session
`check_oob(TRUE)` did not throw an error.
--------------------------------------------------------------------------------
√ |  3       | oauth_signature
√ |  5       | parse_media
√ |  3       | Request [1.3 s]
√ | 17       | Response [2.0 s]
√ |  3       | test-retry
√ |  2       | SSL [1.3 s]
√ | 20       | URL parsing and building

== Results =====================================================================
Duration: 13.5 s

OK:       159
Failed:   1
Warnings: 0
Skipped:  0
@hadley
Copy link
Member

hadley commented Apr 3, 2020

A PR would be great if you're still interested.

@hadley hadley added the bug an unexpected problem or unintended behavior label Apr 3, 2020
@hadley
Copy link
Member

hadley commented Oct 31, 2023

httr has been superseded in favour of httr2, so is no longer under active development. If this problem is still important to you in httr2, I'd suggest filing an issue offer there 😄. Thanks for using httr!

@hadley hadley closed this as completed Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants