sdkloom 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +239 -2
- package/kits/allowed-sources.json +24 -0
- package/kits/csharp/kit.json +6 -0
- package/kits/csharp/src/Runtime/Client.cs +295 -0
- package/kits/csharp/src/Runtime/Context.cs +216 -0
- package/kits/csharp/src/Runtime/Errors.cs +121 -0
- package/kits/csharp/src/Runtime/Http.cs +218 -0
- package/kits/csharp/src/Runtime/Json.cs +650 -0
- package/kits/csharp/src/Runtime/Lang.cs +212 -0
- package/kits/csharp/src/Runtime/Models.cs +206 -0
- package/kits/csharp/src/Runtime/Multipart.cs +108 -0
- package/kits/csharp/src/Runtime/Pagination.cs +122 -0
- package/kits/csharp/src/Runtime/Sse.cs +197 -0
- package/kits/csharp/src/Runtime/Testing.cs +375 -0
- package/kits/csharp/src/Runtime/Transport.cs +212 -0
- package/kits/csharp/src/Runtime/Wait.cs +57 -0
- package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
- package/kits/csharp/tests/ClientTests.cs +128 -0
- package/kits/csharp/tests/HttpTests.cs +44 -0
- package/kits/csharp/tests/JsonTests.cs +132 -0
- package/kits/csharp/tests/LangTests.cs +57 -0
- package/kits/csharp/tests/MultipartTests.cs +31 -0
- package/kits/csharp/tests/PaginationTests.cs +93 -0
- package/kits/csharp/tests/SseTests.cs +122 -0
- package/kits/csharp/tests/TestRunner.cs +93 -0
- package/kits/csharp/tests/TestingTests.cs +80 -0
- package/kits/csharp/tests/TransportTests.cs +145 -0
- package/kits/csharp/tests/WaitTests.cs +39 -0
- package/kits/csharp/tests/WebhooksTests.cs +46 -0
- package/kits/csharp/versions.json +12 -0
- package/kits/go/kit.json +6 -0
- package/kits/go/src/sdkruntime/client.go +322 -0
- package/kits/go/src/sdkruntime/context.go +207 -0
- package/kits/go/src/sdkruntime/errors.go +74 -0
- package/kits/go/src/sdkruntime/http.go +261 -0
- package/kits/go/src/sdkruntime/json.go +527 -0
- package/kits/go/src/sdkruntime/lang.go +462 -0
- package/kits/go/src/sdkruntime/multipart.go +75 -0
- package/kits/go/src/sdkruntime/pagination.go +150 -0
- package/kits/go/src/sdkruntime/sse.go +267 -0
- package/kits/go/src/sdkruntime/testing.go +343 -0
- package/kits/go/src/sdkruntime/transport.go +168 -0
- package/kits/go/src/sdkruntime/wait.go +49 -0
- package/kits/go/src/sdkruntime/webhooks.go +129 -0
- package/kits/go/tests/client_test.go +185 -0
- package/kits/go/tests/http_test.go +78 -0
- package/kits/go/tests/json_test.go +127 -0
- package/kits/go/tests/lang_test.go +98 -0
- package/kits/go/tests/multipart_test.go +29 -0
- package/kits/go/tests/pagination_test.go +69 -0
- package/kits/go/tests/sse_test.go +129 -0
- package/kits/go/tests/testing_test.go +121 -0
- package/kits/go/tests/transport_test.go +133 -0
- package/kits/go/tests/wait_test.go +46 -0
- package/kits/go/tests/webhooks_test.go +62 -0
- package/kits/go/versions.json +12 -0
- package/kits/java/kit.json +6 -0
- package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
- package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
- package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
- package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
- package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
- package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
- package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
- package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
- package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
- package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
- package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
- package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
- package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
- package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
- package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
- package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
- package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
- package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
- package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
- package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
- package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
- package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
- package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
- package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
- package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
- package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
- package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
- package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
- package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
- package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
- package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
- package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
- package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
- package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
- package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
- package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
- package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
- package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
- package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
- package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
- package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
- package/kits/java/versions.json +41 -0
- package/kits/python/kit.json +6 -0
- package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
- package/kits/python/src/sdkloom_runtime/client.py +232 -0
- package/kits/python/src/sdkloom_runtime/compat.py +195 -0
- package/kits/python/src/sdkloom_runtime/context.py +134 -0
- package/kits/python/src/sdkloom_runtime/errors.py +65 -0
- package/kits/python/src/sdkloom_runtime/http.py +192 -0
- package/kits/python/src/sdkloom_runtime/lang.py +207 -0
- package/kits/python/src/sdkloom_runtime/models.py +221 -0
- package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
- package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
- package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
- package/kits/python/src/sdkloom_runtime/sse.py +121 -0
- package/kits/python/src/sdkloom_runtime/testing.py +224 -0
- package/kits/python/src/sdkloom_runtime/transport.py +120 -0
- package/kits/python/src/sdkloom_runtime/wait.py +44 -0
- package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
- package/kits/python/tests/support.py +28 -0
- package/kits/python/tests/test_client.py +275 -0
- package/kits/python/tests/test_compat.py +87 -0
- package/kits/python/tests/test_http.py +94 -0
- package/kits/python/tests/test_lang.py +104 -0
- package/kits/python/tests/test_models.py +110 -0
- package/kits/python/tests/test_multipart.py +37 -0
- package/kits/python/tests/test_pagination.py +70 -0
- package/kits/python/tests/test_sse.py +61 -0
- package/kits/python/tests/test_testing.py +130 -0
- package/kits/python/tests/test_transport.py +124 -0
- package/kits/python/tests/test_wait.py +46 -0
- package/kits/python/tests/test_webhooks.py +57 -0
- package/kits/python/versions.json +33 -0
- package/kits/rust/kit.json +6 -0
- package/kits/rust/src/runtime/client.rs +212 -0
- package/kits/rust/src/runtime/context.rs +182 -0
- package/kits/rust/src/runtime/crypto.rs +84 -0
- package/kits/rust/src/runtime/errors.rs +147 -0
- package/kits/rust/src/runtime/http.rs +248 -0
- package/kits/rust/src/runtime/json.rs +424 -0
- package/kits/rust/src/runtime/lang.rs +296 -0
- package/kits/rust/src/runtime/mod.rs +30 -0
- package/kits/rust/src/runtime/models.rs +266 -0
- package/kits/rust/src/runtime/multipart.rs +81 -0
- package/kits/rust/src/runtime/pagination.rs +117 -0
- package/kits/rust/src/runtime/sse.rs +195 -0
- package/kits/rust/src/runtime/testing.rs +308 -0
- package/kits/rust/src/runtime/transport.rs +324 -0
- package/kits/rust/src/runtime/wait.rs +29 -0
- package/kits/rust/src/runtime/webhooks.rs +142 -0
- package/kits/rust/tests/client.rs +152 -0
- package/kits/rust/tests/http.rs +44 -0
- package/kits/rust/tests/json.rs +82 -0
- package/kits/rust/tests/lang.rs +74 -0
- package/kits/rust/tests/multipart.rs +21 -0
- package/kits/rust/tests/pagination.rs +61 -0
- package/kits/rust/tests/sse.rs +77 -0
- package/kits/rust/tests/testing.rs +90 -0
- package/kits/rust/tests/transport.rs +218 -0
- package/kits/rust/tests/wait.rs +32 -0
- package/kits/rust/tests/webhooks.rs +73 -0
- package/kits/rust/versions.json +6 -0
- package/kits/typescript/kit.json +6 -0
- package/kits/typescript/src/client.ts +223 -0
- package/kits/typescript/src/context.ts +108 -0
- package/kits/typescript/src/errors.ts +63 -0
- package/kits/typescript/src/helpers.ts +49 -0
- package/kits/typescript/src/http.ts +192 -0
- package/kits/typescript/src/models.ts +139 -0
- package/kits/typescript/src/multipart.ts +66 -0
- package/kits/typescript/src/pagination.ts +54 -0
- package/kits/typescript/src/pipeline.ts +25 -0
- package/kits/typescript/src/sse.ts +102 -0
- package/kits/typescript/src/testing.ts +217 -0
- package/kits/typescript/src/transport.ts +101 -0
- package/kits/typescript/src/wait.ts +36 -0
- package/kits/typescript/src/webhooks.ts +168 -0
- package/kits/typescript/tests/client.test.ts +269 -0
- package/kits/typescript/tests/helpers.test.ts +27 -0
- package/kits/typescript/tests/http.test.ts +80 -0
- package/kits/typescript/tests/models.test.ts +71 -0
- package/kits/typescript/tests/multipart.test.ts +28 -0
- package/kits/typescript/tests/pagination.test.ts +76 -0
- package/kits/typescript/tests/sse.test.ts +82 -0
- package/kits/typescript/tests/testing.test.ts +103 -0
- package/kits/typescript/tests/transport.test.ts +127 -0
- package/kits/typescript/tests/wait.test.ts +31 -0
- package/kits/typescript/tests/webhooks.test.ts +53 -0
- package/kits/typescript/versions.json +15 -0
- package/package.json +37 -4
- package/packages/cli/recipes/apiKeyHeader.ts +16 -0
- package/packages/cli/recipes/apiVersionHeader.ts +16 -0
- package/packages/cli/recipes/basic.ts +13 -0
- package/packages/cli/recipes/bearer.ts +10 -0
- package/packages/cli/recipes/hmacSign.ts +23 -0
- package/packages/cli/recipes/idempotencyKey.ts +19 -0
- package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
- package/packages/cli/recipes/retry.ts +42 -0
- package/packages/cli/recipes/timing.ts +12 -0
- package/packages/cli/recipes/userAgent.ts +8 -0
- package/packages/cli/src/build.js +93 -0
- package/packages/cli/src/commands/build.js +31 -0
- package/packages/cli/src/commands/conformance-check.js +50 -0
- package/packages/cli/src/commands/contract.js +181 -0
- package/packages/cli/src/commands/diff.js +113 -0
- package/packages/cli/src/commands/doctor.js +43 -0
- package/packages/cli/src/commands/import-openapi.js +42 -0
- package/packages/cli/src/commands/init.js +27 -0
- package/packages/cli/src/commands/kit-check.js +145 -0
- package/packages/cli/src/commands/publish.js +81 -0
- package/packages/cli/src/commands/recipe.js +49 -0
- package/packages/cli/src/commands/record.js +181 -0
- package/packages/cli/src/commands/serve.js +156 -0
- package/packages/cli/src/commands/spec-check.js +57 -0
- package/packages/cli/src/commands/test.js +177 -0
- package/packages/cli/src/commands/variants.js +32 -0
- package/packages/cli/src/commands/verify.js +57 -0
- package/packages/cli/src/commands/version.js +63 -0
- package/packages/cli/src/containers.js +82 -0
- package/packages/cli/src/coverage.js +72 -0
- package/packages/cli/src/git.js +25 -0
- package/packages/cli/src/io.js +50 -0
- package/packages/cli/src/java/ScenarioHarness.java +297 -0
- package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
- package/packages/cli/src/java.js +70 -0
- package/packages/cli/src/js/harness.mjs +120 -0
- package/packages/cli/src/kits.js +103 -0
- package/packages/cli/src/languages/csharp.js +274 -0
- package/packages/cli/src/languages/go.js +185 -0
- package/packages/cli/src/languages/index.js +18 -0
- package/packages/cli/src/languages/java.js +187 -0
- package/packages/cli/src/languages/python.js +120 -0
- package/packages/cli/src/languages/rust.js +189 -0
- package/packages/cli/src/languages/types.js +93 -0
- package/packages/cli/src/languages/typescript.js +112 -0
- package/packages/cli/src/main.js +13 -0
- package/packages/cli/src/packaging.js +39 -0
- package/packages/cli/src/portability.js +45 -0
- package/packages/cli/src/profiles.js +55 -0
- package/packages/cli/src/project.js +56 -0
- package/packages/cli/src/python/scenario_harness.py +162 -0
- package/packages/cli/src/python/schema_roundtrip.py +42 -0
- package/packages/cli/src/registries.js +214 -0
- package/packages/cli/src/resolve-core.js +39 -0
- package/packages/cli/src/run.js +133 -0
- package/packages/cli/src/runtimes.js +61 -0
- package/packages/cli/src/scenarios-java.js +82 -0
- package/packages/cli/src/scenarios-javascript.js +54 -0
- package/packages/cli/src/scenarios-python.js +90 -0
- package/packages/cli/src/scenarios.js +183 -0
- package/packages/cli/src/schema.js +84 -0
- package/packages/cli/src/templates.js +85 -0
- package/packages/cli/src/toolchains.js +68 -0
- package/packages/cli/src/transpiled-tests.js +92 -0
- package/packages/codegen/src/canonical-json.js +31 -0
- package/packages/codegen/src/capabilities.js +50 -0
- package/packages/codegen/src/code-writer.js +54 -0
- package/packages/codegen/src/file-tree.js +25 -0
- package/packages/codegen/src/hash.js +6 -0
- package/packages/codegen/src/index.js +16 -0
- package/packages/codegen/src/lock.js +106 -0
- package/packages/conformance/src/case.js +143 -0
- package/packages/conformance/src/checks.js +40 -0
- package/packages/conformance/src/index.js +3 -0
- package/packages/conformance/src/matrix.js +48 -0
- package/packages/conformance/src/suite.js +48 -0
- package/packages/differ/src/diff.js +500 -0
- package/packages/differ/src/index.js +3 -0
- package/packages/differ/src/notes.js +112 -0
- package/packages/differ/src/support.js +34 -0
- package/packages/gen-csharp/src/emit-client.js +397 -0
- package/packages/gen-csharp/src/emit-harness.js +333 -0
- package/packages/gen-csharp/src/emit-mock.js +111 -0
- package/packages/gen-csharp/src/emit-models.js +119 -0
- package/packages/gen-csharp/src/emit-webhooks.js +57 -0
- package/packages/gen-csharp/src/generate.js +245 -0
- package/packages/gen-csharp/src/index.js +5 -0
- package/packages/gen-csharp/src/names.js +284 -0
- package/packages/gen-go/src/emit-client.js +405 -0
- package/packages/gen-go/src/emit-harness.js +294 -0
- package/packages/gen-go/src/emit-mock.js +112 -0
- package/packages/gen-go/src/emit-models.js +146 -0
- package/packages/gen-go/src/emit-webhooks.js +58 -0
- package/packages/gen-go/src/generate.js +205 -0
- package/packages/gen-go/src/index.js +5 -0
- package/packages/gen-go/src/names.js +197 -0
- package/packages/gen-java/src/emit-client.js +441 -0
- package/packages/gen-java/src/emit-models.js +342 -0
- package/packages/gen-java/src/emit-testing.js +123 -0
- package/packages/gen-java/src/emit-webhooks.js +70 -0
- package/packages/gen-java/src/generate.js +244 -0
- package/packages/gen-java/src/index.js +4 -0
- package/packages/gen-java/src/names.js +197 -0
- package/packages/gen-python/src/emit-client.js +272 -0
- package/packages/gen-python/src/emit-models.js +139 -0
- package/packages/gen-python/src/emit-testing.js +93 -0
- package/packages/gen-python/src/emit-webhooks.js +48 -0
- package/packages/gen-python/src/generate.js +237 -0
- package/packages/gen-python/src/index.js +2 -0
- package/packages/gen-python/src/names.js +161 -0
- package/packages/gen-python/src/style.js +67 -0
- package/packages/gen-rust/src/emit-client.js +379 -0
- package/packages/gen-rust/src/emit-harness.js +278 -0
- package/packages/gen-rust/src/emit-mock.js +121 -0
- package/packages/gen-rust/src/emit-models.js +117 -0
- package/packages/gen-rust/src/emit-webhooks.js +58 -0
- package/packages/gen-rust/src/generate.js +243 -0
- package/packages/gen-rust/src/index.js +5 -0
- package/packages/gen-rust/src/names.js +223 -0
- package/packages/gen-typescript/src/emit-client.js +434 -0
- package/packages/gen-typescript/src/emit-models.js +93 -0
- package/packages/gen-typescript/src/emit-package.js +90 -0
- package/packages/gen-typescript/src/emit-testing.js +138 -0
- package/packages/gen-typescript/src/emit-webhooks.js +52 -0
- package/packages/gen-typescript/src/generate.js +70 -0
- package/packages/gen-typescript/src/index.js +3 -0
- package/packages/gen-typescript/src/naming.js +64 -0
- package/packages/gen-typescript/src/runtime.js +81 -0
- package/packages/gen-typescript/src/types.js +136 -0
- package/packages/ir/src/api-surface.js +22 -0
- package/packages/ir/src/checks.js +52 -0
- package/packages/ir/src/index.js +32 -0
- package/packages/ir/src/ir.js +214 -0
- package/packages/ir/src/pages.js +18 -0
- package/packages/ir/src/validate-ir.js +462 -0
- package/packages/ir/src/validate-types.js +140 -0
- package/packages/kit/src/block-parser.js +196 -0
- package/packages/kit/src/index.js +15 -0
- package/packages/kit/src/kit-check.js +203 -0
- package/packages/kit/src/sources.js +53 -0
- package/packages/kit/src/version-policy.js +133 -0
- package/packages/kit/src/version-range.js +132 -0
- package/packages/openapi/src/import.js +292 -0
- package/packages/openapi/src/index.js +1 -0
- package/packages/openapi/src/names.js +50 -0
- package/packages/openapi/src/schemas.js +264 -0
- package/packages/packager/src/archive.js +106 -0
- package/packages/packager/src/crate.js +39 -0
- package/packages/packager/src/gomodule.js +11 -0
- package/packages/packager/src/index.js +9 -0
- package/packages/packager/src/maven.js +85 -0
- package/packages/packager/src/metadata.js +34 -0
- package/packages/packager/src/npm.js +71 -0
- package/packages/packager/src/nuget.js +78 -0
- package/packages/packager/src/sbom.js +56 -0
- package/packages/packager/src/wheel.js +62 -0
- package/packages/spec/src/index.js +11 -0
- package/packages/spec/src/requirements.js +139 -0
- package/packages/transpile/src/ast.js +51 -0
- package/packages/transpile/src/check.js +669 -0
- package/packages/transpile/src/emit-csharp.js +469 -0
- package/packages/transpile/src/emit-go.js +529 -0
- package/packages/transpile/src/emit-java.js +360 -0
- package/packages/transpile/src/emit-python.js +320 -0
- package/packages/transpile/src/emit-rust.js +499 -0
- package/packages/transpile/src/errors.js +15 -0
- package/packages/transpile/src/index.js +13 -0
- package/packages/transpile/src/lexer.js +161 -0
- package/packages/transpile/src/parser.js +543 -0
- package/packages/transpile/src/tests/ast.js +46 -0
- package/packages/transpile/src/tests/parse.js +376 -0
- package/packages/transpile/src/tests/translate.js +849 -0
- package/packages/transpile/src/transpile.js +57 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// sdkloom Java kit: a result together with its HTTP response.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
// @block client.raw_result
|
|
5
|
+
// @summary The decoded result of a call together with the HTTP response it came from.
|
|
6
|
+
// @versions >=8
|
|
7
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-15.5
|
|
8
|
+
// @quote [1] "The 4xx (Client Error) class of status code indicates that the client seems to have erred."
|
|
9
|
+
// @source [2] https://www.rfc-editor.org/rfc/rfc9110#section-15.6
|
|
10
|
+
// @quote [2] "The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method."
|
|
11
|
+
// @retrieved 2026-12-05
|
|
12
|
+
// @tests tests/dev/sdkloom/runtime/ClientTest.java
|
|
13
|
+
// @requirement SDKL-ERR-001
|
|
14
|
+
public final class RawResult<T> {
|
|
15
|
+
private final T data;
|
|
16
|
+
private final HttpResponse response;
|
|
17
|
+
|
|
18
|
+
public RawResult(T data, HttpResponse response) {
|
|
19
|
+
this.data = data;
|
|
20
|
+
this.response = response;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public T data() {
|
|
24
|
+
return data;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public HttpResponse response() {
|
|
28
|
+
return response;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// @end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// sdkloom Java kit: an event recorded by a mock client.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
import java.util.Map;
|
|
5
|
+
|
|
6
|
+
// @block testing.recorded_event
|
|
7
|
+
// @summary An event a mock client recorded: its name and fields.
|
|
8
|
+
// @versions >=8
|
|
9
|
+
// @source [1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ConcurrentHashMap.html
|
|
10
|
+
// @quote [1] "A hash table supporting full concurrency of retrievals and high expected concurrency for updates."
|
|
11
|
+
// @retrieved 2026-12-05
|
|
12
|
+
// @tests tests/dev/sdkloom/runtime/TestingTest.java
|
|
13
|
+
// @requirement SDKL-MOCK-002
|
|
14
|
+
public final class RecordedEvent {
|
|
15
|
+
private final String name;
|
|
16
|
+
private final Map<String, Object> fields;
|
|
17
|
+
|
|
18
|
+
public RecordedEvent(String name, Map<String, Object> fields) {
|
|
19
|
+
this.name = name;
|
|
20
|
+
this.fields = fields;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public String name() {
|
|
24
|
+
return name;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public Map<String, Object> fields() {
|
|
28
|
+
return fields;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@Override
|
|
32
|
+
public String toString() {
|
|
33
|
+
return name + " " + fields;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// @end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// sdkloom Java kit: which generated SDK a middleware runs in.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
// @block context.sdk
|
|
5
|
+
// @summary The name, version and language of the generated SDK, shown to middleware as ctx.sdk(), e.g. for a User-Agent such as "acme-java/1.2.0".
|
|
6
|
+
// @versions >=8
|
|
7
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-10.1.5
|
|
8
|
+
// @quote [1] "The "User-Agent" header field contains information about the user agent originating the request, which is often used by servers to help identify the scope of reported interoperability problems, to work around or tailor responses to avoid particular user agent limitations, and for analytics regarding browser or operating system use."
|
|
9
|
+
// @retrieved 2026-12-05
|
|
10
|
+
// @tests tests/dev/sdkloom/runtime/ClientTest.java
|
|
11
|
+
// @requirement SDKL-PIPE-002
|
|
12
|
+
public final class SdkInfo {
|
|
13
|
+
public static final SdkInfo UNKNOWN = new SdkInfo("", "", "java");
|
|
14
|
+
|
|
15
|
+
private final String name;
|
|
16
|
+
private final String version;
|
|
17
|
+
private final String language;
|
|
18
|
+
|
|
19
|
+
public SdkInfo(String name, String version, String language) {
|
|
20
|
+
this.name = name;
|
|
21
|
+
this.version = version;
|
|
22
|
+
this.language = language;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public String name() {
|
|
26
|
+
return name;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public String version() {
|
|
30
|
+
return version;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** "java" for the generated Java SDK. */
|
|
34
|
+
public String language() {
|
|
35
|
+
return language;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// @end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// sdkloom Java kit: per-request transport options.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
// @block transport.options
|
|
5
|
+
// @summary Per-request transport options: the timeout in milliseconds, or null for none, and whether to hand a successful body over as it arrives (event streams).
|
|
6
|
+
// @versions >=8
|
|
7
|
+
// @source [1] https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html
|
|
8
|
+
// @quote [1] "An HttpClient can be used to send requests and retrieve their responses."
|
|
9
|
+
// @retrieved 2026-12-05
|
|
10
|
+
// @tests tests/dev/sdkloom/runtime/JdkTransportTest.java
|
|
11
|
+
// @requirement SDKL-HTTP-004
|
|
12
|
+
public final class SendOptions {
|
|
13
|
+
private final Long timeoutMs;
|
|
14
|
+
private final boolean stream;
|
|
15
|
+
|
|
16
|
+
public SendOptions(Long timeoutMs) {
|
|
17
|
+
this(timeoutMs, false);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** For a stream the timeout bounds the wait for the headers (and, with URLConnection, each read). */
|
|
21
|
+
public SendOptions(Long timeoutMs, boolean stream) {
|
|
22
|
+
this.timeoutMs = timeoutMs;
|
|
23
|
+
this.stream = stream;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public Long timeoutMs() {
|
|
27
|
+
return timeoutMs;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public boolean stream() {
|
|
31
|
+
return stream;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// @end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// sdkloom Java kit: one server-sent event.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
import java.util.Objects;
|
|
5
|
+
|
|
6
|
+
// @block sse.event
|
|
7
|
+
// @summary One server-sent event: its type ("message" unless the server named one), the last event ID and its data.
|
|
8
|
+
// @versions >=8
|
|
9
|
+
// @source [1] https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
10
|
+
// @quote [1] "Set the event type buffer to the field value."
|
|
11
|
+
// @retrieved 2026-12-06
|
|
12
|
+
// @tests tests/dev/sdkloom/runtime/EventStreamTest.java
|
|
13
|
+
// @requirement SDKL-SSE-001
|
|
14
|
+
public final class ServerSentEvent<T> {
|
|
15
|
+
private final String event;
|
|
16
|
+
private final String id;
|
|
17
|
+
private final T data;
|
|
18
|
+
|
|
19
|
+
public ServerSentEvent(String event, String id, T data) {
|
|
20
|
+
this.event = Objects.requireNonNull(event, "event");
|
|
21
|
+
this.id = Objects.requireNonNull(id, "id");
|
|
22
|
+
this.data = data;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The event type: "message" unless the server named one. */
|
|
26
|
+
public String event() {
|
|
27
|
+
return event;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The last event ID the server sent, or "" before it sent one. */
|
|
31
|
+
public String id() {
|
|
32
|
+
return id;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public T data() {
|
|
36
|
+
return data;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@Override
|
|
40
|
+
public boolean equals(Object other) {
|
|
41
|
+
if (!(other instanceof ServerSentEvent)) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
ServerSentEvent<?> that = (ServerSentEvent<?>) other;
|
|
45
|
+
return event.equals(that.event) && id.equals(that.id) && Objects.equals(data, that.data);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@Override
|
|
49
|
+
public int hashCode() {
|
|
50
|
+
return Objects.hash(event, id, data);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@Override
|
|
54
|
+
public String toString() {
|
|
55
|
+
return "ServerSentEvent{event=" + event + ", id=" + id + ", data=" + data + "}";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// @end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// sdkloom Java kit: per-client storage for middleware.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
import java.util.concurrent.ConcurrentHashMap;
|
|
5
|
+
|
|
6
|
+
// @block context.state
|
|
7
|
+
// @summary Per-client storage shared by all calls, e.g. for a cached token, with optional expiry. Thread-safe.
|
|
8
|
+
// @versions >=8
|
|
9
|
+
// @source [1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ConcurrentHashMap.html
|
|
10
|
+
// @quote [1] "A hash table supporting full concurrency of retrievals and high expected concurrency for updates."
|
|
11
|
+
// @source [2] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#nanoTime()
|
|
12
|
+
// @quote [2] "This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time."
|
|
13
|
+
// @retrieved 2026-12-05
|
|
14
|
+
// @tests tests/dev/sdkloom/runtime/ClientTest.java
|
|
15
|
+
// @requirement SDKL-PIPE-002
|
|
16
|
+
public final class State {
|
|
17
|
+
private static final class Entry {
|
|
18
|
+
final Object value;
|
|
19
|
+
final Long expiresAt;
|
|
20
|
+
|
|
21
|
+
Entry(Object value, Long expiresAt) {
|
|
22
|
+
this.value = value;
|
|
23
|
+
this.expiresAt = expiresAt;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private final Clock clock;
|
|
28
|
+
private final ConcurrentHashMap<String, Entry> entries = new ConcurrentHashMap<>();
|
|
29
|
+
|
|
30
|
+
public State(Clock clock) {
|
|
31
|
+
this.clock = clock;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public Object get(String key) {
|
|
35
|
+
Entry entry = entries.get(key);
|
|
36
|
+
if (entry == null) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
if (entry.expiresAt != null && clock.now() >= entry.expiresAt) {
|
|
40
|
+
entries.remove(key, entry);
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return entry.value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public void set(String key, Object value) {
|
|
47
|
+
entries.put(key, new Entry(value, null));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public void set(String key, Object value, long ttlMs) {
|
|
51
|
+
entries.put(key, new Entry(value, clock.now() + ttlMs));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public void delete(String key) {
|
|
55
|
+
entries.remove(key);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// @end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// sdkloom Java kit: marks a test method for TestRunner.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
import java.lang.annotation.ElementType;
|
|
5
|
+
import java.lang.annotation.Retention;
|
|
6
|
+
import java.lang.annotation.RetentionPolicy;
|
|
7
|
+
import java.lang.annotation.Target;
|
|
8
|
+
|
|
9
|
+
// @block testing.test_annotation
|
|
10
|
+
// @summary Marks a public no-argument method as a test for TestRunner, so SDK tests need no test library.
|
|
11
|
+
// @versions >=8
|
|
12
|
+
// @source [1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Method.html#invoke(java.lang.Object,java.lang.Object...)
|
|
13
|
+
// @quote [1] "Invokes the underlying method represented by this Method object, on the specified object with the specified parameters."
|
|
14
|
+
// @retrieved 2026-12-05
|
|
15
|
+
// @tests tests/dev/sdkloom/runtime/TestingTest.java
|
|
16
|
+
// @requirement SDKL-MOCK-002
|
|
17
|
+
@Retention(RetentionPolicy.RUNTIME)
|
|
18
|
+
@Target(ElementType.METHOD)
|
|
19
|
+
public @interface Test {
|
|
20
|
+
}
|
|
21
|
+
// @end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// sdkloom Java kit: runs @Test methods without a test library.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
import java.io.PrintStream;
|
|
5
|
+
import java.lang.reflect.InvocationTargetException;
|
|
6
|
+
import java.lang.reflect.Method;
|
|
7
|
+
import java.lang.reflect.Modifier;
|
|
8
|
+
import java.util.ArrayList;
|
|
9
|
+
import java.util.Arrays;
|
|
10
|
+
import java.util.Comparator;
|
|
11
|
+
import java.util.List;
|
|
12
|
+
|
|
13
|
+
// @block testing.runner
|
|
14
|
+
// @summary Runs every public @Test method of the given classes on a fresh instance, prints one line per test and a summary, and exits non-zero on failure.
|
|
15
|
+
// @versions >=8
|
|
16
|
+
// @source [1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Method.html#invoke(java.lang.Object,java.lang.Object...)
|
|
17
|
+
// @quote [1] "Invokes the underlying method represented by this Method object, on the specified object with the specified parameters."
|
|
18
|
+
// @retrieved 2026-12-05
|
|
19
|
+
// @tests tests/dev/sdkloom/runtime/TestingTest.java
|
|
20
|
+
// @requirement SDKL-MOCK-002
|
|
21
|
+
public final class TestRunner {
|
|
22
|
+
private TestRunner() {
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Usage: java dev.sdkloom.runtime.TestRunner com.acme.FirstTest com.acme.SecondTest */
|
|
26
|
+
public static void main(String[] args) throws ClassNotFoundException {
|
|
27
|
+
List<Class<?>> classes = new ArrayList<>();
|
|
28
|
+
for (String name : args) {
|
|
29
|
+
classes.add(Class.forName(name));
|
|
30
|
+
}
|
|
31
|
+
int failures = run(classes, System.out);
|
|
32
|
+
System.exit(failures == 0 ? 0 : 1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Runs the tests and returns the number of failures. */
|
|
36
|
+
public static int run(List<Class<?>> classes, PrintStream out) {
|
|
37
|
+
int total = 0;
|
|
38
|
+
int failures = 0;
|
|
39
|
+
for (Class<?> type : classes) {
|
|
40
|
+
List<Method> methods = new ArrayList<>(Arrays.asList(type.getMethods()));
|
|
41
|
+
methods.removeIf((method) -> !method.isAnnotationPresent(Test.class) || Modifier.isStatic(method.getModifiers()) || method.getParameterCount() != 0);
|
|
42
|
+
methods.sort(Comparator.comparing(Method::getName));
|
|
43
|
+
for (Method method : methods) {
|
|
44
|
+
total++;
|
|
45
|
+
String label = type.getSimpleName() + "." + method.getName();
|
|
46
|
+
try {
|
|
47
|
+
method.invoke(type.getDeclaredConstructor().newInstance());
|
|
48
|
+
out.println("ok " + label);
|
|
49
|
+
} catch (InvocationTargetException error) {
|
|
50
|
+
failures++;
|
|
51
|
+
out.println("FAIL " + label + ": " + error.getCause());
|
|
52
|
+
} catch (ReflectiveOperationException error) {
|
|
53
|
+
failures++;
|
|
54
|
+
out.println("FAIL " + label + ": cannot run (" + error + ")");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
out.println("Ran " + total + " test" + (total == 1 ? "" : "s"));
|
|
59
|
+
out.println(failures == 0 ? "OK" : "FAILED (failures=" + failures + ")");
|
|
60
|
+
return failures;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// @end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// sdkloom Java kit: what sends a request.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
// @block transport.interface
|
|
5
|
+
// @summary Sends one request and returns the response; non-2xx responses are returned, a missing response is a TransportException.
|
|
6
|
+
// @versions >=8
|
|
7
|
+
// @source [1] https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html
|
|
8
|
+
// @quote [1] "An HttpClient can be used to send requests and retrieve their responses."
|
|
9
|
+
// @retrieved 2026-12-05
|
|
10
|
+
// @tests tests/dev/sdkloom/runtime/JdkTransportTest.java
|
|
11
|
+
// @requirement SDKL-HTTP-004
|
|
12
|
+
@FunctionalInterface
|
|
13
|
+
public interface Transport {
|
|
14
|
+
HttpResponse send(HttpRequest request, SendOptions options);
|
|
15
|
+
}
|
|
16
|
+
// @end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// sdkloom Java kit: the error for a request that got no response.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
// @block errors.transport_error
|
|
5
|
+
// @summary The error for a request that got no HTTP response: a network failure or a timeout.
|
|
6
|
+
// @versions >=8
|
|
7
|
+
// @source [1] https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpTimeoutException.html
|
|
8
|
+
// @quote [1] "Thrown when a response is not received within a specified time period."
|
|
9
|
+
// @retrieved 2026-12-05
|
|
10
|
+
// @tests tests/dev/sdkloom/runtime/JdkTransportTest.java
|
|
11
|
+
// @requirement SDKL-HTTP-004
|
|
12
|
+
public final class TransportException extends RuntimeException {
|
|
13
|
+
private static final long serialVersionUID = 1L;
|
|
14
|
+
private final String reason;
|
|
15
|
+
|
|
16
|
+
public TransportException(String reason, Throwable cause) {
|
|
17
|
+
super("request failed: " + reason, cause);
|
|
18
|
+
this.reason = reason;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** "timeout" or "network". */
|
|
22
|
+
public String reason() {
|
|
23
|
+
return reason;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public String name() {
|
|
27
|
+
return "TransportError";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// @end
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// sdkloom Java kit: the default transport of the Java 8 variant, built on java.net.HttpURLConnection.
|
|
2
|
+
package dev.sdkloom.runtime;
|
|
3
|
+
|
|
4
|
+
import java.io.ByteArrayOutputStream;
|
|
5
|
+
import java.io.IOException;
|
|
6
|
+
import java.io.InputStream;
|
|
7
|
+
import java.io.OutputStream;
|
|
8
|
+
import java.lang.reflect.Field;
|
|
9
|
+
import java.net.HttpURLConnection;
|
|
10
|
+
import java.net.ProtocolException;
|
|
11
|
+
import java.net.Proxy;
|
|
12
|
+
import java.net.ProxySelector;
|
|
13
|
+
import java.net.SocketTimeoutException;
|
|
14
|
+
import java.net.URI;
|
|
15
|
+
import java.net.URL;
|
|
16
|
+
import java.util.AbstractMap;
|
|
17
|
+
import java.util.ArrayList;
|
|
18
|
+
import java.util.Arrays;
|
|
19
|
+
import java.util.HashSet;
|
|
20
|
+
import java.util.List;
|
|
21
|
+
import java.util.Locale;
|
|
22
|
+
import java.util.Map;
|
|
23
|
+
import java.util.Set;
|
|
24
|
+
import javax.net.ssl.HttpsURLConnection;
|
|
25
|
+
import javax.net.ssl.SSLContext;
|
|
26
|
+
|
|
27
|
+
// @block transport.urlconnection
|
|
28
|
+
// @summary The Java 8 transport, built on HttpURLConnection with the same contract as transport.jdk: timeouts, an optional proxy (the system proxy settings otherwise) and TLS context, redirects not followed, non-2xx responses returned with their body, Content-Length: 0 for an empty POST, PUT or PATCH. Differences that remain: PATCH is set through reflection where the JVM allows it; a GET or HEAD body is refused; HttpURLConnection drops some headers (Origin, Via, Keep-Alive, Transfer-Encoding) unless the application sets sun.net.http.allowRestrictedHeaders; the timeout applies to connecting and to each read rather than to the whole response.
|
|
29
|
+
// @versions >=8
|
|
30
|
+
// @source [1] https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html
|
|
31
|
+
// @quote [1] "Returns the error stream if the connection failed but the server sent useful data nonetheless."
|
|
32
|
+
// @quote [1] "GET POST HEAD OPTIONS PUT DELETE TRACE are legal, subject to protocol restrictions."
|
|
33
|
+
// @source [2] https://docs.oracle.com/javase/8/docs/api/java/net/URLConnection.html
|
|
34
|
+
// @quote [2] "If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised."
|
|
35
|
+
// @retrieved 2026-12-05
|
|
36
|
+
// @tests tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java
|
|
37
|
+
// @requirement SDKL-HTTP-004
|
|
38
|
+
public final class UrlConnectionTransport implements Transport {
|
|
39
|
+
/** Headers the connection sets itself; the same set transport.jdk leaves out. */
|
|
40
|
+
private static final Set<String> RESTRICTED = new HashSet<>(Arrays.asList("connection", "content-length", "expect", "host", "upgrade"));
|
|
41
|
+
private static final int CHUNK = 8192;
|
|
42
|
+
private static final Set<String> BODY_METHODS = new HashSet<>(Arrays.asList("POST", "PUT", "PATCH"));
|
|
43
|
+
|
|
44
|
+
private final ProxySelector proxy;
|
|
45
|
+
private final SSLContext sslContext;
|
|
46
|
+
|
|
47
|
+
public UrlConnectionTransport(ProxySelector proxy, SSLContext sslContext) {
|
|
48
|
+
this.proxy = proxy;
|
|
49
|
+
this.sslContext = sslContext;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public UrlConnectionTransport() {
|
|
53
|
+
this(null, null);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
@Override
|
|
57
|
+
public HttpResponse send(HttpRequest request, SendOptions options) {
|
|
58
|
+
HttpURLConnection connection = null;
|
|
59
|
+
try {
|
|
60
|
+
connection = open(URI.create(request.url()));
|
|
61
|
+
// Bodies are read to the end and closed, so the connection goes back to the keep-alive pool.
|
|
62
|
+
return exchange(connection, request, options);
|
|
63
|
+
} catch (SocketTimeoutException error) {
|
|
64
|
+
disconnect(connection);
|
|
65
|
+
throw new TransportException("timeout", error);
|
|
66
|
+
} catch (IOException error) {
|
|
67
|
+
disconnect(connection);
|
|
68
|
+
throw new TransportException("network", error);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private static void disconnect(HttpURLConnection connection) {
|
|
73
|
+
if (connection != null) {
|
|
74
|
+
connection.disconnect();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private HttpURLConnection open(URI uri) throws IOException {
|
|
79
|
+
URL url = uri.toURL();
|
|
80
|
+
// Without a selector, like HttpClient: the default one, which follows http.proxyHost and friends.
|
|
81
|
+
HttpURLConnection connection = (HttpURLConnection) (proxy == null ? url.openConnection() : url.openConnection(first(proxy.select(uri))));
|
|
82
|
+
if (sslContext != null && connection instanceof HttpsURLConnection) {
|
|
83
|
+
((HttpsURLConnection) connection).setSSLSocketFactory(sslContext.getSocketFactory());
|
|
84
|
+
}
|
|
85
|
+
connection.setInstanceFollowRedirects(false);
|
|
86
|
+
connection.setUseCaches(false);
|
|
87
|
+
return connection;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private static Proxy first(List<Proxy> proxies) {
|
|
91
|
+
return proxies == null || proxies.isEmpty() ? Proxy.NO_PROXY : proxies.get(0);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private static HttpResponse exchange(HttpURLConnection connection, HttpRequest request, SendOptions options) throws IOException {
|
|
95
|
+
setMethod(connection, request.method());
|
|
96
|
+
for (Map.Entry<String, String> header : request.headers()) {
|
|
97
|
+
if (!RESTRICTED.contains(header.getKey().toLowerCase(Locale.ROOT))) {
|
|
98
|
+
connection.addRequestProperty(header.getKey(), header.getValue());
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (options != null && options.timeoutMs() != null) {
|
|
102
|
+
int timeout = (int) Math.max(1, Math.min(Integer.MAX_VALUE, options.timeoutMs()));
|
|
103
|
+
connection.setConnectTimeout(timeout);
|
|
104
|
+
connection.setReadTimeout(timeout);
|
|
105
|
+
}
|
|
106
|
+
byte[] body = request.body();
|
|
107
|
+
boolean bodyless = "GET".equals(request.method()) || "HEAD".equals(request.method());
|
|
108
|
+
if (body != null && bodyless) {
|
|
109
|
+
// HttpURLConnection would quietly turn the request into a POST.
|
|
110
|
+
throw new ProtocolException("HttpURLConnection cannot send a body with " + request.method() + "; use the Java 11+ package");
|
|
111
|
+
}
|
|
112
|
+
if (body == null && BODY_METHODS.contains(request.method())) {
|
|
113
|
+
// java.net.http sends Content-Length: 0 here; some servers answer 411 without it.
|
|
114
|
+
body = new byte[0];
|
|
115
|
+
}
|
|
116
|
+
if (body != null) {
|
|
117
|
+
connection.setDoOutput(true);
|
|
118
|
+
connection.setFixedLengthStreamingMode(body.length);
|
|
119
|
+
try (OutputStream out = connection.getOutputStream()) {
|
|
120
|
+
out.write(body);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
int status = connection.getResponseCode();
|
|
124
|
+
if (options != null && options.stream() && status >= 200 && status <= 299) {
|
|
125
|
+
// Handed over unread; the read timeout still bounds each wait for more of the body.
|
|
126
|
+
return new HttpResponse(status, headers(connection), new byte[0], connection.getInputStream());
|
|
127
|
+
}
|
|
128
|
+
return new HttpResponse(status, headers(connection), readBody(connection, status));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** HttpURLConnection refuses PATCH; on JVMs that still allow it, the method field is set directly. */
|
|
132
|
+
private static void setMethod(HttpURLConnection connection, String method) throws ProtocolException {
|
|
133
|
+
try {
|
|
134
|
+
connection.setRequestMethod(method);
|
|
135
|
+
} catch (ProtocolException refused) {
|
|
136
|
+
try {
|
|
137
|
+
Field field = HttpURLConnection.class.getDeclaredField("method");
|
|
138
|
+
field.setAccessible(true);
|
|
139
|
+
field.set(connection, method);
|
|
140
|
+
Object inner = delegate(connection);
|
|
141
|
+
if (inner instanceof HttpURLConnection) {
|
|
142
|
+
field.set(inner, method);
|
|
143
|
+
}
|
|
144
|
+
} catch (ReflectiveOperationException | RuntimeException blocked) {
|
|
145
|
+
ProtocolException error = new ProtocolException(method + " is not supported by HttpURLConnection on this JVM; use the Java 11+ package");
|
|
146
|
+
error.initCause(blocked);
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** HTTPS connections wrap the HttpURLConnection that actually sends the request. */
|
|
153
|
+
private static Object delegate(HttpURLConnection connection) throws IllegalAccessException {
|
|
154
|
+
for (Class<?> type = connection.getClass(); type != null; type = type.getSuperclass()) {
|
|
155
|
+
try {
|
|
156
|
+
Field field = type.getDeclaredField("delegate");
|
|
157
|
+
field.setAccessible(true);
|
|
158
|
+
return field.get(connection);
|
|
159
|
+
} catch (NoSuchFieldException absent) {
|
|
160
|
+
// Not declared here; look in the superclass.
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private static List<Map.Entry<String, String>> headers(HttpURLConnection connection) {
|
|
167
|
+
List<Map.Entry<String, String>> headers = new ArrayList<>();
|
|
168
|
+
for (Map.Entry<String, List<String>> field : connection.getHeaderFields().entrySet()) {
|
|
169
|
+
// The null key holds the status line.
|
|
170
|
+
if (field.getKey() == null) {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
for (String value : field.getValue()) {
|
|
174
|
+
headers.add(new AbstractMap.SimpleImmutableEntry<>(field.getKey().toLowerCase(Locale.ROOT), value));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return headers;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private static byte[] readBody(HttpURLConnection connection, int status) throws IOException {
|
|
181
|
+
InputStream stream = status >= HttpURLConnection.HTTP_BAD_REQUEST ? connection.getErrorStream() : connection.getInputStream();
|
|
182
|
+
if (stream == null) {
|
|
183
|
+
return new byte[0];
|
|
184
|
+
}
|
|
185
|
+
try (InputStream in = stream) {
|
|
186
|
+
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
187
|
+
byte[] chunk = new byte[CHUNK];
|
|
188
|
+
for (int read = in.read(chunk); read != -1; read = in.read(chunk)) {
|
|
189
|
+
out.write(chunk, 0, read);
|
|
190
|
+
}
|
|
191
|
+
return out.toByteArray();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// @end
|