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,168 @@
|
|
|
1
|
+
// sdkloom Go kit: the transport that sends requests.
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"bytes"
|
|
6
|
+
"context"
|
|
7
|
+
"crypto/tls"
|
|
8
|
+
"errors"
|
|
9
|
+
"io"
|
|
10
|
+
"net"
|
|
11
|
+
"net/http"
|
|
12
|
+
"net/url"
|
|
13
|
+
"sync/atomic"
|
|
14
|
+
"time"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
// @block transport.nethttp
|
|
18
|
+
// @summary The default transport, built on net/http with timeouts, an optional proxy (the environment's otherwise) and an optional TLS configuration. Redirects are not followed; non-2xx responses are returned, not errors. A successful body is handed over as it arrives when the caller asks for a stream.
|
|
19
|
+
// @versions >=1.18
|
|
20
|
+
// @source [1] https://pkg.go.dev/net/http
|
|
21
|
+
// @quote [1] "Clients and Transports are safe for concurrent use by multiple goroutines and for efficiency should only be created once and re-used."
|
|
22
|
+
// @source [2] https://pkg.go.dev/net/http
|
|
23
|
+
// @quote [2] "If returned, the next request is not sent and the most recent response is returned with its body unclosed."
|
|
24
|
+
// @source [3] https://pkg.go.dev/net/http
|
|
25
|
+
// @quote [3] "ProxyFromEnvironment returns the URL of the proxy to use for a given request, as indicated by the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions thereof)."
|
|
26
|
+
// @retrieved 2026-12-05
|
|
27
|
+
// @tests tests/transport_test.go
|
|
28
|
+
// @requirement SDKL-HTTP-004
|
|
29
|
+
|
|
30
|
+
// SendOptions change how one request is sent.
|
|
31
|
+
type SendOptions struct {
|
|
32
|
+
// TimeoutMs bounds the whole exchange; for a stream, only the wait for the response headers.
|
|
33
|
+
TimeoutMs Opt[int64]
|
|
34
|
+
// Stream hands a successful response's body over as it arrives, in Response.Stream (event streams).
|
|
35
|
+
Stream bool
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Transport sends one request and returns its response, whatever the status; it fails only without a response.
|
|
39
|
+
type Transport interface {
|
|
40
|
+
Send(ctx context.Context, request Request, options SendOptions) (Response, error)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// TransportFunc adapts a function to Transport.
|
|
44
|
+
type TransportFunc func(ctx context.Context, request Request, options SendOptions) (Response, error)
|
|
45
|
+
|
|
46
|
+
// Send calls f.
|
|
47
|
+
func (f TransportFunc) Send(ctx context.Context, request Request, options SendOptions) (Response, error) {
|
|
48
|
+
return f(ctx, request, options)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// HTTPTransport sends requests with net/http.
|
|
52
|
+
type HTTPTransport struct {
|
|
53
|
+
client *http.Client
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// NewHTTPTransport returns a transport; proxy "" uses HTTP_PROXY and friends, tlsConfig nil uses Go's defaults.
|
|
57
|
+
func NewHTTPTransport(proxy string, tlsConfig *tls.Config) (*HTTPTransport, error) {
|
|
58
|
+
base := http.DefaultTransport.(*http.Transport).Clone()
|
|
59
|
+
if proxy != "" {
|
|
60
|
+
parsed, err := url.Parse(proxy)
|
|
61
|
+
if err != nil {
|
|
62
|
+
return nil, err
|
|
63
|
+
}
|
|
64
|
+
base.Proxy = http.ProxyURL(parsed)
|
|
65
|
+
}
|
|
66
|
+
if tlsConfig != nil {
|
|
67
|
+
base.TLSClientConfig = tlsConfig
|
|
68
|
+
}
|
|
69
|
+
client := &http.Client{
|
|
70
|
+
Transport: base,
|
|
71
|
+
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
72
|
+
return http.ErrUseLastResponse
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
return &HTTPTransport{client: client}, nil
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Send implements Transport.
|
|
79
|
+
func (t *HTTPTransport) Send(ctx context.Context, request Request, options SendOptions) (Response, error) {
|
|
80
|
+
if ctx == nil {
|
|
81
|
+
ctx = context.Background()
|
|
82
|
+
}
|
|
83
|
+
ctx, cancel := context.WithCancel(ctx)
|
|
84
|
+
// A timer instead of a context deadline: for a stream it stops once the headers are in.
|
|
85
|
+
var timer *time.Timer
|
|
86
|
+
var timedOut int32
|
|
87
|
+
if timeout, ok := options.TimeoutMs.Get(); ok {
|
|
88
|
+
timer = time.AfterFunc(time.Duration(timeout)*time.Millisecond, func() {
|
|
89
|
+
atomic.StoreInt32(&timedOut, 1)
|
|
90
|
+
cancel()
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
streaming := false
|
|
94
|
+
defer func() {
|
|
95
|
+
if !streaming {
|
|
96
|
+
if timer != nil {
|
|
97
|
+
timer.Stop()
|
|
98
|
+
}
|
|
99
|
+
cancel()
|
|
100
|
+
}
|
|
101
|
+
}()
|
|
102
|
+
fail := func(err error) error {
|
|
103
|
+
if atomic.LoadInt32(&timedOut) == 1 {
|
|
104
|
+
return &TransportError{Reason: "timeout", Cause: err}
|
|
105
|
+
}
|
|
106
|
+
return &TransportError{Reason: reason(ctx, err), Cause: err}
|
|
107
|
+
}
|
|
108
|
+
var body io.Reader
|
|
109
|
+
if request.Body != nil {
|
|
110
|
+
body = bytes.NewReader(request.Body)
|
|
111
|
+
}
|
|
112
|
+
raw, err := http.NewRequestWithContext(ctx, request.Method, request.URL, body)
|
|
113
|
+
if err != nil {
|
|
114
|
+
return Response{}, &TransportError{Reason: "network", Cause: err}
|
|
115
|
+
}
|
|
116
|
+
for _, header := range request.Headers {
|
|
117
|
+
raw.Header.Add(header.Name, header.Value)
|
|
118
|
+
}
|
|
119
|
+
response, err := t.client.Do(raw)
|
|
120
|
+
if err != nil {
|
|
121
|
+
return Response{}, fail(err)
|
|
122
|
+
}
|
|
123
|
+
headers := make([]Header, 0, len(response.Header))
|
|
124
|
+
for name, values := range response.Header {
|
|
125
|
+
for _, value := range values {
|
|
126
|
+
headers = append(headers, Header{Name: name, Value: value})
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if options.Stream && response.StatusCode >= 200 && response.StatusCode <= 299 && (timer == nil || timer.Stop()) {
|
|
130
|
+
streaming = true
|
|
131
|
+
return Response{Status: response.StatusCode, Headers: headers, Body: []byte{}, Stream: &streamBody{body: response.Body, cancel: cancel}}, nil
|
|
132
|
+
}
|
|
133
|
+
defer response.Body.Close()
|
|
134
|
+
data, err := io.ReadAll(response.Body)
|
|
135
|
+
if err != nil {
|
|
136
|
+
return Response{}, fail(err)
|
|
137
|
+
}
|
|
138
|
+
return Response{Status: response.StatusCode, Headers: headers, Body: data}, nil
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// streamBody is a streamed response body: read errors become TransportErrors and Close ends the request.
|
|
142
|
+
type streamBody struct {
|
|
143
|
+
body io.ReadCloser
|
|
144
|
+
cancel context.CancelFunc
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
func (s *streamBody) Read(buffer []byte) (int, error) {
|
|
148
|
+
count, err := s.body.Read(buffer)
|
|
149
|
+
if err != nil && err != io.EOF {
|
|
150
|
+
return count, &TransportError{Reason: "network", Cause: err}
|
|
151
|
+
}
|
|
152
|
+
return count, err
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
func (s *streamBody) Close() error {
|
|
156
|
+
s.cancel()
|
|
157
|
+
return s.body.Close()
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
func reason(ctx context.Context, err error) string {
|
|
161
|
+
var netError net.Error
|
|
162
|
+
if errors.Is(ctx.Err(), context.DeadlineExceeded) || (errors.As(err, &netError) && netError.Timeout()) {
|
|
163
|
+
return "timeout"
|
|
164
|
+
}
|
|
165
|
+
return "network"
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// @end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// sdkloom Go kit: waiting for a long-running operation.
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"context"
|
|
6
|
+
"strconv"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
// @block lro.wait
|
|
10
|
+
// @summary Waits for a long-running operation: polls until the result says the work is done, sleeping on the client's clock between polls, stops when the context is done, and returns *WaitTimeoutError when the next poll would come after the deadline.
|
|
11
|
+
// @versions >=1.18
|
|
12
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-15.3.3
|
|
13
|
+
// @quote [1] "The 202 (Accepted) status code indicates that the request has been accepted for processing, but the processing has not been completed."
|
|
14
|
+
// @retrieved 2026-12-06
|
|
15
|
+
// @tests tests/wait_test.go
|
|
16
|
+
// @requirement SDKL-WAIT-001
|
|
17
|
+
|
|
18
|
+
// WaitTimeoutError means the work did not finish within the wait's timeout; Last is the last result read.
|
|
19
|
+
type WaitTimeoutError struct {
|
|
20
|
+
Message string
|
|
21
|
+
Last any
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
func (e *WaitTimeoutError) Error() string {
|
|
25
|
+
return e.Message
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// WaitUntil polls until done(result); the first result comes from starting the work.
|
|
29
|
+
func WaitUntil[R any](ctx context.Context, first R, poll func(R) (R, error), done func(R) bool, clock Clock, intervalMs, timeoutMs int64) (R, error) {
|
|
30
|
+
deadline := clock.Now() + timeoutMs
|
|
31
|
+
current := first
|
|
32
|
+
for !done(current) {
|
|
33
|
+
if clock.Now()+intervalMs > deadline {
|
|
34
|
+
return current, &WaitTimeoutError{Message: "the work did not finish within " + strconv.FormatInt(timeoutMs, 10) + " ms", Last: current}
|
|
35
|
+
}
|
|
36
|
+
if ctx != nil && ctx.Err() != nil {
|
|
37
|
+
return current, ctx.Err()
|
|
38
|
+
}
|
|
39
|
+
clock.Sleep(intervalMs)
|
|
40
|
+
next, err := poll(current)
|
|
41
|
+
if err != nil {
|
|
42
|
+
return current, err
|
|
43
|
+
}
|
|
44
|
+
current = next
|
|
45
|
+
}
|
|
46
|
+
return current, nil
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// @end
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// sdkloom Go kit: verifying the signature of a webhook the API sent.
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"crypto/hmac"
|
|
6
|
+
"crypto/sha256"
|
|
7
|
+
"encoding/base64"
|
|
8
|
+
"encoding/hex"
|
|
9
|
+
"fmt"
|
|
10
|
+
"net/http"
|
|
11
|
+
"regexp"
|
|
12
|
+
"strconv"
|
|
13
|
+
"strings"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
// @block webhooks.verify
|
|
17
|
+
// @summary Verifies a webhook's HMAC-SHA256 signature as the definition describes it (signed text from the body and headers, several signatures in one header, an optional timestamp tolerance against replays), comparing in constant time, and returns *WebhookSignatureError.
|
|
18
|
+
// @versions >=1.18
|
|
19
|
+
// @source [1] https://github.com/standard-webhooks/standard-webhooks/blob/main/spec/standard-webhooks.md
|
|
20
|
+
// @quote [1] "When verifying symmetric signatures, use a constant time comparison function to compare the calculated with the expected signature."
|
|
21
|
+
// @quote [1] "The signature header is a space delimited list of signatures associated with this webhook."
|
|
22
|
+
// @source [2] https://pkg.go.dev/crypto/hmac
|
|
23
|
+
// @quote [2] "Equal compares two MACs for equality without leaking timing information."
|
|
24
|
+
// @retrieved 2026-12-05
|
|
25
|
+
// @tests tests/webhooks_test.go
|
|
26
|
+
// @requirement SDKL-HOOK-001
|
|
27
|
+
|
|
28
|
+
// WebhookSignature says how a webhook is signed; the same fields as the definition's hmacSignature().
|
|
29
|
+
type WebhookSignature struct {
|
|
30
|
+
Header string
|
|
31
|
+
Encoding string // "hex" or "base64"
|
|
32
|
+
Prefix string
|
|
33
|
+
Separator string // "" when the header holds one signature
|
|
34
|
+
Signed string // e.g. "{webhook-id}.{webhook-timestamp}.{body}"
|
|
35
|
+
TimestampHeader string // "" when the webhook carries no timestamp
|
|
36
|
+
ToleranceSeconds int64
|
|
37
|
+
Secret string // "text" or "base64"
|
|
38
|
+
SecretPrefix string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// WebhookSignatureError means the webhook was not signed with the secret, or is too old or too new.
|
|
42
|
+
type WebhookSignatureError struct {
|
|
43
|
+
Message string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
func (e *WebhookSignatureError) Error() string {
|
|
47
|
+
return e.Message
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
var (
|
|
51
|
+
webhookPlaceholder = regexp.MustCompile(`\{([^{}]*)\}`)
|
|
52
|
+
webhookTimestamp = regexp.MustCompile(`^[0-9]{1,15}$`)
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
func signatureError(format string, args ...any) error {
|
|
56
|
+
return &WebhookSignatureError{Message: fmt.Sprintf(format, args...)}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// VerifyWebhook returns nil when one of the header's signatures is the HMAC of the signed text, made in time.
|
|
60
|
+
func VerifyWebhook(signature WebhookSignature, body []byte, headers http.Header, secret string, nowSeconds int64) error {
|
|
61
|
+
if signature.TimestampHeader != "" {
|
|
62
|
+
stamp := strings.TrimSpace(headers.Get(signature.TimestampHeader))
|
|
63
|
+
if !webhookTimestamp.MatchString(stamp) {
|
|
64
|
+
return signatureError("missing or invalid %s header", signature.TimestampHeader)
|
|
65
|
+
}
|
|
66
|
+
signed, _ := strconv.ParseInt(stamp, 10, 64)
|
|
67
|
+
age := nowSeconds - signed
|
|
68
|
+
if age > signature.ToleranceSeconds || -age > signature.ToleranceSeconds {
|
|
69
|
+
return signatureError("the webhook was signed at %s, more than %d s from now", stamp, signature.ToleranceSeconds)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
given, present := headers[http.CanonicalHeaderKey(signature.Header)]
|
|
73
|
+
if !present || len(given) == 0 {
|
|
74
|
+
return signatureError("missing %s header", signature.Header)
|
|
75
|
+
}
|
|
76
|
+
var message []byte
|
|
77
|
+
last := 0
|
|
78
|
+
for _, match := range webhookPlaceholder.FindAllStringSubmatchIndex(signature.Signed, -1) {
|
|
79
|
+
message = append(message, signature.Signed[last:match[0]]...)
|
|
80
|
+
name := signature.Signed[match[2]:match[3]]
|
|
81
|
+
if name == "body" {
|
|
82
|
+
message = append(message, body...)
|
|
83
|
+
} else {
|
|
84
|
+
values, found := headers[http.CanonicalHeaderKey(name)]
|
|
85
|
+
if !found || len(values) == 0 {
|
|
86
|
+
return signatureError("missing %s header", name)
|
|
87
|
+
}
|
|
88
|
+
message = append(message, values[0]...)
|
|
89
|
+
}
|
|
90
|
+
last = match[1]
|
|
91
|
+
}
|
|
92
|
+
message = append(message, signature.Signed[last:]...)
|
|
93
|
+
keyText := secret
|
|
94
|
+
if signature.SecretPrefix != "" {
|
|
95
|
+
keyText = strings.TrimPrefix(secret, signature.SecretPrefix)
|
|
96
|
+
}
|
|
97
|
+
key := []byte(keyText)
|
|
98
|
+
if signature.Secret == "base64" {
|
|
99
|
+
decoded, err := base64.StdEncoding.DecodeString(keyText)
|
|
100
|
+
if err != nil {
|
|
101
|
+
return signatureError("the secret is not valid base64")
|
|
102
|
+
}
|
|
103
|
+
key = decoded
|
|
104
|
+
}
|
|
105
|
+
mac := hmac.New(sha256.New, key)
|
|
106
|
+
mac.Write(message)
|
|
107
|
+
digest := mac.Sum(nil)
|
|
108
|
+
expected := base64.StdEncoding.EncodeToString(digest)
|
|
109
|
+
if signature.Encoding == "hex" {
|
|
110
|
+
expected = hex.EncodeToString(digest)
|
|
111
|
+
}
|
|
112
|
+
candidates := []string{given[0]}
|
|
113
|
+
if signature.Separator != "" {
|
|
114
|
+
candidates = strings.Split(given[0], signature.Separator)
|
|
115
|
+
}
|
|
116
|
+
matched := false
|
|
117
|
+
for _, candidate := range candidates {
|
|
118
|
+
if strings.HasPrefix(candidate, signature.Prefix) {
|
|
119
|
+
// hmac.Equal compares in constant time.
|
|
120
|
+
matched = hmac.Equal([]byte(candidate[len(signature.Prefix):]), []byte(expected)) || matched
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if !matched {
|
|
124
|
+
return signatureError("no signature matches: the webhook was not signed with this secret, or was changed after signing")
|
|
125
|
+
}
|
|
126
|
+
return nil
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// @end
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// @requirement SDKL-ERR-001
|
|
2
|
+
// @requirement SDKL-VERSION-001
|
|
3
|
+
// @requirement SDKL-PIPE-001
|
|
4
|
+
// @requirement SDKL-PIPE-002
|
|
5
|
+
// @requirement SDKL-JSON-002
|
|
6
|
+
package sdkruntime
|
|
7
|
+
|
|
8
|
+
import (
|
|
9
|
+
"context"
|
|
10
|
+
"errors"
|
|
11
|
+
"sync"
|
|
12
|
+
"testing"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
var getItem = OperationSpec{Name: "getItem", Method: "GET", Path: "/v1/items/{id}", Result: KindJSON}
|
|
16
|
+
|
|
17
|
+
type notFound struct{ *APIError }
|
|
18
|
+
|
|
19
|
+
func runtimeWith(transport *MockTransport, middleware ...MiddlewareEntry) *Runtime {
|
|
20
|
+
return &Runtime{BaseURL: "https://api.test", Transport: transport, Clock: NewFakeClock(0), Middleware: middleware, SDK: SdkInfo{"acme", "1.0.0", "go"}}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
func TestACallSendsURLHeadersAndBody(t *testing.T) {
|
|
24
|
+
transport := NewMockTransport()
|
|
25
|
+
transport.Route("POST", "/v1/items/{id}", nil).ReplyJSON(201, map[string]any{"ok": true})
|
|
26
|
+
spec := OperationSpec{Name: "putItem", Method: "POST", Path: "/v1/items/{id}", Body: KindJSON, Result: KindJSON}
|
|
27
|
+
|
|
28
|
+
response, err := Call(context.Background(), runtimeWith(transport), spec, Args{
|
|
29
|
+
Path: map[string]any{"id": "a 1"},
|
|
30
|
+
Query: []QueryParam{{"expand", []any{"x", "y"}}},
|
|
31
|
+
Headers: []QueryParam{{"x-trace", "t1"}, {"x-none", nil}},
|
|
32
|
+
Body: map[string]any{"name": "ş"},
|
|
33
|
+
}, WithHeader("x-extra", "e"))
|
|
34
|
+
|
|
35
|
+
if err != nil || response.Status != 201 {
|
|
36
|
+
t.Fatalf("%v %v", response, err)
|
|
37
|
+
}
|
|
38
|
+
sent := transport.Calls()[0]
|
|
39
|
+
if sent.URL != "https://api.test/v1/items/a%201?expand=x&expand=y" || string(sent.Body) != `{"name":"ş"}` {
|
|
40
|
+
t.Fatalf("sent %s %s", sent.URL, sent.Body)
|
|
41
|
+
}
|
|
42
|
+
for name, want := range map[string]string{"accept": "application/json", "content-type": "application/json", "x-trace": "t1", "x-extra": "e"} {
|
|
43
|
+
if got := sent.Header(name); got == nil || *got != want {
|
|
44
|
+
t.Errorf("header %s: %v", name, got)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if sent.Header("x-none") != nil {
|
|
48
|
+
t.Error("a nil header is left out")
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
func TestStatusesBecomeDeclaredOrGenericErrors(t *testing.T) {
|
|
53
|
+
transport := NewMockTransport()
|
|
54
|
+
transport.Route("GET", "/v1/items/{id}", func(r Request) bool { return r.URL == "https://api.test/v1/items/missing" }).ReplyJSON(404, map[string]any{"message": "no"})
|
|
55
|
+
transport.Route("GET", "/v1/items/{id}", func(r Request) bool { return r.URL == "https://api.test/v1/items/down" }).Reply(503, []byte("down"), Header{"X-Request-Id", "req_1"})
|
|
56
|
+
spec := getItem
|
|
57
|
+
spec.Errors = map[int]ErrorFactory{404: func(status int, body any, response Response) error {
|
|
58
|
+
return notFound{NewAPIError("NotFound", status, body, response)}
|
|
59
|
+
}}
|
|
60
|
+
|
|
61
|
+
_, err := Call(context.Background(), runtimeWith(transport), spec, Args{Path: map[string]any{"id": "missing"}})
|
|
62
|
+
var declared notFound
|
|
63
|
+
if !errors.As(err, &declared) || declared.Status != 404 || declared.Body.(map[string]any)["message"] != "no" || declared.Retryable() {
|
|
64
|
+
t.Fatalf("got %#v", err)
|
|
65
|
+
}
|
|
66
|
+
_, err = Call(context.Background(), runtimeWith(transport), spec, Args{Path: map[string]any{"id": "down"}})
|
|
67
|
+
var generic *APIError
|
|
68
|
+
if !errors.As(err, &generic) || generic.Name != "ApiError" || generic.Body != "down" || !generic.Retryable() || *generic.RequestID != "req_1" {
|
|
69
|
+
t.Fatalf("got %#v", err)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
func TestMiddlewareRunsInOrderWithConfigAndContext(t *testing.T) {
|
|
74
|
+
transport := NewMockTransport()
|
|
75
|
+
transport.Route("GET", "/v1/items/{id}", nil).Reply(503, nil).Reply(200, []byte(`{}`))
|
|
76
|
+
var order []string
|
|
77
|
+
var events []string
|
|
78
|
+
tag := func(request Request, next Next, ctx Context) (Response, error) {
|
|
79
|
+
order = append(order, ctx.Config["label"].(string)+":"+ctx.Operation.Name+":"+ctx.SDK.Language)
|
|
80
|
+
return next(request.WithHeader("x-"+ctx.Config["label"].(string), "1"))
|
|
81
|
+
}
|
|
82
|
+
retry := func(request Request, next Next, ctx Context) (Response, error) {
|
|
83
|
+
response, err := next(request)
|
|
84
|
+
if err == nil && response.Status == 503 {
|
|
85
|
+
ctx.Clock.Sleep(100)
|
|
86
|
+
ctx.Emit("retry", map[string]any{"attempt": int64(1)})
|
|
87
|
+
return next(request)
|
|
88
|
+
}
|
|
89
|
+
return response, err
|
|
90
|
+
}
|
|
91
|
+
runtime := runtimeWith(transport, Entry("sdk", tag, map[string]any{"label": "sdk"}), Entry("retry", retry, nil))
|
|
92
|
+
runtime.Registry = map[string]MiddlewareEntry{"op": Entry("op", tag, map[string]any{"label": "op"})}
|
|
93
|
+
runtime.UserMiddleware = []MiddlewareEntry{Entry("user", tag, map[string]any{"label": "user"})}
|
|
94
|
+
runtime.Listeners = []EventListener{func(name string, _ map[string]any) { events = append(events, name) }}
|
|
95
|
+
spec := getItem
|
|
96
|
+
spec.Use = []string{"op"}
|
|
97
|
+
|
|
98
|
+
response, err := Call(context.Background(), runtime, spec, Args{Path: map[string]any{"id": "1"}})
|
|
99
|
+
|
|
100
|
+
if err != nil || response.Status != 200 {
|
|
101
|
+
t.Fatalf("%v %v", response, err)
|
|
102
|
+
}
|
|
103
|
+
want := []string{"sdk:getItem:go", "op:getItem:go", "user:getItem:go", "op:getItem:go", "user:getItem:go"}
|
|
104
|
+
if len(order) != len(want) || order[0] != want[0] || order[2] != want[2] || order[4] != want[4] {
|
|
105
|
+
t.Fatalf("order %v", order)
|
|
106
|
+
}
|
|
107
|
+
if runtime.Clock.Now() != 100 || len(events) != 1 || events[0] != "retry" {
|
|
108
|
+
t.Fatalf("clock %d events %v", runtime.Clock.Now(), events)
|
|
109
|
+
}
|
|
110
|
+
if sent := transport.Calls()[1]; sent.Header("x-sdk") == nil || sent.Header("x-op") == nil || sent.Header("x-user") == nil {
|
|
111
|
+
t.Fatal("every middleware's header reached the transport")
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
func TestAMiddlewareCanAnswerWithoutTheTransport(t *testing.T) {
|
|
116
|
+
cached := func(Request, Next, Context) (Response, error) { return Response{Status: 200, Body: []byte(`{"cached":true}`)}, nil }
|
|
117
|
+
transport := NewMockTransport()
|
|
118
|
+
|
|
119
|
+
response, err := Call(context.Background(), runtimeWith(transport, Entry("cache", cached, nil)), getItem, Args{Path: map[string]any{"id": "x"}})
|
|
120
|
+
|
|
121
|
+
if err != nil || string(response.Body) != `{"cached":true}` || len(transport.Calls()) != 0 {
|
|
122
|
+
t.Fatalf("%v %v", response, err)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
func TestListenersThatPanicAreReportedNotPropagated(t *testing.T) {
|
|
127
|
+
var seen []string
|
|
128
|
+
emit := Emitter([]EventListener{
|
|
129
|
+
func(name string, fields map[string]any) {
|
|
130
|
+
if name != ListenerError {
|
|
131
|
+
panic("boom")
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
func(name string, fields map[string]any) { seen = append(seen, name+" "+JsStr(fields["error"])) },
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
emit("http.request", map[string]any{})
|
|
138
|
+
|
|
139
|
+
if len(seen) != 2 || seen[0] != "http.request null" || seen[1] != "sdkloom.listener_error boom" {
|
|
140
|
+
t.Fatalf("seen %v", seen)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
func TestStateIsSafeForConcurrentUseAndExpires(t *testing.T) {
|
|
145
|
+
clock := NewFakeClock(0)
|
|
146
|
+
state := NewState(clock)
|
|
147
|
+
var group sync.WaitGroup
|
|
148
|
+
for i := 0; i < 8; i++ {
|
|
149
|
+
group.Add(1)
|
|
150
|
+
go func(i int) {
|
|
151
|
+
defer group.Done()
|
|
152
|
+
state.Set("k", i)
|
|
153
|
+
_ = state.Get("k")
|
|
154
|
+
}(i)
|
|
155
|
+
}
|
|
156
|
+
group.Wait()
|
|
157
|
+
state.Set("token", "t", 1000)
|
|
158
|
+
clock.Advance(999)
|
|
159
|
+
if state.Get("token") != "t" {
|
|
160
|
+
t.Fatal("still valid")
|
|
161
|
+
}
|
|
162
|
+
clock.Advance(1)
|
|
163
|
+
if state.Get("token") != nil {
|
|
164
|
+
t.Fatal("expired")
|
|
165
|
+
}
|
|
166
|
+
state.Set("x", 1)
|
|
167
|
+
state.Delete("x")
|
|
168
|
+
if state.Get("x") != nil {
|
|
169
|
+
t.Fatal("deleted")
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
func TestAPIVersionRefusesNewerOperationsAndUnknownVersions(t *testing.T) {
|
|
174
|
+
version := &APIVersion{Header: "api-version", Value: "2024-06-20", Values: []string{"2024-06-20", "2025-01-01"}}
|
|
175
|
+
if version.Check("getOrder", "2024-06-20") != nil || version.Check("getOrder", "") != nil || (*APIVersion)(nil).Check("getOrder", "x") != nil {
|
|
176
|
+
t.Fatal("refused an available operation")
|
|
177
|
+
}
|
|
178
|
+
var argument *ArgumentError
|
|
179
|
+
if err := version.Check("refundOrder", "2025-01-01"); !errors.As(err, &argument) || argument.Message != "refundOrder needs API version 2025-01-01 or later; this client uses 2024-06-20" {
|
|
180
|
+
t.Fatal(err)
|
|
181
|
+
}
|
|
182
|
+
if err := (&APIVersion{Header: "api-version", Value: "1999", Values: version.Values}).Check("getOrder", ""); err == nil {
|
|
183
|
+
t.Fatal("accepted an unknown version")
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// @requirement SDKL-HTTP-001
|
|
2
|
+
// @requirement SDKL-HTTP-002
|
|
3
|
+
// @requirement SDKL-HTTP-003
|
|
4
|
+
package sdkruntime
|
|
5
|
+
|
|
6
|
+
import (
|
|
7
|
+
"errors"
|
|
8
|
+
"strings"
|
|
9
|
+
"testing"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
func TestHeadersAreCaseInsensitiveAndRequestsImmutable(t *testing.T) {
|
|
13
|
+
original := NewRequest("GET", "https://a.test").WithHeader("Accept", "a")
|
|
14
|
+
changed := original.WithHeader("accept", "b").WithURL("https://b.test")
|
|
15
|
+
|
|
16
|
+
if got := *changed.Header("ACCEPT"); got != "b" || len(changed.Headers) != 1 {
|
|
17
|
+
t.Fatalf("headers %v", changed.Headers)
|
|
18
|
+
}
|
|
19
|
+
if *original.Header("accept") != "a" || original.URL != "https://a.test" {
|
|
20
|
+
t.Fatal("the original changed")
|
|
21
|
+
}
|
|
22
|
+
if changed.WithoutHeader("Accept").Header("accept") != nil {
|
|
23
|
+
t.Fatal("header not removed")
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func TestURLsAreBuiltLikeTheTypeScriptKit(t *testing.T) {
|
|
28
|
+
cases := []struct {
|
|
29
|
+
path map[string]any
|
|
30
|
+
query []QueryParam
|
|
31
|
+
want string
|
|
32
|
+
}{
|
|
33
|
+
{map[string]any{"id": "a b/c"}, []QueryParam{{"expand", []any{"x", "y"}}, {"skip", nil}, {"flag", true}}, "https://api.test/v1/orders/a%20b%2Fc/items?expand=x&expand=y&flag=true"},
|
|
34
|
+
{map[string]any{"id": "!'()*~-_."}, []QueryParam{{"q", "a b*~!"}}, "https://api.test/v1/orders/!'()*~-_./items?q=a+b*%7E%21"},
|
|
35
|
+
{map[string]any{"id": int64(7)}, []QueryParam{{"x", 1.5}, {"y", 2.0}, {"z", 1e-7}, {"w", 1e23}, {"d", Decimal("1E+3")}}, "https://api.test/v1/orders/7/items?x=1.5&y=2&z=1e-7&w=1e%2B23&d=1000"},
|
|
36
|
+
}
|
|
37
|
+
for _, c := range cases {
|
|
38
|
+
got, err := BuildURL("https://api.test/v1/", "/orders/{id}/items", c.path, c.query)
|
|
39
|
+
if err != nil || got != c.want {
|
|
40
|
+
t.Errorf("got %q (%v), want %q", got, err, c.want)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
_, err := BuildURL("https://a.test", "/x/{id}", map[string]any{}, nil)
|
|
44
|
+
var missing *ArgumentError
|
|
45
|
+
if !errors.As(err, &missing) || missing.Message != "missing path parameter id" {
|
|
46
|
+
t.Fatalf("want a missing path parameter, got %v", err)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
func TestResponsesReadAsTextAndJSONAnyNumberOfTimes(t *testing.T) {
|
|
51
|
+
response := Response{Status: 200, Headers: []Header{{"Content-Type", "application/json"}}, Body: []byte(`{"a":1}`)}
|
|
52
|
+
|
|
53
|
+
first, _ := response.JSON()
|
|
54
|
+
second, _ := response.JSON()
|
|
55
|
+
if JsStr(first.(map[string]any)["a"]) != "1" || JsStr(second.(map[string]any)["a"]) != "1" || response.Text() != `{"a":1}` {
|
|
56
|
+
t.Fatal("body not readable twice")
|
|
57
|
+
}
|
|
58
|
+
if *response.Header("content-type") != "application/json" {
|
|
59
|
+
t.Fatal("header lookup")
|
|
60
|
+
}
|
|
61
|
+
if empty, err := (Response{Status: 204}).JSON(); empty != nil || err != nil {
|
|
62
|
+
t.Fatal("an empty body is nil")
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
func TestAPathParameterOfDotsIsRefused(t *testing.T) {
|
|
67
|
+
for _, value := range []string{".", ".."} {
|
|
68
|
+
_, err := BuildURL("https://a.test", "/f/{id}", map[string]any{"id": value}, nil)
|
|
69
|
+
var argument *ArgumentError
|
|
70
|
+
if !errors.As(err, &argument) || !strings.Contains(argument.Message, "cannot be") {
|
|
71
|
+
t.Fatalf("%q: %v", value, err)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
got, _ := BuildURL("https://a.test", "/f/{id}", map[string]any{"id": "a..b"}, nil)
|
|
75
|
+
if got != "https://a.test/f/a..b" {
|
|
76
|
+
t.Fatalf("%q", got)
|
|
77
|
+
}
|
|
78
|
+
}
|