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,295 @@
|
|
|
1
|
+
// sdkloom C# kit: calling one operation.
|
|
2
|
+
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections.Generic;
|
|
5
|
+
using System.Linq;
|
|
6
|
+
using System.Threading;
|
|
7
|
+
using System.Threading.Tasks;
|
|
8
|
+
|
|
9
|
+
namespace Sdkloom.Runtime
|
|
10
|
+
{
|
|
11
|
+
// @block client.call
|
|
12
|
+
// @summary Sends one operation: refuses one newer than the pinned API version, builds URL, headers and body, runs the middleware chain, sends it and turns a non-2xx response into the declared error.
|
|
13
|
+
// @versions >=3
|
|
14
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-15.5
|
|
15
|
+
// @quote [1] "The 4xx (Client Error) class of status code indicates that the client seems to have erred."
|
|
16
|
+
// @source [2] https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/
|
|
17
|
+
// @quote [2] "The Task asynchronous programming (TAP) model provides a layer of abstraction over typical asynchronous coding."
|
|
18
|
+
// @retrieved 2026-12-05
|
|
19
|
+
// @tests tests/ClientTests.cs
|
|
20
|
+
// @requirement SDKL-ERR-001
|
|
21
|
+
// @requirement SDKL-PIPE-001
|
|
22
|
+
|
|
23
|
+
/// <summary>Sends a request on to the rest of the chain.</summary>
|
|
24
|
+
public delegate Task<HttpResponse> Next(HttpRequest request);
|
|
25
|
+
|
|
26
|
+
/// <summary>Wraps every call: change the request, call next zero or more times, change the response.</summary>
|
|
27
|
+
public delegate Task<HttpResponse> Middleware(HttpRequest request, Next next, Context ctx);
|
|
28
|
+
|
|
29
|
+
/// <summary>A middleware with its configuration bound.</summary>
|
|
30
|
+
public sealed class MiddlewareEntry
|
|
31
|
+
{
|
|
32
|
+
public MiddlewareEntry(string name, Middleware run)
|
|
33
|
+
{
|
|
34
|
+
Name = name;
|
|
35
|
+
Run = run;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public string Name { get; }
|
|
39
|
+
|
|
40
|
+
public Middleware Run { get; }
|
|
41
|
+
|
|
42
|
+
/// <summary>Binds a middleware function to its configuration.</summary>
|
|
43
|
+
public static MiddlewareEntry Of(string name, Middleware middleware, IReadOnlyList<KeyValuePair<string, Json>> config) =>
|
|
44
|
+
new MiddlewareEntry(name, (request, next, ctx) => middleware(request, next, ctx.WithConfig(config)));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// <summary>What a request body or result is.</summary>
|
|
48
|
+
public enum BodyKind
|
|
49
|
+
{
|
|
50
|
+
Json,
|
|
51
|
+
Bytes,
|
|
52
|
+
Stream,
|
|
53
|
+
|
|
54
|
+
/// <summary>A result of server-sent events, read with EventStreams.Read.</summary>
|
|
55
|
+
Events,
|
|
56
|
+
|
|
57
|
+
/// <summary>A multipart/form-data body (MultipartForm.ToBody).</summary>
|
|
58
|
+
Multipart,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// <summary>Builds the declared error for a status.</summary>
|
|
62
|
+
public delegate ApiException ErrorFactory(int status, Json body, HttpResponse response);
|
|
63
|
+
|
|
64
|
+
/// <summary>What generated code knows about one operation.</summary>
|
|
65
|
+
public sealed class OperationSpec
|
|
66
|
+
{
|
|
67
|
+
public OperationSpec(string name, string method, string path, BodyKind? body, BodyKind? result, IReadOnlyDictionary<int, ErrorFactory> errors, IReadOnlyList<string> uses, string? since = null)
|
|
68
|
+
{
|
|
69
|
+
Since = since;
|
|
70
|
+
Name = name;
|
|
71
|
+
Method = method;
|
|
72
|
+
Path = path;
|
|
73
|
+
Body = body;
|
|
74
|
+
Result = result;
|
|
75
|
+
Errors = errors;
|
|
76
|
+
Uses = uses;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public string Name { get; }
|
|
80
|
+
|
|
81
|
+
public string Method { get; }
|
|
82
|
+
|
|
83
|
+
public string Path { get; }
|
|
84
|
+
|
|
85
|
+
public BodyKind? Body { get; }
|
|
86
|
+
|
|
87
|
+
public BodyKind? Result { get; }
|
|
88
|
+
|
|
89
|
+
public IReadOnlyDictionary<int, ErrorFactory> Errors { get; }
|
|
90
|
+
|
|
91
|
+
public IReadOnlyList<string> Uses { get; }
|
|
92
|
+
|
|
93
|
+
/// <summary>The first API version that has the operation; null for every version.</summary>
|
|
94
|
+
public string? Since { get; }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// <summary>One call's parameters; null query and header values are left out.</summary>
|
|
98
|
+
public sealed class CallArgs
|
|
99
|
+
{
|
|
100
|
+
public CallArgs(IReadOnlyList<KeyValuePair<string, Json>> path, IReadOnlyList<KeyValuePair<string, Json>> query, IReadOnlyList<KeyValuePair<string, Json>> headers, byte[]? body)
|
|
101
|
+
{
|
|
102
|
+
Path = path;
|
|
103
|
+
Query = query;
|
|
104
|
+
Headers = headers;
|
|
105
|
+
Body = body;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public IReadOnlyList<KeyValuePair<string, Json>> Path { get; }
|
|
109
|
+
|
|
110
|
+
public IReadOnlyList<KeyValuePair<string, Json>> Query { get; }
|
|
111
|
+
|
|
112
|
+
public IReadOnlyList<KeyValuePair<string, Json>> Headers { get; }
|
|
113
|
+
|
|
114
|
+
public byte[]? Body { get; }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// <summary>Changes one call.</summary>
|
|
118
|
+
public sealed class CallOptions
|
|
119
|
+
{
|
|
120
|
+
public long? TimeoutMs { get; set; }
|
|
121
|
+
|
|
122
|
+
public IReadOnlyList<KeyValuePair<string, string>> Headers { get; set; } = Array.Empty<KeyValuePair<string, string>>();
|
|
123
|
+
|
|
124
|
+
public string? BaseUrl { get; set; }
|
|
125
|
+
|
|
126
|
+
/// <summary>Called instead of the URL the path and query make (the next page a Link header named); see Links.Resolve.</summary>
|
|
127
|
+
public string? Url { get; set; }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// <summary>Everything a client shares between calls. Thread-safe.</summary>
|
|
131
|
+
public sealed class ClientRuntime
|
|
132
|
+
{
|
|
133
|
+
public ClientRuntime(string baseUrl, ITransport transport, IClock clock, IReadOnlyList<MiddlewareEntry> middleware, IReadOnlyDictionary<string, MiddlewareEntry> registry, IReadOnlyList<MiddlewareEntry> userMiddleware, IReadOnlyList<EventListener> listeners, long? timeoutMs, SdkInfo sdk, ApiVersion? apiVersion = null)
|
|
134
|
+
{
|
|
135
|
+
ApiVersion = apiVersion;
|
|
136
|
+
BaseUrl = baseUrl;
|
|
137
|
+
Transport = transport;
|
|
138
|
+
Clock = clock;
|
|
139
|
+
State = new State(clock);
|
|
140
|
+
Middleware = middleware;
|
|
141
|
+
Registry = registry;
|
|
142
|
+
UserMiddleware = userMiddleware;
|
|
143
|
+
Listeners = listeners;
|
|
144
|
+
TimeoutMs = timeoutMs;
|
|
145
|
+
Sdk = sdk;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public string BaseUrl { get; }
|
|
149
|
+
|
|
150
|
+
public ITransport Transport { get; }
|
|
151
|
+
|
|
152
|
+
public IClock Clock { get; }
|
|
153
|
+
|
|
154
|
+
public State State { get; }
|
|
155
|
+
|
|
156
|
+
public IReadOnlyList<MiddlewareEntry> Middleware { get; }
|
|
157
|
+
|
|
158
|
+
public IReadOnlyDictionary<string, MiddlewareEntry> Registry { get; }
|
|
159
|
+
|
|
160
|
+
public IReadOnlyList<MiddlewareEntry> UserMiddleware { get; }
|
|
161
|
+
|
|
162
|
+
public IReadOnlyList<EventListener> Listeners { get; }
|
|
163
|
+
|
|
164
|
+
public long? TimeoutMs { get; }
|
|
165
|
+
|
|
166
|
+
public SdkInfo Sdk { get; }
|
|
167
|
+
|
|
168
|
+
/// <summary>The API version the client is pinned to; null when the API has none.</summary>
|
|
169
|
+
public ApiVersion? ApiVersion { get; }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/// <summary>The API version a client is pinned to (Value), the header that carries it and every version, oldest first.</summary>
|
|
173
|
+
public sealed class ApiVersion
|
|
174
|
+
{
|
|
175
|
+
public ApiVersion(string header, string value, IReadOnlyList<string> values)
|
|
176
|
+
{
|
|
177
|
+
Header = header;
|
|
178
|
+
Value = value;
|
|
179
|
+
Values = values;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public string Header { get; }
|
|
183
|
+
|
|
184
|
+
public string Value { get; }
|
|
185
|
+
|
|
186
|
+
public IReadOnlyList<string> Values { get; }
|
|
187
|
+
|
|
188
|
+
/// <summary>Refuses an operation newer than the pinned version, or a version the SDK does not know.</summary>
|
|
189
|
+
public void Check(string operation, string? since)
|
|
190
|
+
{
|
|
191
|
+
var pinned = Values.ToList().IndexOf(Value);
|
|
192
|
+
if (pinned < 0)
|
|
193
|
+
{
|
|
194
|
+
throw new SdkArgumentException("unknown API version " + Value + "; this SDK knows " + string.Join(", ", Values));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (since != null && Values.ToList().IndexOf(since) > pinned)
|
|
198
|
+
{
|
|
199
|
+
throw new SdkArgumentException(operation + " needs API version " + since + " or later; this client uses " + Value);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/// <summary>A call's decoded data and the response it came from.</summary>
|
|
205
|
+
public sealed class RawResult<T>
|
|
206
|
+
{
|
|
207
|
+
public RawResult(T data, HttpResponse response)
|
|
208
|
+
{
|
|
209
|
+
Data = data;
|
|
210
|
+
Response = response;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
public T Data { get; }
|
|
214
|
+
|
|
215
|
+
public HttpResponse Response { get; }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/// <summary>Sends operations; generated clients call it.</summary>
|
|
219
|
+
public static class Call
|
|
220
|
+
{
|
|
221
|
+
private static HttpRequest BuildRequest(ClientRuntime runtime, OperationSpec spec, CallArgs args, CallOptions options)
|
|
222
|
+
{
|
|
223
|
+
var baseUrl = options.BaseUrl ?? runtime.BaseUrl;
|
|
224
|
+
var url = options.Url == null ? Url.Build(baseUrl, spec.Path, args.Path, args.Query) : Links.Resolve(baseUrl, options.Url);
|
|
225
|
+
var accept = spec.Result == BodyKind.Events ? "text/event-stream" : spec.Result == BodyKind.Bytes || spec.Result == BodyKind.Stream ? "*/*" : "application/json";
|
|
226
|
+
var request = new HttpRequest(spec.Method, url).WithHeader("accept", accept);
|
|
227
|
+
if (runtime.ApiVersion != null)
|
|
228
|
+
{
|
|
229
|
+
request = request.WithHeader(runtime.ApiVersion.Header, runtime.ApiVersion.Value);
|
|
230
|
+
}
|
|
231
|
+
if (spec.Body.HasValue && args.Body != null)
|
|
232
|
+
{
|
|
233
|
+
var contentType = spec.Body == BodyKind.Json ? "application/json" : spec.Body == BodyKind.Multipart ? "multipart/form-data; boundary=" + MultipartForm.BoundaryOf(args.Body) : "application/octet-stream";
|
|
234
|
+
request = request.WithHeader("content-type", contentType);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
foreach (var header in args.Headers.Where(header => !header.Value.IsNull))
|
|
238
|
+
{
|
|
239
|
+
request = request.WithHeader(header.Key, Url.Scalar(header.Value));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
foreach (var header in options.Headers)
|
|
243
|
+
{
|
|
244
|
+
request = request.WithHeader(header.Key, header.Value);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return request.WithBody(args.Body);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private static ApiException ErrorFor(OperationSpec spec, HttpResponse response)
|
|
251
|
+
{
|
|
252
|
+
Json body;
|
|
253
|
+
try
|
|
254
|
+
{
|
|
255
|
+
body = response.Json();
|
|
256
|
+
}
|
|
257
|
+
catch (SdkException)
|
|
258
|
+
{
|
|
259
|
+
body = Json.Str(response.Text());
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return spec.Errors.TryGetValue(response.Status, out var factory) ? factory(response.Status, body, response) : new ApiException("ApiError", response.Status, body, response);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
private static Task<HttpResponse> RunChain(IReadOnlyList<Middleware> handlers, int index, HttpRequest request, Context ctx, Next last) =>
|
|
266
|
+
index >= handlers.Count ? last(request) : handlers[index](request, next => RunChain(handlers, index + 1, next, ctx, last), ctx);
|
|
267
|
+
|
|
268
|
+
/// <summary>Sends one operation through the middleware chain; returns the 2xx response, or throws the error for any other.</summary>
|
|
269
|
+
public static async Task<HttpResponse> Send(ClientRuntime runtime, OperationSpec spec, CallArgs args, CallOptions? options, CancellationToken cancellationToken)
|
|
270
|
+
{
|
|
271
|
+
var settings = options ?? new CallOptions();
|
|
272
|
+
runtime.ApiVersion?.Check(spec.Name, spec.Since);
|
|
273
|
+
var request = BuildRequest(runtime, spec, args, settings);
|
|
274
|
+
var sendOptions = new SendOptions(settings.TimeoutMs ?? runtime.TimeoutMs, cancellationToken, spec.Result == BodyKind.Events);
|
|
275
|
+
Task<HttpResponse> Transmit(HttpRequest current) => runtime.Transport.Send(current, sendOptions);
|
|
276
|
+
var ctx = new Context(Array.Empty<KeyValuePair<string, Json>>(), new OperationInfo(spec.Name, spec.Method, spec.Path), runtime.Clock, runtime.State, runtime.Sdk, runtime.Listeners, Transmit, cancellationToken);
|
|
277
|
+
var handlers = runtime.Middleware.Select(entry => entry.Run).ToList();
|
|
278
|
+
foreach (var name in spec.Uses)
|
|
279
|
+
{
|
|
280
|
+
handlers.Add(runtime.Registry.TryGetValue(name, out var found) ? found.Run : throw new SdkException("unknown middleware " + name));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
handlers.AddRange(runtime.UserMiddleware.Select(entry => entry.Run));
|
|
284
|
+
var response = await RunChain(handlers, 0, request, ctx, Transmit).ConfigureAwait(false);
|
|
285
|
+
if (response.Status < 200 || response.Status > 299)
|
|
286
|
+
{
|
|
287
|
+
throw ErrorFor(spec, response);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return response;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// @end
|
|
295
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// sdkloom C# kit: the capabilities a middleware receives as ctx.
|
|
2
|
+
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections.Generic;
|
|
5
|
+
using System.Diagnostics;
|
|
6
|
+
using System.Linq;
|
|
7
|
+
using System.Threading;
|
|
8
|
+
using System.Threading.Tasks;
|
|
9
|
+
|
|
10
|
+
namespace Sdkloom.Runtime
|
|
11
|
+
{
|
|
12
|
+
// @block context.create
|
|
13
|
+
// @summary The capabilities a middleware gets through ctx: a clock, thread-safe per-client state with expiry, and events whose listeners can never break a call.
|
|
14
|
+
// @versions >=3
|
|
15
|
+
// @source [1] https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch
|
|
16
|
+
// @quote [1] "Provides a set of methods and properties that you can use to accurately measure elapsed time."
|
|
17
|
+
// @source [2] https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock
|
|
18
|
+
// @quote [2] "The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock."
|
|
19
|
+
// @retrieved 2026-12-05
|
|
20
|
+
// @tests tests/ClientTests.cs
|
|
21
|
+
// @requirement SDKL-PIPE-002
|
|
22
|
+
|
|
23
|
+
/// <summary>Time in milliseconds and a way to wait; tests use FakeClock.</summary>
|
|
24
|
+
public interface IClock
|
|
25
|
+
{
|
|
26
|
+
long Now();
|
|
27
|
+
|
|
28
|
+
Task Sleep(long ms, CancellationToken cancellationToken = default);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// <summary>Milliseconds on a monotonic clock.</summary>
|
|
32
|
+
public sealed class SystemClock : IClock
|
|
33
|
+
{
|
|
34
|
+
private readonly Stopwatch _watch = Stopwatch.StartNew();
|
|
35
|
+
|
|
36
|
+
public long Now() => _watch.ElapsedMilliseconds;
|
|
37
|
+
|
|
38
|
+
/// <summary>Waits ms milliseconds; Task.Delay takes at most int.MaxValue, so a longer wait waits until cancelled.</summary>
|
|
39
|
+
public Task Sleep(long ms, CancellationToken cancellationToken = default) =>
|
|
40
|
+
ms >= int.MaxValue ? Task.Delay(Timeout.Infinite, cancellationToken) : Task.Delay(TimeSpan.FromMilliseconds(Math.Max(0, ms)), cancellationToken);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// <summary>Per-client storage shared by all calls, e.g. for a cached token. Thread-safe.</summary>
|
|
44
|
+
public sealed class State
|
|
45
|
+
{
|
|
46
|
+
private readonly IClock _clock;
|
|
47
|
+
private readonly object _gate = new object();
|
|
48
|
+
private readonly Dictionary<string, (Json Value, long? Expires)> _entries = new Dictionary<string, (Json, long?)>();
|
|
49
|
+
|
|
50
|
+
public State(IClock clock)
|
|
51
|
+
{
|
|
52
|
+
_clock = clock;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// <summary>The value, or null when it is absent or expired.</summary>
|
|
56
|
+
public Json Get(string key)
|
|
57
|
+
{
|
|
58
|
+
lock (_gate)
|
|
59
|
+
{
|
|
60
|
+
if (!_entries.TryGetValue(key, out var entry))
|
|
61
|
+
{
|
|
62
|
+
return Json.Null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (entry.Expires.HasValue && _clock.Now() >= entry.Expires.Value)
|
|
66
|
+
{
|
|
67
|
+
_entries.Remove(key);
|
|
68
|
+
return Json.Null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return entry.Value;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public void Set(string key, Json value, long? ttlMs = null)
|
|
76
|
+
{
|
|
77
|
+
lock (_gate)
|
|
78
|
+
{
|
|
79
|
+
_entries[key] = (value, ttlMs.HasValue ? _clock.Now() + ttlMs.Value : (long?)null);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public void Delete(string key)
|
|
84
|
+
{
|
|
85
|
+
lock (_gate)
|
|
86
|
+
{
|
|
87
|
+
_entries.Remove(key);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/// <summary>Receives every event a middleware emits.</summary>
|
|
93
|
+
public delegate void EventListener(string name, Json fields);
|
|
94
|
+
|
|
95
|
+
/// <summary>Delivers events; a listener that throws is reported as sdkloom.listener_error, never propagated.</summary>
|
|
96
|
+
public static class Events
|
|
97
|
+
{
|
|
98
|
+
public const string ListenerError = "sdkloom.listener_error";
|
|
99
|
+
|
|
100
|
+
public static void Emit(IReadOnlyList<EventListener> listeners, string name, Json fields) => Deliver(listeners, name, fields, -1);
|
|
101
|
+
|
|
102
|
+
private static void Deliver(IReadOnlyList<EventListener> listeners, string name, Json fields, int skip)
|
|
103
|
+
{
|
|
104
|
+
var failures = new List<(int Index, string Message)>();
|
|
105
|
+
for (var index = 0; index < listeners.Count; index++)
|
|
106
|
+
{
|
|
107
|
+
if (index == skip)
|
|
108
|
+
{
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
try
|
|
113
|
+
{
|
|
114
|
+
listeners[index](name, fields);
|
|
115
|
+
}
|
|
116
|
+
catch (Exception error)
|
|
117
|
+
{
|
|
118
|
+
failures.Add((index, error.Message));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (name == ListenerError)
|
|
123
|
+
{
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
foreach (var (index, message) in failures)
|
|
128
|
+
{
|
|
129
|
+
Deliver(listeners, ListenerError, Json.Object(("event", Json.Str(name)), ("error", Json.Str(message))), index);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/// <summary>Names the operation being called.</summary>
|
|
135
|
+
public sealed class OperationInfo
|
|
136
|
+
{
|
|
137
|
+
public OperationInfo(string name, string method, string path)
|
|
138
|
+
{
|
|
139
|
+
Name = name;
|
|
140
|
+
Method = method;
|
|
141
|
+
Path = path;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public string Name { get; }
|
|
145
|
+
|
|
146
|
+
public string Method { get; }
|
|
147
|
+
|
|
148
|
+
public string Path { get; }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/// <summary>Which generated SDK a middleware runs in, e.g. for a User-Agent such as "acme-csharp/1.2.0".</summary>
|
|
152
|
+
public sealed class SdkInfo
|
|
153
|
+
{
|
|
154
|
+
public SdkInfo(string name, string version, string language)
|
|
155
|
+
{
|
|
156
|
+
Name = name;
|
|
157
|
+
Version = version;
|
|
158
|
+
Language = language;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public string Name { get; }
|
|
162
|
+
|
|
163
|
+
public string Version { get; }
|
|
164
|
+
|
|
165
|
+
public string Language { get; }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/// <summary>What a middleware gets as ctx.</summary>
|
|
169
|
+
public sealed class Context
|
|
170
|
+
{
|
|
171
|
+
private readonly IReadOnlyList<KeyValuePair<string, Json>> _config;
|
|
172
|
+
private readonly Func<HttpRequest, Task<HttpResponse>> _send;
|
|
173
|
+
|
|
174
|
+
public Context(IReadOnlyList<KeyValuePair<string, Json>> config, OperationInfo operation, IClock clock, State state, SdkInfo sdk, IReadOnlyList<EventListener> listeners, Func<HttpRequest, Task<HttpResponse>> send, CancellationToken cancellationToken)
|
|
175
|
+
{
|
|
176
|
+
_config = config;
|
|
177
|
+
Operation = operation;
|
|
178
|
+
Clock = clock;
|
|
179
|
+
State = state;
|
|
180
|
+
Sdk = sdk;
|
|
181
|
+
Listeners = listeners;
|
|
182
|
+
_send = send;
|
|
183
|
+
CancellationToken = cancellationToken;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
public OperationInfo Operation { get; }
|
|
187
|
+
|
|
188
|
+
public IClock Clock { get; }
|
|
189
|
+
|
|
190
|
+
public State State { get; }
|
|
191
|
+
|
|
192
|
+
public SdkInfo Sdk { get; }
|
|
193
|
+
|
|
194
|
+
public IReadOnlyList<EventListener> Listeners { get; }
|
|
195
|
+
|
|
196
|
+
/// <summary>Cancels the call; Sleep and Send observe it.</summary>
|
|
197
|
+
public CancellationToken CancellationToken { get; }
|
|
198
|
+
|
|
199
|
+
/// <summary>A config value; null when the field is not set.</summary>
|
|
200
|
+
public Json Config(string name) => _config.Where(entry => entry.Key == name).Select(entry => entry.Value).LastOrDefault() ?? Json.Null;
|
|
201
|
+
|
|
202
|
+
public void Emit(string name, Json fields) => Events.Emit(Listeners, name, fields);
|
|
203
|
+
|
|
204
|
+
/// <summary>Waits on the clock, observing the call's cancellation.</summary>
|
|
205
|
+
public Task Sleep(long ms) => Clock.Sleep(ms, CancellationToken);
|
|
206
|
+
|
|
207
|
+
/// <summary>Sends a request through the transport, without the middleware chain (e.g. to fetch a token).</summary>
|
|
208
|
+
public Task<HttpResponse> Send(HttpRequest request) => _send(request);
|
|
209
|
+
|
|
210
|
+
public HttpRequest Request(string method, string url) => new HttpRequest(method, url);
|
|
211
|
+
|
|
212
|
+
public Context WithConfig(IReadOnlyList<KeyValuePair<string, Json>> config) => new Context(config, Operation, Clock, State, Sdk, Listeners, _send, CancellationToken);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// @end
|
|
216
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// sdkloom C# kit: errors.
|
|
2
|
+
|
|
3
|
+
using System;
|
|
4
|
+
|
|
5
|
+
namespace Sdkloom.Runtime
|
|
6
|
+
{
|
|
7
|
+
// @block errors.api_error
|
|
8
|
+
// @summary One exception family for every failure: ApiException for an HTTP error response (with status, decoded body, request id and whether a retry may succeed; each declared error is a subclass), TransportException for a failed request, DecodeException for an unexpected response body, and SdkException for an error a middleware threw.
|
|
9
|
+
// @versions >=3
|
|
10
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-15.5
|
|
11
|
+
// @quote [1] "The 4xx (Client Error) class of status code indicates that the client seems to have erred."
|
|
12
|
+
// @source [2] https://www.rfc-editor.org/rfc/rfc9110#section-15.6
|
|
13
|
+
// @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."
|
|
14
|
+
// @retrieved 2026-12-05
|
|
15
|
+
// @tests tests/ClientTests.cs
|
|
16
|
+
// @requirement SDKL-ERR-001
|
|
17
|
+
|
|
18
|
+
/// <summary>Every failure of a call; Name is the declared error's name for HTTP errors, else "TransportError", "DecodeError" or "Error".</summary>
|
|
19
|
+
public class SdkException : Exception
|
|
20
|
+
{
|
|
21
|
+
public SdkException(string message)
|
|
22
|
+
: base(message)
|
|
23
|
+
{
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public SdkException(string message, Exception? inner)
|
|
27
|
+
: base(message, inner)
|
|
28
|
+
{
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public virtual string Name => "Error";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// <summary>A non-2xx response. Declared errors are subclasses, e.g. PaymentNotFoundException; others are ApiException with Name "ApiError".</summary>
|
|
35
|
+
public class ApiException : SdkException
|
|
36
|
+
{
|
|
37
|
+
private const int TooManyRequests = 429;
|
|
38
|
+
private const int FirstServerError = 500;
|
|
39
|
+
private readonly string _name;
|
|
40
|
+
|
|
41
|
+
/// <summary>How much of the body the message shows; Body has all of it.</summary>
|
|
42
|
+
private const int MessageBodyLength = 200;
|
|
43
|
+
|
|
44
|
+
public ApiException(string name, int status, Json body, HttpResponse response)
|
|
45
|
+
: base(Describe(name, status, body, response))
|
|
46
|
+
{
|
|
47
|
+
_name = name;
|
|
48
|
+
Status = status;
|
|
49
|
+
Body = body;
|
|
50
|
+
Response = response;
|
|
51
|
+
RequestId = response.Header("x-request-id");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public override string Name => _name;
|
|
55
|
+
|
|
56
|
+
/// <summary>"NotFound: HTTP 404: {"message":"no such payment"} (request id r1)": logs show what the server said.</summary>
|
|
57
|
+
private static string Describe(string name, int status, Json body, HttpResponse response)
|
|
58
|
+
{
|
|
59
|
+
var text = body.IsNull ? "" : body.Kind == JsonKind.String ? body.AsString() ?? "" : body.ToJsonString();
|
|
60
|
+
var shown = text.Length > MessageBodyLength ? text.Substring(0, MessageBodyLength) + "…" : text;
|
|
61
|
+
var requestId = response.Header("x-request-id");
|
|
62
|
+
return name + ": HTTP " + status + (shown.Length == 0 ? "" : ": " + shown) + (requestId == null ? "" : " (request id " + requestId + ")");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public int Status { get; }
|
|
66
|
+
|
|
67
|
+
/// <summary>The response body as JSON, or as a JSON string when it is not JSON.</summary>
|
|
68
|
+
public Json Body { get; }
|
|
69
|
+
|
|
70
|
+
public HttpResponse Response { get; }
|
|
71
|
+
|
|
72
|
+
public string? RequestId { get; }
|
|
73
|
+
|
|
74
|
+
/// <summary>True for 429 and 5xx: sending the same request again may succeed.</summary>
|
|
75
|
+
public bool Retryable => Status == TooManyRequests || Status >= FirstServerError;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// <summary>A call argument the SDK cannot send (a missing path parameter, a path parameter of "." or ".."), found before any request is made.</summary>
|
|
79
|
+
public sealed class SdkArgumentException : SdkException
|
|
80
|
+
{
|
|
81
|
+
public SdkArgumentException(string message)
|
|
82
|
+
: base(message)
|
|
83
|
+
{
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public override string Name => "ArgumentError";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// <summary>A request that got no response; Reason is "timeout" or "network".</summary>
|
|
90
|
+
public sealed class TransportException : SdkException
|
|
91
|
+
{
|
|
92
|
+
public TransportException(string reason, Exception? cause)
|
|
93
|
+
: base("TransportError: request failed: " + reason, cause)
|
|
94
|
+
{
|
|
95
|
+
Reason = reason;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public override string Name => "TransportError";
|
|
99
|
+
|
|
100
|
+
public string Reason { get; }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/// <summary>A JSON value that did not match the expected type, e.g. at "$.items[0].amount".</summary>
|
|
104
|
+
public sealed class DecodeException : SdkException
|
|
105
|
+
{
|
|
106
|
+
public DecodeException(string path, string problem)
|
|
107
|
+
: base(path + ": " + problem)
|
|
108
|
+
{
|
|
109
|
+
Path = path;
|
|
110
|
+
Problem = problem;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public override string Name => "DecodeError";
|
|
114
|
+
|
|
115
|
+
public string Path { get; }
|
|
116
|
+
|
|
117
|
+
public string Problem { get; }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// @end
|
|
121
|
+
}
|