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,207 @@
|
|
|
1
|
+
// sdkloom Go kit: the capabilities a middleware receives as ctx.
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"context"
|
|
6
|
+
"sync"
|
|
7
|
+
"time"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
// @block context.create
|
|
11
|
+
// @summary The capabilities a middleware gets through ctx: a clock, goroutine-safe per-client state with expiry, and events whose listeners can never break a call.
|
|
12
|
+
// @versions >=1.18
|
|
13
|
+
// @source [1] https://pkg.go.dev/time
|
|
14
|
+
// @quote [1] "Sleep pauses the current goroutine for at least the duration d."
|
|
15
|
+
// @source [2] https://pkg.go.dev/sync
|
|
16
|
+
// @quote [2] "A Mutex is a mutual exclusion lock."
|
|
17
|
+
// @source [3] https://pkg.go.dev/context
|
|
18
|
+
// @quote [3] "Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes."
|
|
19
|
+
// @retrieved 2026-12-05
|
|
20
|
+
// @tests tests/client_test.go
|
|
21
|
+
// @requirement SDKL-PIPE-002
|
|
22
|
+
|
|
23
|
+
// Clock gives the time in milliseconds and waits; tests use FakeClock.
|
|
24
|
+
type Clock interface {
|
|
25
|
+
Now() int64
|
|
26
|
+
Sleep(ms int64)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type systemClock struct {
|
|
30
|
+
start time.Time
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// SystemClock measures milliseconds on the monotonic clock.
|
|
34
|
+
func SystemClock() Clock {
|
|
35
|
+
return systemClock{start: time.Now()}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
func (c systemClock) Now() int64 {
|
|
39
|
+
return time.Since(c.start).Milliseconds()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
func (c systemClock) Sleep(ms int64) {
|
|
43
|
+
time.Sleep(time.Duration(ms) * time.Millisecond)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type stateEntry struct {
|
|
47
|
+
value any
|
|
48
|
+
expiresAt int64
|
|
49
|
+
expires bool
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// State is per-client storage shared by all calls, e.g. for a cached token. Safe for concurrent use.
|
|
53
|
+
type State struct {
|
|
54
|
+
clock Clock
|
|
55
|
+
mutex sync.Mutex
|
|
56
|
+
entries map[string]stateEntry
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// NewState returns empty state that measures expiry with clock.
|
|
60
|
+
func NewState(clock Clock) *State {
|
|
61
|
+
return &State{clock: clock, entries: map[string]stateEntry{}}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Get returns the value, or nil when it is absent or expired.
|
|
65
|
+
func (s *State) Get(key string) any {
|
|
66
|
+
s.mutex.Lock()
|
|
67
|
+
defer s.mutex.Unlock()
|
|
68
|
+
entry, ok := s.entries[key]
|
|
69
|
+
if !ok {
|
|
70
|
+
return nil
|
|
71
|
+
}
|
|
72
|
+
if entry.expires && s.clock.Now() >= entry.expiresAt {
|
|
73
|
+
delete(s.entries, key)
|
|
74
|
+
return nil
|
|
75
|
+
}
|
|
76
|
+
return entry.value
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Set stores a value, optionally for ttlMs milliseconds.
|
|
80
|
+
func (s *State) Set(key string, value any, ttlMs ...int64) {
|
|
81
|
+
s.mutex.Lock()
|
|
82
|
+
defer s.mutex.Unlock()
|
|
83
|
+
entry := stateEntry{value: value}
|
|
84
|
+
if len(ttlMs) > 0 {
|
|
85
|
+
entry.expires, entry.expiresAt = true, s.clock.Now()+ttlMs[0]
|
|
86
|
+
}
|
|
87
|
+
s.entries[key] = entry
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Delete removes a value.
|
|
91
|
+
func (s *State) Delete(key string) {
|
|
92
|
+
s.mutex.Lock()
|
|
93
|
+
defer s.mutex.Unlock()
|
|
94
|
+
delete(s.entries, key)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// EventListener receives every event a middleware emits.
|
|
98
|
+
type EventListener func(name string, fields map[string]any)
|
|
99
|
+
|
|
100
|
+
// ListenerError is the event that reports a listener that panicked; it is never delivered to that listener.
|
|
101
|
+
const ListenerError = "sdkloom.listener_error"
|
|
102
|
+
|
|
103
|
+
// Emitter delivers events to listeners; a listener that panics is reported, never propagated.
|
|
104
|
+
func Emitter(listeners []EventListener) func(name string, fields map[string]any) {
|
|
105
|
+
var deliver func(name string, fields map[string]any, skip int)
|
|
106
|
+
deliver = func(name string, fields map[string]any, skip int) {
|
|
107
|
+
type failure struct {
|
|
108
|
+
index int
|
|
109
|
+
message string
|
|
110
|
+
}
|
|
111
|
+
var failures []failure
|
|
112
|
+
for index, listener := range listeners {
|
|
113
|
+
if index == skip {
|
|
114
|
+
continue
|
|
115
|
+
}
|
|
116
|
+
if message, failed := safely(listener, name, fields); failed {
|
|
117
|
+
failures = append(failures, failure{index, message})
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if name == ListenerError {
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
for _, failed := range failures {
|
|
124
|
+
deliver(ListenerError, map[string]any{"event": name, "error": failed.message}, failed.index)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return func(name string, fields map[string]any) {
|
|
128
|
+
deliver(name, fields, -1)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
func safely(listener EventListener, name string, fields map[string]any) (message string, failed bool) {
|
|
133
|
+
defer func() {
|
|
134
|
+
if recovered := recover(); recovered != nil {
|
|
135
|
+
message, failed = panicText(recovered), true
|
|
136
|
+
}
|
|
137
|
+
}()
|
|
138
|
+
listener(name, fields)
|
|
139
|
+
return "", false
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
func panicText(value any) string {
|
|
143
|
+
if err, ok := value.(error); ok {
|
|
144
|
+
return err.Error()
|
|
145
|
+
}
|
|
146
|
+
if text, ok := value.(string); ok {
|
|
147
|
+
return text
|
|
148
|
+
}
|
|
149
|
+
return "panic"
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// OperationInfo names the operation being called.
|
|
153
|
+
type OperationInfo struct {
|
|
154
|
+
Name string
|
|
155
|
+
Method string
|
|
156
|
+
Path string
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// SdkInfo says which generated SDK a middleware runs in, e.g. for a User-Agent such as "acme-go/1.2.0".
|
|
160
|
+
type SdkInfo struct {
|
|
161
|
+
Name string
|
|
162
|
+
Version string
|
|
163
|
+
Language string
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Context is what a middleware gets as ctx.
|
|
167
|
+
type Context struct {
|
|
168
|
+
// Go is the caller's context: deadlines and cancellation.
|
|
169
|
+
Go context.Context
|
|
170
|
+
Config map[string]any
|
|
171
|
+
Operation OperationInfo
|
|
172
|
+
Clock Clock
|
|
173
|
+
State *State
|
|
174
|
+
SDK SdkInfo
|
|
175
|
+
emit func(name string, fields map[string]any)
|
|
176
|
+
send func(request Request) (Response, error)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// NewContext builds a ctx outside a call, e.g. to test a middleware on its own.
|
|
180
|
+
func NewContext(goContext context.Context, config map[string]any, operation OperationInfo, clock Clock, state *State, sdk SdkInfo, emit func(name string, fields map[string]any), send func(Request) (Response, error)) Context {
|
|
181
|
+
return Context{Go: goContext, Config: config, Operation: operation, Clock: clock, State: state, SDK: sdk, emit: emit, send: send}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Emit reports an event to the client's listeners.
|
|
185
|
+
func (c Context) Emit(name string, fields map[string]any) {
|
|
186
|
+
if c.emit != nil {
|
|
187
|
+
c.emit(name, fields)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Send sends a request through the transport, without the middleware chain (e.g. to fetch a token).
|
|
192
|
+
func (c Context) Send(request Request) (Response, error) {
|
|
193
|
+
return c.send(request)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Request returns a new request, e.g. for Send.
|
|
197
|
+
func (c Context) Request(method, url string) Request {
|
|
198
|
+
return NewRequest(method, url)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// WithConfig returns a copy with a middleware's configuration.
|
|
202
|
+
func (c Context) WithConfig(config map[string]any) Context {
|
|
203
|
+
c.Config = config
|
|
204
|
+
return c
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// @end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// sdkloom Go kit: errors.
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import "strconv"
|
|
5
|
+
|
|
6
|
+
// @block errors.api_error
|
|
7
|
+
// @summary The error for a non-2xx response; carries status, decoded body and request id, and marks 429 and 5xx as retryable.
|
|
8
|
+
// @versions >=1.18
|
|
9
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-15.5
|
|
10
|
+
// @quote [1] "The 4xx (Client Error) class of status code indicates that the client seems to have erred."
|
|
11
|
+
// @source [2] https://www.rfc-editor.org/rfc/rfc9110#section-15.6
|
|
12
|
+
// @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."
|
|
13
|
+
// @retrieved 2026-12-05
|
|
14
|
+
// @tests tests/client_test.go
|
|
15
|
+
// @requirement SDKL-ERR-001
|
|
16
|
+
|
|
17
|
+
const (
|
|
18
|
+
tooManyRequests = 429
|
|
19
|
+
firstServerError = 500
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
// APIError is a non-2xx response. Generated SDKs wrap it in one type per declared error; use errors.As to tell them apart.
|
|
23
|
+
type APIError struct {
|
|
24
|
+
// Name is the declared error's name, e.g. "PaymentNotFound", or "ApiError".
|
|
25
|
+
Name string
|
|
26
|
+
Status int
|
|
27
|
+
Body any
|
|
28
|
+
Response Response
|
|
29
|
+
RequestID *string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// NewAPIError builds the error for a response.
|
|
33
|
+
func NewAPIError(name string, status int, body any, response Response) *APIError {
|
|
34
|
+
return &APIError{Name: name, Status: status, Body: body, Response: response, RequestID: response.Header("x-request-id")}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
func (e *APIError) Error() string {
|
|
38
|
+
return e.Name + ": HTTP " + strconv.Itoa(e.Status)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Retryable is true for 429 and 5xx: sending the same request again may succeed.
|
|
42
|
+
func (e *APIError) Retryable() bool {
|
|
43
|
+
return e.Status == tooManyRequests || e.Status >= firstServerError
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ErrorFactory builds the declared error for a status.
|
|
47
|
+
type ErrorFactory func(status int, body any, response Response) error
|
|
48
|
+
|
|
49
|
+
// @end
|
|
50
|
+
|
|
51
|
+
// @block errors.transport_error
|
|
52
|
+
// @summary The error for a request that got no HTTP response: a network failure or a timeout. A redirect is a response, not this error.
|
|
53
|
+
// @versions >=1.18
|
|
54
|
+
// @source [1] https://pkg.go.dev/net/http
|
|
55
|
+
// @quote [1] "ErrUseLastResponse can be returned by Client.CheckRedirect hooks to control how redirects are processed."
|
|
56
|
+
// @retrieved 2026-12-05
|
|
57
|
+
// @tests tests/transport_test.go
|
|
58
|
+
// @requirement SDKL-HTTP-004
|
|
59
|
+
|
|
60
|
+
// TransportError is a request that got no response; Reason is "timeout" or "network".
|
|
61
|
+
type TransportError struct {
|
|
62
|
+
Reason string
|
|
63
|
+
Cause error
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
func (e *TransportError) Error() string {
|
|
67
|
+
return "TransportError: request failed: " + e.Reason
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
func (e *TransportError) Unwrap() error {
|
|
71
|
+
return e.Cause
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// @end
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// sdkloom Go kit: HTTP request and response values, URL building.
|
|
2
|
+
package sdkruntime
|
|
3
|
+
|
|
4
|
+
import (
|
|
5
|
+
"bytes"
|
|
6
|
+
"io"
|
|
7
|
+
"net/url"
|
|
8
|
+
"regexp"
|
|
9
|
+
"strings"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
// @block http.request
|
|
13
|
+
// @summary An immutable HTTP request whose header names are matched case-insensitively.
|
|
14
|
+
// @versions >=1.18
|
|
15
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-5.1
|
|
16
|
+
// @quote [1] "Field names are case-insensitive"
|
|
17
|
+
// @retrieved 2026-12-05
|
|
18
|
+
// @tests tests/http_test.go
|
|
19
|
+
// @requirement SDKL-HTTP-001
|
|
20
|
+
|
|
21
|
+
// Header is one header field; a request or response keeps them in order.
|
|
22
|
+
type Header struct {
|
|
23
|
+
Name string
|
|
24
|
+
Value string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Request is an HTTP request. Its With methods return changed copies; the original never changes.
|
|
28
|
+
type Request struct {
|
|
29
|
+
Method string
|
|
30
|
+
URL string
|
|
31
|
+
Headers []Header
|
|
32
|
+
Body []byte
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// NewRequest returns a request without headers or body.
|
|
36
|
+
func NewRequest(method, url string) Request {
|
|
37
|
+
return Request{Method: method, URL: url}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
func findHeader(headers []Header, name string) *string {
|
|
41
|
+
for _, header := range headers {
|
|
42
|
+
if strings.EqualFold(header.Name, name) {
|
|
43
|
+
value := header.Value
|
|
44
|
+
return &value
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return nil
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
func withoutHeader(headers []Header, name string) []Header {
|
|
51
|
+
kept := make([]Header, 0, len(headers))
|
|
52
|
+
for _, header := range headers {
|
|
53
|
+
if !strings.EqualFold(header.Name, name) {
|
|
54
|
+
kept = append(kept, header)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return kept
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Header returns the first value of the named header, or nil when it is absent.
|
|
61
|
+
func (r Request) Header(name string) *string {
|
|
62
|
+
return findHeader(r.Headers, name)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// WithHeader returns a copy with the header set, replacing any value it had.
|
|
66
|
+
func (r Request) WithHeader(name, value string) Request {
|
|
67
|
+
r.Headers = append(withoutHeader(r.Headers, name), Header{Name: name, Value: value})
|
|
68
|
+
return r
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// WithoutHeader returns a copy without the header.
|
|
72
|
+
func (r Request) WithoutHeader(name string) Request {
|
|
73
|
+
r.Headers = withoutHeader(r.Headers, name)
|
|
74
|
+
return r
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// WithURL returns a copy with another URL.
|
|
78
|
+
func (r Request) WithURL(url string) Request {
|
|
79
|
+
r.URL = url
|
|
80
|
+
return r
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// WithBody returns a copy with another body.
|
|
84
|
+
func (r Request) WithBody(body []byte) Request {
|
|
85
|
+
r.Body = append([]byte(nil), body...)
|
|
86
|
+
return r
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Text returns the body as text, "" without one.
|
|
90
|
+
func (r Request) Text() string {
|
|
91
|
+
return string(r.Body)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// @end
|
|
95
|
+
|
|
96
|
+
// @block http.response
|
|
97
|
+
// @summary A received HTTP response whose body can be read as bytes, text or JSON any number of times, or as a stream as it arrives when the transport streamed it.
|
|
98
|
+
// @versions >=1.18
|
|
99
|
+
// @source [1] https://pkg.go.dev/encoding/json
|
|
100
|
+
// @quote [1] "UseNumber causes the Decoder to unmarshal a number into an interface value as a Number instead of as a float64."
|
|
101
|
+
// @retrieved 2026-12-05
|
|
102
|
+
// @tests tests/http_test.go
|
|
103
|
+
// @requirement SDKL-HTTP-003
|
|
104
|
+
|
|
105
|
+
// Response is a received HTTP response; Body holds all of it, except for a streamed response (an event stream), whose
|
|
106
|
+
// Body is empty and whose bytes are read from Stream as they arrive. Close Stream when done.
|
|
107
|
+
type Response struct {
|
|
108
|
+
Status int
|
|
109
|
+
Headers []Header
|
|
110
|
+
Body []byte
|
|
111
|
+
Stream io.ReadCloser
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// BodyReader returns the body as it arrives: Stream for a streamed response, else the buffered Body.
|
|
115
|
+
func (r Response) BodyReader() io.ReadCloser {
|
|
116
|
+
if r.Stream != nil {
|
|
117
|
+
return r.Stream
|
|
118
|
+
}
|
|
119
|
+
return io.NopCloser(bytes.NewReader(r.Body))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Header returns the first value of the named header, or nil when it is absent.
|
|
123
|
+
func (r Response) Header(name string) *string {
|
|
124
|
+
return findHeader(r.Headers, name)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Text returns the body as text.
|
|
128
|
+
func (r Response) Text() string {
|
|
129
|
+
return string(r.Body)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// JSON parses the body; an empty body is nil. Numbers stay exact (json.Number).
|
|
133
|
+
func (r Response) JSON() (any, error) {
|
|
134
|
+
if len(bytes.TrimSpace(r.Body)) == 0 {
|
|
135
|
+
return nil, nil
|
|
136
|
+
}
|
|
137
|
+
return ParseJSON(r.Body)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// @end
|
|
141
|
+
|
|
142
|
+
// @block http.url
|
|
143
|
+
// @summary Builds a request URL: percent-encodes path parameters like encodeURIComponent and appends query parameters in order, like the TypeScript kit. A path parameter of "." or ".." is refused with an ArgumentError: URL parsers read it as a step up the path, even percent-encoded. A {+name} placeholder (RFC 6570 reserved expansion, limited to "/") takes a value that spans segments, each encoded on its own.
|
|
144
|
+
// @versions >=1.18
|
|
145
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc3986#section-2.1
|
|
146
|
+
// @quote [1] "A percent-encoding mechanism is used to represent a data octet in a component when that octet's corresponding character is outside the allowed set or is being used as a delimiter of, or within, the component."
|
|
147
|
+
// @source [2] https://pkg.go.dev/net/url
|
|
148
|
+
// @quote [2] "QueryEscape escapes the string so it can be safely placed inside a URL query."
|
|
149
|
+
// @source [3] https://www.rfc-editor.org/rfc/rfc3986#section-3.3
|
|
150
|
+
// @quote [3] "also known as dot-segments, are defined for relative reference within the path name hierarchy."
|
|
151
|
+
// @source [4] https://www.rfc-editor.org/rfc/rfc6570#section-3.2.3
|
|
152
|
+
// @quote [4] "is identical to simple string expansion except that the substituted values may also contain pct-encoded triplets and characters in the reserved set."
|
|
153
|
+
// @retrieved 2026-12-05
|
|
154
|
+
// @tests tests/http_test.go
|
|
155
|
+
// @requirement SDKL-HTTP-002
|
|
156
|
+
|
|
157
|
+
// QueryParam is one query parameter; a nil Value is left out, a slice is repeated.
|
|
158
|
+
type QueryParam struct {
|
|
159
|
+
Name string
|
|
160
|
+
Value any
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
var placeholder = regexp.MustCompile(`\{([^{}]+)\}`)
|
|
164
|
+
|
|
165
|
+
// Scalar writes a parameter value exactly like the TypeScript, Python and Java kits: true/false, 2 (not 2.0), 1e-7,
|
|
166
|
+
// decimals in plain notation.
|
|
167
|
+
func Scalar(value any) string {
|
|
168
|
+
return JsStr(value)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
// PathSegment percent-encodes like encodeURIComponent: letters, digits and - _ . ! ~ * ' ( ) stay.
|
|
173
|
+
func PathSegment(text string) string {
|
|
174
|
+
escaped := url.QueryEscape(text)
|
|
175
|
+
replacer := strings.NewReplacer("+", "%20", "%21", "!", "%27", "'", "%28", "(", "%29", ")", "%2A", "*", "%7E", "~")
|
|
176
|
+
return replacer.Replace(escaped)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Form encodes like application/x-www-form-urlencoded in browsers: space is "+", "~" is encoded, "*" is not.
|
|
180
|
+
func Form(text string) string {
|
|
181
|
+
// "~" explicitly: url.QueryEscape encodes it in Go 1.18 and leaves it alone in later versions.
|
|
182
|
+
return strings.NewReplacer("%2A", "*", "~", "%7E").Replace(url.QueryEscape(text))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// BuildURL joins base and template, fills {name} placeholders from path and appends the query.
|
|
186
|
+
func BuildURL(base, template string, path map[string]any, query []QueryParam) (string, error) {
|
|
187
|
+
var problem error
|
|
188
|
+
filled := placeholder.ReplaceAllStringFunc(template, func(match string) string {
|
|
189
|
+
// {+name} (RFC 6570 reserved expansion, limited to "/"): the value may span segments, each encoded on its own.
|
|
190
|
+
name := match[1 : len(match)-1]
|
|
191
|
+
reserved := strings.HasPrefix(name, "+")
|
|
192
|
+
name = strings.TrimPrefix(name, "+")
|
|
193
|
+
value, ok := path[name]
|
|
194
|
+
if !ok {
|
|
195
|
+
problem = &ArgumentError{Message: "missing path parameter " + name}
|
|
196
|
+
return match
|
|
197
|
+
}
|
|
198
|
+
segments := []string{Scalar(value)}
|
|
199
|
+
if reserved {
|
|
200
|
+
segments = strings.Split(segments[0], "/")
|
|
201
|
+
}
|
|
202
|
+
for i, text := range segments {
|
|
203
|
+
// URL parsers treat "." and "..", even percent-encoded, as steps up the path: the request would reach
|
|
204
|
+
// another resource.
|
|
205
|
+
if text == "." || text == ".." {
|
|
206
|
+
problem = &ArgumentError{Message: "path parameter " + name + ` cannot be "` + text + `": a URL would read it as a step up the path`}
|
|
207
|
+
}
|
|
208
|
+
segments[i] = PathSegment(text)
|
|
209
|
+
}
|
|
210
|
+
return strings.Join(segments, "/")
|
|
211
|
+
})
|
|
212
|
+
if problem != nil {
|
|
213
|
+
return "", problem
|
|
214
|
+
}
|
|
215
|
+
var pairs []string
|
|
216
|
+
for _, param := range query {
|
|
217
|
+
for _, item := range queryItems(param.Value) {
|
|
218
|
+
pairs = append(pairs, Form(param.Name)+"="+Form(Scalar(item)))
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
result := strings.TrimRight(base, "/") + filled
|
|
222
|
+
if len(pairs) > 0 {
|
|
223
|
+
result += "?" + strings.Join(pairs, "&")
|
|
224
|
+
}
|
|
225
|
+
return result, nil
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
func queryItems(value any) []any {
|
|
229
|
+
switch v := value.(type) {
|
|
230
|
+
case nil:
|
|
231
|
+
return nil
|
|
232
|
+
case []any:
|
|
233
|
+
return v
|
|
234
|
+
case []string:
|
|
235
|
+
items := make([]any, len(v))
|
|
236
|
+
for i, item := range v {
|
|
237
|
+
items[i] = item
|
|
238
|
+
}
|
|
239
|
+
return items
|
|
240
|
+
case []int64:
|
|
241
|
+
items := make([]any, len(v))
|
|
242
|
+
for i, item := range v {
|
|
243
|
+
items[i] = item
|
|
244
|
+
}
|
|
245
|
+
return items
|
|
246
|
+
default:
|
|
247
|
+
return []any{value}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ArgumentError is a call argument the SDK cannot send (a missing path parameter, a path parameter of "." or ".."),
|
|
252
|
+
// reported before any request is made.
|
|
253
|
+
type ArgumentError struct {
|
|
254
|
+
Message string
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
func (e *ArgumentError) Error() string {
|
|
258
|
+
return e.Message
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// @end
|