Enhance HTTP API for extensions (#16067)
# HTTP Client Improvements for Extension API This PR enhances the HTTP client functionality in the Zed extension API, providing more control over requests and allowing for streaming responses. ## Key Changes 1. Extended `HttpRequest` struct: - Added `method` field to specify HTTP method - Added `headers` field for custom headers - Added optional `body` field for request payload 2. Introduced `HttpMethod` enum for supported HTTP methods 3. Updated `HttpResponse` struct: - Added `headers` field to access response headers - Changed `body` type from `String` to `Vec<u8>` for binary data support 4. Added streaming support: - New `fetch_stream` function to get a response stream - Introduced `HttpResponseStream` resource for chunked reading 5. Updated internal implementations to support these new features 6. Modified the Gleam extension to use the new API structure ## Motivation These changes provide extension developers with more flexibility and control over HTTP requests. The streaming support is particularly useful for handling large responses efficiently or ideally streaming into the UI. ## Testing - [x] Updated existing tests - [ ] Added new tests for streaming functionality ## Next Steps - Consider adding more comprehensive examples in the documentation - Evaluate performance impact of streaming for large responses Please review and let me know if any adjustments are needed. Release Notes: - N/A --------- Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
This commit is contained in:
parent
f952126319
commit
fc64843dd5
5 changed files with 173 additions and 32 deletions
|
@ -19,7 +19,9 @@ pub use wit::{
|
|||
github_release_by_tag_name, latest_github_release, GithubRelease, GithubReleaseAsset,
|
||||
GithubReleaseOptions,
|
||||
},
|
||||
zed::extension::http_client::{fetch, HttpRequest, HttpResponse},
|
||||
zed::extension::http_client::{
|
||||
fetch, fetch_stream, HttpMethod, HttpRequest, HttpResponse, HttpResponseStream,
|
||||
},
|
||||
zed::extension::nodejs::{
|
||||
node_binary_path, npm_install_package, npm_package_installed_version,
|
||||
npm_package_latest_version,
|
||||
|
|
|
@ -1,16 +1,45 @@
|
|||
interface http-client {
|
||||
/// An HTTP request.
|
||||
record http-request {
|
||||
/// The HTTP method for the request.
|
||||
method: http-method,
|
||||
/// The URL to which the request should be made.
|
||||
url: string,
|
||||
/// Headers for the request.
|
||||
headers: list<tuple<string, string>>,
|
||||
/// The request body.
|
||||
body: option<list<u8>>,
|
||||
}
|
||||
|
||||
/// HTTP methods.
|
||||
enum http-method {
|
||||
get,
|
||||
post,
|
||||
put,
|
||||
delete,
|
||||
head,
|
||||
options,
|
||||
patch,
|
||||
}
|
||||
|
||||
/// An HTTP response.
|
||||
record http-response {
|
||||
/// The response headers.
|
||||
headers: list<tuple<string, string>>,
|
||||
/// The response body.
|
||||
body: string,
|
||||
body: list<u8>,
|
||||
}
|
||||
|
||||
/// Performs an HTTP request and returns the response.
|
||||
fetch: func(req: http-request) -> result<http-response, string>;
|
||||
|
||||
/// An HTTP response stream.
|
||||
resource http-response-stream {
|
||||
/// Retrieves the next chunk of data from the response stream.
|
||||
/// Returns None if the stream has ended.
|
||||
next-chunk: func() -> result<option<list<u8>>, string>;
|
||||
}
|
||||
|
||||
/// Performs an HTTP request and returns a response stream.
|
||||
fetch-stream: func(req: http-request) -> result<http-response-stream, string>;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue