## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(biohttp)

## -----------------------------------------------------------------------------
res <- httr2::with_mocked_responses(
  list(httr2::response(
    status_code = 200,
    headers = list(`content-type` = "application/json"),
    body = charToRaw('{"symbol":"BRCA1","entrezgene":672}')
  )),
  get_json("https://mygene.info/v3", path = "gene/672", source = "MyGene")
)

res$ok
res$status
res$data$symbol

## -----------------------------------------------------------------------------
STATUS_LEVELS

## -----------------------------------------------------------------------------
render <- function(res) {
  switch(res$status,
    ok = paste("got", length(res$data), "fields"),
    no_data = "nothing found for that query",
    skipped = "source paused, try again shortly",
    rate_limited = "slow down",
    res$error
  )
}

render(status_ok(data = list(a = 1, b = 2), source = "MyGene"))
render(status_no_data(source = "MyGene"))
render(status_error(source = "MyGene", http = 503L))

## -----------------------------------------------------------------------------
body_or_null(status_ok(data = list(n = 1)))
body_or_null(status_error(source = "MyGene"))

## -----------------------------------------------------------------------------
mygene_query <- function(symbol, species = "human") {
  res <- get_json(
    "https://mygene.info/v3",
    path = "query",
    query = list(q = symbol, species = species),
    source = "MyGene"
  )
  if (!res$ok) {
    return(res)
  }
  # Reshape the body, and return an envelope so the caller's branching still
  # works. Never return a bare value on success and NULL on failure: that is the
  # shape this package exists to replace.
  status_ok(
    data = pluck_at(res$data, "hits", default = list()),
    source = "MyGene",
    http = res$http
  )
}

## -----------------------------------------------------------------------------
req <- req_defaults(
  httr2::request("https://example.org/v1"),
  headers = list(Authorization = "Bearer a-real-token")
)
# The value is not in the printed request.
any(grepl("a-real-token", capture.output(print(req)), fixed = TRUE))

## ----eval = FALSE-------------------------------------------------------------
# get_json(
#   "https://example.org/v1",
#   path = "lookup",
#   source = "Example",
#   throttle = list(capacity = 10, fill_time_s = 60)
# )

## ----eval = FALSE-------------------------------------------------------------
# res <- get_json_many(
#   "https://mygene.info/v3",
#   path = "query",
#   queries = lapply(c("BRCA1", "TP53", "EGFR"), function(g) list(q = g)),
#   source = "MyGene",
#   throttle = list(capacity = 10, fill_time_s = 60)
# )
# 
# vapply(res, function(r) r$status, character(1))
# #> [1] "ok" "ok" "ok"

## -----------------------------------------------------------------------------
res <- status_ok(data = list(errors = list(list(message = "bad field"))))
bad <- graphql_error(res, "gnomAD")
bad$status

## ----eval = FALSE-------------------------------------------------------------
# res <- post_json(url, body = list(query = q), source = "gnomAD")
# bad <- graphql_error(res, "gnomAD")
# if (!is.null(bad)) {
#   return(bad)
# }

## -----------------------------------------------------------------------------
old <- as_legacy_envelope(status_ok(data = list(n = 1), source = "MyGene"))
str(old)

## ----eval = FALSE-------------------------------------------------------------
# httr2::with_mocked_responses(
#   list(httr2::response(status_code = 503)),
#   expect_identical(mygene_query("BRCA1")$status, "error")
# )

