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:
parent
0dbecee03f
commit
8a9c58e515
5 changed files with 125 additions and 34 deletions
|
@ -1,11 +1,11 @@
|
|||
mod hexdocs;
|
||||
|
||||
use std::{fs, io};
|
||||
use zed::http_client::{HttpMethod, HttpRequest, RedirectPolicy};
|
||||
use zed::lsp::CompletionKind;
|
||||
use zed::{
|
||||
CodeLabel, CodeLabelSpan, HttpMethod, HttpRequest, KeyValueStore, LanguageServerId,
|
||||
RedirectPolicy, SlashCommand, SlashCommandArgumentCompletion, SlashCommandOutput,
|
||||
SlashCommandOutputSection,
|
||||
CodeLabel, CodeLabelSpan, KeyValueStore, LanguageServerId, SlashCommand,
|
||||
SlashCommandArgumentCompletion, SlashCommandOutput, SlashCommandOutputSection,
|
||||
};
|
||||
use zed_extension_api::{self as zed, Result};
|
||||
|
||||
|
@ -194,23 +194,20 @@ impl zed::Extension for GleamExtension {
|
|||
.ok_or_else(|| "missing package name".to_string())?;
|
||||
let module_path = components.map(ToString::to_string).collect::<Vec<_>>();
|
||||
|
||||
let response = zed::fetch(&HttpRequest {
|
||||
method: HttpMethod::Get,
|
||||
url: format!(
|
||||
let response = HttpRequest::builder()
|
||||
.method(HttpMethod::Get)
|
||||
.url(format!(
|
||||
"https://hexdocs.pm/{package_name}{maybe_path}",
|
||||
maybe_path = if !module_path.is_empty() {
|
||||
format!("/{}.html", module_path.join("/"))
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
),
|
||||
headers: vec![(
|
||||
"User-Agent".to_string(),
|
||||
"Zed (Gleam Extension)".to_string(),
|
||||
)],
|
||||
body: None,
|
||||
redirect_policy: RedirectPolicy::FollowAll,
|
||||
})?;
|
||||
))
|
||||
.header("User-Agent", "Zed (Gleam Extension)")
|
||||
.redirect_policy(RedirectPolicy::FollowAll)
|
||||
.build()?
|
||||
.fetch()?;
|
||||
|
||||
let (markdown, _modules) =
|
||||
convert_hexdocs_to_markdown(&mut io::Cursor::new(response.body))?;
|
||||
|
|
|
@ -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))?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue