Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only create a single Managed_Resource for the response stream in HTTP.request #12405

Merged
merged 4 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## Enso Signatures 1.0
## module Standard.Base.Network.HTTP.Response
- type Enso_Http_Response
- new java_response:Standard.Base.Network.HTTP.Response.EnsoHttpResponse -> Standard.Base.Network.HTTP.Response.Enso_Http_Response
- type Response
- Value internal_http_response:Standard.Base.Any.Any body_object:Standard.Base.Any.Any=
- body self -> Standard.Base.Any.Any
- code self -> Standard.Base.Any.Any
- content_length self -> Standard.Base.Any.Any
Expand All @@ -11,6 +12,7 @@
- decode_as_text self encoding:(Standard.Base.Data.Text.Encoding.Encoding|Standard.Base.System.File_Format.Infer)= -> Standard.Base.Any.Any
- get_header self name:Standard.Base.Data.Text.Text ~if_missing:Standard.Base.Any.Any= -> Standard.Base.Any.Any
- headers self -> Standard.Base.Any.Any
- new java_http_response:Standard.Base.Network.HTTP.Response.EnsoHttpResponse -> Standard.Base.Any.Any
- to_js_object self -> Standard.Base.Any.Any
- uri self -> Standard.Base.Any.Any
- with_materialized_body self -> Standard.Base.Any.Any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- decode_as_json self encoding:(Standard.Base.Data.Text.Encoding.Encoding|Standard.Base.System.File_Format.Infer)= -> Standard.Base.Any.Any
- decode_as_text self encoding:(Standard.Base.Data.Text.Encoding.Encoding|Standard.Base.System.File_Format.Infer)= -> Standard.Base.Any.Any
- materialize self -> Standard.Base.Any.Any
- new stream:Standard.Base.Any.Any metadata:Standard.Base.System.File_Format_Metadata.File_Format_Metadata uri:Standard.Base.Network.URI.URI -> Standard.Base.Any.Any
- new input_stream:Standard.Base.Any.Any metadata:Standard.Base.System.File_Format_Metadata.File_Format_Metadata uri:Standard.Base.Network.URI.URI -> Standard.Base.Any.Any
- to_text self -> Standard.Base.Any.Any
- with_stream self action:Standard.Base.Any.Any -> Standard.Base.Any.Any
- write self file:Standard.Base.System.File.Generic.Writable_File.Writable_File on_existing_file:Standard.Base.Any.Any= -> Standard.Base.Any.Any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ type HTTP
all_headers = headers + boundary_header_list
mapped_headers = all_headers.map on_problems=No_Wrap.Value .to_java_pair

response = Response.Value (EnsoSecretHelper.makeRequest (self.make_client self resolved_body.hash) builder req.uri.to_java_representation mapped_headers (cache_policy.should_use_cache req))
response = Response.new (EnsoSecretHelper.makeRequest (self.make_client self resolved_body.hash) builder req.uri.to_java_representation mapped_headers (cache_policy.should_use_cache req))
if error_on_failure_code.not || response.code.is_success then response else
body = response.body.decode_as_text.catch Any _->""
message = if body.is_empty then Nothing else body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import project.Data.Vector.Vector
import project.Error.Error
import project.Errors.File_Error.File_Error
import project.Network.HTTP.Header.Header
import project.Network.HTTP.HTTP_Error.HTTP_Error
import project.Network.HTTP.HTTP_Status_Code.HTTP_Status_Code
import project.Network.HTTP.Response_Body.Response_Body
import project.Network.URI.URI
Expand All @@ -20,6 +21,7 @@ import project.System.File_Format.Auto_Detect
import project.System.File_Format.File_Format
import project.System.File_Format.Infer
import project.System.File_Format_Metadata.File_Format_Metadata
import project.System.Input_Stream.Input_Stream
from project.Data.Text.Extensions import all
from project.Metadata import Display, Widget
from project.Network.HTTP.Response_Body import decode_format_selector
Expand All @@ -38,7 +40,11 @@ type Response
response.
- body_object: The body of the response. If nothing will be read from the
internal_http_response. Allows for materialization.
Value internal_http_response body_object=Nothing
private Value internal_http_response:Enso_Http_Response body_object=Nothing

## PRIVATE
new java_http_response:EnsoHttpResponse =
Response.Value (Enso_Http_Response.new java_http_response)

## PRIVATE
Creates a new Response with the body materialized.
Expand All @@ -50,9 +56,7 @@ type Response
ICON metadata
Get the uri for the response.
uri : URI
uri self =
uri_string = self.internal_http_response.uri.toString
URI.parse uri_string
uri self = self.internal_http_response.uri

## GROUP Metadata
ICON metadata
Expand Down Expand Up @@ -126,7 +130,7 @@ type Response

example_code = Examples.get_response.code
code : HTTP_Status_Code
code self = HTTP_Status_Code.Value self.internal_http_response.statusCode
code self = HTTP_Status_Code.Value self.internal_http_response.status_code

## ALIAS parse
GROUP Conversions
Expand Down Expand Up @@ -238,3 +242,15 @@ filename_from_content_disposition content_disposition =
content_disposition.if_not_nothing <|
match = 'filename="(.*)"'.to_regex.match content_disposition
match.if_not_nothing <| match.get 1

## PRIVATE
After an `EnsoHttpResponse` has been returned from Java to Enso, it should be
converted to this, which takes care of ensuring that the body `InputStream`
is wrapped as a `Managed_Resource`.
type Enso_Http_Response
private Value uri:URI (headers:HttpHeaders) body:Input_Stream status_code:Integer

new java_response:EnsoHttpResponse -> Enso_Http_Response =
uri = URI.parse java_response.uri.toString
error_handler = (HTTP_Error.handle_java_exceptions uri)
Enso_Http_Response.Value uri java_response.headers (Input_Stream.new java_response.body error_handler) java_response.statusCode
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ from project.Metadata.Choice import Option
from project.Metadata.Widget import Single_Choice
from project.System.File_Format import format_types

polyglot java import java.io.InputStream

## PRIVATE
How large a response body can be before it is written to a temporary file.
maximum_body_in_memory = 4192
Expand All @@ -51,9 +49,8 @@ type Response_Body
- stream: The body of the response as an InputStream.
- metadata: File format metadata associated with the response.
- uri: The URI of the response.
new : InputStream -> File_Format_Metadata -> URI -> Response_Body
new stream (metadata : File_Format_Metadata) (uri : URI) =
input_stream = Input_Stream.new stream (HTTP_Error.handle_java_exceptions uri)
new : Input_Stream -> File_Format_Metadata -> URI -> Response_Body
new input_stream (metadata : File_Format_Metadata) (uri : URI) =
Response_Body.Raw_Stream input_stream metadata uri

## PRIVATE
Expand Down
Loading