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,61 @@
|
|
|
1
|
+
# sdkloom Python kit: iterating over every item of a paginated operation.
|
|
2
|
+
|
|
3
|
+
from __future__ import absolute_import, division, unicode_literals
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
try: # type names for annotations; Python 2.7 has no typing module
|
|
8
|
+
from typing import Any, Callable, Iterator, Optional, Sequence # noqa: F401
|
|
9
|
+
except ImportError: # pragma: no cover - Python 2.7
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
# @block pagination.iterate
|
|
13
|
+
# @summary Walks the pages of a paginated operation lazily: fetches a page, yields its items, asks for the next call's parameters (None ends), so callers write one for loop.
|
|
14
|
+
# @versions >=2.7
|
|
15
|
+
# @source [1] https://docs.python.org/3/glossary.html#term-generator-function
|
|
16
|
+
# @quote [1] "A function which returns a generator object. It looks like a normal function except that it contains yield expressions"
|
|
17
|
+
# @retrieved 2026-12-05
|
|
18
|
+
# @tests tests/test_pagination.py
|
|
19
|
+
# @requirement SDKL-PAGE-001
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def paginate(first, fetch, items, next_params):
|
|
23
|
+
# type: (Any, Callable[[Any], Any], Callable[[Any], Sequence[Any]], Callable[[Any, Any, Sequence[Any]], Optional[Any]]) -> Iterator[Any]
|
|
24
|
+
"""Every item of every page, fetched one page at a time as the caller iterates."""
|
|
25
|
+
params = first
|
|
26
|
+
while params is not None:
|
|
27
|
+
page = fetch(params)
|
|
28
|
+
found = items(page)
|
|
29
|
+
for item in found:
|
|
30
|
+
yield item
|
|
31
|
+
params = next_params(params, page, found)
|
|
32
|
+
# @end
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# @block pagination.link
|
|
36
|
+
# @summary Finds the next page's URL in a Link header (RFC 8288): splits link-values at commas outside <...>, reads the rel parameter (quoted or not, several relation types allowed, compared case-insensitively) and returns the target of rel="next".
|
|
37
|
+
# @versions >=2.7
|
|
38
|
+
# @source [1] https://www.rfc-editor.org/rfc/rfc8288#section-3
|
|
39
|
+
# @quote [1] "The Link header field provides a means for serialising one or more links into HTTP headers."
|
|
40
|
+
# @quote [1] "The rel parameter can, however, contain multiple link relation types."
|
|
41
|
+
# @quote [1] "MUST be compared character by character in a case-insensitive fashion."
|
|
42
|
+
# @retrieved 2026-12-06
|
|
43
|
+
# @tests tests/test_pagination.py
|
|
44
|
+
# @requirement SDKL-PAGE-001
|
|
45
|
+
|
|
46
|
+
_LINK_VALUE = re.compile(r"<([^>]*)>([^,]*)")
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def next_link(header):
|
|
50
|
+
# type: (Optional[str]) -> Optional[str]
|
|
51
|
+
"""The URL of the link with relation type "next" in a Link header, or None."""
|
|
52
|
+
for target, params in _LINK_VALUE.findall(header or ""):
|
|
53
|
+
for param in params.split(";"):
|
|
54
|
+
name, _, value = param.partition("=")
|
|
55
|
+
if name.strip().lower() != "rel" or not value:
|
|
56
|
+
continue
|
|
57
|
+
if "next" in value.strip().strip('"').lower().split():
|
|
58
|
+
return target
|
|
59
|
+
break
|
|
60
|
+
return None
|
|
61
|
+
# @end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# sdkloom Python kit: the middleware pipeline.
|
|
2
|
+
from __future__ import absolute_import, division, unicode_literals
|
|
3
|
+
|
|
4
|
+
try: # type names for annotations in generated code and type checkers; Python 2.7 has no typing module
|
|
5
|
+
from typing import Callable, Sequence # noqa: F401
|
|
6
|
+
|
|
7
|
+
from .http import HttpRequest, HttpResponse # noqa: F401
|
|
8
|
+
except ImportError: # pragma: no cover
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
# @block pipeline.run
|
|
12
|
+
# @summary Runs handlers in order around a final step; each handler may call next zero or more times.
|
|
13
|
+
# @versions >=2.7
|
|
14
|
+
# @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-15.6
|
|
15
|
+
# @quote [1] "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."
|
|
16
|
+
# @retrieved 2026-12-05
|
|
17
|
+
# @tests tests/test_client.py
|
|
18
|
+
# @requirement SDKL-PIPE-001
|
|
19
|
+
try:
|
|
20
|
+
Next = Callable[[HttpRequest], HttpResponse]
|
|
21
|
+
Handler = Callable[[HttpRequest, Next], HttpResponse]
|
|
22
|
+
except NameError: # pragma: no cover - Python 2.7
|
|
23
|
+
Next = Handler = object
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def run_pipeline(handlers, request, last):
|
|
27
|
+
# type: (Sequence[Handler], HttpRequest, Next) -> HttpResponse
|
|
28
|
+
def step(index):
|
|
29
|
+
# type: (int) -> Next
|
|
30
|
+
def call(current):
|
|
31
|
+
# type: (HttpRequest) -> HttpResponse
|
|
32
|
+
if index >= len(handlers):
|
|
33
|
+
return last(current)
|
|
34
|
+
return handlers[index](current, step(index + 1))
|
|
35
|
+
|
|
36
|
+
return call
|
|
37
|
+
|
|
38
|
+
return step(0)(request)
|
|
39
|
+
# @end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# sdkloom Python kit: reading a stream of server-sent events.
|
|
2
|
+
|
|
3
|
+
from __future__ import absolute_import, division, unicode_literals
|
|
4
|
+
|
|
5
|
+
import codecs
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
from .compat import Record, json_loads, message_of, raise_from
|
|
9
|
+
from .models import DecodeError, encode_json
|
|
10
|
+
|
|
11
|
+
try: # type names for annotations; Python 2.7 has no typing module
|
|
12
|
+
from typing import Any, Callable, Iterable, Iterator, Optional, Sequence # noqa: F401
|
|
13
|
+
except ImportError: # pragma: no cover - Python 2.7
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
# @block sse.parse
|
|
17
|
+
# @summary Reads a text/event-stream response as it arrives: splits lines on CRLF, LF or CR (also across chunks), collects event, data and id fields, dispatches on a blank line, drops an unfinished event at the end, and decodes each event's data (JSON for a typed stream, text for a string stream).
|
|
18
|
+
# @versions >=2.7
|
|
19
|
+
# @source [1] https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
20
|
+
# @quote [1] "Event streams in this format must always be encoded as UTF-8."
|
|
21
|
+
# @quote [1] "Once the end of the file is reached, any pending data must be discarded."
|
|
22
|
+
# @quote [1] "If value starts with a U+0020 SPACE"
|
|
23
|
+
# @source [2] https://docs.python.org/3/library/codecs.html#codecs.getincrementaldecoder
|
|
24
|
+
# @quote [2] "Look up the codec for the given encoding and return its incremental decoder class or factory function."
|
|
25
|
+
# @retrieved 2026-12-06
|
|
26
|
+
# @tests tests/test_sse.py
|
|
27
|
+
# @requirement SDKL-SSE-001
|
|
28
|
+
|
|
29
|
+
_LINE_END = re.compile(r"\r\n|\r|\n")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ServerSentEvent(Record):
|
|
33
|
+
"""One server-sent event: event (its type, "message" unless the server named one), id (the last event ID) and data."""
|
|
34
|
+
|
|
35
|
+
_fields = ("event", "id", "data")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class _Parser(object):
|
|
39
|
+
def __init__(self):
|
|
40
|
+
# type: () -> None
|
|
41
|
+
self.type = ""
|
|
42
|
+
self.data = ""
|
|
43
|
+
self.last_id = ""
|
|
44
|
+
|
|
45
|
+
def line(self, text):
|
|
46
|
+
# type: (str) -> Optional[ServerSentEvent]
|
|
47
|
+
if text == "":
|
|
48
|
+
if self.data == "":
|
|
49
|
+
self.type = ""
|
|
50
|
+
return None
|
|
51
|
+
data = self.data[:-1] if self.data.endswith("\n") else self.data
|
|
52
|
+
event = ServerSentEvent(self.type or "message", self.last_id, data)
|
|
53
|
+
self.type, self.data = "", ""
|
|
54
|
+
return event
|
|
55
|
+
if text.startswith(":"):
|
|
56
|
+
return None
|
|
57
|
+
field, colon, value = text.partition(":")
|
|
58
|
+
if colon and value.startswith(" "):
|
|
59
|
+
value = value[1:]
|
|
60
|
+
if field == "event":
|
|
61
|
+
self.type = value
|
|
62
|
+
elif field == "data":
|
|
63
|
+
self.data += value + "\n"
|
|
64
|
+
elif field == "id" and "\0" not in value:
|
|
65
|
+
self.last_id = value
|
|
66
|
+
return None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def parse_events(chunks):
|
|
70
|
+
# type: (Iterable[bytes]) -> Iterator[ServerSentEvent]
|
|
71
|
+
"""Parses the WHATWG event-stream format; yields each event as soon as its blank line arrives."""
|
|
72
|
+
decoder = codecs.getincrementaldecoder("utf-8")("replace")
|
|
73
|
+
parser = _Parser()
|
|
74
|
+
buffer = ""
|
|
75
|
+
first = True
|
|
76
|
+
final = False
|
|
77
|
+
chunks = iter(chunks)
|
|
78
|
+
while not final:
|
|
79
|
+
chunk = next(chunks, None)
|
|
80
|
+
final = chunk is None
|
|
81
|
+
buffer += decoder.decode(chunk or b"", final)
|
|
82
|
+
if first and buffer:
|
|
83
|
+
# One leading byte order mark is not part of the stream.
|
|
84
|
+
buffer = buffer[1:] if buffer.startswith("\ufeff") else buffer
|
|
85
|
+
first = False
|
|
86
|
+
while True:
|
|
87
|
+
match = _LINE_END.search(buffer)
|
|
88
|
+
# A CR at the end of what has arrived may be the first half of a CRLF.
|
|
89
|
+
if match is None or (match.group() == "\r" and match.end() == len(buffer) and not final):
|
|
90
|
+
break
|
|
91
|
+
event = parser.line(buffer[: match.start()])
|
|
92
|
+
buffer = buffer[match.end():]
|
|
93
|
+
if event is not None:
|
|
94
|
+
yield event
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def decode_events(events, decode):
|
|
98
|
+
# type: (Iterable[ServerSentEvent], Optional[Callable[[Any, str], Any]]) -> Iterator[ServerSentEvent]
|
|
99
|
+
"""Events whose data is read with the decoder after parsing it as JSON, or kept as text when there is no decoder."""
|
|
100
|
+
for event in events:
|
|
101
|
+
if decode is None:
|
|
102
|
+
yield event
|
|
103
|
+
continue
|
|
104
|
+
try:
|
|
105
|
+
value = json_loads(event.data)
|
|
106
|
+
except ValueError as error:
|
|
107
|
+
raise_from(DecodeError("$", "the event data is not JSON: " + message_of(error)), error)
|
|
108
|
+
yield ServerSentEvent(event.event, event.id, decode(value, "$"))
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def encode_events(events):
|
|
112
|
+
# type: (Sequence[ServerSentEvent]) -> bytes
|
|
113
|
+
"""Writes events in the event-stream format (mocks and tests); data that is not text is written as JSON."""
|
|
114
|
+
out = []
|
|
115
|
+
for event in events:
|
|
116
|
+
data = event.data if isinstance(event.data, type("")) else encode_json(event.data).decode("utf-8")
|
|
117
|
+
lines = ([] if event.event == "message" else ["event: " + event.event]) + ([] if event.id == "" else ["id: " + event.id])
|
|
118
|
+
lines += ["data: " + line for line in _LINE_END.split(data)]
|
|
119
|
+
out.append("\n".join(lines) + "\n\n")
|
|
120
|
+
return "".join(out).encode("utf-8")
|
|
121
|
+
# @end
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# sdkloom Python kit: test helpers shipped inside every generated SDK.
|
|
2
|
+
from __future__ import absolute_import, division, unicode_literals
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
from .compat import PY2, json_loads, urlsplit
|
|
7
|
+
from .http import HttpResponse
|
|
8
|
+
from .models import encode_json
|
|
9
|
+
from .sse import encode_events
|
|
10
|
+
|
|
11
|
+
try: # type names for type checkers; Python 2.7 has no typing module
|
|
12
|
+
from typing import Any, Callable, List, Optional, Sequence, Tuple # noqa: F401
|
|
13
|
+
|
|
14
|
+
from .http import HttpRequest # noqa: F401
|
|
15
|
+
from .models import Decoder # noqa: F401
|
|
16
|
+
from .transport import SendOptions # noqa: F401
|
|
17
|
+
except ImportError: # pragma: no cover
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
# @block testing.mock_transport
|
|
21
|
+
# @summary A transport for tests: answers requests from routes registered by method and path template, records every call and fails requests no route answers.
|
|
22
|
+
# @versions >=2.7
|
|
23
|
+
# @source [1] https://docs.python.org/3/library/json.html#json.dumps
|
|
24
|
+
# @quote [1] "Serialize obj to a JSON formatted str using this conversion table."
|
|
25
|
+
# @retrieved 2026-12-05
|
|
26
|
+
# @tests tests/test_testing.py
|
|
27
|
+
# @requirement SDKL-MOCK-001
|
|
28
|
+
try:
|
|
29
|
+
RequestPredicate = Callable[[HttpRequest], bool]
|
|
30
|
+
except NameError: # pragma: no cover - Python 2.7
|
|
31
|
+
RequestPredicate = object
|
|
32
|
+
_NO_JSON = object()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class MockRoute(object):
|
|
36
|
+
def __init__(self, transport, method, template, predicate=None):
|
|
37
|
+
# type: (MockTransport, str, str, Optional[RequestPredicate]) -> None
|
|
38
|
+
self.method = method
|
|
39
|
+
self.template = template
|
|
40
|
+
self._transport = transport
|
|
41
|
+
self._predicate = predicate or (lambda request: True)
|
|
42
|
+
# {name} matches one segment, {+name} one or more.
|
|
43
|
+
parts = re.split(r"(\{[^{}]+\})", template)
|
|
44
|
+
self._pattern = re.compile("^" + "".join(re.escape(part) if index % 2 == 0 else (".+" if part.startswith("{+") else "[^/]+") for index, part in enumerate(parts)) + "$")
|
|
45
|
+
self._answers = [] # type: List[Any]
|
|
46
|
+
self._calls = [] # type: List[HttpRequest]
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def answered(self):
|
|
50
|
+
# type: () -> bool
|
|
51
|
+
"""Whether the route has an answer yet."""
|
|
52
|
+
return bool(self._answers)
|
|
53
|
+
|
|
54
|
+
def matches(self, request):
|
|
55
|
+
# type: (HttpRequest) -> bool
|
|
56
|
+
return request.method == self.method and bool(self._pattern.match(urlsplit(request.url).path)) and self._predicate(request)
|
|
57
|
+
|
|
58
|
+
def reply(self, status=200, json=_NO_JSON, body=None, headers=()):
|
|
59
|
+
# type: (int, Any, Optional[bytes], Sequence[Tuple[str, str]]) -> MockRoute
|
|
60
|
+
"""Adds an answer. Answers are used in order; the last one repeats."""
|
|
61
|
+
if body is None:
|
|
62
|
+
body = b"" if json is _NO_JSON else encode_json(json)
|
|
63
|
+
content_type = () if json is _NO_JSON else (("content-type", "application/json"),)
|
|
64
|
+
self._answers.append(HttpResponse(status, content_type + tuple(headers), body))
|
|
65
|
+
return self
|
|
66
|
+
|
|
67
|
+
def reply_error(self, error):
|
|
68
|
+
# type: (BaseException) -> MockRoute
|
|
69
|
+
self._answers.append(error)
|
|
70
|
+
return self
|
|
71
|
+
|
|
72
|
+
def narrow(self, predicate):
|
|
73
|
+
# type: (RequestPredicate) -> MockRoute
|
|
74
|
+
"""A new route for the same operation that only answers requests matching predicate."""
|
|
75
|
+
return self._transport.route(self.method, self.template, predicate)
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def calls(self):
|
|
79
|
+
# type: () -> List[HttpRequest]
|
|
80
|
+
return list(self._calls)
|
|
81
|
+
|
|
82
|
+
def answer(self, request):
|
|
83
|
+
# type: (HttpRequest) -> HttpResponse
|
|
84
|
+
self._calls.append(request)
|
|
85
|
+
if not self._answers:
|
|
86
|
+
raise AssertionError("mock for " + self.method + " " + self.template + " has no answer")
|
|
87
|
+
answer = self._answers[min(len(self._calls), len(self._answers)) - 1]
|
|
88
|
+
if isinstance(answer, BaseException):
|
|
89
|
+
raise answer
|
|
90
|
+
return answer
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class MockTransport(object):
|
|
94
|
+
def __init__(self):
|
|
95
|
+
# type: () -> None
|
|
96
|
+
self._routes = [] # type: List[MockRoute]
|
|
97
|
+
self._calls = [] # type: List[HttpRequest]
|
|
98
|
+
|
|
99
|
+
def route(self, method, template, predicate=None):
|
|
100
|
+
# type: (str, str, Optional[RequestPredicate]) -> MockRoute
|
|
101
|
+
route = MockRoute(self, method, template, predicate)
|
|
102
|
+
self._routes.append(route)
|
|
103
|
+
return route
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def calls(self):
|
|
107
|
+
# type: () -> List[HttpRequest]
|
|
108
|
+
return list(self._calls)
|
|
109
|
+
|
|
110
|
+
def send(self, request, options):
|
|
111
|
+
# type: (HttpRequest, SendOptions) -> HttpResponse
|
|
112
|
+
"""The Transport function; routes added later take priority, and one with answers beats one without."""
|
|
113
|
+
self._calls.append(request)
|
|
114
|
+
matching = [route for route in reversed(self._routes) if route.matches(request)]
|
|
115
|
+
answered = [route for route in matching if route.answered]
|
|
116
|
+
if matching:
|
|
117
|
+
return (answered or matching)[0].answer(request)
|
|
118
|
+
raise AssertionError("no mock for " + request.method + " " + urlsplit(request.url).path)
|
|
119
|
+
# @end
|
|
120
|
+
|
|
121
|
+
# @block testing.operation_mock
|
|
122
|
+
# @summary A typed mock for one operation: returns() turns values into their wire form and checks them against the response type, fails() and raises() simulate errors, calls records requests.
|
|
123
|
+
# @versions >=2.7
|
|
124
|
+
# @source [1] https://docs.python.org/3/library/json.html#json.loads
|
|
125
|
+
# @quote [1] "deserialize s (a str, bytes or bytearray instance containing a JSON document) to a Python object"
|
|
126
|
+
# @retrieved 2026-12-05
|
|
127
|
+
# @tests tests/test_testing.py
|
|
128
|
+
# @requirement SDKL-MOCK-002
|
|
129
|
+
class OperationMock(object):
|
|
130
|
+
def __init__(self, name, route, decode=None, encode=None, events=False):
|
|
131
|
+
# type: (str, MockRoute, Optional[Decoder[Any]], Optional[Callable[[Any], Any]], bool) -> None
|
|
132
|
+
"""encode turns a value given to returns() into its JSON form, e.g. adding a union tag; None when no help is needed.
|
|
133
|
+
|
|
134
|
+
For an event stream (events=True) returns() takes a list of ServerSentEvent; decode and encode apply to each data.
|
|
135
|
+
"""
|
|
136
|
+
self.name = name
|
|
137
|
+
self._route = route
|
|
138
|
+
self._decode = decode
|
|
139
|
+
self._encode = encode
|
|
140
|
+
self._events = events
|
|
141
|
+
|
|
142
|
+
def returns(self, value):
|
|
143
|
+
# type: (Any) -> OperationMock
|
|
144
|
+
"""Answers with a success; the value is checked against the operation's response type first.
|
|
145
|
+
|
|
146
|
+
bytes answer a bytes operation as they are; on Python 2.7, where "abc" is bytes, pass a bytearray instead.
|
|
147
|
+
"""
|
|
148
|
+
if self._events:
|
|
149
|
+
return self._emit(value)
|
|
150
|
+
if isinstance(value, bytearray) or (isinstance(value, bytes) and not PY2):
|
|
151
|
+
self._route.reply(200, body=bytes(value))
|
|
152
|
+
return self
|
|
153
|
+
if value is not None and self._encode is not None:
|
|
154
|
+
value = self._encode(value)
|
|
155
|
+
if self._decode is not None:
|
|
156
|
+
self._decode(json_loads(encode_json(value).decode("utf-8")), "$")
|
|
157
|
+
self._route.reply(200, json=value) if value is not None else self._route.reply(200)
|
|
158
|
+
return self
|
|
159
|
+
|
|
160
|
+
def _emit(self, events):
|
|
161
|
+
# type: (Sequence[Any]) -> OperationMock
|
|
162
|
+
written = []
|
|
163
|
+
for event in events:
|
|
164
|
+
data = self._encode(event.data) if self._encode is not None else event.data
|
|
165
|
+
if self._decode is not None:
|
|
166
|
+
self._decode(json_loads(encode_json(data).decode("utf-8")), "$")
|
|
167
|
+
written.append(type(event)(event.event, event.id, data))
|
|
168
|
+
self._route.reply(200, body=encode_events(written), headers=(("content-type", "text/event-stream"),))
|
|
169
|
+
return self
|
|
170
|
+
|
|
171
|
+
def fails(self, status, body=_NO_JSON):
|
|
172
|
+
# type: (int, Any) -> OperationMock
|
|
173
|
+
"""Answers with an HTTP error status and an optional JSON body."""
|
|
174
|
+
self._route.reply(status, json=body)
|
|
175
|
+
return self
|
|
176
|
+
|
|
177
|
+
def raises(self, error):
|
|
178
|
+
# type: (BaseException) -> OperationMock
|
|
179
|
+
"""Makes the call fail without a response, e.g. a network error or a timeout."""
|
|
180
|
+
self._route.reply_error(error)
|
|
181
|
+
return self
|
|
182
|
+
|
|
183
|
+
@property
|
|
184
|
+
def then(self):
|
|
185
|
+
# type: () -> OperationMock
|
|
186
|
+
"""Reads naturally between answers: mock.op.fails(503).then.returns(value)."""
|
|
187
|
+
return self
|
|
188
|
+
|
|
189
|
+
def when(self, predicate):
|
|
190
|
+
# type: (RequestPredicate) -> OperationMock
|
|
191
|
+
"""A mock for the same operation that only answers requests matching predicate."""
|
|
192
|
+
return OperationMock(self.name, self._route.narrow(predicate), self._decode, self._encode, self._events)
|
|
193
|
+
|
|
194
|
+
@property
|
|
195
|
+
def calls(self):
|
|
196
|
+
# type: () -> List[HttpRequest]
|
|
197
|
+
return self._route.calls
|
|
198
|
+
# @end
|
|
199
|
+
|
|
200
|
+
# @block testing.fake_clock
|
|
201
|
+
# @summary A clock for tests that moves only when slept or advanced, so retries and expiries run instantly.
|
|
202
|
+
# @versions >=2.7
|
|
203
|
+
# @source [1] https://docs.python.org/3/library/time.html#time.monotonic
|
|
204
|
+
# @quote [1] "Return the value (in fractional seconds) of a monotonic clock, i.e. a clock that cannot go backwards."
|
|
205
|
+
# @retrieved 2026-12-05
|
|
206
|
+
# @tests tests/test_testing.py
|
|
207
|
+
# @requirement SDKL-MOCK-002
|
|
208
|
+
class FakeClock(object):
|
|
209
|
+
def __init__(self, start=0):
|
|
210
|
+
# type: (int) -> None
|
|
211
|
+
self._now = start
|
|
212
|
+
|
|
213
|
+
def now(self):
|
|
214
|
+
# type: () -> int
|
|
215
|
+
return self._now
|
|
216
|
+
|
|
217
|
+
def sleep(self, ms):
|
|
218
|
+
# type: (int) -> None
|
|
219
|
+
self._now += ms
|
|
220
|
+
|
|
221
|
+
def advance(self, ms):
|
|
222
|
+
# type: (int) -> None
|
|
223
|
+
self._now += ms
|
|
224
|
+
# @end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# sdkloom Python kit: the transport that sends requests.
|
|
2
|
+
from __future__ import absolute_import, division, unicode_literals
|
|
3
|
+
|
|
4
|
+
import socket
|
|
5
|
+
import ssl
|
|
6
|
+
|
|
7
|
+
from .compat import PY2, HTTPError, HTTPException, Record, URLError, raise_from, request, to_text
|
|
8
|
+
from .errors import TransportError
|
|
9
|
+
from .http import HttpResponse
|
|
10
|
+
|
|
11
|
+
try: # type names for type checkers; Python 2.7 has no typing module
|
|
12
|
+
from typing import Callable, Iterator, Optional # noqa: F401
|
|
13
|
+
|
|
14
|
+
from .http import HttpRequest # noqa: F401
|
|
15
|
+
except ImportError: # pragma: no cover
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
# @block transport.urllib
|
|
19
|
+
# @summary The default transport, built on urllib (urllib2 on Python 2.7) with timeouts, an optional proxy and an optional TLS context. Non-2xx responses are returned, not raised. A successful response's body is handed over as it arrives when the caller asks for a stream.
|
|
20
|
+
# @versions >=2.7
|
|
21
|
+
# @source [1] https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen
|
|
22
|
+
# @quote [1] "Open url, which can be either a string containing a valid, properly encoded URL, or a Request object."
|
|
23
|
+
# @source [2] https://docs.python.org/3/library/urllib.request.html#urllib.request.ProxyHandler
|
|
24
|
+
# @quote [2] "Cause requests to go through a proxy."
|
|
25
|
+
# @source [3] https://docs.python.org/3/library/ssl.html#ssl.create_default_context
|
|
26
|
+
# @quote [3] "Return a new SSLContext object with default settings for the given purpose."
|
|
27
|
+
# @source [4] https://docs.python.org/2.7/library/urllib2.html
|
|
28
|
+
# @quote [4] "Return a string indicating the HTTP request method."
|
|
29
|
+
# @retrieved 2026-12-05
|
|
30
|
+
# @tests tests/test_transport.py
|
|
31
|
+
# @requirement SDKL-HTTP-004
|
|
32
|
+
class SendOptions(Record):
|
|
33
|
+
"""timeout_ms: the time a request may take, or None for no limit; for a streamed response, the longest wait for the
|
|
34
|
+
headers and then for each piece of the body. stream: hand a successful response's body over as it arrives."""
|
|
35
|
+
|
|
36
|
+
_fields = ("timeout_ms", "stream")
|
|
37
|
+
_defaults = {"timeout_ms": None, "stream": False}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
Transport = Callable[[HttpRequest, SendOptions], HttpResponse]
|
|
42
|
+
except NameError: # pragma: no cover - Python 2.7
|
|
43
|
+
Transport = object
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _is_timeout(error):
|
|
47
|
+
# type: (BaseException) -> bool
|
|
48
|
+
return isinstance(error, socket.timeout) or (isinstance(error, URLError) and isinstance(error.reason, socket.timeout))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _headers(message):
|
|
52
|
+
# type: (object) -> tuple
|
|
53
|
+
if PY2: # pragma: no cover - mimetools.Message keeps the raw lines
|
|
54
|
+
return tuple((line.split(":", 1)[0].strip(), line.split(":", 1)[1].strip()) for line in message.headers if ":" in line)
|
|
55
|
+
return tuple(message.items())
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _status(response):
|
|
59
|
+
# type: (object) -> int
|
|
60
|
+
status = getattr(response, "status", None) # Python 3; urllib2 responses only have getcode()
|
|
61
|
+
return status if status is not None else response.getcode()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class UrllibTransport(object):
|
|
65
|
+
"""Sends requests with the standard library. Safe to share between threads."""
|
|
66
|
+
|
|
67
|
+
def __init__(self, proxy=None, ssl_context=None):
|
|
68
|
+
# type: (Optional[str], Optional[ssl.SSLContext]) -> None
|
|
69
|
+
if not hasattr(ssl, "create_default_context"): # pragma: no cover - Python 2.7.8 and older
|
|
70
|
+
raise RuntimeError("the default transport needs Python 2.7.9 or later for TLS 1.2, SNI and certificate checks")
|
|
71
|
+
handlers = [request.HTTPSHandler(context=ssl_context or ssl.create_default_context())]
|
|
72
|
+
handlers.append(request.ProxyHandler({"http": proxy, "https": proxy} if proxy is not None else {}))
|
|
73
|
+
self._opener = request.build_opener(*handlers)
|
|
74
|
+
|
|
75
|
+
def __call__(self, http_request, options):
|
|
76
|
+
# type: (HttpRequest, SendOptions) -> HttpResponse
|
|
77
|
+
raw = request.Request(_native(http_request.url), data=http_request.body, headers=dict((_native(name), _native(value)) for name, value in http_request.headers))
|
|
78
|
+
method = _native(http_request.method)
|
|
79
|
+
raw.get_method = lambda: method
|
|
80
|
+
timeout = None if options.timeout_ms is None else options.timeout_ms / 1000
|
|
81
|
+
try:
|
|
82
|
+
response = self._opener.open(raw, timeout=timeout)
|
|
83
|
+
if options.stream:
|
|
84
|
+
return HttpResponse(_status(response), _headers(response.info()), b"", stream=_chunks(response))
|
|
85
|
+
try:
|
|
86
|
+
return HttpResponse(_status(response), _headers(response.info()), response.read())
|
|
87
|
+
finally:
|
|
88
|
+
response.close()
|
|
89
|
+
except HTTPError as error:
|
|
90
|
+
try:
|
|
91
|
+
return HttpResponse(error.code, _headers(error.info()), error.read())
|
|
92
|
+
finally:
|
|
93
|
+
error.close()
|
|
94
|
+
# HTTPException: a connection closed before or during the response (BadStatusLine on 2.7, IncompleteRead).
|
|
95
|
+
except (URLError, HTTPException, socket.error, IOError, OSError) as error:
|
|
96
|
+
raise_from(TransportError("timeout" if _is_timeout(error) else "network", error), error)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _chunks(response):
|
|
100
|
+
# type: (object) -> Iterator[bytes]
|
|
101
|
+
"""The body as it arrives. read1 returns what is there instead of waiting for a full buffer; Python 2.7 reads lines."""
|
|
102
|
+
read = getattr(response, "read1", None)
|
|
103
|
+
try:
|
|
104
|
+
while True:
|
|
105
|
+
try:
|
|
106
|
+
chunk = read(8192) if read is not None else response.readline()
|
|
107
|
+
except (HTTPException, socket.error, IOError, OSError) as error:
|
|
108
|
+
raise_from(TransportError("timeout" if _is_timeout(error) else "network", error), error)
|
|
109
|
+
if not chunk:
|
|
110
|
+
return
|
|
111
|
+
yield chunk
|
|
112
|
+
finally:
|
|
113
|
+
response.close()
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _native(text):
|
|
117
|
+
# type: (str) -> str
|
|
118
|
+
"""The str type urllib wants: bytes on Python 2.7, text on 3."""
|
|
119
|
+
return to_text(text).encode("utf-8") if PY2 else text
|
|
120
|
+
# @end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# sdkloom Python kit: waiting for a long-running operation.
|
|
2
|
+
|
|
3
|
+
from __future__ import absolute_import, division, unicode_literals
|
|
4
|
+
|
|
5
|
+
try: # type names for annotations; Python 2.7 has no typing module
|
|
6
|
+
from typing import Any, Callable, TypeVar # noqa: F401
|
|
7
|
+
|
|
8
|
+
R = TypeVar("R")
|
|
9
|
+
except ImportError: # pragma: no cover - Python 2.7
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
# @block lro.wait
|
|
13
|
+
# @summary Waits for a long-running operation: polls until the result says the work is done, sleeping on the client's clock between polls, and raises WaitTimeoutError when the next poll would come after the deadline.
|
|
14
|
+
# @versions >=2.7
|
|
15
|
+
# @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-15.3.3
|
|
16
|
+
# @quote [1] "The 202 (Accepted) status code indicates that the request has been accepted for processing, but the processing has not been completed."
|
|
17
|
+
# @retrieved 2026-12-06
|
|
18
|
+
# @tests tests/test_wait.py
|
|
19
|
+
# @requirement SDKL-WAIT-001
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class WaitTimeoutError(Exception):
|
|
23
|
+
"""The work did not finish within the wait's timeout; last is the last result read."""
|
|
24
|
+
|
|
25
|
+
name = "WaitTimeoutError"
|
|
26
|
+
|
|
27
|
+
def __init__(self, message, last):
|
|
28
|
+
# type: (str, Any) -> None
|
|
29
|
+
super(WaitTimeoutError, self).__init__(message)
|
|
30
|
+
self.last = last
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def wait_until(first, poll, done, clock, interval_ms, timeout_ms):
|
|
34
|
+
# type: (R, Callable[[R], R], Callable[[R], bool], Any, int, int) -> R
|
|
35
|
+
"""Polls until done(result); the first result comes from starting the work."""
|
|
36
|
+
deadline = clock.now() + timeout_ms
|
|
37
|
+
current = first
|
|
38
|
+
while not done(current):
|
|
39
|
+
if clock.now() + interval_ms > deadline:
|
|
40
|
+
raise WaitTimeoutError("the work did not finish within %d ms" % timeout_ms, current)
|
|
41
|
+
clock.sleep(interval_ms)
|
|
42
|
+
current = poll(current)
|
|
43
|
+
return current
|
|
44
|
+
# @end
|