zed_extension_api: Add HttpRequestBuilder (#16165)

This PR adds an `HttpRequestBuilder` to the extension API to allow for a
more ergonomic way for constructing HTTP requests within extensions.

The HTTP client functionality is now also exposed via the
`zed_extension_api::http_client` module instead of top-level.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers
2024-08-13 11:12:10 -04:00
committed by GitHub
parent 0dbecee03f
commit 8a9c58e515
5 changed files with 125 additions and 34 deletions
+16 -15
View File
@@ -11,7 +11,8 @@ use html_to_markdown::{
StartTagOutcome, TagHandler,
};
use zed_extension_api::{
self as zed, HttpMethod, HttpRequest, KeyValueStore, RedirectPolicy, Result,
http_client::{HttpMethod, HttpRequest, RedirectPolicy},
KeyValueStore, Result,
};
pub fn index(package: String, database: &KeyValueStore) -> Result<()> {
@@ -20,13 +21,13 @@ pub fn index(package: String, database: &KeyValueStore) -> Result<()> {
"Zed (Gleam Extension)".to_string(),
)];
let response = zed::fetch(&HttpRequest {
method: HttpMethod::Get,
url: format!("https://hexdocs.pm/{package}"),
headers: headers.clone(),
body: None,
redirect_policy: RedirectPolicy::FollowAll,
})?;
let response = HttpRequest::builder()
.method(HttpMethod::Get)
.url(format!("https://hexdocs.pm/{package}"))
.headers(headers.clone())
.redirect_policy(RedirectPolicy::FollowAll)
.build()?
.fetch()?;
let (package_root_markdown, modules) =
convert_hexdocs_to_markdown(&mut io::Cursor::new(&response.body))?;
@@ -34,13 +35,13 @@ pub fn index(package: String, database: &KeyValueStore) -> Result<()> {
database.insert(&package, &package_root_markdown)?;
for module in modules {
let response = zed::fetch(&HttpRequest {
method: HttpMethod::Get,
url: format!("https://hexdocs.pm/{package}/{module}.html"),
headers: headers.clone(),
body: None,
redirect_policy: RedirectPolicy::FollowAll,
})?;
let response = HttpRequest::builder()
.method(HttpMethod::Get)
.url(format!("https://hexdocs.pm/{package}/{module}.html"))
.headers(headers.clone())
.redirect_policy(RedirectPolicy::FollowAll)
.build()?
.fetch()?;
let (markdown, _modules) =
convert_hexdocs_to_markdown(&mut io::Cursor::new(&response.body))?;