Skip to content

Migrate HTTP requests to use httr2#21

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-20
Draft

Migrate HTTP requests to use httr2#21
Copilot wants to merge 2 commits intomainfrom
copilot/fix-20

Conversation

Copy link
Contributor

Copilot AI commented Aug 24, 2025

This PR migrates all HTTP requests in the reservatoriosBR package from direct URL calls to using the modern httr2 package for improved reliability and error handling.

Changes Made

Before:

# Direct URL calls with potential issues
nodes_table <- rvest::read_html(url) %>%
  rvest::html_nodes("table")

json_data <- jsonlite::fromJSON(url, simplifyVector = FALSE)

After:

# Robust HTTP requests with httr2
html_content <- http_get_html(url)
nodes_table <- rvest::read_html(html_content) %>%
  rvest::html_nodes("table")

json_data <- http_get_json(url)

Implementation Details

  • Added httr2 dependency to DESCRIPTION
  • Created HTTP utility functions in R/utils-http.R:
    • http_get_html(): Makes HTTP requests and returns content for HTML parsing
    • http_get_json(): Makes HTTP requests and returns parsed JSON data
  • Updated all functions that make HTTP requests:
    • reservatorio_cantareira()
    • reservatorio_sin()
    • reservatorio_nordeste_semiarido()
    • ultima_medicao()
    • ONS_reservatorios()

Benefits

  • Better error handling: httr2 provides superior HTTP error management compared to direct URL calls
  • Improved reliability: More robust handling of network issues, timeouts, and server errors
  • Future extensibility: Easy to add features like retries, custom headers, or authentication
  • Modern best practices: Follows current R ecosystem standards for HTTP requests

Backward Compatibility

All function APIs remain unchanged - users can upgrade seamlessly without modifying their code. The migration is completely internal to the package implementation.

Fixes #20.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cloud.r-project.org
    • Triggering command: /usr/lib/R/bin/exec/R -e install.packages(c(&#39;devtools&#39;,~&#43;~&#39;httr2&#39;,~&#43;~&#39;rvest&#39;,~&#43;~&#39;glue&#39;,~&#43;~&#39;janitor&#39;,~&#43;~&#39;dplyr&#39;,~&#43;~&#39;jsonlite&#39;,~&#43;~&#39;magrittr&#39;,~&#43;~&#39;readr&#39;,~&#43;~&#39;usethis&#39;,~&#43;~&#39;tibble&#39;,~&#43;~&#39;tidyr&#39;),~&#43;~repos=&#39;REDACTED&#39;) (dns block)
  • cran.r-project.org
    • Triggering command: /usr/lib/R/bin/exec/R -e options(repos=&#39;REDACTED&#39;);~&#43;~install.packages(&#39;httr2&#39;) (dns block)
  • cran.rstudio.com
    • Triggering command: /usr/lib/R/bin/exec/R -e install.packages(c(&#39;devtools&#39;,~&#43;~&#39;httr2&#39;,~&#43;~&#39;rvest&#39;,~&#43;~&#39;glue&#39;,~&#43;~&#39;janitor&#39;,~&#43;~&#39;dplyr&#39;,~&#43;~&#39;jsonlite&#39;,~&#43;~&#39;magrittr&#39;,~&#43;~&#39;utils&#39;,~&#43;~&#39;readr&#39;,~&#43;~&#39;usethis&#39;,~&#43;~&#39;tibble&#39;,~&#43;~&#39;tidyr&#39;),~&#43;~repos=&#39;REDACTED&#39;) (dns block)
  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor Author

Copilot AI commented Aug 24, 2025

@brunomioto 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Co-authored-by: brunomioto <10459392+brunomioto@users.noreply.github.com>
Copilot AI changed the title [WIP] Use httr2 Migrate HTTP requests to use httr2 Aug 24, 2025
Copilot AI requested a review from brunomioto August 24, 2025 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use httr2

2 participants