From ddfd1f19754d5566b0c22ccc1643d46ed6e3b105 Mon Sep 17 00:00:00 2001 From: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com> Date: Tue, 31 Mar 2026 18:59:09 +0000 Subject: [PATCH 1/6] feat: support optional username and password in basic auth across all generators Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../src/root-client/RootClientGenerator.ts | 4 +-- generators/csharp/sdk/versions.yml | 12 +++++++++ .../auth/BasicAuthProviderGenerator.java | 6 ++--- generators/java/sdk/versions.yml | 11 ++++++++ .../src/root-client/RootClientGenerator.ts | 4 +-- generators/php/sdk/versions.yml | 12 +++++++++ generators/python/sdk/versions.yml | 12 +++++++++ .../client_wrapper_generator.py | 10 +++---- .../src/root-client/RootClientGenerator.ts | 8 +++--- generators/ruby-v2/sdk/versions.yml | 12 +++++++++ .../BasicAuthProviderGenerator.ts | 22 +++++----------- generators/typescript/sdk/versions.yml | 12 +++++++++ .../core-utilities/src/core/auth/BasicAuth.ts | 11 +++++--- .../tests/unit/auth/BasicAuth.test.ts | 26 ++++++++++++++++--- 14 files changed, 124 insertions(+), 38 deletions(-) diff --git a/generators/csharp/sdk/src/root-client/RootClientGenerator.ts b/generators/csharp/sdk/src/root-client/RootClientGenerator.ts index 12e7511588ff..d43bfb7bb8c8 100644 --- a/generators/csharp/sdk/src/root-client/RootClientGenerator.ts +++ b/generators/csharp/sdk/src/root-client/RootClientGenerator.ts @@ -470,11 +470,11 @@ export class RootClientGenerator extends FileGenerator 1) { innerWriter.endControlFlow(); diff --git a/generators/csharp/sdk/versions.yml b/generators/csharp/sdk/versions.yml index 3da02cfc0193..f8a34f6076fd 100644 --- a/generators/csharp/sdk/versions.yml +++ b/generators/csharp/sdk/versions.yml @@ -1,4 +1,16 @@ # yaml-language-server: $schema=../../../fern-versions-yml.schema.json +- version: 2.55.3 + changelogEntry: + - summary: | + Support optional username and password in basic auth. The SDK now accepts + username-only, password-only, or both credentials. Missing fields are treated + as empty strings (e.g., username-only encodes `username:`, password-only + encodes `:password`). When neither is provided, the Authorization header is + omitted entirely. + type: feat + createdAt: "2026-03-31" + irVersion: 65 + - version: 2.55.2 changelogEntry: - summary: | diff --git a/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java b/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java index 916a0609ba53..e633e099c085 100644 --- a/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java +++ b/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java @@ -121,10 +121,10 @@ private MethodSpec buildGetAuthHeaders( .returns(ParameterizedTypeName.get(Map.class, String.class, String.class)) .addStatement("String username = $N.get()", usernameSupplierField) .addStatement("String password = $N.get()", passwordSupplierField) - .beginControlFlow("if (username == null || password == null)") + .beginControlFlow("if (username == null && password == null)") .addStatement("throw new $T(AUTH_CONFIG_ERROR_MESSAGE)", RuntimeException.class) .endControlFlow() - .addStatement("String credentials = username + \":\" + password") + .addStatement("String credentials = (username != null ? username : \"\") + \":\" + (password != null ? password : \"\")") .addStatement( "String encoded = $T.getEncoder().encodeToString(credentials.getBytes($T.UTF_8))", Base64.class, @@ -151,7 +151,7 @@ private MethodSpec buildCanCreateMethod(String usernameEnvVar, String passwordEn if (usernameEnvVar != null) { condition.append(" || System.getenv(\"").append(usernameEnvVar).append("\") != null"); } - condition.append(") && (passwordSupplier != null"); + condition.append(") || (passwordSupplier != null"); if (passwordEnvVar != null) { condition.append(" || System.getenv(\"").append(passwordEnvVar).append("\") != null"); } diff --git a/generators/java/sdk/versions.yml b/generators/java/sdk/versions.yml index 93e66796ccc4..f4cdd9340662 100644 --- a/generators/java/sdk/versions.yml +++ b/generators/java/sdk/versions.yml @@ -1,4 +1,15 @@ # yaml-language-server: $schema=../../../fern-versions-yml.schema.json +- version: 4.0.10 + changelogEntry: + - summary: | + Support optional username and password in basic auth. The SDK now accepts + username-only, password-only, or both credentials. Missing fields are treated + as empty strings (e.g., username-only encodes `username:`, password-only + encodes `:password`). When neither is provided, a RuntimeException is thrown. + type: feat + createdAt: "2026-03-31" + irVersion: 65 + - version: 4.0.9 changelogEntry: - summary: | diff --git a/generators/php/sdk/src/root-client/RootClientGenerator.ts b/generators/php/sdk/src/root-client/RootClientGenerator.ts index 5b39fa323a2f..8e231367880f 100644 --- a/generators/php/sdk/src/root-client/RootClientGenerator.ts +++ b/generators/php/sdk/src/root-client/RootClientGenerator.ts @@ -360,11 +360,11 @@ export class RootClientGenerator extends FileGenerator 1) { writer.endControlFlow(); diff --git a/generators/php/sdk/versions.yml b/generators/php/sdk/versions.yml index c5db51bb94ff..613468c103d3 100644 --- a/generators/php/sdk/versions.yml +++ b/generators/php/sdk/versions.yml @@ -1,4 +1,16 @@ # yaml-language-server: $schema=../../../fern-versions-yml.schema.json +- version: 2.2.7 + changelogEntry: + - summary: | + Support optional username and password in basic auth. The SDK now accepts + username-only, password-only, or both credentials. Missing fields are treated + as empty strings (e.g., username-only encodes `username:`, password-only + encodes `:password`). When neither is provided, the Authorization header is + omitted entirely. + type: feat + createdAt: "2026-03-31" + irVersion: 62 + - version: 2.2.6 changelogEntry: - summary: | diff --git a/generators/python/sdk/versions.yml b/generators/python/sdk/versions.yml index b23f9bf014f1..508756ea4891 100644 --- a/generators/python/sdk/versions.yml +++ b/generators/python/sdk/versions.yml @@ -1,5 +1,17 @@ # yaml-language-server: $schema=../../../fern-versions-yml.schema.json # For unreleased changes, use unreleased.yml +- version: 5.1.4 + changelogEntry: + - summary: | + Support optional username and password in basic auth. The SDK now accepts + username-only, password-only, or both credentials. Missing fields are treated + as empty strings (e.g., username-only encodes `username:`, password-only + encodes `:password`). When neither is provided, the Authorization header is + omitted entirely. + type: feat + createdAt: "2026-03-31" + irVersion: 65 + - version: 5.1.3 changelogEntry: - summary: | diff --git a/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py b/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py index 1b22151cb9d2..308514985ba9 100644 --- a/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py +++ b/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py @@ -528,15 +528,15 @@ def _write_get_headers_body(writer: AST.NodeWriter) -> None: password_var = names.get_password_constructor_parameter_name(basic_auth_scheme) writer.write_line(f"{username_var} = self.{names.get_username_getter_name(basic_auth_scheme)}()") writer.write_line(f"{password_var} = self.{names.get_password_getter_name(basic_auth_scheme)}()") - writer.write_line(f"if {username_var} is not None and {password_var} is not None:") + writer.write_line(f"if {username_var} is not None or {password_var} is not None:") with writer.indent(): writer.write(f'headers["{ClientWrapperGenerator.AUTHORIZATION_HEADER}"] = ') writer.write_node( AST.ClassInstantiation( class_=httpx.HttpX.BASIC_AUTH, args=[ - AST.Expression(f"{username_var}"), - AST.Expression(f"{password_var}"), + AST.Expression(f'{username_var} or ""'), + AST.Expression(f'{password_var} or ""'), ], ) ) @@ -548,8 +548,8 @@ def _write_get_headers_body(writer: AST.NodeWriter) -> None: AST.ClassInstantiation( class_=httpx.HttpX.BASIC_AUTH, args=[ - AST.Expression(f"self.{names.get_username_getter_name(basic_auth_scheme)}()"), - AST.Expression(f"self.{names.get_password_getter_name(basic_auth_scheme)}()"), + AST.Expression(f"self.{names.get_username_getter_name(basic_auth_scheme)}() or \"\""), + AST.Expression(f"self.{names.get_password_getter_name(basic_auth_scheme)}() or \"\""), ], ) ) diff --git a/generators/ruby-v2/sdk/src/root-client/RootClientGenerator.ts b/generators/ruby-v2/sdk/src/root-client/RootClientGenerator.ts index 8bd1772c05d4..f658b9e3b974 100644 --- a/generators/ruby-v2/sdk/src/root-client/RootClientGenerator.ts +++ b/generators/ruby-v2/sdk/src/root-client/RootClientGenerator.ts @@ -124,19 +124,19 @@ export class RootClientGenerator extends FileGenerator 1) { if (i === 0) { - writer.writeLine(`if !${usernameName}.nil? && !${passwordName}.nil?`); + writer.writeLine(`if !${usernameName}.nil? || !${passwordName}.nil?`); } else { - writer.writeLine(`elsif !${usernameName}.nil? && !${passwordName}.nil?`); + writer.writeLine(`elsif !${usernameName}.nil? || !${passwordName}.nil?`); } writer.writeLine( - ` headers["Authorization"] = "Basic #{Base64.strict_encode64("#{${usernameName}}:#{${passwordName}}")}"` + ` headers["Authorization"] = "Basic #{Base64.strict_encode64("#{${usernameName} || ""}:#{${passwordName} || ""}")}"` ); if (i === basicAuthSchemes.length - 1) { writer.writeLine(`end`); } } else { writer.writeLine( - `headers["Authorization"] = "Basic #{Base64.strict_encode64("#{${usernameName}}:#{${passwordName}}")}"` + `headers["Authorization"] = "Basic #{Base64.strict_encode64("#{${usernameName} || ""}:#{${passwordName} || ""}")}"` ); } } diff --git a/generators/ruby-v2/sdk/versions.yml b/generators/ruby-v2/sdk/versions.yml index 4153536705a4..20591725adac 100644 --- a/generators/ruby-v2/sdk/versions.yml +++ b/generators/ruby-v2/sdk/versions.yml @@ -1,5 +1,17 @@ # yaml-language-server: $schema=../../../fern-versions-yml.schema.json +- version: 1.1.12 + changelogEntry: + - summary: | + Support optional username and password in basic auth. The SDK now accepts + username-only, password-only, or both credentials. Missing fields are treated + as empty strings (e.g., username-only encodes `username:`, password-only + encodes `:password`). When neither is provided, the Authorization header is + omitted entirely. + type: feat + createdAt: "2026-03-31" + irVersion: 61 + - version: 1.1.11 changelogEntry: - summary: | diff --git a/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts b/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts index 8a0b8328a7f6..fb745edc8f39 100644 --- a/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts +++ b/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts @@ -253,7 +253,7 @@ export class BasicAuthProviderGenerator implements AuthProviderGenerator { const usernameEnvCheck = usernameEnvVar != null ? " || process.env?.[ENV_USERNAME] != null" : ""; const passwordEnvCheck = passwordEnvVar != null ? " || process.env?.[ENV_PASSWORD] != null" : ""; - return `return (options?.${wrapperAccess}[USERNAME_PARAM] != null${usernameEnvCheck}) && (options?.${wrapperAccess}[PASSWORD_PARAM] != null${passwordEnvCheck});`; + return `return (options?.${wrapperAccess}[USERNAME_PARAM] != null${usernameEnvCheck}) || (options?.${wrapperAccess}[PASSWORD_PARAM] != null${passwordEnvCheck});`; } private generateGetAuthRequestStatements(context: SdkContext): string { @@ -319,15 +319,11 @@ export class BasicAuthProviderGenerator implements AuthProviderGenerator { : passwordSupplierGetCode; if (this.neverThrowErrors) { - // When neverThrowErrors is true, return empty headers if credentials are missing + // When neverThrowErrors is true, return empty headers if neither credential is provided return ` const ${usernameVar} = ${usernameEnvFallback}; - if (${usernameVar} == null) { - return { headers: {} }; - } - const ${passwordVar} = ${passwordEnvFallback}; - if (${passwordVar} == null) { + if (${usernameVar} == null && ${passwordVar} == null) { return { headers: {} }; } @@ -343,23 +339,17 @@ export class BasicAuthProviderGenerator implements AuthProviderGenerator { }; `; } else { - // When neverThrowErrors is false, throw an error if credentials are missing + // When neverThrowErrors is false, throw an error only if neither credential is provided const errorConstructor = getTextOfTsNode( context.genericAPISdkError.getReferenceToGenericAPISdkError().getExpression() ); return ` const ${usernameVar} = ${usernameEnvFallback}; - if (${usernameVar} == null) { - throw new ${errorConstructor}({ - message: ${CLASS_NAME}.AUTH_CONFIG_ERROR_MESSAGE_USERNAME, - }); - } - const ${passwordVar} = ${passwordEnvFallback}; - if (${passwordVar} == null) { + if (${usernameVar} == null && ${passwordVar} == null) { throw new ${errorConstructor}({ - message: ${CLASS_NAME}.AUTH_CONFIG_ERROR_MESSAGE_PASSWORD, + message: ${CLASS_NAME}.AUTH_CONFIG_ERROR_MESSAGE, }); } diff --git a/generators/typescript/sdk/versions.yml b/generators/typescript/sdk/versions.yml index 3ad8d1366bef..cad703996fef 100644 --- a/generators/typescript/sdk/versions.yml +++ b/generators/typescript/sdk/versions.yml @@ -1,4 +1,16 @@ # yaml-language-server: $schema=../../../fern-versions-yml.schema.json +- version: 3.60.7 + changelogEntry: + - summary: | + Support optional username and password in basic auth. The SDK now accepts + username-only, password-only, or both credentials. Missing fields are treated + as empty strings (e.g., username-only encodes `username:`, password-only + encodes `:password`). When neither is provided, the Authorization header is + omitted entirely. + type: feat + createdAt: "2026-03-31" + irVersion: 65 + - version: 3.60.6 changelogEntry: - summary: | diff --git a/generators/typescript/utils/core-utilities/src/core/auth/BasicAuth.ts b/generators/typescript/utils/core-utilities/src/core/auth/BasicAuth.ts index c6efa5e2652b..60d55148568b 100644 --- a/generators/typescript/utils/core-utilities/src/core/auth/BasicAuth.ts +++ b/generators/typescript/utils/core-utilities/src/core/auth/BasicAuth.ts @@ -1,8 +1,8 @@ import { base64Decode, base64Encode } from "../base64"; export interface BasicAuth { - username: string; - password: string; + username?: string; + password?: string; } const BASIC_AUTH_HEADER_PREFIX = /^Basic /i; @@ -12,7 +12,12 @@ export const BasicAuth = { if (basicAuth == null) { return undefined; } - const token = base64Encode(`${basicAuth.username}:${basicAuth.password}`); + const username = basicAuth.username ?? ""; + const password = basicAuth.password ?? ""; + if (username === "" && password === "") { + return undefined; + } + const token = base64Encode(`${username}:${password}`); return `Basic ${token}`; }, fromAuthorizationHeader: (header: string): BasicAuth => { diff --git a/generators/typescript/utils/core-utilities/tests/unit/auth/BasicAuth.test.ts b/generators/typescript/utils/core-utilities/tests/unit/auth/BasicAuth.test.ts index 9b5123364c47..8c82c1b723db 100644 --- a/generators/typescript/utils/core-utilities/tests/unit/auth/BasicAuth.test.ts +++ b/generators/typescript/utils/core-utilities/tests/unit/auth/BasicAuth.test.ts @@ -3,8 +3,8 @@ import { BasicAuth } from "../../../src/core/auth/BasicAuth"; describe("BasicAuth", () => { interface ToHeaderTestCase { description: string; - input: { username: string; password: string }; - expected: string; + input: { username?: string; password?: string }; + expected: string | undefined; } interface FromHeaderTestCase { @@ -22,10 +22,30 @@ describe("BasicAuth", () => { describe("toAuthorizationHeader", () => { const toHeaderTests: ToHeaderTestCase[] = [ { - description: "correctly converts to header", + description: "correctly converts to header with both username and password", input: { username: "username", password: "password" }, expected: "Basic dXNlcm5hbWU6cGFzc3dvcmQ=", }, + { + description: "encodes username only with trailing colon", + input: { username: "username" }, + expected: "Basic dXNlcm5hbWU6", + }, + { + description: "encodes password only with leading colon", + input: { password: "password" }, + expected: "Basic OnBhc3N3b3Jk", + }, + { + description: "returns undefined when neither provided", + input: {}, + expected: undefined, + }, + { + description: "returns undefined when both are empty strings", + input: { username: "", password: "" }, + expected: undefined, + }, ]; toHeaderTests.forEach(({ description, input, expected }) => { From 072175fc9987f4303005970cd693cd76b27a5472 Mon Sep 17 00:00:00 2001 From: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com> Date: Tue, 31 Mar 2026 19:12:47 +0000 Subject: [PATCH 2/6] fix: make optional basic auth fields conditional on usernameOmit/passwordOmit IR flags Default behavior (both required) is preserved. Optional handling only applies when usernameOmit or passwordOmit is explicitly set in the IR. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../src/root-client/RootClientGenerator.ts | 20 +++-- .../auth/BasicAuthProviderGenerator.java | 32 ++++++-- .../src/root-client/RootClientGenerator.ts | 20 +++-- .../client_wrapper_generator.py | 57 ++++++++++---- .../src/root-client/RootClientGenerator.ts | 13 +++- .../BasicAuthProviderGenerator.ts | 74 +++++++++++++++++-- 6 files changed, 173 insertions(+), 43 deletions(-) diff --git a/generators/csharp/sdk/src/root-client/RootClientGenerator.ts b/generators/csharp/sdk/src/root-client/RootClientGenerator.ts index d43bfb7bb8c8..6e9590ba7846 100644 --- a/generators/csharp/sdk/src/root-client/RootClientGenerator.ts +++ b/generators/csharp/sdk/src/root-client/RootClientGenerator.ts @@ -466,16 +466,24 @@ export class RootClientGenerator extends FileGenerator 1) { const controlFlowKeyword = i === 0 ? "if" : "else if"; - innerWriter.controlFlow( - controlFlowKeyword, - this.csharp.codeblock(`${usernameAccess} != null || ${passwordAccess} != null`) + innerWriter.controlFlow(controlFlowKeyword, this.csharp.codeblock(condition)); + } + if (eitherOmitted) { + innerWriter.writeTextStatement( + `clientOptionsWithAuth.Headers["Authorization"] = $"Basic {Convert.ToBase64String(global::System.Text.Encoding.UTF8.GetBytes($"{${usernameAccess} ?? ""}:{${passwordAccess} ?? ""}"))}"` + ); + } else { + innerWriter.writeTextStatement( + `clientOptionsWithAuth.Headers["Authorization"] = $"Basic {Convert.ToBase64String(global::System.Text.Encoding.UTF8.GetBytes($"{${usernameAccess}}:{${passwordAccess}}"))}"` ); } - innerWriter.writeTextStatement( - `clientOptionsWithAuth.Headers["Authorization"] = $"Basic {Convert.ToBase64String(global::System.Text.Encoding.UTF8.GetBytes($"{${usernameAccess} ?? ""}:{${passwordAccess} ?? ""}"))}"` - ); if (isAuthOptional || basicSchemes.length > 1) { innerWriter.endControlFlow(); } diff --git a/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java b/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java index e633e099c085..6fdb5c6369e5 100644 --- a/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java +++ b/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java @@ -114,17 +114,31 @@ public GeneratedJavaFile generateFile() { private MethodSpec buildGetAuthHeaders( ClassName endpointMetadataClassName, FieldSpec usernameSupplierField, FieldSpec passwordSupplierField) { - return MethodSpec.methodBuilder("getAuthHeaders") + boolean eitherOmitted = basicAuthScheme.getUsernameOmit().orElse(false) + || basicAuthScheme.getPasswordOmit().orElse(false); + + MethodSpec.Builder builder = MethodSpec.methodBuilder("getAuthHeaders") .addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class) .addParameter(endpointMetadataClassName, "endpointMetadata") .returns(ParameterizedTypeName.get(Map.class, String.class, String.class)) .addStatement("String username = $N.get()", usernameSupplierField) - .addStatement("String password = $N.get()", passwordSupplierField) - .beginControlFlow("if (username == null && password == null)") - .addStatement("throw new $T(AUTH_CONFIG_ERROR_MESSAGE)", RuntimeException.class) - .endControlFlow() - .addStatement("String credentials = (username != null ? username : \"\") + \":\" + (password != null ? password : \"\")") + .addStatement("String password = $N.get()", passwordSupplierField); + + if (eitherOmitted) { + builder.beginControlFlow("if (username == null && password == null)") + .addStatement("throw new $T(AUTH_CONFIG_ERROR_MESSAGE)", RuntimeException.class) + .endControlFlow() + .addStatement( + "String credentials = (username != null ? username : \"\") + \":\" + (password != null ? password : \"\")"); + } else { + builder.beginControlFlow("if (username == null || password == null)") + .addStatement("throw new $T(AUTH_CONFIG_ERROR_MESSAGE)", RuntimeException.class) + .endControlFlow() + .addStatement("String credentials = username + \":\" + password"); + } + + return builder .addStatement( "String encoded = $T.getEncoder().encodeToString(credentials.getBytes($T.UTF_8))", Base64.class, @@ -136,6 +150,10 @@ private MethodSpec buildGetAuthHeaders( } private MethodSpec buildCanCreateMethod(String usernameEnvVar, String passwordEnvVar) { + boolean eitherOmitted = basicAuthScheme.getUsernameOmit().orElse(false) + || basicAuthScheme.getPasswordOmit().orElse(false); + String combiner = eitherOmitted ? ") || (" : ") && ("; + ParameterizedTypeName stringSupplierType = ParameterizedTypeName.get(ClassName.get(Supplier.class), ClassName.get(String.class)); @@ -151,7 +169,7 @@ private MethodSpec buildCanCreateMethod(String usernameEnvVar, String passwordEn if (usernameEnvVar != null) { condition.append(" || System.getenv(\"").append(usernameEnvVar).append("\") != null"); } - condition.append(") || (passwordSupplier != null"); + condition.append(combiner).append("passwordSupplier != null"); if (passwordEnvVar != null) { condition.append(" || System.getenv(\"").append(passwordEnvVar).append("\") != null"); } diff --git a/generators/php/sdk/src/root-client/RootClientGenerator.ts b/generators/php/sdk/src/root-client/RootClientGenerator.ts index 8e231367880f..6e9f754e6db8 100644 --- a/generators/php/sdk/src/root-client/RootClientGenerator.ts +++ b/generators/php/sdk/src/root-client/RootClientGenerator.ts @@ -356,16 +356,24 @@ export class RootClientGenerator extends FileGenerator 1) { const controlFlowKeyword = i === 0 ? "if" : "else if"; - writer.controlFlow( - controlFlowKeyword, - php.codeblock(`$${usernameName} !== null || $${passwordName} !== null`) + const condition = eitherOmitted + ? `$${usernameName} !== null || $${passwordName} !== null` + : `$${usernameName} !== null && $${passwordName} !== null`; + writer.controlFlow(controlFlowKeyword, php.codeblock(condition)); + } + if (eitherOmitted) { + writer.writeLine( + `$defaultHeaders['Authorization'] = "Basic " . base64_encode(($${usernameName} ?? "") . ":" . ($${passwordName} ?? ""));` + ); + } else { + writer.writeLine( + `$defaultHeaders['Authorization'] = "Basic " . base64_encode($${usernameName} . ":" . $${passwordName});` ); } - writer.writeLine( - `$defaultHeaders['Authorization'] = "Basic " . base64_encode(($${usernameName} ?? "") . ":" . ($${passwordName} ?? ""));` - ); if (isAuthOptional || basicAuthSchemes.length > 1) { writer.endControlFlow(); } diff --git a/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py b/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py index 308514985ba9..3ad547adf4da 100644 --- a/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py +++ b/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py @@ -523,36 +523,63 @@ def _write_get_headers_body(writer: AST.NodeWriter) -> None: writer.write_newline_if_last_line_not() basic_auth_scheme = self._get_basic_auth_scheme() if basic_auth_scheme is not None: + either_omitted = ( + getattr(basic_auth_scheme, "username_omit", None) is True + or getattr(basic_auth_scheme, "password_omit", None) is True + ) if not self._context.ir.sdk_config.is_auth_mandatory: username_var = names.get_username_constructor_parameter_name(basic_auth_scheme) password_var = names.get_password_constructor_parameter_name(basic_auth_scheme) writer.write_line(f"{username_var} = self.{names.get_username_getter_name(basic_auth_scheme)}()") writer.write_line(f"{password_var} = self.{names.get_password_getter_name(basic_auth_scheme)}()") - writer.write_line(f"if {username_var} is not None or {password_var} is not None:") + condition_op = "or" if either_omitted else "and" + writer.write_line(f"if {username_var} is not None {condition_op} {password_var} is not None:") with writer.indent(): writer.write(f'headers["{ClientWrapperGenerator.AUTHORIZATION_HEADER}"] = ') + if either_omitted: + writer.write_node( + AST.ClassInstantiation( + class_=httpx.HttpX.BASIC_AUTH, + args=[ + AST.Expression(f'{username_var} or ""'), + AST.Expression(f'{password_var} or ""'), + ], + ) + ) + else: + writer.write_node( + AST.ClassInstantiation( + class_=httpx.HttpX.BASIC_AUTH, + args=[ + AST.Expression(f"{username_var}"), + AST.Expression(f"{password_var}"), + ], + ) + ) + writer.write("._auth_header") + writer.write_newline_if_last_line_not() + else: + writer.write(f'headers["{ClientWrapperGenerator.AUTHORIZATION_HEADER}"] = ') + if either_omitted: writer.write_node( AST.ClassInstantiation( class_=httpx.HttpX.BASIC_AUTH, args=[ - AST.Expression(f'{username_var} or ""'), - AST.Expression(f'{password_var} or ""'), + AST.Expression(f"self.{names.get_username_getter_name(basic_auth_scheme)}() or \"\""), + AST.Expression(f"self.{names.get_password_getter_name(basic_auth_scheme)}() or \"\""), ], ) ) - writer.write("._auth_header") - writer.write_newline_if_last_line_not() - else: - writer.write(f'headers["{ClientWrapperGenerator.AUTHORIZATION_HEADER}"] = ') - writer.write_node( - AST.ClassInstantiation( - class_=httpx.HttpX.BASIC_AUTH, - args=[ - AST.Expression(f"self.{names.get_username_getter_name(basic_auth_scheme)}() or \"\""), - AST.Expression(f"self.{names.get_password_getter_name(basic_auth_scheme)}() or \"\""), - ], + else: + writer.write_node( + AST.ClassInstantiation( + class_=httpx.HttpX.BASIC_AUTH, + args=[ + AST.Expression(f"self.{names.get_username_getter_name(basic_auth_scheme)}()"), + AST.Expression(f"self.{names.get_password_getter_name(basic_auth_scheme)}()"), + ], + ) ) - ) writer.write("._auth_header") writer.write_newline_if_last_line_not() for param in constructor_parameters: diff --git a/generators/ruby-v2/sdk/src/root-client/RootClientGenerator.ts b/generators/ruby-v2/sdk/src/root-client/RootClientGenerator.ts index f658b9e3b974..6bbc93ae4c11 100644 --- a/generators/ruby-v2/sdk/src/root-client/RootClientGenerator.ts +++ b/generators/ruby-v2/sdk/src/root-client/RootClientGenerator.ts @@ -122,21 +122,26 @@ export class RootClientGenerator extends FileGenerator 1) { if (i === 0) { - writer.writeLine(`if !${usernameName}.nil? || !${passwordName}.nil?`); + writer.writeLine(`if !${usernameName}.nil? ${conditionOp} !${passwordName}.nil?`); } else { - writer.writeLine(`elsif !${usernameName}.nil? || !${passwordName}.nil?`); + writer.writeLine(`elsif !${usernameName}.nil? ${conditionOp} !${passwordName}.nil?`); } writer.writeLine( - ` headers["Authorization"] = "Basic #{Base64.strict_encode64("#{${usernameName} || ""}:#{${passwordName} || ""}")}"` + ` headers["Authorization"] = "Basic #{Base64.strict_encode64("#{${usernameExpr}}:#{${passwordExpr}}")}"` ); if (i === basicAuthSchemes.length - 1) { writer.writeLine(`end`); } } else { writer.writeLine( - `headers["Authorization"] = "Basic #{Base64.strict_encode64("#{${usernameName} || ""}:#{${passwordName} || ""}")}"` + `headers["Authorization"] = "Basic #{Base64.strict_encode64("#{${usernameExpr}}:#{${passwordExpr}}")}"` ); } } diff --git a/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts b/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts index fb745edc8f39..005994f57272 100644 --- a/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts +++ b/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts @@ -249,11 +249,15 @@ export class BasicAuthProviderGenerator implements AuthProviderGenerator { const usernameEnvVar = this.authScheme.usernameEnvVar; const passwordEnvVar = this.authScheme.passwordEnvVar; const wrapperAccess = this.keepIfWrapper("[WRAPPER_PROPERTY]?."); + const usernameOmit = this.authScheme.usernameOmit === true; + const passwordOmit = this.authScheme.passwordOmit === true; const usernameEnvCheck = usernameEnvVar != null ? " || process.env?.[ENV_USERNAME] != null" : ""; const passwordEnvCheck = passwordEnvVar != null ? " || process.env?.[ENV_PASSWORD] != null" : ""; - return `return (options?.${wrapperAccess}[USERNAME_PARAM] != null${usernameEnvCheck}) || (options?.${wrapperAccess}[PASSWORD_PARAM] != null${passwordEnvCheck});`; + // When either field is omitted, use || so providing just one credential is enough + const combiner = usernameOmit || passwordOmit ? "||" : "&&"; + return `return (options?.${wrapperAccess}[USERNAME_PARAM] != null${usernameEnvCheck}) ${combiner} (options?.${wrapperAccess}[PASSWORD_PARAM] != null${passwordEnvCheck});`; } private generateGetAuthRequestStatements(context: SdkContext): string { @@ -318,9 +322,14 @@ export class BasicAuthProviderGenerator implements AuthProviderGenerator { ? `\n (${passwordSupplierGetCode}) ??\n process.env?.[ENV_PASSWORD]` : passwordSupplierGetCode; + const usernameOmit = this.authScheme.usernameOmit === true; + const passwordOmit = this.authScheme.passwordOmit === true; + const eitherOmitted = usernameOmit || passwordOmit; + if (this.neverThrowErrors) { - // When neverThrowErrors is true, return empty headers if neither credential is provided - return ` + if (eitherOmitted) { + // When a field is omitted, return empty headers if neither credential is provided + return ` const ${usernameVar} = ${usernameEnvFallback}; const ${passwordVar} = ${passwordEnvFallback}; if (${usernameVar} == null && ${passwordVar} == null) { @@ -338,13 +347,39 @@ export class BasicAuthProviderGenerator implements AuthProviderGenerator { headers: authHeader != null ? { Authorization: authHeader } : {}, }; `; + } else { + // Default: return empty headers if credentials are missing + return ` + const ${usernameVar} = ${usernameEnvFallback}; + if (${usernameVar} == null) { + return { headers: {} }; + } + + const ${passwordVar} = ${passwordEnvFallback}; + if (${passwordVar} == null) { + return { headers: {} }; + } + + return { + headers: { + Authorization: ${getTextOfTsNode( + context.coreUtilities.auth.BasicAuth.toAuthorizationHeader( + ts.factory.createIdentifier(usernameVar), + ts.factory.createIdentifier(passwordVar) + ) + )}, + }, + }; + `; + } } else { - // When neverThrowErrors is false, throw an error only if neither credential is provided const errorConstructor = getTextOfTsNode( context.genericAPISdkError.getReferenceToGenericAPISdkError().getExpression() ); - return ` + if (eitherOmitted) { + // When a field is omitted, throw only if neither credential is provided + return ` const ${usernameVar} = ${usernameEnvFallback}; const ${passwordVar} = ${passwordEnvFallback}; if (${usernameVar} == null && ${passwordVar} == null) { @@ -364,6 +399,35 @@ export class BasicAuthProviderGenerator implements AuthProviderGenerator { headers: authHeader != null ? { Authorization: authHeader } : {}, }; `; + } else { + // Default: throw if either credential is missing + return ` + const ${usernameVar} = ${usernameEnvFallback}; + if (${usernameVar} == null) { + throw new ${errorConstructor}({ + message: ${CLASS_NAME}.AUTH_CONFIG_ERROR_MESSAGE_USERNAME, + }); + } + + const ${passwordVar} = ${passwordEnvFallback}; + if (${passwordVar} == null) { + throw new ${errorConstructor}({ + message: ${CLASS_NAME}.AUTH_CONFIG_ERROR_MESSAGE_PASSWORD, + }); + } + + return { + headers: { + Authorization: ${getTextOfTsNode( + context.coreUtilities.auth.BasicAuth.toAuthorizationHeader( + ts.factory.createIdentifier(usernameVar), + ts.factory.createIdentifier(passwordVar) + ) + )}, + }, + }; + `; + } } } From b15016a07eec414893e9aec0b004fec4584d5e7c Mon Sep 17 00:00:00 2001 From: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com> Date: Tue, 31 Mar 2026 20:06:04 +0000 Subject: [PATCH 3/6] fix: formatting issues in Java and Python generators Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../generators/auth/BasicAuthProviderGenerator.java | 13 +++++++++---- .../sdk/core_utilities/client_wrapper_generator.py | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java b/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java index 62b2c42f1633..4fab7c03c1c8 100644 --- a/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java +++ b/generators/java/sdk/src/main/java/com/fern/java/client/generators/auth/BasicAuthProviderGenerator.java @@ -124,8 +124,14 @@ private MethodSpec buildGetAuthHeaders( .returns(ParameterizedTypeName.get(Map.class, String.class, String.class)); if (eitherOmitted) { - builder.addStatement("String username = $N != null ? $N.get() : null", usernameSupplierField, usernameSupplierField) - .addStatement("String password = $N != null ? $N.get() : null", passwordSupplierField, passwordSupplierField) + builder.addStatement( + "String username = $N != null ? $N.get() : null", + usernameSupplierField, + usernameSupplierField) + .addStatement( + "String password = $N != null ? $N.get() : null", + passwordSupplierField, + passwordSupplierField) .beginControlFlow("if (username == null && password == null)") .addStatement("throw new $T(AUTH_CONFIG_ERROR_MESSAGE)", RuntimeException.class) .endControlFlow() @@ -140,8 +146,7 @@ private MethodSpec buildGetAuthHeaders( .addStatement("String credentials = username + \":\" + password"); } - return builder - .addStatement( + return builder.addStatement( "String encoded = $T.getEncoder().encodeToString(credentials.getBytes($T.UTF_8))", Base64.class, StandardCharsets.class) diff --git a/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py b/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py index 3ad547adf4da..78d1c5da0e66 100644 --- a/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py +++ b/generators/python/src/fern_python/generators/sdk/core_utilities/client_wrapper_generator.py @@ -565,8 +565,8 @@ def _write_get_headers_body(writer: AST.NodeWriter) -> None: AST.ClassInstantiation( class_=httpx.HttpX.BASIC_AUTH, args=[ - AST.Expression(f"self.{names.get_username_getter_name(basic_auth_scheme)}() or \"\""), - AST.Expression(f"self.{names.get_password_getter_name(basic_auth_scheme)}() or \"\""), + AST.Expression(f'self.{names.get_username_getter_name(basic_auth_scheme)}() or ""'), + AST.Expression(f'self.{names.get_password_getter_name(basic_auth_scheme)}() or ""'), ], ) ) From 899333ab8865d2499cf9a25046f321d4cd1a04fc Mon Sep 17 00:00:00 2001 From: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com> Date: Tue, 31 Mar 2026 21:13:21 +0000 Subject: [PATCH 4/6] test: add basic-auth-optional seed fixture for conditional optional password Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../basic-auth-optional/.editorconfig | 35 + .../basic-auth-optional/.fern/metadata.json | 8 + .../.github/workflows/ci.yml | 52 + .../csharp-sdk/basic-auth-optional/.gitignore | 484 +++++++ seed/csharp-sdk/basic-auth-optional/README.md | 172 +++ .../SeedBasicAuthOptional.slnx | 4 + .../basic-auth-optional/reference.md | 97 ++ .../basic-auth-optional/snippet.json | 29 + .../src/SeedApi.DynamicSnippets/Example0.cs | 19 + .../src/SeedApi.DynamicSnippets/Example1.cs | 19 + .../src/SeedApi.DynamicSnippets/Example2.cs | 19 + .../src/SeedApi.DynamicSnippets/Example3.cs | 24 + .../src/SeedApi.DynamicSnippets/Example4.cs | 24 + .../src/SeedApi.DynamicSnippets/Example5.cs | 24 + .../src/SeedApi.DynamicSnippets/Example6.cs | 24 + .../SeedApi.DynamicSnippets.csproj | 13 + .../Core/HeadersBuilderTests.cs | 326 +++++ .../Core/Json/AdditionalPropertiesTests.cs | 365 ++++++ .../Core/Json/DateOnlyJsonTests.cs | 100 ++ .../Core/Json/DateTimeJsonTests.cs | 134 ++ .../Core/Json/JsonAccessAttributeTests.cs | 160 +++ .../Core/QueryStringBuilderTests.cs | 560 ++++++++ .../Core/QueryStringConverterTests.cs | 158 +++ .../Core/RawClientTests/MultipartFormTests.cs | 1121 +++++++++++++++++ .../RawClientTests/QueryParameterTests.cs | 108 ++ .../Core/RawClientTests/RetriesTests.cs | 406 ++++++ .../Core/WithRawResponseTests.cs | 269 ++++ .../SeedBasicAuthOptional.Test.Custom.props | 6 + .../SeedBasicAuthOptional.Test.csproj | 39 + .../SeedBasicAuthOptional.Test/TestClient.cs | 6 + .../Unit/MockServer/BaseMockServerTest.cs | 39 + .../BasicAuth/GetWithBasicAuthTest.cs | 50 + .../BasicAuth/PostWithBasicAuthTest.cs | 78 ++ .../Utils/AdditionalPropertiesComparer.cs | 219 ++++ .../Utils/JsonAssert.cs | 29 + .../Utils/JsonElementComparer.cs | 236 ++++ .../Utils/NUnitExtensions.cs | 32 + .../Utils/OneOfComparer.cs | 86 ++ .../Utils/OptionalComparer.cs | 104 ++ .../Utils/ReadOnlyMemoryComparer.cs | 87 ++ .../BasicAuth/BasicAuthClient.cs | 207 +++ .../BasicAuth/IBasicAuthClient.cs | 21 + .../SeedBasicAuthOptional/Core/ApiResponse.cs | 13 + .../SeedBasicAuthOptional/Core/BaseRequest.cs | 67 + .../Core/CollectionItemSerializer.cs | 91 ++ .../SeedBasicAuthOptional/Core/Constants.cs | 7 + .../Core/DateOnlyConverter.cs | 747 +++++++++++ .../Core/DateTimeSerializer.cs | 40 + .../Core/EmptyRequest.cs | 11 + .../Core/EncodingCache.cs | 11 + .../SeedBasicAuthOptional/Core/Extensions.cs | 55 + .../Core/FormUrlEncoder.cs | 33 + .../SeedBasicAuthOptional/Core/HeaderValue.cs | 52 + .../src/SeedBasicAuthOptional/Core/Headers.cs | 28 + .../Core/HeadersBuilder.cs | 197 +++ .../Core/HttpContentExtensions.cs | 20 + .../Core/HttpMethodExtensions.cs | 8 + .../Core/IIsRetryableContent.cs | 6 + .../Core/IRequestOptions.cs | 83 ++ .../Core/JsonAccessAttribute.cs | 15 + .../Core/JsonConfiguration.cs | 275 ++++ .../SeedBasicAuthOptional/Core/JsonRequest.cs | 36 + .../Core/MultipartFormRequest.cs | 294 +++++ .../Core/NullableAttribute.cs | 18 + .../Core/OneOfSerializer.cs | 145 +++ .../SeedBasicAuthOptional/Core/Optional.cs | 474 +++++++ .../Core/OptionalAttribute.cs | 17 + .../Core/Public/AdditionalProperties.cs | 353 ++++++ .../Core/Public/ClientOptions.cs | 84 ++ .../Core/Public/FileParameter.cs | 63 + .../Core/Public/RawResponse.cs | 24 + .../Core/Public/RequestOptions.cs | 86 ++ .../SeedBasicAuthOptionalApiException.cs | 22 + .../Public/SeedBasicAuthOptionalException.cs | 7 + .../Core/Public/Version.cs | 7 + .../Core/Public/WithRawResponse.cs | 18 + .../Core/Public/WithRawResponseTask.cs | 144 +++ .../Core/QueryStringBuilder.cs | 469 +++++++ .../Core/QueryStringConverter.cs | 259 ++++ .../SeedBasicAuthOptional/Core/RawClient.cs | 344 +++++ .../SeedBasicAuthOptional/Core/RawResponse.cs | 24 + .../Core/ResponseHeaders.cs | 108 ++ .../Core/StreamRequest.cs | 29 + .../SeedBasicAuthOptional/Core/StringEnum.cs | 6 + .../Core/StringEnumExtensions.cs | 6 + .../Core/ValueConvert.cs | 114 ++ .../Errors/Exceptions/BadRequest.cs | 7 + .../Errors/Exceptions/UnauthorizedRequest.cs | 14 + .../Types/UnauthorizedRequestErrorBody.cs | 28 + .../ISeedBasicAuthOptionalClient.cs | 6 + .../SeedBasicAuthOptional.Custom.props | 20 + .../SeedBasicAuthOptional.csproj | 63 + .../SeedBasicAuthOptionalClient.cs | 40 + .../basic-auth-optional/.fern/metadata.json | 10 + .../.github/workflows/ci.yml | 62 + seed/go-sdk/basic-auth-optional/README.md | 199 +++ .../basic-auth-optional/basicauth/client.go | 65 + .../basicauth/raw_client.go | 114 ++ .../basic-auth-optional/client/client.go | 33 + .../basic-auth-optional/client/client_test.go | 45 + .../basic-auth-optional/core/api_error.go | 47 + seed/go-sdk/basic-auth-optional/core/http.go | 15 + .../core/request_option.go | 139 ++ .../dynamic-snippets/example0/snippet.go | 23 + .../dynamic-snippets/example1/snippet.go | 23 + .../dynamic-snippets/example2/snippet.go | 23 + .../dynamic-snippets/example3/snippet.go | 27 + .../dynamic-snippets/example4/snippet.go | 27 + .../dynamic-snippets/example5/snippet.go | 27 + .../dynamic-snippets/example6/snippet.go | 27 + .../go-sdk/basic-auth-optional/error_codes.go | 21 + seed/go-sdk/basic-auth-optional/errors.go | 44 + seed/go-sdk/basic-auth-optional/file_param.go | 41 + seed/go-sdk/basic-auth-optional/go.mod | 16 + seed/go-sdk/basic-auth-optional/go.sum | 12 + .../basic-auth-optional/internal/caller.go | 311 +++++ .../internal/caller_test.go | 705 +++++++++++ .../internal/error_decoder.go | 64 + .../internal/error_decoder_test.go | 59 + .../internal/explicit_fields.go | 116 ++ .../internal/explicit_fields_test.go | 645 ++++++++++ .../internal/extra_properties.go | 141 +++ .../internal/extra_properties_test.go | 228 ++++ .../basic-auth-optional/internal/http.go | 71 ++ .../basic-auth-optional/internal/query.go | 358 ++++++ .../internal/query_test.go | 395 ++++++ .../basic-auth-optional/internal/retrier.go | 239 ++++ .../internal/retrier_test.go | 352 ++++++ .../basic-auth-optional/internal/stringer.go | 13 + .../basic-auth-optional/internal/time.go | 165 +++ .../option/request_option.go | 81 ++ seed/go-sdk/basic-auth-optional/pointer.go | 137 ++ .../basic-auth-optional/pointer_test.go | 211 ++++ seed/go-sdk/basic-auth-optional/reference.md | 105 ++ seed/go-sdk/basic-auth-optional/snippet.json | 26 + seed/go-sdk/basic-auth-optional/types.go | 94 ++ seed/go-sdk/basic-auth-optional/types_test.go | 153 +++ .../basic-auth-optional/.fern/metadata.json | 7 + .../.github/workflows/ci.yml | 65 + seed/java-sdk/basic-auth-optional/.gitignore | 24 + seed/java-sdk/basic-auth-optional/README.md | 216 ++++ .../java-sdk/basic-auth-optional/build.gradle | 102 ++ .../java-sdk/basic-auth-optional/reference.md | 97 ++ .../sample-app/build.gradle | 19 + .../sample-app/src/main/java/sample/App.java | 13 + .../basic-auth-optional/settings.gradle | 3 + .../java-sdk/basic-auth-optional/snippet.json | 96 ++ .../AsyncSeedBasicAuthOptionalClient.java | 28 + ...yncSeedBasicAuthOptionalClientBuilder.java | 230 ++++ .../SeedBasicAuthOptionalClient.java | 28 + .../SeedBasicAuthOptionalClientBuilder.java | 230 ++++ .../basicAuthOptional/core/ClientOptions.java | 221 ++++ .../basicAuthOptional/core/ConsoleLogger.java | 51 + .../core/DateTimeDeserializer.java | 55 + .../core/DoubleSerializer.java | 43 + .../basicAuthOptional/core/Environment.java | 20 + .../basicAuthOptional/core/FileStream.java | 60 + .../seed/basicAuthOptional/core/ILogger.java | 38 + .../core/InputStreamRequestBody.java | 74 ++ .../basicAuthOptional/core/LogConfig.java | 98 ++ .../seed/basicAuthOptional/core/LogLevel.java | 36 + .../seed/basicAuthOptional/core/Logger.java | 97 ++ .../core/LoggingInterceptor.java | 104 ++ .../basicAuthOptional/core/MediaTypes.java | 13 + .../seed/basicAuthOptional/core/Nullable.java | 140 ++ .../core/NullableNonemptyFilter.java | 22 + .../basicAuthOptional/core/ObjectMappers.java | 46 + .../core/QueryStringMapper.java | 142 +++ .../core/RequestOptions.java | 118 ++ .../core/ResponseBodyInputStream.java | 45 + .../core/ResponseBodyReader.java | 44 + .../core/RetryInterceptor.java | 181 +++ .../core/Rfc2822DateTimeDeserializer.java | 25 + .../SeedBasicAuthOptionalApiException.java | 73 ++ .../core/SeedBasicAuthOptionalException.java | 17 + .../SeedBasicAuthOptionalHttpResponse.java | 37 + .../seed/basicAuthOptional/core/SseEvent.java | 114 ++ .../core/SseEventParser.java | 228 ++++ .../seed/basicAuthOptional/core/Stream.java | 513 ++++++++ .../basicAuthOptional/core/Suppliers.java | 23 + .../basicauth/AsyncBasicAuthClient.java | 54 + .../basicauth/AsyncRawBasicAuthClient.java | 192 +++ .../resources/basicauth/BasicAuthClient.java | 53 + .../basicauth/RawBasicAuthClient.java | 151 +++ .../resources/errors/errors/BadRequest.java | 17 + .../errors/errors/UnauthorizedRequest.java | 33 + .../types/UnauthorizedRequestErrorBody.java | 118 ++ .../src/main/java/com/snippets/Example0.java | 14 + .../src/main/java/com/snippets/Example1.java | 14 + .../src/main/java/com/snippets/Example2.java | 14 + .../src/main/java/com/snippets/Example3.java | 19 + .../src/main/java/com/snippets/Example4.java | 19 + .../src/main/java/com/snippets/Example5.java | 19 + .../src/main/java/com/snippets/Example6.java | 19 + .../seed/basicAuthOptional/StreamTest.java | 120 ++ .../seed/basicAuthOptional/TestClient.java | 11 + .../core/QueryStringMapperTest.java | 339 +++++ .../basic-auth-optional/.fern/metadata.json | 7 + .../.github/workflows/ci.yml | 52 + seed/php-sdk/basic-auth-optional/.gitignore | 5 + seed/php-sdk/basic-auth-optional/README.md | 145 +++ .../php-sdk/basic-auth-optional/composer.json | 46 + seed/php-sdk/basic-auth-optional/phpstan.neon | 6 + seed/php-sdk/basic-auth-optional/phpunit.xml | 7 + seed/php-sdk/basic-auth-optional/reference.md | 99 ++ seed/php-sdk/basic-auth-optional/snippet.json | 0 .../src/BasicAuth/BasicAuthClient.php | 146 +++ .../src/Core/Client/BaseApiRequest.php | 22 + .../src/Core/Client/HttpClientBuilder.php | 56 + .../src/Core/Client/HttpMethod.php | 12 + .../src/Core/Client/MockHttpClient.php | 75 ++ .../src/Core/Client/RawClient.php | 310 +++++ .../src/Core/Client/RetryDecoratingClient.php | 241 ++++ .../src/Core/Json/JsonApiRequest.php | 28 + .../src/Core/Json/JsonDecoder.php | 161 +++ .../src/Core/Json/JsonDeserializer.php | 218 ++++ .../src/Core/Json/JsonEncoder.php | 20 + .../src/Core/Json/JsonProperty.php | 13 + .../src/Core/Json/JsonSerializableType.php | 225 ++++ .../src/Core/Json/JsonSerializer.php | 205 +++ .../src/Core/Json/Utils.php | 62 + .../Core/Multipart/MultipartApiRequest.php | 28 + .../src/Core/Multipart/MultipartFormData.php | 58 + .../Core/Multipart/MultipartFormDataPart.php | 62 + .../src/Core/Types/ArrayType.php | 16 + .../src/Core/Types/Constant.php | 12 + .../src/Core/Types/Date.php | 16 + .../src/Core/Types/Union.php | 62 + .../Types/UnauthorizedRequestErrorBody.php | 34 + .../src/Exceptions/SeedApiException.php | 53 + .../src/Exceptions/SeedException.php | 12 + .../basic-auth-optional/src/SeedClient.php | 69 + .../basic-auth-optional/src/Utils/File.php | 129 ++ .../src/dynamic-snippets/example0/snippet.php | 14 + .../src/dynamic-snippets/example1/snippet.php | 14 + .../src/dynamic-snippets/example2/snippet.php | 14 + .../src/dynamic-snippets/example3/snippet.php | 18 + .../src/dynamic-snippets/example4/snippet.php | 18 + .../src/dynamic-snippets/example5/snippet.php | 18 + .../src/dynamic-snippets/example6/snippet.php | 18 + .../tests/Core/Client/RawClientTest.php | 1074 ++++++++++++++++ .../Core/Json/AdditionalPropertiesTest.php | 76 ++ .../tests/Core/Json/DateArrayTest.php | 54 + .../tests/Core/Json/EmptyArrayTest.php | 71 ++ .../tests/Core/Json/EnumTest.php | 77 ++ .../tests/Core/Json/ExhaustiveTest.php | 197 +++ .../tests/Core/Json/InvalidTest.php | 42 + .../tests/Core/Json/NestedUnionArrayTest.php | 89 ++ .../tests/Core/Json/NullPropertyTest.php | 53 + .../tests/Core/Json/NullableArrayTest.php | 49 + .../tests/Core/Json/ScalarTest.php | 116 ++ .../tests/Core/Json/TraitTest.php | 60 + .../tests/Core/Json/UnionArrayTest.php | 57 + .../tests/Core/Json/UnionPropertyTest.php | 111 ++ .../basic-auth-optional/.fern/metadata.json | 7 + .../.github/workflows/ci.yml | 65 + .../python-sdk/basic-auth-optional/.gitignore | 5 + seed/python-sdk/basic-auth-optional/README.md | 168 +++ .../basic-auth-optional/poetry.lock | 646 ++++++++++ .../basic-auth-optional/pyproject.toml | 92 ++ .../basic-auth-optional/reference.md | 138 ++ .../basic-auth-optional/requirements.txt | 4 + .../basic-auth-optional/snippet.json | 31 + .../basic-auth-optional/src/seed/__init__.py | 55 + .../src/seed/basic_auth/__init__.py | 4 + .../src/seed/basic_auth/client.py | 178 +++ .../src/seed/basic_auth/raw_client.py | 238 ++++ .../basic-auth-optional/src/seed/client.py | 164 +++ .../src/seed/core/__init__.py | 125 ++ .../src/seed/core/api_error.py | 23 + .../src/seed/core/client_wrapper.py | 120 ++ .../src/seed/core/datetime_utils.py | 70 + .../basic-auth-optional/src/seed/core/file.py | 67 + .../src/seed/core/force_multipart.py | 18 + .../src/seed/core/http_client.py | 840 ++++++++++++ .../src/seed/core/http_response.py | 59 + .../src/seed/core/http_sse/__init__.py | 42 + .../src/seed/core/http_sse/_api.py | 112 ++ .../src/seed/core/http_sse/_decoders.py | 61 + .../src/seed/core/http_sse/_exceptions.py | 7 + .../src/seed/core/http_sse/_models.py | 17 + .../src/seed/core/jsonable_encoder.py | 108 ++ .../src/seed/core/logging.py | 107 ++ .../src/seed/core/parse_error.py | 36 + .../src/seed/core/pydantic_utilities.py | 634 ++++++++++ .../src/seed/core/query_encoder.py | 58 + .../src/seed/core/remove_none_from_dict.py | 11 + .../src/seed/core/request_options.py | 35 + .../src/seed/core/serialization.py | 276 ++++ .../src/seed/errors/__init__.py | 39 + .../src/seed/errors/errors/__init__.py | 35 + .../src/seed/errors/errors/bad_request.py | 13 + .../errors/errors/unauthorized_request.py | 11 + .../src/seed/errors/types/__init__.py | 34 + .../types/unauthorized_request_error_body.py | 19 + .../basic-auth-optional/src/seed/py.typed | 0 .../basic-auth-optional/src/seed/version.py | 3 + .../tests/custom/test_client.py | 7 + .../tests/utils/__init__.py | 2 + .../tests/utils/assets/models/__init__.py | 21 + .../tests/utils/assets/models/circle.py | 11 + .../tests/utils/assets/models/color.py | 7 + .../assets/models/object_with_defaults.py | 15 + .../models/object_with_optional_field.py | 35 + .../tests/utils/assets/models/shape.py | 28 + .../tests/utils/assets/models/square.py | 11 + .../assets/models/undiscriminated_shape.py | 10 + .../tests/utils/test_http_client.py | 662 ++++++++++ .../tests/utils/test_query_encoding.py | 36 + .../tests/utils/test_serialization.py | 72 ++ .../basic-auth-optional/.fern/metadata.json | 7 + .../.github/workflows/ci.yml | 75 ++ .../basic-auth-optional/.gitignore | 1 + .../basic-auth-optional/.rubocop.yml | 69 + seed/ruby-sdk-v2/basic-auth-optional/Gemfile | 23 + .../basic-auth-optional/Gemfile.custom | 14 + .../ruby-sdk-v2/basic-auth-optional/README.md | 157 +++ seed/ruby-sdk-v2/basic-auth-optional/Rakefile | 20 + .../basic-auth-optional/custom.gemspec.rb | 16 + .../dynamic-snippets/example0/snippet.rb | 9 + .../dynamic-snippets/example1/snippet.rb | 9 + .../dynamic-snippets/example2/snippet.rb | 9 + .../dynamic-snippets/example3/snippet.rb | 9 + .../dynamic-snippets/example4/snippet.rb | 9 + .../dynamic-snippets/example5/snippet.rb | 9 + .../dynamic-snippets/example6/snippet.rb | 9 + .../basic-auth-optional/lib/seed.rb | 41 + .../lib/seed/basic_auth/client.rb | 77 ++ .../basic-auth-optional/lib/seed/client.rb | 27 + .../lib/seed/errors/api_error.rb | 8 + .../lib/seed/errors/client_error.rb | 17 + .../lib/seed/errors/redirect_error.rb | 8 + .../lib/seed/errors/response_error.rb | 42 + .../lib/seed/errors/server_error.rb | 11 + .../lib/seed/errors/timeout_error.rb | 8 + .../types/unauthorized_request_error_body.rb | 11 + .../seed/internal/errors/constraint_error.rb | 10 + .../lib/seed/internal/errors/type_error.rb | 10 + .../lib/seed/internal/http/base_request.rb | 51 + .../lib/seed/internal/http/raw_client.rb | 214 ++++ .../iterators/cursor_item_iterator.rb | 28 + .../iterators/cursor_page_iterator.rb | 51 + .../seed/internal/iterators/item_iterator.rb | 59 + .../iterators/offset_item_iterator.rb | 30 + .../iterators/offset_page_iterator.rb | 83 ++ .../lib/seed/internal/json/request.rb | 41 + .../lib/seed/internal/json/serializable.rb | 25 + .../internal/multipart/multipart_encoder.rb | 141 +++ .../internal/multipart/multipart_form_data.rb | 78 ++ .../multipart/multipart_form_data_part.rb | 51 + .../internal/multipart/multipart_request.rb | 40 + .../lib/seed/internal/types/array.rb | 47 + .../lib/seed/internal/types/boolean.rb | 34 + .../lib/seed/internal/types/enum.rb | 56 + .../lib/seed/internal/types/hash.rb | 36 + .../lib/seed/internal/types/model.rb | 208 +++ .../lib/seed/internal/types/model/field.rb | 38 + .../lib/seed/internal/types/type.rb | 35 + .../lib/seed/internal/types/union.rb | 161 +++ .../lib/seed/internal/types/unknown.rb | 15 + .../lib/seed/internal/types/utils.rb | 116 ++ .../basic-auth-optional/lib/seed/version.rb | 5 + .../basic-auth-optional/reference.md | 118 ++ .../basic-auth-optional/seed.gemspec | 37 + .../basic-auth-optional/snippet.json | 0 .../basic-auth-optional/test/custom.test.rb | 15 + .../basic-auth-optional/test/test_helper.rb | 3 + .../iterators/test_cursor_item_iterator.rb | 189 +++ .../iterators/test_offset_item_iterator.rb | 151 +++ .../test/unit/internal/types/test_array.rb | 37 + .../test/unit/internal/types/test_boolean.rb | 35 + .../test/unit/internal/types/test_enum.rb | 42 + .../test/unit/internal/types/test_hash.rb | 50 + .../test/unit/internal/types/test_model.rb | 154 +++ .../test/unit/internal/types/test_union.rb | 62 + .../test/unit/internal/types/test_utils.rb | 212 ++++ .../basic-auth-optional/.fern/metadata.json | 7 + .../.github/workflows/ci.yml | 46 + seed/ts-sdk/basic-auth-optional/.gitignore | 3 + .../basic-auth-optional/CONTRIBUTING.md | 133 ++ seed/ts-sdk/basic-auth-optional/README.md | 275 ++++ seed/ts-sdk/basic-auth-optional/biome.json | 74 ++ seed/ts-sdk/basic-auth-optional/package.json | 80 ++ .../basic-auth-optional/pnpm-workspace.yaml | 1 + seed/ts-sdk/basic-auth-optional/reference.md | 122 ++ .../scripts/rename-to-esm-files.js | 123 ++ seed/ts-sdk/basic-auth-optional/snippet.json | 27 + .../basic-auth-optional/src/BaseClient.ts | 84 ++ seed/ts-sdk/basic-auth-optional/src/Client.ts | 56 + .../basic-auth-optional/src/api/index.ts | 1 + .../api/resources/basicAuth/client/Client.ts | 158 +++ .../api/resources/basicAuth/client/index.ts | 1 + .../src/api/resources/basicAuth/exports.ts | 4 + .../src/api/resources/basicAuth/index.ts | 1 + .../api/resources/errors/errors/BadRequest.ts | 20 + .../errors/errors/UnauthorizedRequest.ts | 22 + .../src/api/resources/errors/errors/index.ts | 2 + .../src/api/resources/errors/exports.ts | 3 + .../src/api/resources/errors/index.ts | 2 + .../types/UnauthorizedRequestErrorBody.ts | 5 + .../src/api/resources/errors/types/index.ts | 1 + .../src/api/resources/index.ts | 4 + .../src/auth/BasicAuthProvider.ts | 58 + .../basic-auth-optional/src/auth/index.ts | 1 + .../src/core/auth/AuthProvider.ts | 6 + .../src/core/auth/AuthRequest.ts | 9 + .../src/core/auth/BasicAuth.ts | 37 + .../src/core/auth/BearerToken.ts | 20 + .../src/core/auth/NoOpAuthProvider.ts | 8 + .../src/core/auth/index.ts | 5 + .../basic-auth-optional/src/core/base64.ts | 27 + .../basic-auth-optional/src/core/exports.ts | 1 + .../src/core/fetcher/APIResponse.ts | 23 + .../src/core/fetcher/BinaryResponse.ts | 34 + .../src/core/fetcher/EndpointMetadata.ts | 13 + .../src/core/fetcher/EndpointSupplier.ts | 14 + .../src/core/fetcher/Fetcher.ts | 398 ++++++ .../src/core/fetcher/Headers.ts | 93 ++ .../src/core/fetcher/HttpResponsePromise.ts | 116 ++ .../src/core/fetcher/RawResponse.ts | 61 + .../src/core/fetcher/Supplier.ts | 11 + .../src/core/fetcher/createRequestUrl.ts | 6 + .../src/core/fetcher/getErrorResponseBody.ts | 33 + .../src/core/fetcher/getFetchFn.ts | 3 + .../src/core/fetcher/getHeader.ts | 8 + .../src/core/fetcher/getRequestBody.ts | 20 + .../src/core/fetcher/getResponseBody.ts | 58 + .../src/core/fetcher/index.ts | 13 + .../core/fetcher/makePassthroughRequest.ts | 189 +++ .../src/core/fetcher/makeRequest.ts | 70 + .../src/core/fetcher/requestWithRetries.ts | 64 + .../src/core/fetcher/signals.ts | 26 + .../basic-auth-optional/src/core/headers.ts | 33 + .../basic-auth-optional/src/core/index.ts | 6 + .../basic-auth-optional/src/core/json.ts | 27 + .../src/core/logging/exports.ts | 19 + .../src/core/logging/index.ts | 1 + .../src/core/logging/logger.ts | 203 +++ .../src/core/runtime/index.ts | 1 + .../src/core/runtime/runtime.ts | 134 ++ .../src/core/url/encodePathParam.ts | 18 + .../basic-auth-optional/src/core/url/index.ts | 3 + .../basic-auth-optional/src/core/url/join.ts | 79 ++ .../basic-auth-optional/src/core/url/qs.ts | 74 ++ .../src/errors/SeedBasicAuthOptionalError.ts | 58 + .../SeedBasicAuthOptionalTimeoutError.ts | 13 + .../src/errors/handleNonStatusCodeError.ts | 37 + .../basic-auth-optional/src/errors/index.ts | 2 + .../ts-sdk/basic-auth-optional/src/exports.ts | 1 + seed/ts-sdk/basic-auth-optional/src/index.ts | 5 + .../ts-sdk/basic-auth-optional/src/version.ts | 1 + .../basic-auth-optional/tests/custom.test.ts | 13 + .../tests/mock-server/MockServer.ts | 29 + .../tests/mock-server/MockServerPool.ts | 106 ++ .../tests/mock-server/mockEndpointBuilder.ts | 234 ++++ .../tests/mock-server/randomBaseUrl.ts | 4 + .../tests/mock-server/setup.ts | 10 + .../tests/mock-server/withFormUrlEncoded.ts | 104 ++ .../tests/mock-server/withHeaders.ts | 70 + .../tests/mock-server/withJson.ts | 173 +++ .../ts-sdk/basic-auth-optional/tests/setup.ts | 80 ++ .../basic-auth-optional/tests/tsconfig.json | 10 + .../tests/unit/auth/BasicAuth.test.ts | 112 ++ .../tests/unit/auth/BearerToken.test.ts | 14 + .../tests/unit/base64.test.ts | 53 + .../tests/unit/fetcher/Fetcher.test.ts | 262 ++++ .../unit/fetcher/HttpResponsePromise.test.ts | 143 +++ .../tests/unit/fetcher/RawResponse.test.ts | 34 + .../unit/fetcher/createRequestUrl.test.ts | 163 +++ .../tests/unit/fetcher/getRequestBody.test.ts | 129 ++ .../unit/fetcher/getResponseBody.test.ts | 97 ++ .../tests/unit/fetcher/logging.test.ts | 517 ++++++++ .../fetcher/makePassthroughRequest.test.ts | 398 ++++++ .../tests/unit/fetcher/makeRequest.test.ts | 158 +++ .../tests/unit/fetcher/redacting.test.ts | 1115 ++++++++++++++++ .../unit/fetcher/requestWithRetries.test.ts | 230 ++++ .../tests/unit/fetcher/signals.test.ts | 69 + .../tests/unit/fetcher/test-file.txt | 1 + .../tests/unit/logging/logger.test.ts | 454 +++++++ .../tests/unit/url/join.test.ts | 284 +++++ .../tests/unit/url/qs.test.ts | 278 ++++ .../basic-auth-optional/tests/wire/.gitkeep | 0 .../tests/wire/basicAuth.test.ts | 114 ++ .../basic-auth-optional/tsconfig.base.json | 17 + .../basic-auth-optional/tsconfig.cjs.json | 9 + .../basic-auth-optional/tsconfig.esm.json | 10 + seed/ts-sdk/basic-auth-optional/tsconfig.json | 3 + .../basic-auth-optional/vitest.config.mts | 32 + .../basic-auth-optional/definition/api.yml | 12 + .../definition/basic-auth.yml | 39 + .../basic-auth-optional/definition/errors.yml | 11 + .../apis/basic-auth-optional/generators.yml | 22 + 492 files changed, 44947 insertions(+) create mode 100644 seed/csharp-sdk/basic-auth-optional/.editorconfig create mode 100644 seed/csharp-sdk/basic-auth-optional/.fern/metadata.json create mode 100644 seed/csharp-sdk/basic-auth-optional/.github/workflows/ci.yml create mode 100644 seed/csharp-sdk/basic-auth-optional/.gitignore create mode 100644 seed/csharp-sdk/basic-auth-optional/README.md create mode 100644 seed/csharp-sdk/basic-auth-optional/SeedBasicAuthOptional.slnx create mode 100644 seed/csharp-sdk/basic-auth-optional/reference.md create mode 100644 seed/csharp-sdk/basic-auth-optional/snippet.json create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example0.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example1.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example2.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example3.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example4.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example5.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example6.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/SeedApi.DynamicSnippets.csproj create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/HeadersBuilderTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/AdditionalPropertiesTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/DateOnlyJsonTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/DateTimeJsonTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/JsonAccessAttributeTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/QueryStringBuilderTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/QueryStringConverterTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/MultipartFormTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/QueryParameterTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/RetriesTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/WithRawResponseTests.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.Custom.props create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.csproj create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/TestClient.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BaseMockServerTest.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BasicAuth/GetWithBasicAuthTest.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BasicAuth/PostWithBasicAuthTest.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/AdditionalPropertiesComparer.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/JsonAssert.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/JsonElementComparer.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/NUnitExtensions.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/OneOfComparer.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/OptionalComparer.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/ReadOnlyMemoryComparer.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/BasicAuth/BasicAuthClient.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/BasicAuth/IBasicAuthClient.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ApiResponse.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/BaseRequest.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/CollectionItemSerializer.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Constants.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/DateOnlyConverter.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/DateTimeSerializer.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/EmptyRequest.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/EncodingCache.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Extensions.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/FormUrlEncoder.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HeaderValue.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Headers.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HeadersBuilder.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HttpContentExtensions.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HttpMethodExtensions.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/IIsRetryableContent.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/IRequestOptions.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonAccessAttribute.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonConfiguration.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonRequest.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/MultipartFormRequest.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/NullableAttribute.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/OneOfSerializer.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Optional.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/OptionalAttribute.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/AdditionalProperties.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/ClientOptions.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/FileParameter.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/RawResponse.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/RequestOptions.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/SeedBasicAuthOptionalApiException.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/SeedBasicAuthOptionalException.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/Version.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/WithRawResponse.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/WithRawResponseTask.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/QueryStringBuilder.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/QueryStringConverter.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/RawClient.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/RawResponse.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ResponseHeaders.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StreamRequest.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StringEnum.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StringEnumExtensions.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ValueConvert.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Exceptions/BadRequest.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Exceptions/UnauthorizedRequest.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Types/UnauthorizedRequestErrorBody.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/ISeedBasicAuthOptionalClient.cs create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptional.Custom.props create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptional.csproj create mode 100644 seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptionalClient.cs create mode 100644 seed/go-sdk/basic-auth-optional/.fern/metadata.json create mode 100644 seed/go-sdk/basic-auth-optional/.github/workflows/ci.yml create mode 100644 seed/go-sdk/basic-auth-optional/README.md create mode 100644 seed/go-sdk/basic-auth-optional/basicauth/client.go create mode 100644 seed/go-sdk/basic-auth-optional/basicauth/raw_client.go create mode 100644 seed/go-sdk/basic-auth-optional/client/client.go create mode 100644 seed/go-sdk/basic-auth-optional/client/client_test.go create mode 100644 seed/go-sdk/basic-auth-optional/core/api_error.go create mode 100644 seed/go-sdk/basic-auth-optional/core/http.go create mode 100644 seed/go-sdk/basic-auth-optional/core/request_option.go create mode 100644 seed/go-sdk/basic-auth-optional/dynamic-snippets/example0/snippet.go create mode 100644 seed/go-sdk/basic-auth-optional/dynamic-snippets/example1/snippet.go create mode 100644 seed/go-sdk/basic-auth-optional/dynamic-snippets/example2/snippet.go create mode 100644 seed/go-sdk/basic-auth-optional/dynamic-snippets/example3/snippet.go create mode 100644 seed/go-sdk/basic-auth-optional/dynamic-snippets/example4/snippet.go create mode 100644 seed/go-sdk/basic-auth-optional/dynamic-snippets/example5/snippet.go create mode 100644 seed/go-sdk/basic-auth-optional/dynamic-snippets/example6/snippet.go create mode 100644 seed/go-sdk/basic-auth-optional/error_codes.go create mode 100644 seed/go-sdk/basic-auth-optional/errors.go create mode 100644 seed/go-sdk/basic-auth-optional/file_param.go create mode 100644 seed/go-sdk/basic-auth-optional/go.mod create mode 100644 seed/go-sdk/basic-auth-optional/go.sum create mode 100644 seed/go-sdk/basic-auth-optional/internal/caller.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/caller_test.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/error_decoder.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/error_decoder_test.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/explicit_fields.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/explicit_fields_test.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/extra_properties.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/extra_properties_test.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/http.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/query.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/query_test.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/retrier.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/retrier_test.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/stringer.go create mode 100644 seed/go-sdk/basic-auth-optional/internal/time.go create mode 100644 seed/go-sdk/basic-auth-optional/option/request_option.go create mode 100644 seed/go-sdk/basic-auth-optional/pointer.go create mode 100644 seed/go-sdk/basic-auth-optional/pointer_test.go create mode 100644 seed/go-sdk/basic-auth-optional/reference.md create mode 100644 seed/go-sdk/basic-auth-optional/snippet.json create mode 100644 seed/go-sdk/basic-auth-optional/types.go create mode 100644 seed/go-sdk/basic-auth-optional/types_test.go create mode 100644 seed/java-sdk/basic-auth-optional/.fern/metadata.json create mode 100644 seed/java-sdk/basic-auth-optional/.github/workflows/ci.yml create mode 100644 seed/java-sdk/basic-auth-optional/.gitignore create mode 100644 seed/java-sdk/basic-auth-optional/README.md create mode 100644 seed/java-sdk/basic-auth-optional/build.gradle create mode 100644 seed/java-sdk/basic-auth-optional/reference.md create mode 100644 seed/java-sdk/basic-auth-optional/sample-app/build.gradle create mode 100644 seed/java-sdk/basic-auth-optional/sample-app/src/main/java/sample/App.java create mode 100644 seed/java-sdk/basic-auth-optional/settings.gradle create mode 100644 seed/java-sdk/basic-auth-optional/snippet.json create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/AsyncSeedBasicAuthOptionalClient.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/AsyncSeedBasicAuthOptionalClientBuilder.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/SeedBasicAuthOptionalClient.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/SeedBasicAuthOptionalClientBuilder.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ClientOptions.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ConsoleLogger.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/DateTimeDeserializer.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/DoubleSerializer.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Environment.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/FileStream.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ILogger.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/InputStreamRequestBody.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LogConfig.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LogLevel.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Logger.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LoggingInterceptor.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/MediaTypes.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Nullable.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/NullableNonemptyFilter.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ObjectMappers.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/QueryStringMapper.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/RequestOptions.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ResponseBodyInputStream.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ResponseBodyReader.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/RetryInterceptor.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Rfc2822DateTimeDeserializer.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalApiException.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalException.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalHttpResponse.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SseEvent.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SseEventParser.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Stream.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Suppliers.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/AsyncBasicAuthClient.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/AsyncRawBasicAuthClient.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/BasicAuthClient.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/RawBasicAuthClient.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/errors/BadRequest.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/errors/UnauthorizedRequest.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/types/UnauthorizedRequestErrorBody.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example0.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example1.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example2.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example3.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example4.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example5.java create mode 100644 seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example6.java create mode 100644 seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/StreamTest.java create mode 100644 seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/TestClient.java create mode 100644 seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/core/QueryStringMapperTest.java create mode 100644 seed/php-sdk/basic-auth-optional/.fern/metadata.json create mode 100644 seed/php-sdk/basic-auth-optional/.github/workflows/ci.yml create mode 100644 seed/php-sdk/basic-auth-optional/.gitignore create mode 100644 seed/php-sdk/basic-auth-optional/README.md create mode 100644 seed/php-sdk/basic-auth-optional/composer.json create mode 100644 seed/php-sdk/basic-auth-optional/phpstan.neon create mode 100644 seed/php-sdk/basic-auth-optional/phpunit.xml create mode 100644 seed/php-sdk/basic-auth-optional/reference.md create mode 100644 seed/php-sdk/basic-auth-optional/snippet.json create mode 100644 seed/php-sdk/basic-auth-optional/src/BasicAuth/BasicAuthClient.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Client/BaseApiRequest.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Client/HttpClientBuilder.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Client/HttpMethod.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Client/MockHttpClient.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Client/RawClient.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Client/RetryDecoratingClient.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Json/JsonApiRequest.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Json/JsonDecoder.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Json/JsonDeserializer.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Json/JsonEncoder.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Json/JsonProperty.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Json/JsonSerializableType.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Json/JsonSerializer.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Json/Utils.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartApiRequest.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartFormData.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartFormDataPart.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Types/ArrayType.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Types/Constant.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Types/Date.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Core/Types/Union.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Errors/Types/UnauthorizedRequestErrorBody.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Exceptions/SeedApiException.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Exceptions/SeedException.php create mode 100644 seed/php-sdk/basic-auth-optional/src/SeedClient.php create mode 100644 seed/php-sdk/basic-auth-optional/src/Utils/File.php create mode 100644 seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example0/snippet.php create mode 100644 seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example1/snippet.php create mode 100644 seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example2/snippet.php create mode 100644 seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example3/snippet.php create mode 100644 seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example4/snippet.php create mode 100644 seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example5/snippet.php create mode 100644 seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example6/snippet.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Client/RawClientTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/AdditionalPropertiesTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/DateArrayTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/EmptyArrayTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/EnumTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/ExhaustiveTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/InvalidTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/NestedUnionArrayTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/NullPropertyTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/NullableArrayTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/ScalarTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/TraitTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/UnionArrayTest.php create mode 100644 seed/php-sdk/basic-auth-optional/tests/Core/Json/UnionPropertyTest.php create mode 100644 seed/python-sdk/basic-auth-optional/.fern/metadata.json create mode 100644 seed/python-sdk/basic-auth-optional/.github/workflows/ci.yml create mode 100644 seed/python-sdk/basic-auth-optional/.gitignore create mode 100644 seed/python-sdk/basic-auth-optional/README.md create mode 100644 seed/python-sdk/basic-auth-optional/poetry.lock create mode 100644 seed/python-sdk/basic-auth-optional/pyproject.toml create mode 100644 seed/python-sdk/basic-auth-optional/reference.md create mode 100644 seed/python-sdk/basic-auth-optional/requirements.txt create mode 100644 seed/python-sdk/basic-auth-optional/snippet.json create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/__init__.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/basic_auth/__init__.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/basic_auth/client.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/basic_auth/raw_client.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/client.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/__init__.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/api_error.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/client_wrapper.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/datetime_utils.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/file.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/force_multipart.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/http_client.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/http_response.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/__init__.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_api.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_decoders.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_exceptions.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_models.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/jsonable_encoder.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/logging.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/parse_error.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/pydantic_utilities.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/query_encoder.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/remove_none_from_dict.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/request_options.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/core/serialization.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/errors/__init__.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/errors/errors/__init__.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/errors/errors/bad_request.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/errors/errors/unauthorized_request.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/errors/types/__init__.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/errors/types/unauthorized_request_error_body.py create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/py.typed create mode 100644 seed/python-sdk/basic-auth-optional/src/seed/version.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/custom/test_client.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/__init__.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/assets/models/__init__.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/assets/models/circle.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/assets/models/color.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/assets/models/object_with_defaults.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/assets/models/object_with_optional_field.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/assets/models/shape.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/assets/models/square.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/assets/models/undiscriminated_shape.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/test_http_client.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/test_query_encoding.py create mode 100644 seed/python-sdk/basic-auth-optional/tests/utils/test_serialization.py create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/.fern/metadata.json create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/.github/workflows/ci.yml create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/.gitignore create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/.rubocop.yml create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/Gemfile create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/Gemfile.custom create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/README.md create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/Rakefile create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/custom.gemspec.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example0/snippet.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example1/snippet.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example2/snippet.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example3/snippet.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example4/snippet.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example5/snippet.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example6/snippet.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/basic_auth/client.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/client.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/api_error.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/client_error.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/redirect_error.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/response_error.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/server_error.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/timeout_error.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/types/unauthorized_request_error_body.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/errors/constraint_error.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/errors/type_error.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/http/base_request.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/http/raw_client.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/cursor_item_iterator.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/cursor_page_iterator.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/item_iterator.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/offset_item_iterator.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/offset_page_iterator.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/json/request.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/json/serializable.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_encoder.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_form_data.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_form_data_part.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_request.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/array.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/boolean.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/enum.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/hash.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/model.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/model/field.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/type.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/union.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/unknown.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/utils.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/lib/seed/version.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/reference.md create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/seed.gemspec create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/snippet.json create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/custom.test.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/test_helper.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/iterators/test_cursor_item_iterator.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/iterators/test_offset_item_iterator.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_array.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_boolean.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_enum.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_hash.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_model.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_union.rb create mode 100644 seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_utils.rb create mode 100644 seed/ts-sdk/basic-auth-optional/.fern/metadata.json create mode 100644 seed/ts-sdk/basic-auth-optional/.github/workflows/ci.yml create mode 100644 seed/ts-sdk/basic-auth-optional/.gitignore create mode 100644 seed/ts-sdk/basic-auth-optional/CONTRIBUTING.md create mode 100644 seed/ts-sdk/basic-auth-optional/README.md create mode 100644 seed/ts-sdk/basic-auth-optional/biome.json create mode 100644 seed/ts-sdk/basic-auth-optional/package.json create mode 100644 seed/ts-sdk/basic-auth-optional/pnpm-workspace.yaml create mode 100644 seed/ts-sdk/basic-auth-optional/reference.md create mode 100644 seed/ts-sdk/basic-auth-optional/scripts/rename-to-esm-files.js create mode 100644 seed/ts-sdk/basic-auth-optional/snippet.json create mode 100644 seed/ts-sdk/basic-auth-optional/src/BaseClient.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/Client.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/client/Client.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/client/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/exports.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/BadRequest.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/UnauthorizedRequest.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/errors/exports.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/errors/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/errors/types/UnauthorizedRequestErrorBody.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/errors/types/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/api/resources/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/auth/BasicAuthProvider.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/auth/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/auth/AuthProvider.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/auth/AuthRequest.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/auth/BasicAuth.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/auth/BearerToken.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/auth/NoOpAuthProvider.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/auth/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/base64.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/exports.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/APIResponse.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/BinaryResponse.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/EndpointMetadata.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/EndpointSupplier.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/Fetcher.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/Headers.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/HttpResponsePromise.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/RawResponse.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/Supplier.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/createRequestUrl.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/getErrorResponseBody.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/getFetchFn.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/getHeader.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/getRequestBody.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/getResponseBody.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/makePassthroughRequest.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/makeRequest.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/requestWithRetries.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/fetcher/signals.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/headers.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/json.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/logging/exports.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/logging/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/logging/logger.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/runtime/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/runtime/runtime.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/url/encodePathParam.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/url/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/url/join.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/core/url/qs.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/errors/SeedBasicAuthOptionalError.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/errors/SeedBasicAuthOptionalTimeoutError.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/errors/handleNonStatusCodeError.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/errors/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/exports.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/index.ts create mode 100644 seed/ts-sdk/basic-auth-optional/src/version.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/custom.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/mock-server/MockServer.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/mock-server/MockServerPool.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/mock-server/mockEndpointBuilder.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/mock-server/randomBaseUrl.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/mock-server/setup.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/mock-server/withFormUrlEncoded.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/mock-server/withHeaders.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/mock-server/withJson.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/setup.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/tsconfig.json create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/auth/BasicAuth.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/auth/BearerToken.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/base64.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/Fetcher.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/HttpResponsePromise.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/RawResponse.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/createRequestUrl.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/getRequestBody.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/getResponseBody.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/logging.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/makePassthroughRequest.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/makeRequest.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/redacting.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/requestWithRetries.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/signals.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/test-file.txt create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/logging/logger.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/url/join.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/unit/url/qs.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tests/wire/.gitkeep create mode 100644 seed/ts-sdk/basic-auth-optional/tests/wire/basicAuth.test.ts create mode 100644 seed/ts-sdk/basic-auth-optional/tsconfig.base.json create mode 100644 seed/ts-sdk/basic-auth-optional/tsconfig.cjs.json create mode 100644 seed/ts-sdk/basic-auth-optional/tsconfig.esm.json create mode 100644 seed/ts-sdk/basic-auth-optional/tsconfig.json create mode 100644 seed/ts-sdk/basic-auth-optional/vitest.config.mts create mode 100644 test-definitions/fern/apis/basic-auth-optional/definition/api.yml create mode 100644 test-definitions/fern/apis/basic-auth-optional/definition/basic-auth.yml create mode 100644 test-definitions/fern/apis/basic-auth-optional/definition/errors.yml create mode 100644 test-definitions/fern/apis/basic-auth-optional/generators.yml diff --git a/seed/csharp-sdk/basic-auth-optional/.editorconfig b/seed/csharp-sdk/basic-auth-optional/.editorconfig new file mode 100644 index 000000000000..1e7a0adbac80 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/.editorconfig @@ -0,0 +1,35 @@ +root = true + +[*.cs] +resharper_arrange_object_creation_when_type_evident_highlighting = hint +resharper_auto_property_can_be_made_get_only_global_highlighting = hint +resharper_check_namespace_highlighting = hint +resharper_class_never_instantiated_global_highlighting = hint +resharper_class_never_instantiated_local_highlighting = hint +resharper_collection_never_updated_global_highlighting = hint +resharper_convert_type_check_pattern_to_null_check_highlighting = hint +resharper_inconsistent_naming_highlighting = hint +resharper_member_can_be_private_global_highlighting = hint +resharper_member_hides_static_from_outer_class_highlighting = hint +resharper_not_accessed_field_local_highlighting = hint +resharper_nullable_warning_suppression_is_used_highlighting = suggestion +resharper_partial_type_with_single_part_highlighting = hint +resharper_prefer_concrete_value_over_default_highlighting = none +resharper_private_field_can_be_converted_to_local_variable_highlighting = hint +resharper_property_can_be_made_init_only_global_highlighting = hint +resharper_property_can_be_made_init_only_local_highlighting = hint +resharper_redundant_name_qualifier_highlighting = none +resharper_redundant_using_directive_highlighting = hint +resharper_replace_slice_with_range_indexer_highlighting = none +resharper_unused_auto_property_accessor_global_highlighting = hint +resharper_unused_auto_property_accessor_local_highlighting = hint +resharper_unused_member_global_highlighting = hint +resharper_unused_type_global_highlighting = hint +resharper_use_string_interpolation_highlighting = hint +dotnet_diagnostic.CS1591.severity = suggestion + +[src/**/Types/*.cs] +resharper_check_namespace_highlighting = none + +[src/**/Core/Public/*.cs] +resharper_check_namespace_highlighting = none \ No newline at end of file diff --git a/seed/csharp-sdk/basic-auth-optional/.fern/metadata.json b/seed/csharp-sdk/basic-auth-optional/.fern/metadata.json new file mode 100644 index 000000000000..91d0855bee07 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/.fern/metadata.json @@ -0,0 +1,8 @@ +{ + "cliVersion": "DUMMY", + "generatorName": "fernapi/fern-csharp-sdk", + "generatorVersion": "latest", + "generatorConfig": {}, + "originGitCommit": "DUMMY", + "sdkVersion": "0.0.1" +} \ No newline at end of file diff --git a/seed/csharp-sdk/basic-auth-optional/.github/workflows/ci.yml b/seed/csharp-sdk/basic-auth-optional/.github/workflows/ci.yml new file mode 100644 index 000000000000..5a0b0300d85c --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/.github/workflows/ci.yml @@ -0,0 +1,52 @@ +name: ci + +on: [push] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +env: + DOTNET_NOLOGO: true + +jobs: + ci: + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v6 + + - name: Setup .NET + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 10.x + + - name: Install tools + run: dotnet tool restore + + - name: Restore dependencies + run: dotnet restore src/SeedBasicAuthOptional/SeedBasicAuthOptional.csproj + + - name: Build + run: dotnet build src/SeedBasicAuthOptional/SeedBasicAuthOptional.csproj --no-restore -c Release + + - name: Restore test dependencies + run: dotnet restore src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.csproj + + - name: Build tests + run: dotnet build src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.csproj --no-restore -c Release + + - name: Test + run: dotnet test src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.csproj --no-restore --no-build -c Release + + - name: Pack + if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') + run: dotnet pack src/SeedBasicAuthOptional/SeedBasicAuthOptional.csproj --no-build --no-restore -c Release + + - name: Publish to NuGet.org + if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') + env: + NUGET_API_KEY: ${{ secrets.NUGET_API_TOKEN }} + run: dotnet nuget push src/SeedBasicAuthOptional/bin/Release/*.nupkg --api-key $NUGET_API_KEY --source "nuget.org" + diff --git a/seed/csharp-sdk/basic-auth-optional/.gitignore b/seed/csharp-sdk/basic-auth-optional/.gitignore new file mode 100644 index 000000000000..11014f2b33d7 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/.gitignore @@ -0,0 +1,484 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +## This is based on `dotnet new gitignore` and customized by Fern + +# dotenv files +.env + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +# [Rr]elease/ (Ignored by Fern) +# [Rr]eleases/ (Ignored by Fern) +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +# [Ll]og/ (Ignored by Fern) +# [Ll]ogs/ (Ignored by Fern) + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET +project.lock.json +project.fragment.lock.json +artifacts/ + +# Tye +.tye/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files +*.ncb +*.aps + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +*.sln.iml +.idea + +## +## Visual studio for Mac +## + + +# globs +Makefile.in +*.userprefs +*.usertasks +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.tar.gz +tarballs/ +test-results/ + +# Mac bundle stuff +*.dmg +*.app + +# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# Vim temporary swap files +*.swp diff --git a/seed/csharp-sdk/basic-auth-optional/README.md b/seed/csharp-sdk/basic-auth-optional/README.md new file mode 100644 index 000000000000..68e678269f42 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/README.md @@ -0,0 +1,172 @@ +# Seed C# Library + +[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=Seed%2FC%23) +[![nuget shield](https://img.shields.io/nuget/v/Fernbasic-auth-optional)](https://nuget.org/packages/Fernbasic-auth-optional) + +The Seed C# library provides convenient access to the Seed APIs from C#. + +## Table of Contents + +- [Requirements](#requirements) +- [Installation](#installation) +- [Reference](#reference) +- [Usage](#usage) +- [Exception Handling](#exception-handling) +- [Advanced](#advanced) + - [Retries](#retries) + - [Timeouts](#timeouts) + - [Raw Response](#raw-response) + - [Additional Headers](#additional-headers) + - [Additional Query Parameters](#additional-query-parameters) +- [Contributing](#contributing) + +## Requirements + +This SDK requires: + +## Installation + +```sh +dotnet add package Fernbasic-auth-optional +``` + +## Reference + +A full reference for this library is available [here](./reference.md). + +## Usage + +Instantiate and use the client with the following: + +```csharp +using SeedBasicAuthOptional; + +var client = new SeedBasicAuthOptionalClient("USERNAME", "PASSWORD"); +await client.BasicAuth.PostWithBasicAuthAsync( + new Dictionary() { { "key", "value" } } +); +``` + +## Exception Handling + +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error +will be thrown. + +```csharp +using SeedBasicAuthOptional; + +try { + var response = await client.BasicAuth.PostWithBasicAuthAsync(...); +} catch (SeedBasicAuthOptionalApiException e) { + System.Console.WriteLine(e.Body); + System.Console.WriteLine(e.StatusCode); +} +``` + +## Advanced + +### Retries + +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured +retry limit (default: 2). + +A request is deemed retryable when any of the following HTTP status codes is returned: + +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) + +Use the `MaxRetries` request option to configure this behavior. + +```csharp +var response = await client.BasicAuth.PostWithBasicAuthAsync( + ..., + new RequestOptions { + MaxRetries: 0 // Override MaxRetries at the request level + } +); +``` + +### Timeouts + +The SDK defaults to a 30 second timeout. Use the `Timeout` option to configure this behavior. + +```csharp +var response = await client.BasicAuth.PostWithBasicAuthAsync( + ..., + new RequestOptions { + Timeout: TimeSpan.FromSeconds(3) // Override timeout to 3s + } +); +``` + +### Raw Response + +Access raw HTTP response data (status code, headers, URL) alongside parsed response data using the `.WithRawResponse()` method. + +```csharp +using SeedBasicAuthOptional; + +// Access raw response data (status code, headers, etc.) alongside the parsed response +var result = await client.BasicAuth.PostWithBasicAuthAsync(...).WithRawResponse(); + +// Access the parsed data +var data = result.Data; + +// Access raw response metadata +var statusCode = result.RawResponse.StatusCode; +var headers = result.RawResponse.Headers; +var url = result.RawResponse.Url; + +// Access specific headers (case-insensitive) +if (headers.TryGetValue("X-Request-Id", out var requestId)) +{ + System.Console.WriteLine($"Request ID: {requestId}"); +} + +// For the default behavior, simply await without .WithRawResponse() +var data = await client.BasicAuth.PostWithBasicAuthAsync(...); +``` + +### Additional Headers + +If you would like to send additional headers as part of the request, use the `AdditionalHeaders` request option. + +```csharp +var response = await client.BasicAuth.PostWithBasicAuthAsync( + ..., + new RequestOptions { + AdditionalHeaders = new Dictionary + { + { "X-Custom-Header", "custom-value" } + } + } +); +``` + +### Additional Query Parameters + +If you would like to send additional query parameters as part of the request, use the `AdditionalQueryParameters` request option. + +```csharp +var response = await client.BasicAuth.PostWithBasicAuthAsync( + ..., + new RequestOptions { + AdditionalQueryParameters = new Dictionary + { + { "custom_param", "custom-value" } + } + } +); +``` + +## Contributing + +While we value open-source contributions to this SDK, this library is generated programmatically. +Additions made directly to this library would have to be moved over to our generation code, +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening +an issue first to discuss with us! + +On the other hand, contributions to the README are always very welcome! diff --git a/seed/csharp-sdk/basic-auth-optional/SeedBasicAuthOptional.slnx b/seed/csharp-sdk/basic-auth-optional/SeedBasicAuthOptional.slnx new file mode 100644 index 000000000000..9870a035ef0a --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/SeedBasicAuthOptional.slnx @@ -0,0 +1,4 @@ + + + + diff --git a/seed/csharp-sdk/basic-auth-optional/reference.md b/seed/csharp-sdk/basic-auth-optional/reference.md new file mode 100644 index 000000000000..3109a2c4b705 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/reference.md @@ -0,0 +1,97 @@ +# Reference +## BasicAuth +
client.BasicAuth.GetWithBasicAuthAsync() -> WithRawResponseTask<bool> +
+
+ +#### 📝 Description + +
+
+ +
+
+ +GET request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```csharp +await client.BasicAuth.GetWithBasicAuthAsync(); +``` +
+
+
+
+ + +
+
+
+ +
client.BasicAuth.PostWithBasicAuthAsync(object { ... }) -> WithRawResponseTask<bool> +
+
+ +#### 📝 Description + +
+
+ +
+
+ +POST request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```csharp +await client.BasicAuth.PostWithBasicAuthAsync( + new Dictionary() { { "key", "value" } } +); +``` +
+
+
+
+ +#### ⚙️ Parameters + +
+
+ +
+
+ +**request:** `object` + +
+
+
+
+ + +
+
+
+ diff --git a/seed/csharp-sdk/basic-auth-optional/snippet.json b/seed/csharp-sdk/basic-auth-optional/snippet.json new file mode 100644 index 000000000000..3c004d73f893 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/snippet.json @@ -0,0 +1,29 @@ +{ + "types": {}, + "endpoints": [ + { + "example_identifier": null, + "id": { + "path": "/basic-auth", + "method": "GET", + "identifier_override": "endpoint_basic-auth.getWithBasicAuth" + }, + "snippet": { + "type": "csharp", + "client": "using SeedBasicAuthOptional;\n\nvar client = new SeedBasicAuthOptionalClient(\"USERNAME\", \"PASSWORD\");\nawait client.BasicAuth.GetWithBasicAuthAsync();\n" + } + }, + { + "example_identifier": null, + "id": { + "path": "/basic-auth", + "method": "POST", + "identifier_override": "endpoint_basic-auth.postWithBasicAuth" + }, + "snippet": { + "type": "csharp", + "client": "using SeedBasicAuthOptional;\n\nvar client = new SeedBasicAuthOptionalClient(\"USERNAME\", \"PASSWORD\");\nawait client.BasicAuth.PostWithBasicAuthAsync(\n new Dictionary() { { \"key\", \"value\" } }\n);\n" + } + } + ] +} \ No newline at end of file diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example0.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example0.cs new file mode 100644 index 000000000000..0a6d097846c7 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example0.cs @@ -0,0 +1,19 @@ +using SeedBasicAuthOptional; + +namespace Usage; + +public class Example0 +{ + public async Task Do() { + var client = new SeedBasicAuthOptionalClient( + username: "", + password: "", + clientOptions: new ClientOptions { + BaseUrl = "https://api.fern.com" + } + ); + + await client.BasicAuth.GetWithBasicAuthAsync(); + } + +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example1.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example1.cs new file mode 100644 index 000000000000..2b220847e903 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example1.cs @@ -0,0 +1,19 @@ +using SeedBasicAuthOptional; + +namespace Usage; + +public class Example1 +{ + public async Task Do() { + var client = new SeedBasicAuthOptionalClient( + username: "", + password: "", + clientOptions: new ClientOptions { + BaseUrl = "https://api.fern.com" + } + ); + + await client.BasicAuth.GetWithBasicAuthAsync(); + } + +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example2.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example2.cs new file mode 100644 index 000000000000..de30b520c866 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example2.cs @@ -0,0 +1,19 @@ +using SeedBasicAuthOptional; + +namespace Usage; + +public class Example2 +{ + public async Task Do() { + var client = new SeedBasicAuthOptionalClient( + username: "", + password: "", + clientOptions: new ClientOptions { + BaseUrl = "https://api.fern.com" + } + ); + + await client.BasicAuth.GetWithBasicAuthAsync(); + } + +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example3.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example3.cs new file mode 100644 index 000000000000..1994ca07d935 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example3.cs @@ -0,0 +1,24 @@ +using SeedBasicAuthOptional; + +namespace Usage; + +public class Example3 +{ + public async Task Do() { + var client = new SeedBasicAuthOptionalClient( + username: "", + password: "", + clientOptions: new ClientOptions { + BaseUrl = "https://api.fern.com" + } + ); + + await client.BasicAuth.PostWithBasicAuthAsync( + new Dictionary() + { + ["key"] = "value", + } + ); + } + +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example4.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example4.cs new file mode 100644 index 000000000000..2b5d846975d2 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example4.cs @@ -0,0 +1,24 @@ +using SeedBasicAuthOptional; + +namespace Usage; + +public class Example4 +{ + public async Task Do() { + var client = new SeedBasicAuthOptionalClient( + username: "", + password: "", + clientOptions: new ClientOptions { + BaseUrl = "https://api.fern.com" + } + ); + + await client.BasicAuth.PostWithBasicAuthAsync( + new Dictionary() + { + ["key"] = "value", + } + ); + } + +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example5.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example5.cs new file mode 100644 index 000000000000..fecaff239a15 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example5.cs @@ -0,0 +1,24 @@ +using SeedBasicAuthOptional; + +namespace Usage; + +public class Example5 +{ + public async Task Do() { + var client = new SeedBasicAuthOptionalClient( + username: "", + password: "", + clientOptions: new ClientOptions { + BaseUrl = "https://api.fern.com" + } + ); + + await client.BasicAuth.PostWithBasicAuthAsync( + new Dictionary() + { + ["key"] = "value", + } + ); + } + +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example6.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example6.cs new file mode 100644 index 000000000000..52319b55bf66 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/Example6.cs @@ -0,0 +1,24 @@ +using SeedBasicAuthOptional; + +namespace Usage; + +public class Example6 +{ + public async Task Do() { + var client = new SeedBasicAuthOptionalClient( + username: "", + password: "", + clientOptions: new ClientOptions { + BaseUrl = "https://api.fern.com" + } + ); + + await client.BasicAuth.PostWithBasicAuthAsync( + new Dictionary() + { + ["key"] = "value", + } + ); + } + +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/SeedApi.DynamicSnippets.csproj b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/SeedApi.DynamicSnippets.csproj new file mode 100644 index 000000000000..3417db2e58e2 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedApi.DynamicSnippets/SeedApi.DynamicSnippets.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + 12 + enable + enable + + + + + + \ No newline at end of file diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/HeadersBuilderTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/HeadersBuilderTests.cs new file mode 100644 index 000000000000..bc0e7759e1f1 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/HeadersBuilderTests.cs @@ -0,0 +1,326 @@ +using NUnit.Framework; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional.Test.Core; + +[TestFixture] +public class HeadersBuilderTests +{ + [Test] + public async global::System.Threading.Tasks.Task Add_SimpleHeaders() + { + var headers = await new HeadersBuilder.Builder() + .Add("Content-Type", "application/json") + .Add("Authorization", "Bearer token123") + .Add("X-API-Key", "key456") + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(headers.Count, Is.EqualTo(3)); + Assert.That(headers["Content-Type"], Is.EqualTo("application/json")); + Assert.That(headers["Authorization"], Is.EqualTo("Bearer token123")); + Assert.That(headers["X-API-Key"], Is.EqualTo("key456")); + } + + [Test] + public async global::System.Threading.Tasks.Task Add_NullValuesIgnored() + { + var headers = await new HeadersBuilder.Builder() + .Add("Header1", "value1") + .Add("Header2", null) + .Add("Header3", "value3") + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(headers.Count, Is.EqualTo(2)); + Assert.That(headers.ContainsKey("Header1"), Is.True); + Assert.That(headers.ContainsKey("Header2"), Is.False); + Assert.That(headers.ContainsKey("Header3"), Is.True); + } + + [Test] + public async global::System.Threading.Tasks.Task Add_OverwritesExistingHeader() + { + var headers = await new HeadersBuilder.Builder() + .Add("Content-Type", "application/json") + .Add("Content-Type", "application/xml") + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(headers.Count, Is.EqualTo(1)); + Assert.That(headers["Content-Type"], Is.EqualTo("application/xml")); + } + + [Test] + public async global::System.Threading.Tasks.Task Add_HeadersOverload_MergesExistingHeaders() + { + var existingHeaders = new Headers( + new Dictionary { { "Header1", "value1" }, { "Header2", "value2" } } + ); + + var result = await new HeadersBuilder.Builder() + .Add("Header3", "value3") + .Add(existingHeaders) + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(result.Count, Is.EqualTo(3)); + Assert.That(result["Header1"], Is.EqualTo("value1")); + Assert.That(result["Header2"], Is.EqualTo("value2")); + Assert.That(result["Header3"], Is.EqualTo("value3")); + } + + [Test] + public async global::System.Threading.Tasks.Task Add_HeadersOverload_OverwritesExistingHeaders() + { + var existingHeaders = new Headers( + new Dictionary { { "Header1", "override" } } + ); + + var result = await new HeadersBuilder.Builder() + .Add("Header1", "original") + .Add("Header2", "keep") + .Add(existingHeaders) + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(result.Count, Is.EqualTo(2)); + Assert.That(result["Header1"], Is.EqualTo("override")); + Assert.That(result["Header2"], Is.EqualTo("keep")); + } + + [Test] + public async global::System.Threading.Tasks.Task Add_HeadersOverload_NullHeadersIgnored() + { + var result = await new HeadersBuilder.Builder() + .Add("Header1", "value1") + .Add((Headers?)null) + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(result.Count, Is.EqualTo(1)); + Assert.That(result["Header1"], Is.EqualTo("value1")); + } + + [Test] + public async global::System.Threading.Tasks.Task Add_KeyValuePairOverload_AddsHeaders() + { + var additionalHeaders = new List> + { + new("Header1", "value1"), + new("Header2", "value2"), + }; + + var headers = await new HeadersBuilder.Builder() + .Add("Header3", "value3") + .Add(additionalHeaders) + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(headers.Count, Is.EqualTo(3)); + Assert.That(headers["Header1"], Is.EqualTo("value1")); + Assert.That(headers["Header2"], Is.EqualTo("value2")); + Assert.That(headers["Header3"], Is.EqualTo("value3")); + } + + [Test] + public async global::System.Threading.Tasks.Task Add_KeyValuePairOverload_IgnoresNullValues() + { + var additionalHeaders = new List> + { + new("Header1", "value1"), + new("Header2", null), // Should be ignored + }; + + var headers = await new HeadersBuilder.Builder() + .Add(additionalHeaders) + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(headers.Count, Is.EqualTo(1)); + Assert.That(headers.ContainsKey("Header2"), Is.False); + } + + [Test] + public async global::System.Threading.Tasks.Task Add_DictionaryOverload_AddsHeaders() + { + var dict = new Dictionary + { + { "Header1", "value1" }, + { "Header2", "value2" }, + }; + + var headers = await new HeadersBuilder.Builder() + .Add("Header3", "value3") + .Add(dict) + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(headers.Count, Is.EqualTo(3)); + Assert.That(headers["Header1"], Is.EqualTo("value1")); + Assert.That(headers["Header2"], Is.EqualTo("value2")); + Assert.That(headers["Header3"], Is.EqualTo("value3")); + } + + [Test] + public async global::System.Threading.Tasks.Task EmptyBuilder_ReturnsEmptyHeaders() + { + var headers = await new HeadersBuilder.Builder().BuildAsync().ConfigureAwait(false); + + Assert.That(headers.Count, Is.EqualTo(0)); + } + + [Test] + public async global::System.Threading.Tasks.Task OnlyNullValues_ReturnsEmptyHeaders() + { + var headers = await new HeadersBuilder.Builder() + .Add("Header1", null) + .Add("Header2", null) + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(headers.Count, Is.EqualTo(0)); + } + + [Test] + public async global::System.Threading.Tasks.Task ComplexMergingScenario() + { + // Simulates real SDK usage: endpoint headers + client headers + request options + var clientHeaders = new Headers( + new Dictionary + { + { "X-Client-Version", "1.0.0" }, + { "User-Agent", "MyClient/1.0" }, + } + ); + + var clientAdditionalHeaders = new List> + { + new("X-Custom-Header", "custom-value"), + }; + + var requestOptionsHeaders = new Headers( + new Dictionary + { + { "Authorization", "Bearer user-token" }, + { "User-Agent", "MyClient/2.0" }, // Override + } + ); + + var requestAdditionalHeaders = new List> + { + new("X-Request-ID", "req-123"), + new("X-Custom-Header", "overridden-value"), // Override + }; + + var headers = await new HeadersBuilder.Builder() + .Add("Content-Type", "application/json") // Endpoint header + .Add("X-Endpoint-ID", "endpoint-1") + .Add(clientHeaders) + .Add(clientAdditionalHeaders) + .Add(requestOptionsHeaders) + .Add(requestAdditionalHeaders) + .BuildAsync() + .ConfigureAwait(false); + + // Verify precedence + Assert.That(headers["Content-Type"], Is.EqualTo("application/json")); + Assert.That(headers["X-Endpoint-ID"], Is.EqualTo("endpoint-1")); + Assert.That(headers["X-Client-Version"], Is.EqualTo("1.0.0")); + Assert.That(headers["User-Agent"], Is.EqualTo("MyClient/2.0")); // Overridden + Assert.That(headers["Authorization"], Is.EqualTo("Bearer user-token")); + Assert.That(headers["X-Request-ID"], Is.EqualTo("req-123")); + Assert.That(headers["X-Custom-Header"], Is.EqualTo("overridden-value")); // Overridden + } + + [Test] + public async global::System.Threading.Tasks.Task Builder_WithCapacity() + { + // Test that capacity constructor works without errors + var headers = await new HeadersBuilder.Builder(capacity: 10) + .Add("Header1", "value1") + .Add("Header2", "value2") + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(headers.Count, Is.EqualTo(2)); + Assert.That(headers["Header1"], Is.EqualTo("value1")); + Assert.That(headers["Header2"], Is.EqualTo("value2")); + } + + [Test] + public async global::System.Threading.Tasks.Task Add_HeadersOverload_ResolvesDynamicHeaderValues() + { + // Test that BuildAsync properly resolves HeaderValue instances + var existingHeaders = new Headers(); + existingHeaders["DynamicHeader"] = + (Func>)( + () => global::System.Threading.Tasks.Task.FromResult("dynamic-value") + ); + + var result = await new HeadersBuilder.Builder() + .Add("StaticHeader", "static-value") + .Add(existingHeaders) + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(result.Count, Is.EqualTo(2)); + Assert.That(result["StaticHeader"], Is.EqualTo("static-value")); + Assert.That(result["DynamicHeader"], Is.EqualTo("dynamic-value")); + } + + [Test] + public async global::System.Threading.Tasks.Task MultipleSyncAdds() + { + var headers1 = new Headers(new Dictionary { { "H1", "v1" } }); + var headers2 = new Headers(new Dictionary { { "H2", "v2" } }); + var headers3 = new Headers(new Dictionary { { "H3", "v3" } }); + + var result = await new HeadersBuilder.Builder() + .Add(headers1) + .Add(headers2) + .Add(headers3) + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(result.Count, Is.EqualTo(3)); + Assert.That(result["H1"], Is.EqualTo("v1")); + Assert.That(result["H2"], Is.EqualTo("v2")); + Assert.That(result["H3"], Is.EqualTo("v3")); + } + + [Test] + public async global::System.Threading.Tasks.Task PrecedenceOrder_LatestWins() + { + // Test that later operations override earlier ones + var headers1 = new Headers(new Dictionary { { "Key", "value1" } }); + var headers2 = new Headers(new Dictionary { { "Key", "value2" } }); + var additional = new List> { new("Key", "value3") }; + + var result = await new HeadersBuilder.Builder() + .Add("Key", "value0") + .Add(headers1) + .Add(headers2) + .Add(additional) + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(result["Key"], Is.EqualTo("value3")); + } + + [Test] + public async global::System.Threading.Tasks.Task CaseInsensitiveKeys() + { + // Test that header keys are case-insensitive + var headers = await new HeadersBuilder.Builder() + .Add("content-type", "application/json") + .Add("Content-Type", "application/xml") // Should overwrite + .BuildAsync() + .ConfigureAwait(false); + + Assert.That(headers.Count, Is.EqualTo(1)); + Assert.That(headers["content-type"], Is.EqualTo("application/xml")); + Assert.That(headers["Content-Type"], Is.EqualTo("application/xml")); + Assert.That(headers["CONTENT-TYPE"], Is.EqualTo("application/xml")); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/AdditionalPropertiesTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/AdditionalPropertiesTests.cs new file mode 100644 index 000000000000..fbc8b32bd84e --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/AdditionalPropertiesTests.cs @@ -0,0 +1,365 @@ +using global::System.Text.Json; +using global::System.Text.Json.Serialization; +using NUnit.Framework; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional.Test.Core.Json; + +[TestFixture] +public class AdditionalPropertiesTests +{ + [Test] + public void Record_OnDeserialized_ShouldPopulateAdditionalProperties() + { + // Arrange + const string json = """ + { + "id": "1", + "category": "fiction", + "title": "The Hobbit" + } + """; + + // Act + var record = JsonUtils.Deserialize(json); + + // Assert + Assert.That(record, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(record.Id, Is.EqualTo("1")); + Assert.That(record.AdditionalProperties["category"].GetString(), Is.EqualTo("fiction")); + Assert.That(record.AdditionalProperties["title"].GetString(), Is.EqualTo("The Hobbit")); + }); + } + + [Test] + public void RecordWithWriteableAdditionalProperties_OnSerialization_ShouldIncludeAdditionalProperties() + { + // Arrange + var record = new WriteableRecord + { + Id = "1", + AdditionalProperties = { ["category"] = "fiction", ["title"] = "The Hobbit" }, + }; + + // Act + var json = JsonUtils.Serialize(record); + var deserializedRecord = JsonUtils.Deserialize(json); + + // Assert + Assert.That(deserializedRecord, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(deserializedRecord.Id, Is.EqualTo("1")); + Assert.That( + deserializedRecord.AdditionalProperties["category"], + Is.InstanceOf() + ); + Assert.That( + ((JsonElement)deserializedRecord.AdditionalProperties["category"]!).GetString(), + Is.EqualTo("fiction") + ); + Assert.That( + deserializedRecord.AdditionalProperties["title"], + Is.InstanceOf() + ); + Assert.That( + ((JsonElement)deserializedRecord.AdditionalProperties["title"]!).GetString(), + Is.EqualTo("The Hobbit") + ); + }); + } + + [Test] + public void ReadOnlyAdditionalProperties_ShouldRetrieveValuesCorrectly() + { + // Arrange + var extensionData = new Dictionary + { + ["key1"] = JsonUtils.SerializeToElement("value1"), + ["key2"] = JsonUtils.SerializeToElement(123), + }; + var readOnlyProps = new ReadOnlyAdditionalProperties(); + readOnlyProps.CopyFromExtensionData(extensionData); + + // Act & Assert + Assert.That(readOnlyProps["key1"].GetString(), Is.EqualTo("value1")); + Assert.That(readOnlyProps["key2"].GetInt32(), Is.EqualTo(123)); + } + + [Test] + public void AdditionalProperties_ShouldBehaveAsDictionary() + { + // Arrange + var additionalProps = new AdditionalProperties { ["key1"] = "value1", ["key2"] = 123 }; + + // Act + additionalProps["key3"] = true; + + // Assert + Assert.Multiple(() => + { + Assert.That(additionalProps["key1"], Is.EqualTo("value1")); + Assert.That(additionalProps["key2"], Is.EqualTo(123)); + Assert.That((bool)additionalProps["key3"]!, Is.True); + Assert.That(additionalProps.Count, Is.EqualTo(3)); + }); + } + + [Test] + public void AdditionalProperties_ToJsonObject_ShouldSerializeCorrectly() + { + // Arrange + var additionalProps = new AdditionalProperties { ["key1"] = "value1", ["key2"] = 123 }; + + // Act + var jsonObject = additionalProps.ToJsonObject(); + + Assert.Multiple(() => + { + // Assert + Assert.That(jsonObject["key1"]!.GetValue(), Is.EqualTo("value1")); + Assert.That(jsonObject["key2"]!.GetValue(), Is.EqualTo(123)); + }); + } + + [Test] + public void AdditionalProperties_MixReadAndWrite_ShouldOverwriteDeserializedProperty() + { + // Arrange + const string json = """ + { + "id": "1", + "category": "fiction", + "title": "The Hobbit" + } + """; + var record = JsonUtils.Deserialize(json); + + // Act + record.AdditionalProperties["category"] = "non-fiction"; + + // Assert + Assert.Multiple(() => + { + Assert.That(record, Is.Not.Null); + Assert.That(record.Id, Is.EqualTo("1")); + Assert.That(record.AdditionalProperties["category"], Is.EqualTo("non-fiction")); + Assert.That(record.AdditionalProperties["title"], Is.InstanceOf()); + Assert.That( + ((JsonElement)record.AdditionalProperties["title"]!).GetString(), + Is.EqualTo("The Hobbit") + ); + }); + } + + [Test] + public void RecordWithReadonlyAdditionalPropertiesInts_OnDeserialized_ShouldPopulateAdditionalProperties() + { + // Arrange + const string json = """ + { + "extra1": 42, + "extra2": 99 + } + """; + + // Act + var record = JsonUtils.Deserialize(json); + + // Assert + Assert.That(record, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(record.AdditionalProperties["extra1"], Is.EqualTo(42)); + Assert.That(record.AdditionalProperties["extra2"], Is.EqualTo(99)); + }); + } + + [Test] + public void RecordWithAdditionalPropertiesInts_OnSerialization_ShouldIncludeAdditionalProperties() + { + // Arrange + var record = new WriteableRecordWithInts + { + AdditionalProperties = { ["extra1"] = 42, ["extra2"] = 99 }, + }; + + // Act + var json = JsonUtils.Serialize(record); + var deserializedRecord = JsonUtils.Deserialize(json); + + // Assert + Assert.That(deserializedRecord, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(deserializedRecord.AdditionalProperties["extra1"], Is.EqualTo(42)); + Assert.That(deserializedRecord.AdditionalProperties["extra2"], Is.EqualTo(99)); + }); + } + + [Test] + public void RecordWithReadonlyAdditionalPropertiesDictionaries_OnDeserialized_ShouldPopulateAdditionalProperties() + { + // Arrange + const string json = """ + { + "extra1": { "key1": true, "key2": false }, + "extra2": { "key3": true } + } + """; + + // Act + var record = JsonUtils.Deserialize(json); + + // Assert + Assert.That(record, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(record.AdditionalProperties["extra1"]["key1"], Is.True); + Assert.That(record.AdditionalProperties["extra1"]["key2"], Is.False); + Assert.That(record.AdditionalProperties["extra2"]["key3"], Is.True); + }); + } + + [Test] + public void RecordWithAdditionalPropertiesDictionaries_OnSerialization_ShouldIncludeAdditionalProperties() + { + // Arrange + var record = new WriteableRecordWithDictionaries + { + AdditionalProperties = + { + ["extra1"] = new Dictionary { { "key1", true }, { "key2", false } }, + ["extra2"] = new Dictionary { { "key3", true } }, + }, + }; + + // Act + var json = JsonUtils.Serialize(record); + var deserializedRecord = JsonUtils.Deserialize(json); + + // Assert + Assert.That(deserializedRecord, Is.Not.Null); + Assert.Multiple(() => + { + Assert.That(deserializedRecord.AdditionalProperties["extra1"]["key1"], Is.True); + Assert.That(deserializedRecord.AdditionalProperties["extra1"]["key2"], Is.False); + Assert.That(deserializedRecord.AdditionalProperties["extra2"]["key3"], Is.True); + }); + } + + private record Record : IJsonOnDeserialized + { + [JsonPropertyName("id")] + public required string Id { get; set; } + + [JsonExtensionData] + private readonly IDictionary _extensionData = + new Dictionary(); + + [JsonIgnore] + public ReadOnlyAdditionalProperties AdditionalProperties { get; } = new(); + + void IJsonOnDeserialized.OnDeserialized() + { + AdditionalProperties.CopyFromExtensionData(_extensionData); + } + } + + private record WriteableRecord : IJsonOnDeserialized, IJsonOnSerializing + { + [JsonPropertyName("id")] + public required string Id { get; set; } + + [JsonExtensionData] + private readonly IDictionary _extensionData = + new Dictionary(); + + [JsonIgnore] + public AdditionalProperties AdditionalProperties { get; set; } = new(); + + void IJsonOnDeserialized.OnDeserialized() + { + AdditionalProperties.CopyFromExtensionData(_extensionData); + } + + void IJsonOnSerializing.OnSerializing() + { + AdditionalProperties.CopyToExtensionData(_extensionData); + } + } + + private record RecordWithInts : IJsonOnDeserialized + { + [JsonExtensionData] + private readonly IDictionary _extensionData = + new Dictionary(); + + [JsonIgnore] + public ReadOnlyAdditionalProperties AdditionalProperties { get; } = new(); + + void IJsonOnDeserialized.OnDeserialized() + { + AdditionalProperties.CopyFromExtensionData(_extensionData); + } + } + + private record WriteableRecordWithInts : IJsonOnDeserialized, IJsonOnSerializing + { + [JsonExtensionData] + private readonly IDictionary _extensionData = + new Dictionary(); + + [JsonIgnore] + public AdditionalProperties AdditionalProperties { get; } = new(); + + void IJsonOnDeserialized.OnDeserialized() + { + AdditionalProperties.CopyFromExtensionData(_extensionData); + } + + void IJsonOnSerializing.OnSerializing() + { + AdditionalProperties.CopyToExtensionData(_extensionData); + } + } + + private record RecordWithDictionaries : IJsonOnDeserialized + { + [JsonExtensionData] + private readonly IDictionary _extensionData = + new Dictionary(); + + [JsonIgnore] + public ReadOnlyAdditionalProperties< + Dictionary + > AdditionalProperties { get; } = new(); + + void IJsonOnDeserialized.OnDeserialized() + { + AdditionalProperties.CopyFromExtensionData(_extensionData); + } + } + + private record WriteableRecordWithDictionaries : IJsonOnDeserialized, IJsonOnSerializing + { + [JsonExtensionData] + private readonly IDictionary _extensionData = + new Dictionary(); + + [JsonIgnore] + public AdditionalProperties> AdditionalProperties { get; } = new(); + + void IJsonOnDeserialized.OnDeserialized() + { + AdditionalProperties.CopyFromExtensionData(_extensionData); + } + + void IJsonOnSerializing.OnSerializing() + { + AdditionalProperties.CopyToExtensionData(_extensionData); + } + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/DateOnlyJsonTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/DateOnlyJsonTests.cs new file mode 100644 index 000000000000..df03e0d70b29 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/DateOnlyJsonTests.cs @@ -0,0 +1,100 @@ +using NUnit.Framework; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional.Test.Core.Json; + +[TestFixture] +public class DateOnlyJsonTests +{ + [Test] + public void SerializeDateOnly_ShouldMatchExpectedFormat() + { + (DateOnly dateOnly, string expected)[] testCases = + [ + (new DateOnly(2023, 10, 5), "\"2023-10-05\""), + (new DateOnly(2023, 1, 1), "\"2023-01-01\""), + (new DateOnly(2023, 12, 31), "\"2023-12-31\""), + (new DateOnly(2023, 6, 15), "\"2023-06-15\""), + (new DateOnly(2023, 3, 10), "\"2023-03-10\""), + ]; + foreach (var (dateOnly, expected) in testCases) + { + var json = JsonUtils.Serialize(dateOnly); + Assert.That(json, Is.EqualTo(expected)); + } + } + + [Test] + public void DeserializeDateOnly_ShouldMatchExpectedDateOnly() + { + (DateOnly expected, string json)[] testCases = + [ + (new DateOnly(2023, 10, 5), "\"2023-10-05\""), + (new DateOnly(2023, 1, 1), "\"2023-01-01\""), + (new DateOnly(2023, 12, 31), "\"2023-12-31\""), + (new DateOnly(2023, 6, 15), "\"2023-06-15\""), + (new DateOnly(2023, 3, 10), "\"2023-03-10\""), + ]; + + foreach (var (expected, json) in testCases) + { + var dateOnly = JsonUtils.Deserialize(json); + Assert.That(dateOnly, Is.EqualTo(expected)); + } + } + + [Test] + public void SerializeNullableDateOnly_ShouldMatchExpectedFormat() + { + (DateOnly? dateOnly, string expected)[] testCases = + [ + (new DateOnly(2023, 10, 5), "\"2023-10-05\""), + (null, "null"), + ]; + foreach (var (dateOnly, expected) in testCases) + { + var json = JsonUtils.Serialize(dateOnly); + Assert.That(json, Is.EqualTo(expected)); + } + } + + [Test] + public void DeserializeNullableDateOnly_ShouldMatchExpectedDateOnly() + { + (DateOnly? expected, string json)[] testCases = + [ + (new DateOnly(2023, 10, 5), "\"2023-10-05\""), + (null, "null"), + ]; + + foreach (var (expected, json) in testCases) + { + var dateOnly = JsonUtils.Deserialize(json); + Assert.That(dateOnly, Is.EqualTo(expected)); + } + } + + [Test] + public void ShouldSerializeDictionaryWithDateOnlyKey() + { + var key = new DateOnly(2023, 10, 5); + var dict = new Dictionary { { key, "value_a" } }; + var json = JsonUtils.Serialize(dict); + Assert.That(json, Does.Contain("2023-10-05")); + Assert.That(json, Does.Contain("value_a")); + } + + [Test] + public void ShouldDeserializeDictionaryWithDateOnlyKey() + { + var json = """ + { + "2023-10-05": "value_a" + } + """; + var dict = JsonUtils.Deserialize>(json); + Assert.That(dict, Is.Not.Null); + var key = new DateOnly(2023, 10, 5); + Assert.That(dict![key], Is.EqualTo("value_a")); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/DateTimeJsonTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/DateTimeJsonTests.cs new file mode 100644 index 000000000000..6807d966d800 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/DateTimeJsonTests.cs @@ -0,0 +1,134 @@ +using NUnit.Framework; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional.Test.Core.Json; + +[TestFixture] +public class DateTimeJsonTests +{ + [Test] + public void SerializeDateTime_ShouldMatchExpectedFormat() + { + (DateTime dateTime, string expected)[] testCases = + [ + ( + new DateTime(2023, 10, 5, 14, 30, 0, DateTimeKind.Utc), + "\"2023-10-05T14:30:00.000Z\"" + ), + (new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc), "\"2023-01-01T00:00:00.000Z\""), + ( + new DateTime(2023, 12, 31, 23, 59, 59, DateTimeKind.Utc), + "\"2023-12-31T23:59:59.000Z\"" + ), + (new DateTime(2023, 6, 15, 12, 0, 0, DateTimeKind.Utc), "\"2023-06-15T12:00:00.000Z\""), + ( + new DateTime(2023, 3, 10, 8, 45, 30, DateTimeKind.Utc), + "\"2023-03-10T08:45:30.000Z\"" + ), + ( + new DateTime(2023, 3, 10, 8, 45, 30, 123, DateTimeKind.Utc), + "\"2023-03-10T08:45:30.123Z\"" + ), + ]; + foreach (var (dateTime, expected) in testCases) + { + var json = JsonUtils.Serialize(dateTime); + Assert.That(json, Is.EqualTo(expected)); + } + } + + [Test] + public void DeserializeDateTime_ShouldMatchExpectedDateTime() + { + (DateTime expected, string json)[] testCases = + [ + ( + new DateTime(2023, 10, 5, 14, 30, 0, DateTimeKind.Utc), + "\"2023-10-05T14:30:00.000Z\"" + ), + (new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc), "\"2023-01-01T00:00:00.000Z\""), + ( + new DateTime(2023, 12, 31, 23, 59, 59, DateTimeKind.Utc), + "\"2023-12-31T23:59:59.000Z\"" + ), + (new DateTime(2023, 6, 15, 12, 0, 0, DateTimeKind.Utc), "\"2023-06-15T12:00:00.000Z\""), + ( + new DateTime(2023, 3, 10, 8, 45, 30, DateTimeKind.Utc), + "\"2023-03-10T08:45:30.000Z\"" + ), + (new DateTime(2023, 3, 10, 8, 45, 30, DateTimeKind.Utc), "\"2023-03-10T08:45:30Z\""), + ( + new DateTime(2023, 3, 10, 8, 45, 30, 123, DateTimeKind.Utc), + "\"2023-03-10T08:45:30.123Z\"" + ), + ]; + + foreach (var (expected, json) in testCases) + { + var dateTime = JsonUtils.Deserialize(json); + Assert.That(dateTime, Is.EqualTo(expected)); + } + } + + [Test] + public void SerializeNullableDateTime_ShouldMatchExpectedFormat() + { + (DateTime? expected, string json)[] testCases = + [ + ( + new DateTime(2023, 10, 5, 14, 30, 0, DateTimeKind.Utc), + "\"2023-10-05T14:30:00.000Z\"" + ), + (null, "null"), + ]; + + foreach (var (expected, json) in testCases) + { + var dateTime = JsonUtils.Deserialize(json); + Assert.That(dateTime, Is.EqualTo(expected)); + } + } + + [Test] + public void DeserializeNullableDateTime_ShouldMatchExpectedDateTime() + { + (DateTime? expected, string json)[] testCases = + [ + ( + new DateTime(2023, 10, 5, 14, 30, 0, DateTimeKind.Utc), + "\"2023-10-05T14:30:00.000Z\"" + ), + (null, "null"), + ]; + + foreach (var (expected, json) in testCases) + { + var dateTime = JsonUtils.Deserialize(json); + Assert.That(dateTime, Is.EqualTo(expected)); + } + } + + [Test] + public void ShouldSerializeDictionaryWithDateTimeKey() + { + var key = new DateTime(2023, 10, 5, 14, 30, 0, DateTimeKind.Utc); + var dict = new Dictionary { { key, "value_a" } }; + var json = JsonUtils.Serialize(dict); + Assert.That(json, Does.Contain("2023-10-05T14:30:00.000Z")); + Assert.That(json, Does.Contain("value_a")); + } + + [Test] + public void ShouldDeserializeDictionaryWithDateTimeKey() + { + var json = """ + { + "2023-10-05T14:30:00.000Z": "value_a" + } + """; + var dict = JsonUtils.Deserialize>(json); + Assert.That(dict, Is.Not.Null); + var key = new DateTime(2023, 10, 5, 14, 30, 0, DateTimeKind.Utc); + Assert.That(dict![key], Is.EqualTo("value_a")); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/JsonAccessAttributeTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/JsonAccessAttributeTests.cs new file mode 100644 index 000000000000..3d965c92fe55 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/Json/JsonAccessAttributeTests.cs @@ -0,0 +1,160 @@ +using global::System.Text.Json.Serialization; +using NUnit.Framework; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional.Test.Core.Json; + +[TestFixture] +public class JsonAccessAttributeTests +{ + private class MyClass + { + [JsonPropertyName("read_only_prop")] + [JsonAccess(JsonAccessType.ReadOnly)] + public string? ReadOnlyProp { get; set; } + + [JsonPropertyName("write_only_prop")] + [JsonAccess(JsonAccessType.WriteOnly)] + public string? WriteOnlyProp { get; set; } + + [JsonPropertyName("normal_prop")] + public string? NormalProp { get; set; } + + [JsonPropertyName("read_only_nullable_list")] + [JsonAccess(JsonAccessType.ReadOnly)] + public IEnumerable? ReadOnlyNullableList { get; set; } + + [JsonPropertyName("read_only_list")] + [JsonAccess(JsonAccessType.ReadOnly)] + public IEnumerable ReadOnlyList { get; set; } = []; + + [JsonPropertyName("write_only_nullable_list")] + [JsonAccess(JsonAccessType.WriteOnly)] + public IEnumerable? WriteOnlyNullableList { get; set; } + + [JsonPropertyName("write_only_list")] + [JsonAccess(JsonAccessType.WriteOnly)] + public IEnumerable WriteOnlyList { get; set; } = []; + + [JsonPropertyName("normal_list")] + public IEnumerable NormalList { get; set; } = []; + + [JsonPropertyName("normal_nullable_list")] + public IEnumerable? NullableNormalList { get; set; } + } + + [Test] + public void JsonAccessAttribute_ShouldWorkAsExpected() + { + const string json = """ + { + "read_only_prop": "read", + "write_only_prop": "write", + "normal_prop": "normal_prop", + "read_only_nullable_list": ["item1", "item2"], + "read_only_list": ["item3", "item4"], + "write_only_nullable_list": ["item5", "item6"], + "write_only_list": ["item7", "item8"], + "normal_list": ["normal1", "normal2"], + "normal_nullable_list": ["normal1", "normal2"] + } + """; + var obj = JsonUtils.Deserialize(json); + + Assert.Multiple(() => + { + // String properties + Assert.That(obj.ReadOnlyProp, Is.EqualTo("read")); + Assert.That(obj.WriteOnlyProp, Is.Null); + Assert.That(obj.NormalProp, Is.EqualTo("normal_prop")); + + // List properties - read only + var nullableReadOnlyList = obj.ReadOnlyNullableList?.ToArray(); + Assert.That(nullableReadOnlyList, Is.Not.Null); + Assert.That(nullableReadOnlyList, Has.Length.EqualTo(2)); + Assert.That(nullableReadOnlyList![0], Is.EqualTo("item1")); + Assert.That(nullableReadOnlyList![1], Is.EqualTo("item2")); + + var readOnlyList = obj.ReadOnlyList.ToArray(); + Assert.That(readOnlyList, Is.Not.Null); + Assert.That(readOnlyList, Has.Length.EqualTo(2)); + Assert.That(readOnlyList[0], Is.EqualTo("item3")); + Assert.That(readOnlyList[1], Is.EqualTo("item4")); + + // List properties - write only + Assert.That(obj.WriteOnlyNullableList, Is.Null); + Assert.That(obj.WriteOnlyList, Is.Not.Null); + Assert.That(obj.WriteOnlyList, Is.Empty); + + // Normal list property + var normalList = obj.NormalList.ToArray(); + Assert.That(normalList, Is.Not.Null); + Assert.That(normalList, Has.Length.EqualTo(2)); + Assert.That(normalList[0], Is.EqualTo("normal1")); + Assert.That(normalList[1], Is.EqualTo("normal2")); + }); + + // Set up values for serialization + obj.WriteOnlyProp = "write"; + obj.NormalProp = "new_value"; + obj.WriteOnlyNullableList = new List { "write1", "write2" }; + obj.WriteOnlyList = new List { "write3", "write4" }; + obj.NormalList = new List { "new_normal" }; + obj.NullableNormalList = new List { "new_normal" }; + + var serializedJson = JsonUtils.Serialize(obj); + const string expectedJson = """ + { + "write_only_prop": "write", + "normal_prop": "new_value", + "write_only_nullable_list": [ + "write1", + "write2" + ], + "write_only_list": [ + "write3", + "write4" + ], + "normal_list": [ + "new_normal" + ], + "normal_nullable_list": [ + "new_normal" + ] + } + """; + Assert.That(serializedJson, Is.EqualTo(expectedJson).IgnoreWhiteSpace); + } + + [Test] + public void JsonAccessAttribute_WithNullListsInJson_ShouldWorkAsExpected() + { + const string json = """ + { + "read_only_prop": "read", + "normal_prop": "normal_prop", + "read_only_nullable_list": null, + "read_only_list": [] + } + """; + var obj = JsonUtils.Deserialize(json); + + Assert.Multiple(() => + { + // Read-only nullable list should be null when JSON contains null + var nullableReadOnlyList = obj.ReadOnlyNullableList?.ToArray(); + Assert.That(nullableReadOnlyList, Is.Null); + + // Read-only non-nullable list should never be null, but empty when JSON contains null + var readOnlyList = obj.ReadOnlyList.ToArray(); // This should be initialized to an empty list by default + Assert.That(readOnlyList, Is.Not.Null); + Assert.That(readOnlyList, Is.Empty); + }); + + // Serialize and verify read-only lists are not included + var serializedJson = JsonUtils.Serialize(obj); + Assert.That(serializedJson, Does.Not.Contain("read_only_prop")); + Assert.That(serializedJson, Does.Not.Contain("read_only_nullable_list")); + Assert.That(serializedJson, Does.Not.Contain("read_only_list")); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/QueryStringBuilderTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/QueryStringBuilderTests.cs new file mode 100644 index 000000000000..2a84be60aa64 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/QueryStringBuilderTests.cs @@ -0,0 +1,560 @@ +using NUnit.Framework; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional.Test.Core; + +[TestFixture] +public class QueryStringBuilderTests +{ + [Test] + public void Build_SimpleParameters() + { + var parameters = new List> + { + new("name", "John Doe"), + new("age", "30"), + new("city", "New York"), + }; + + var result = QueryStringBuilder.Build(parameters); + + Assert.That(result, Is.EqualTo("?name=John%20Doe&age=30&city=New%20York")); + } + + [Test] + public void Build_EmptyList_ReturnsEmptyString() + { + var parameters = new List>(); + + var result = QueryStringBuilder.Build(parameters); + + Assert.That(result, Is.EqualTo(string.Empty)); + } + + [Test] + public void Build_SpecialCharacters() + { + var parameters = new List> + { + new("email", "test@example.com"), + new("url", "https://example.com/path?query=value"), + new("special", "a+b=c&d"), + }; + + var result = QueryStringBuilder.Build(parameters); + + Assert.That( + result, + Is.EqualTo( + "?email=test%40example.com&url=https%3A%2F%2Fexample.com%2Fpath%3Fquery%3Dvalue&special=a%2Bb%3Dc%26d" + ) + ); + } + + [Test] + public void Build_UnicodeCharacters() + { + var parameters = new List> { new("greeting", "Hello 世界") }; + + var result = QueryStringBuilder.Build(parameters); + + // Verify the Chinese characters are properly UTF-8 encoded + Assert.That(result, Does.StartWith("?greeting=Hello%20")); + Assert.That(result, Does.Contain("%E4%B8%96%E7%95%8C")); // 世界 + } + + [Test] + public void Build_SessionSettings_DeepObject() + { + // Simulate session settings with nested properties + var sessionSettings = new + { + custom_session_id = "my-custom-session-id", + system_prompt = "You are a helpful assistant", + variables = new Dictionary + { + { "userName", "John" }, + { "userAge", 30 }, + { "isPremium", true }, + }, + }; + + // Build query parameters list + var queryParams = new List> { new("api_key", "test_key_123") }; + + // Add session_settings with prefix using the new overload + queryParams.AddRange( + QueryStringConverter.ToDeepObject("session_settings", sessionSettings) + ); + + var result = QueryStringBuilder.Build(queryParams); + + // Verify the result contains properly formatted deep object notation + // Note: Square brackets are URL-encoded as %5B and %5D + Assert.That(result, Does.StartWith("?api_key=test_key_123")); + Assert.That( + result, + Does.Contain("session_settings%5Bcustom_session_id%5D=my-custom-session-id") + ); + Assert.That( + result, + Does.Contain("session_settings%5Bsystem_prompt%5D=You%20are%20a%20helpful%20assistant") + ); + Assert.That(result, Does.Contain("session_settings%5Bvariables%5D%5BuserName%5D=John")); + Assert.That(result, Does.Contain("session_settings%5Bvariables%5D%5BuserAge%5D=30")); + Assert.That(result, Does.Contain("session_settings%5Bvariables%5D%5BisPremium%5D=true")); + + // Verify it's NOT JSON encoded (no braces or quotes in the original format) + Assert.That(result, Does.Not.Contain("%7B%22")); // Not {" sequence + } + + [Test] + public void Build_ChatApiLikeParameters() + { + // Simulate what ChatApi constructor does + var sessionSettings = new + { + system_prompt = "You are helpful", + variables = new Dictionary { { "name", "Alice" } }, + }; + + var queryParams = new List>(); + + // Simple parameters + var simpleParams = new Dictionary + { + { "access_token", "token123" }, + { "config_id", "config456" }, + { "api_key", "key789" }, + }; + queryParams.AddRange(QueryStringConverter.ToExplodedForm(simpleParams)); + + // Session settings as deep object with prefix + queryParams.AddRange( + QueryStringConverter.ToDeepObject("session_settings", sessionSettings) + ); + + var result = QueryStringBuilder.Build(queryParams); + + // Verify structure (square brackets are URL-encoded) + Assert.That(result, Does.StartWith("?")); + Assert.That(result, Does.Contain("access_token=token123")); + Assert.That(result, Does.Contain("config_id=config456")); + Assert.That(result, Does.Contain("api_key=key789")); + Assert.That( + result, + Does.Contain("session_settings%5Bsystem_prompt%5D=You%20are%20helpful") + ); + Assert.That(result, Does.Contain("session_settings%5Bvariables%5D%5Bname%5D=Alice")); + } + + [Test] + public void Build_ReservedCharacters_NotEncoded() + { + var parameters = new List> + { + new("path", "some-path"), + new("id", "123-456_789.test~value"), + }; + + var result = QueryStringBuilder.Build(parameters); + + // Unreserved characters: A-Z a-z 0-9 - _ . ~ + Assert.That(result, Is.EqualTo("?path=some-path&id=123-456_789.test~value")); + } + + [Test] + public void Builder_Add_SimpleParameters() + { + var result = new QueryStringBuilder.Builder() + .Add("name", "John Doe") + .Add("age", 30) + .Add("active", true) + .Build(); + + Assert.That(result, Does.Contain("name=John%20Doe")); + Assert.That(result, Does.Contain("age=30")); + Assert.That(result, Does.Contain("active=true")); + } + + [Test] + public void Builder_Add_NullValuesIgnored() + { + var result = new QueryStringBuilder.Builder() + .Add("name", "John") + .Add("middle", null) + .Add("age", 30) + .Build(); + + Assert.That(result, Does.Contain("name=John")); + Assert.That(result, Does.Contain("age=30")); + Assert.That(result, Does.Not.Contain("middle")); + } + + [Test] + public void Builder_AddDeepObject_WithPrefix() + { + var settings = new + { + custom_session_id = "id-123", + system_prompt = "You are helpful", + variables = new { name = "Alice", age = 25 }, + }; + + var result = new QueryStringBuilder.Builder() + .Add("api_key", "key123") + .AddDeepObject("session_settings", settings) + .Build(); + + Assert.That(result, Does.Contain("api_key=key123")); + Assert.That(result, Does.Contain("session_settings%5Bcustom_session_id%5D=id-123")); + Assert.That( + result, + Does.Contain("session_settings%5Bsystem_prompt%5D=You%20are%20helpful") + ); + Assert.That(result, Does.Contain("session_settings%5Bvariables%5D%5Bname%5D=Alice")); + Assert.That(result, Does.Contain("session_settings%5Bvariables%5D%5Bage%5D=25")); + } + + [Test] + public void Builder_AddDeepObject_NullIgnored() + { + var result = new QueryStringBuilder.Builder() + .Add("api_key", "key123") + .AddDeepObject("settings", null) + .Build(); + + Assert.That(result, Is.EqualTo("?api_key=key123")); + Assert.That(result, Does.Not.Contain("settings")); + } + + [Test] + public void Builder_AddExploded_WithPrefix() + { + var filter = new { status = "active", type = "user" }; + + var result = new QueryStringBuilder.Builder() + .Add("api_key", "key123") + .AddExploded("filter", filter) + .Build(); + + Assert.That(result, Does.Contain("api_key=key123")); + Assert.That(result, Does.Contain("filter%5Bstatus%5D=active")); + Assert.That(result, Does.Contain("filter%5Btype%5D=user")); + } + + [Test] + public void Builder_AddExploded_NullIgnored() + { + var result = new QueryStringBuilder.Builder() + .Add("api_key", "key123") + .AddExploded("filter", null) + .Build(); + + Assert.That(result, Is.EqualTo("?api_key=key123")); + Assert.That(result, Does.Not.Contain("filter")); + } + + [Test] + public void Builder_WithCapacity() + { + // Test that capacity constructor works without errors + var result = new QueryStringBuilder.Builder(capacity: 10) + .Add("param1", "value1") + .Add("param2", "value2") + .Build(); + + Assert.That(result, Does.Contain("param1=value1")); + Assert.That(result, Does.Contain("param2=value2")); + } + + [Test] + public void Builder_ChatApiLikeUsage() + { + // Simulate real usage from ChatApi + var sessionSettings = new + { + custom_session_id = "session-123", + variables = new Dictionary + { + { "userName", "John" }, + { "userAge", 30 }, + }, + }; + + var result = new QueryStringBuilder.Builder(capacity: 16) + .Add("access_token", "token123") + .Add("allow_connection", true) + .Add("config_id", "config456") + .Add("api_key", "key789") + .AddDeepObject("session_settings", sessionSettings) + .Build(); + + Assert.That(result, Does.StartWith("?")); + Assert.That(result, Does.Contain("access_token=token123")); + Assert.That(result, Does.Contain("allow_connection=true")); + Assert.That(result, Does.Contain("config_id=config456")); + Assert.That(result, Does.Contain("api_key=key789")); + Assert.That(result, Does.Contain("session_settings%5Bcustom_session_id%5D=session-123")); + Assert.That(result, Does.Contain("session_settings%5Bvariables%5D%5BuserName%5D=John")); + Assert.That(result, Does.Contain("session_settings%5Bvariables%5D%5BuserAge%5D=30")); + } + + [Test] + public void Builder_EmptyBuilder_ReturnsEmptyString() + { + var result = new QueryStringBuilder.Builder().Build(); + + Assert.That(result, Is.EqualTo(string.Empty)); + } + + [Test] + public void Builder_OnlyNullValues_ReturnsEmptyString() + { + var result = new QueryStringBuilder.Builder() + .Add("param1", null) + .Add("param2", null) + .AddDeepObject("settings", null) + .Build(); + + Assert.That(result, Is.EqualTo(string.Empty)); + } + + [Test] + public void Builder_Set_OverridesSingleValue() + { + var result = new QueryStringBuilder.Builder() + .Add("foo", "original") + .Set("foo", "override") + .Build(); + + Assert.That(result, Is.EqualTo("?foo=override")); + } + + [Test] + public void Builder_Set_OverridesMultipleValues() + { + var result = new QueryStringBuilder.Builder() + .Add("foo", "value1") + .Add("foo", "value2") + .Set("foo", "override") + .Build(); + + Assert.That(result, Is.EqualTo("?foo=override")); + } + + [Test] + public void Builder_Set_WithArray_CreatesMultipleParameters() + { + var result = new QueryStringBuilder.Builder() + .Add("foo", "original") + .Set("foo", new[] { "value1", "value2" }) + .Build(); + + Assert.That(result, Is.EqualTo("?foo=value1&foo=value2")); + } + + [Test] + public void Builder_Set_WithNull_RemovesParameter() + { + var result = new QueryStringBuilder.Builder() + .Add("foo", "original") + .Add("bar", "keep") + .Set("foo", null) + .Build(); + + Assert.That(result, Is.EqualTo("?bar=keep")); + } + + [Test] + public void Builder_MergeAdditional_WithSingleValues() + { + var additional = new List> + { + new("foo", "bar"), + new("baz", "qux"), + }; + + var result = new QueryStringBuilder.Builder() + .Add("existing", "value") + .MergeAdditional(additional) + .Build(); + + Assert.That(result, Does.Contain("existing=value")); + Assert.That(result, Does.Contain("foo=bar")); + Assert.That(result, Does.Contain("baz=qux")); + } + + [Test] + public void Builder_MergeAdditional_WithDuplicateKeys_CreatesList() + { + var additional = new List> + { + new("foo", "bar1"), + new("foo", "bar2"), + new("baz", "qux"), + }; + + var result = new QueryStringBuilder.Builder() + .Add("existing", "value") + .MergeAdditional(additional) + .Build(); + + Assert.That(result, Does.Contain("existing=value")); + Assert.That(result, Does.Contain("foo=bar1")); + Assert.That(result, Does.Contain("foo=bar2")); + Assert.That(result, Does.Contain("baz=qux")); + } + + [Test] + public void Builder_MergeAdditional_OverridesExistingParameters() + { + var additional = new List> { new("foo", "override") }; + + var result = new QueryStringBuilder.Builder() + .Add("foo", "original1") + .Add("foo", "original2") + .Add("bar", "keep") + .MergeAdditional(additional) + .Build(); + + Assert.That(result, Does.Contain("bar=keep")); + Assert.That(result, Does.Contain("foo=override")); + Assert.That(result, Does.Not.Contain("original1")); + Assert.That(result, Does.Not.Contain("original2")); + } + + [Test] + public void Builder_MergeAdditional_WithDuplicates_OverridesExisting() + { + var additional = new List> + { + new("foo", "new1"), + new("foo", "new2"), + new("foo", "new3"), + }; + + var result = new QueryStringBuilder.Builder() + .Add("foo", "original1") + .Add("foo", "original2") + .Add("bar", "keep") + .MergeAdditional(additional) + .Build(); + + Assert.That(result, Does.Contain("bar=keep")); + Assert.That(result, Does.Contain("foo=new1")); + Assert.That(result, Does.Contain("foo=new2")); + Assert.That(result, Does.Contain("foo=new3")); + Assert.That(result, Does.Not.Contain("original1")); + Assert.That(result, Does.Not.Contain("original2")); + } + + [Test] + public void Builder_MergeAdditional_WithNull_NoOp() + { + var result = new QueryStringBuilder.Builder() + .Add("foo", "value") + .MergeAdditional(null) + .Build(); + + Assert.That(result, Is.EqualTo("?foo=value")); + } + + [Test] + public void Builder_MergeAdditional_WithEmptyList_NoOp() + { + var additional = new List>(); + + var result = new QueryStringBuilder.Builder() + .Add("foo", "value") + .MergeAdditional(additional) + .Build(); + + Assert.That(result, Is.EqualTo("?foo=value")); + } + + [Test] + public void Builder_MergeAdditional_RealWorldScenario() + { + // SDK generates foo=foo1&foo=foo2 + var builder = new QueryStringBuilder.Builder() + .Add("foo", "foo1") + .Add("foo", "foo2") + .Add("bar", "baz"); + + // User provides foo=override in AdditionalQueryParameters + var additional = new List> { new("foo", "override") }; + + var result = builder.MergeAdditional(additional).Build(); + + // Result should be foo=override&bar=baz (user overrides SDK) + Assert.That(result, Does.Contain("bar=baz")); + Assert.That(result, Does.Contain("foo=override")); + Assert.That(result, Does.Not.Contain("foo1")); + Assert.That(result, Does.Not.Contain("foo2")); + } + + [Test] + public void Builder_MergeAdditional_UserProvidesMultipleValues() + { + // SDK generates no foo parameter + var builder = new QueryStringBuilder.Builder().Add("bar", "baz"); + + // User provides foo=bar1&foo=bar2 in AdditionalQueryParameters + var additional = new List> + { + new("foo", "bar1"), + new("foo", "bar2"), + }; + + var result = builder.MergeAdditional(additional).Build(); + + // Result should be bar=baz&foo=bar1&foo=bar2 + Assert.That(result, Does.Contain("bar=baz")); + Assert.That(result, Does.Contain("foo=bar1")); + Assert.That(result, Does.Contain("foo=bar2")); + } + + [Test] + public void Builder_Add_WithCollection_CreatesMultipleParameters() + { + var tags = new[] { "tag1", "tag2", "tag3" }; + var result = new QueryStringBuilder.Builder().Add("tag", tags).Build(); + + Assert.That(result, Does.Contain("tag=tag1")); + Assert.That(result, Does.Contain("tag=tag2")); + Assert.That(result, Does.Contain("tag=tag3")); + } + + [Test] + public void Builder_Add_WithList_CreatesMultipleParameters() + { + var ids = new List { 1, 2, 3 }; + var result = new QueryStringBuilder.Builder().Add("id", ids).Build(); + + Assert.That(result, Does.Contain("id=1")); + Assert.That(result, Does.Contain("id=2")); + Assert.That(result, Does.Contain("id=3")); + } + + [Test] + public void Builder_Set_WithCollection_ReplacesAllPreviousValues() + { + var result = new QueryStringBuilder.Builder() + .Add("id", 1) + .Add("id", 2) + .Set("id", new[] { 10, 20, 30 }) + .Build(); + + Assert.That(result, Does.Contain("id=10")); + Assert.That(result, Does.Contain("id=20")); + Assert.That(result, Does.Contain("id=30")); + // Check that old values are not present (use word boundaries to avoid false positives with id=10) + Assert.That(result, Does.Not.Contain("id=1&")); + Assert.That(result, Does.Not.Contain("id=2&")); + Assert.That(result, Does.Not.Contain("id=1?")); + Assert.That(result, Does.Not.Contain("id=2?")); + Assert.That(result, Does.Not.EndWith("id=1")); + Assert.That(result, Does.Not.EndWith("id=2")); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/QueryStringConverterTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/QueryStringConverterTests.cs new file mode 100644 index 000000000000..9c137198ad52 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/QueryStringConverterTests.cs @@ -0,0 +1,158 @@ +using NUnit.Framework; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional.Test.Core; + +[TestFixture] +public class QueryStringConverterTests +{ + [Test] + public void ToQueryStringCollection_Form() + { + var obj = new + { + Name = "John", + Age = 30, + Address = new + { + Street = "123 Main St", + City = "Anytown", + Coordinates = new[] { 39.781721f, -89.650148f }, + }, + Tags = new[] { "Developer", "Blogger" }, + }; + var result = QueryStringConverter.ToForm(obj); + var expected = new List> + { + new("Name", "John"), + new("Age", "30"), + new("Address[Street]", "123 Main St"), + new("Address[City]", "Anytown"), + new("Address[Coordinates]", "39.78172,-89.65015"), + new("Tags", "Developer,Blogger"), + }; + Assert.That(result, Is.EqualTo(expected)); + } + + [Test] + public void ToQueryStringCollection_ExplodedForm() + { + var obj = new + { + Name = "John", + Age = 30, + Address = new + { + Street = "123 Main St", + City = "Anytown", + Coordinates = new[] { 39.781721f, -89.650148f }, + }, + Tags = new[] { "Developer", "Blogger" }, + }; + var result = QueryStringConverter.ToExplodedForm(obj); + var expected = new List> + { + new("Name", "John"), + new("Age", "30"), + new("Address[Street]", "123 Main St"), + new("Address[City]", "Anytown"), + new("Address[Coordinates]", "39.78172"), + new("Address[Coordinates]", "-89.65015"), + new("Tags", "Developer"), + new("Tags", "Blogger"), + }; + Assert.That(result, Is.EqualTo(expected)); + } + + [Test] + public void ToQueryStringCollection_DeepObject() + { + var obj = new + { + Name = "John", + Age = 30, + Address = new + { + Street = "123 Main St", + City = "Anytown", + Coordinates = new[] { 39.781721f, -89.650148f }, + }, + Tags = new[] { "Developer", "Blogger" }, + }; + var result = QueryStringConverter.ToDeepObject(obj); + var expected = new List> + { + new("Name", "John"), + new("Age", "30"), + new("Address[Street]", "123 Main St"), + new("Address[City]", "Anytown"), + new("Address[Coordinates][0]", "39.78172"), + new("Address[Coordinates][1]", "-89.65015"), + new("Tags[0]", "Developer"), + new("Tags[1]", "Blogger"), + }; + Assert.That(result, Is.EqualTo(expected)); + } + + [Test] + public void ToQueryStringCollection_OnString_ThrowsException() + { + var exception = Assert.Throws(() => + QueryStringConverter.ToForm("invalid") + ); + Assert.That( + exception.Message, + Is.EqualTo( + "Only objects can be converted to query string collections. Given type is String." + ) + ); + } + + [Test] + public void ToQueryStringCollection_OnArray_ThrowsException() + { + var exception = Assert.Throws(() => + QueryStringConverter.ToForm(Array.Empty()) + ); + Assert.That( + exception.Message, + Is.EqualTo( + "Only objects can be converted to query string collections. Given type is Array." + ) + ); + } + + [Test] + public void ToQueryStringCollection_DeepObject_WithPrefix() + { + var obj = new + { + custom_session_id = "my-id", + system_prompt = "You are helpful", + variables = new { name = "Alice", age = 25 }, + }; + var result = QueryStringConverter.ToDeepObject("session_settings", obj); + var expected = new List> + { + new("session_settings[custom_session_id]", "my-id"), + new("session_settings[system_prompt]", "You are helpful"), + new("session_settings[variables][name]", "Alice"), + new("session_settings[variables][age]", "25"), + }; + Assert.That(result, Is.EqualTo(expected)); + } + + [Test] + public void ToQueryStringCollection_ExplodedForm_WithPrefix() + { + var obj = new { Name = "John", Tags = new[] { "Developer", "Blogger" } }; + var result = QueryStringConverter.ToExplodedForm("user", obj); + var expected = new List> + { + new("user[Name]", "John"), + new("user[Tags]", "Developer"), + new("user[Tags]", "Blogger"), + }; + Assert.That(result, Is.EqualTo(expected)); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/MultipartFormTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/MultipartFormTests.cs new file mode 100644 index 000000000000..e65385d0e14d --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/MultipartFormTests.cs @@ -0,0 +1,1121 @@ +using global::System.Net.Http; +using global::System.Text; +using global::System.Text.Json.Serialization; +using NUnit.Framework; +using SeedBasicAuthOptional.Core; +using SystemTask = global::System.Threading.Tasks.Task; + +namespace SeedBasicAuthOptional.Test.Core.RawClientTests; + +[TestFixture] +[Parallelizable(ParallelScope.Self)] +public class MultipartFormTests +{ + private static SimpleObject _simpleObject = new(); + + private static string _simpleFormEncoded = + "meta=data&Date=2023-10-01&Time=12:00:00&Duration=01:00:00&Id=1a1bb98f-47c6-407b-9481-78476affe52a&IsActive=true&Count=42&Initial=A&Values=data,2023-10-01,12:00:00,01:00:00,1a1bb98f-47c6-407b-9481-78476affe52a,true,42,A"; + + private static string _simpleExplodedFormEncoded = + "meta=data&Date=2023-10-01&Time=12:00:00&Duration=01:00:00&Id=1a1bb98f-47c6-407b-9481-78476affe52a&IsActive=true&Count=42&Initial=A&Values=data&Values=2023-10-01&Values=12:00:00&Values=01:00:00&Values=1a1bb98f-47c6-407b-9481-78476affe52a&Values=true&Values=42&Values=A"; + + private static ComplexObject _complexObject = new(); + + private static string _complexJson = """ + { + "meta": "data", + "Nested": { + "foo": "value" + }, + "NestedDictionary": { + "key": { + "foo": "value" + } + }, + "ListOfObjects": [ + { + "foo": "value" + }, + { + "foo": "value2" + } + ], + "Date": "2023-10-01", + "Time": "12:00:00", + "Duration": "01:00:00", + "Id": "1a1bb98f-47c6-407b-9481-78476affe52a", + "IsActive": true, + "Count": 42, + "Initial": "A" + } + """; + + [Test] + public async SystemTask ShouldAddStringPart() + { + const string partInput = "string content"; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddStringPart("string", partInput); + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/plain + Content-Disposition: form-data; name=string + + {partInput} + --{boundary}-- + """; + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddStringParts() + { + const string partInput = "string content"; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddStringParts("strings", [partInput, partInput]); + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/plain + Content-Disposition: form-data; name=strings + + {partInput} + --{boundary} + Content-Type: text/plain + Content-Disposition: form-data; name=strings + + {partInput} + --{boundary}-- + """; + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask GivenNull_ShouldNotAddStringPart() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddStringPart("string", null); + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + --{boundary}-- + """; + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddStringParts_WithNullsInList() + { + const string partInput = "string content"; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddStringParts("strings", [partInput, null, partInput]); + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/plain + Content-Disposition: form-data; name=strings + + {partInput} + --{boundary} + Content-Type: text/plain + Content-Disposition: form-data; name=strings + + {partInput} + --{boundary}-- + """; + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddStringPart_WithContentType() + { + const string partInput = "string content"; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddStringPart("string", partInput, "text/xml"); + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/xml + Content-Disposition: form-data; name=string + + {partInput} + --{boundary}-- + """; + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddStringPart_WithContentTypeAndCharset() + { + const string partInput = "string content"; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddStringPart("string", partInput, "text/xml; charset=utf-8"); + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/xml; charset=utf-8 + Content-Disposition: form-data; name=string + + {partInput} + --{boundary}-- + """; + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddStringParts_WithContentType() + { + const string partInput = "string content"; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddStringParts("strings", [partInput, partInput], "text/xml"); + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/xml + Content-Disposition: form-data; name=strings + + {partInput} + --{boundary} + Content-Type: text/xml + Content-Disposition: form-data; name=strings + + {partInput} + --{boundary}-- + """; + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddStringParts_WithContentTypeAndCharset() + { + const string partInput = "string content"; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddStringParts( + "strings", + [partInput, partInput], + "text/xml; charset=utf-8" + ); + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/xml; charset=utf-8 + Content-Disposition: form-data; name=strings + + {partInput} + --{boundary} + Content-Type: text/xml; charset=utf-8 + Content-Disposition: form-data; name=strings + + {partInput} + --{boundary}-- + """; + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFileParameter_WithFileName() + { + var (partInput, partExpectedString) = GetFileParameterTestData(); + var file = new FileParameter { Stream = partInput, FileName = "test.txt" }; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFileParameterPart("file", file); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/octet-stream + Content-Disposition: form-data; name=file; filename=test.txt; filename*=utf-8''test.txt + + {partExpectedString} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFileParameter_WithoutFileName() + { + var (partInput, partExpectedString) = GetFileParameterTestData(); + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFileParameterPart("file", partInput); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/octet-stream + Content-Disposition: form-data; name=file + + {partExpectedString} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFileParameter_WithContentType() + { + var (partInput, partExpectedString) = GetFileParameterTestData(); + var file = new FileParameter + { + Stream = partInput, + FileName = "test.txt", + ContentType = "text/plain", + }; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFileParameterPart("file", file, "ignored-fallback-content-type"); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/plain + Content-Disposition: form-data; name=file; filename=test.txt; filename*=utf-8''test.txt + + {partExpectedString} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFileParameter_WithContentTypeAndCharset() + { + var (partInput, partExpectedString) = GetFileParameterTestData(); + var file = new FileParameter + { + Stream = partInput, + FileName = "test.txt", + ContentType = "text/plain; charset=utf-8", + }; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFileParameterPart("file", file, "ignored-fallback-content-type"); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/plain; charset=utf-8 + Content-Disposition: form-data; name=file; filename=test.txt; filename*=utf-8''test.txt + + {partExpectedString} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFileParameter_WithFallbackContentType() + { + var (partInput, partExpectedString) = GetFileParameterTestData(); + var file = new FileParameter { Stream = partInput, FileName = "test.txt" }; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFileParameterPart("file", file, "text/plain"); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/plain + Content-Disposition: form-data; name=file; filename=test.txt; filename*=utf-8''test.txt + + {partExpectedString} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFileParameter_WithFallbackContentTypeAndCharset() + { + var (partInput, partExpectedString) = GetFileParameterTestData(); + var file = new FileParameter { Stream = partInput, FileName = "test.txt" }; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFileParameterPart("file", file, "text/plain; charset=utf-8"); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: text/plain; charset=utf-8 + Content-Disposition: form-data; name=file; filename=test.txt; filename*=utf-8''test.txt + + {partExpectedString} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFileParameters() + { + var (partInput1, partExpectedString1) = GetFileParameterTestData(); + var (partInput2, partExpectedString2) = GetFileParameterTestData(); + var file1 = new FileParameter { Stream = partInput1, FileName = "test1.txt" }; + var file2 = new FileParameter { Stream = partInput2, FileName = "test2.txt" }; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFileParameterParts("file", [file1, file2]); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/octet-stream + Content-Disposition: form-data; name=file; filename=test1.txt; filename*=utf-8''test1.txt + + {partExpectedString1} + --{boundary} + Content-Type: application/octet-stream + Content-Disposition: form-data; name=file; filename=test2.txt; filename*=utf-8''test2.txt + + {partExpectedString2} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFileParameters_WithNullsInList() + { + var (partInput1, partExpectedString1) = GetFileParameterTestData(); + var (partInput2, partExpectedString2) = GetFileParameterTestData(); + var file1 = new FileParameter { Stream = partInput1, FileName = "test1.txt" }; + var file2 = new FileParameter { Stream = partInput2, FileName = "test2.txt" }; + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFileParameterParts("file", [file1, null, file2]); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/octet-stream + Content-Disposition: form-data; name=file; filename=test1.txt; filename*=utf-8''test1.txt + + {partExpectedString1} + --{boundary} + Content-Type: application/octet-stream + Content-Disposition: form-data; name=file; filename=test2.txt; filename*=utf-8''test2.txt + + {partExpectedString2} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask GivenNull_ShouldNotAddFileParameter() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFileParameterPart("file", null); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddJsonPart_WithComplexObject() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddJsonPart("object", _complexObject); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/json + Content-Disposition: form-data; name=object + + {_complexJson} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddJsonPart_WithComplexObjectList() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddJsonParts("objects", [_complexObject, _complexObject]); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/json + Content-Disposition: form-data; name=objects + + {_complexJson} + --{boundary} + Content-Type: application/json + Content-Disposition: form-data; name=objects + + {_complexJson} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask GivenNull_ShouldNotAddJsonPart() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddJsonPart("object", null); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddJsonParts_WithNullsInList() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddJsonParts("objects", [_complexObject, null]); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/json + Content-Disposition: form-data; name=objects + + {_complexJson} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddJsonParts_WithContentType() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddJsonParts("objects", [new { }], "application/json-patch+json"); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $$""" + --{{boundary}} + Content-Type: application/json-patch+json + Content-Disposition: form-data; name=objects + + {} + --{{boundary}}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFormEncodedParts_WithSimpleObject() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFormEncodedPart("object", _simpleObject); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=object + + {EscapeFormEncodedString(_simpleFormEncoded)} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFormEncodedParts_WithSimpleObjectList() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFormEncodedParts("objects", [_simpleObject, _simpleObject]); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=objects + + {EscapeFormEncodedString(_simpleFormEncoded)} + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=objects + + {EscapeFormEncodedString(_simpleFormEncoded)} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldNotAddFormEncodedParts_WithNull() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFormEncodedParts("object", null); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldNotAddFormEncodedParts_WithNullsInList() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFormEncodedParts("objects", [_simpleObject, null]); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=objects + + {EscapeFormEncodedString(_simpleFormEncoded)} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFormEncodedPart_WithContentType() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFormEncodedPart( + "objects", + new { foo = "bar" }, + "application/x-www-form-urlencoded" + ); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=objects + + foo=bar + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFormEncodedPart_WithContentTypeAndCharset() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFormEncodedPart( + "objects", + new { foo = "bar" }, + "application/x-www-form-urlencoded; charset=utf-8" + ); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded; charset=utf-8 + Content-Disposition: form-data; name=objects + + foo=bar + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFormEncodedParts_WithContentType() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFormEncodedParts( + "objects", + [new { foo = "bar" }], + "application/x-www-form-urlencoded" + ); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=objects + + foo=bar + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddFormEncodedParts_WithContentTypeAndCharset() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddFormEncodedParts( + "objects", + [new { foo = "bar" }], + "application/x-www-form-urlencoded; charset=utf-8" + ); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded; charset=utf-8 + Content-Disposition: form-data; name=objects + + foo=bar + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddExplodedFormEncodedParts_WithSimpleObject() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddExplodedFormEncodedPart("object", _simpleObject); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=object + + {EscapeFormEncodedString(_simpleExplodedFormEncoded)} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddExplodedFormEncodedParts_WithSimpleObjectList() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddExplodedFormEncodedParts("objects", [_simpleObject, _simpleObject]); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=objects + + {EscapeFormEncodedString(_simpleExplodedFormEncoded)} + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=objects + + {EscapeFormEncodedString(_simpleExplodedFormEncoded)} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldNotAddExplodedFormEncodedParts_WithNull() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddExplodedFormEncodedPart("object", null); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldNotAddExplodedFormEncodedParts_WithNullsInList() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddExplodedFormEncodedParts("objects", [_simpleObject, null]); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=objects + + {EscapeFormEncodedString(_simpleExplodedFormEncoded)} + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddExplodedFormEncodedPart_WithContentType() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddExplodedFormEncodedPart( + "objects", + new { foo = "bar" }, + "application/x-www-form-urlencoded" + ); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=objects + + foo=bar + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddExplodedFormEncodedPart_WithContentTypeAndCharset() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddExplodedFormEncodedPart( + "objects", + new { foo = "bar" }, + "application/x-www-form-urlencoded; charset=utf-8" + ); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded; charset=utf-8 + Content-Disposition: form-data; name=objects + + foo=bar + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddExplodedFormEncodedParts_WithContentType() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddExplodedFormEncodedParts( + "objects", + [new { foo = "bar" }], + "application/x-www-form-urlencoded" + ); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded + Content-Disposition: form-data; name=objects + + foo=bar + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + [Test] + public async SystemTask ShouldAddExplodedFormEncodedParts_WithContentTypeAndCharset() + { + var multipartFormRequest = CreateMultipartFormRequest(); + multipartFormRequest.AddExplodedFormEncodedParts( + "objects", + [new { foo = "bar" }], + "application/x-www-form-urlencoded; charset=utf-8" + ); + + var httpContent = multipartFormRequest.CreateContent(); + Assert.That(httpContent, Is.InstanceOf()); + var multipartContent = (MultipartFormDataContent)httpContent; + + var boundary = GetBoundary(multipartContent); + var expected = $""" + --{boundary} + Content-Type: application/x-www-form-urlencoded; charset=utf-8 + Content-Disposition: form-data; name=objects + + foo=bar + --{boundary}-- + """; + + var actual = await multipartContent.ReadAsStringAsync(); + Assert.That(actual, Is.EqualTo(expected).IgnoreWhiteSpace); + } + + private static string EscapeFormEncodedString(string input) + { + return string.Join( + "&", + input + .Split('&') + .Select(x => x.Split('=')) + .Select(x => $"{Uri.EscapeDataString(x[0])}={Uri.EscapeDataString(x[1])}") + ); + } + + private static string GetBoundary(MultipartFormDataContent content) + { + return content + .Headers.ContentType?.Parameters.Single(p => + p.Name.Equals("boundary", StringComparison.OrdinalIgnoreCase) + ) + .Value?.Trim('"') + ?? throw new global::System.Exception("Boundary not found"); + } + + private static SeedBasicAuthOptional.Core.MultipartFormRequest CreateMultipartFormRequest() + { + return new SeedBasicAuthOptional.Core.MultipartFormRequest + { + BaseUrl = "https://localhost", + Method = HttpMethod.Post, + Path = "", + }; + } + + private static (Stream partInput, string partExpectedString) GetFileParameterTestData() + { + const string partExpectedString = "file content"; + var partInput = new MemoryStream(Encoding.Default.GetBytes(partExpectedString)); + return (partInput, partExpectedString); + } + + private class SimpleObject + { + [JsonPropertyName("meta")] + public string Meta { get; set; } = "data"; + public DateOnly Date { get; set; } = DateOnly.Parse("2023-10-01"); + public TimeOnly Time { get; set; } = TimeOnly.Parse("12:00:00"); + public TimeSpan Duration { get; set; } = TimeSpan.FromHours(1); + public Guid Id { get; set; } = Guid.Parse("1a1bb98f-47c6-407b-9481-78476affe52a"); + public bool IsActive { get; set; } = true; + public int Count { get; set; } = 42; + public char Initial { get; set; } = 'A'; + public IEnumerable Values { get; set; } = + [ + "data", + DateOnly.Parse("2023-10-01"), + TimeOnly.Parse("12:00:00"), + TimeSpan.FromHours(1), + Guid.Parse("1a1bb98f-47c6-407b-9481-78476affe52a"), + true, + 42, + 'A', + ]; + } + + private class ComplexObject + { + [JsonPropertyName("meta")] + public string Meta { get; set; } = "data"; + + public object Nested { get; set; } = new { foo = "value" }; + + public Dictionary NestedDictionary { get; set; } = + new() { { "key", new { foo = "value" } } }; + + public IEnumerable ListOfObjects { get; set; } = + new List { new { foo = "value" }, new { foo = "value2" } }; + + public DateOnly Date { get; set; } = DateOnly.Parse("2023-10-01"); + public TimeOnly Time { get; set; } = TimeOnly.Parse("12:00:00"); + public TimeSpan Duration { get; set; } = TimeSpan.FromHours(1); + public Guid Id { get; set; } = Guid.Parse("1a1bb98f-47c6-407b-9481-78476affe52a"); + public bool IsActive { get; set; } = true; + public int Count { get; set; } = 42; + public char Initial { get; set; } = 'A'; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/QueryParameterTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/QueryParameterTests.cs new file mode 100644 index 000000000000..7362c5e111d5 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/QueryParameterTests.cs @@ -0,0 +1,108 @@ +using NUnit.Framework; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional.Test.Core.RawClientTests; + +[TestFixture] +[Parallelizable(ParallelScope.Self)] +public class QueryParameterTests +{ + [Test] + public void QueryParameters_BasicParameters() + { + var queryString = new QueryStringBuilder.Builder() + .Add("foo", "bar") + .Add("baz", "qux") + .Build(); + + Assert.That(queryString, Is.EqualTo("?foo=bar&baz=qux")); + } + + [Test] + public void QueryParameters_SpecialCharacterEscaping() + { + var queryString = new QueryStringBuilder.Builder() + .Add("email", "bob+test@example.com") + .Add("%Complete", "100") + .Add("space test", "hello world") + .Build(); + + Assert.That(queryString, Does.Contain("email=bob%2Btest%40example.com")); + Assert.That(queryString, Does.Contain("%25Complete=100")); + Assert.That(queryString, Does.Contain("space%20test=hello%20world")); + } + + [Test] + public void QueryParameters_MergeAdditionalParameters() + { + var queryString = new QueryStringBuilder.Builder() + .Add("sdk", "param") + .MergeAdditional(new List> { new("user", "value") }) + .Build(); + + Assert.That(queryString, Does.Contain("sdk=param")); + Assert.That(queryString, Does.Contain("user=value")); + } + + [Test] + public void QueryParameters_AdditionalOverridesSdk() + { + var queryString = new QueryStringBuilder.Builder() + .Add("foo", "sdk_value") + .MergeAdditional(new List> { new("foo", "user_override") }) + .Build(); + + Assert.That(queryString, Does.Contain("foo=user_override")); + Assert.That(queryString, Does.Not.Contain("sdk_value")); + } + + [Test] + public void QueryParameters_AdditionalMultipleValues() + { + var queryString = new QueryStringBuilder.Builder() + .Add("foo", "sdk_value") + .MergeAdditional( + new List> { new("foo", "user1"), new("foo", "user2") } + ) + .Build(); + + Assert.That(queryString, Does.Contain("foo=user1")); + Assert.That(queryString, Does.Contain("foo=user2")); + Assert.That(queryString, Does.Not.Contain("sdk_value")); + } + + [Test] + public void QueryParameters_OnlyAdditionalParameters() + { + var queryString = new QueryStringBuilder.Builder() + .MergeAdditional( + new List> { new("foo", "bar"), new("baz", "qux") } + ) + .Build(); + + Assert.That(queryString, Does.Contain("foo=bar")); + Assert.That(queryString, Does.Contain("baz=qux")); + } + + [Test] + public void QueryParameters_EmptyAdditionalParameters() + { + var queryString = new QueryStringBuilder.Builder() + .Add("foo", "bar") + .MergeAdditional(new List>()) + .Build(); + + Assert.That(queryString, Is.EqualTo("?foo=bar")); + } + + [Test] + public void QueryParameters_NullAdditionalParameters() + { + var queryString = new QueryStringBuilder.Builder() + .Add("foo", "bar") + .MergeAdditional(null) + .Build(); + + Assert.That(queryString, Is.EqualTo("?foo=bar")); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/RetriesTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/RetriesTests.cs new file mode 100644 index 000000000000..bd03b5d5b477 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/RawClientTests/RetriesTests.cs @@ -0,0 +1,406 @@ +using global::System.Net.Http; +using global::System.Text.Json; +using NUnit.Framework; +using SeedBasicAuthOptional.Core; +using WireMock.Server; +using SystemTask = global::System.Threading.Tasks.Task; +using WireMockRequest = WireMock.RequestBuilders.Request; +using WireMockResponse = WireMock.ResponseBuilders.Response; + +namespace SeedBasicAuthOptional.Test.Core.RawClientTests; + +[TestFixture] +[Parallelizable(ParallelScope.Self)] +public class RetriesTests +{ + private const int MaxRetries = 3; + private WireMockServer _server; + private HttpClient _httpClient; + private RawClient _rawClient; + private string _baseUrl; + + [SetUp] + public void SetUp() + { + _server = WireMockServer.Start(); + _baseUrl = _server.Url ?? ""; + _httpClient = new HttpClient { BaseAddress = new Uri(_baseUrl) }; + _rawClient = new RawClient( + new ClientOptions { HttpClient = _httpClient, MaxRetries = MaxRetries } + ) + { + BaseRetryDelay = 0, + }; + } + + [Test] + [TestCase(408)] + [TestCase(429)] + [TestCase(500)] + [TestCase(504)] + public async SystemTask SendRequestAsync_ShouldRetry_OnRetryableStatusCodes(int statusCode) + { + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) + .InScenario("Retry") + .WillSetStateTo("Server Error") + .RespondWith(WireMockResponse.Create().WithStatusCode(statusCode)); + + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) + .InScenario("Retry") + .WhenStateIs("Server Error") + .WillSetStateTo("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(statusCode)); + + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) + .InScenario("Retry") + .WhenStateIs("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(200).WithBody("Success")); + + var request = new EmptyRequest + { + BaseUrl = _baseUrl, + Method = HttpMethod.Get, + Path = "/test", + }; + + var response = await _rawClient.SendRequestAsync(request); + Assert.That(response.StatusCode, Is.EqualTo(200)); + + var content = await response.Raw.Content.ReadAsStringAsync(); + using (Assert.EnterMultipleScope()) + { + Assert.That(content, Is.EqualTo("Success")); + + Assert.That(_server.LogEntries, Has.Count.EqualTo(MaxRetries)); + } + } + + [Test] + [TestCase(400)] + [TestCase(409)] + public async SystemTask SendRequestAsync_ShouldRetry_OnNonRetryableStatusCodes(int statusCode) + { + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) + .InScenario("Retry") + .WillSetStateTo("Server Error") + .RespondWith(WireMockResponse.Create().WithStatusCode(statusCode).WithBody("Failure")); + + var request = new JsonRequest + { + BaseUrl = _baseUrl, + Method = HttpMethod.Get, + Path = "/test", + Body = new { }, + }; + + var response = await _rawClient.SendRequestAsync(request); + Assert.That(response.StatusCode, Is.EqualTo(statusCode)); + + var content = await response.Raw.Content.ReadAsStringAsync(); + Assert.Multiple(() => + { + Assert.That(content, Is.EqualTo("Failure")); + + Assert.That(_server.LogEntries, Has.Count.EqualTo(1)); + }); + } + + [Test] + public async SystemTask SendRequestAsync_ShouldNotRetry_WithStreamRequest() + { + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingPost()) + .InScenario("Retry") + .WillSetStateTo("Server Error") + .RespondWith(WireMockResponse.Create().WithStatusCode(429).WithBody("Failure")); + + var request = new StreamRequest + { + BaseUrl = _baseUrl, + Method = HttpMethod.Post, + Path = "/test", + Body = new MemoryStream(), + }; + + var response = await _rawClient.SendRequestAsync(request); + Assert.That(response.StatusCode, Is.EqualTo(429)); + + var content = await response.Raw.Content.ReadAsStringAsync(); + Assert.Multiple(() => + { + Assert.That(content, Is.EqualTo("Failure")); + Assert.That(_server.LogEntries, Has.Count.EqualTo(1)); + }); + } + + [Test] + public async SystemTask SendRequestAsync_ShouldNotRetry_WithMultiPartFormRequest_WithStream() + { + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingPost()) + .InScenario("Retry") + .WillSetStateTo("Server Error") + .RespondWith(WireMockResponse.Create().WithStatusCode(429).WithBody("Failure")); + + var request = new SeedBasicAuthOptional.Core.MultipartFormRequest + { + BaseUrl = _baseUrl, + Method = HttpMethod.Post, + Path = "/test", + }; + request.AddFileParameterPart("file", new MemoryStream()); + + var response = await _rawClient.SendRequestAsync(request); + Assert.That(response.StatusCode, Is.EqualTo(429)); + + var content = await response.Raw.Content.ReadAsStringAsync(); + Assert.Multiple(() => + { + Assert.That(content, Is.EqualTo("Failure")); + Assert.That(_server.LogEntries, Has.Count.EqualTo(1)); + }); + } + + [Test] + public async SystemTask SendRequestAsync_ShouldRetry_WithMultiPartFormRequest_WithoutStream() + { + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingPost()) + .InScenario("Retry") + .WillSetStateTo("Server Error") + .RespondWith(WireMockResponse.Create().WithStatusCode(429)); + + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingPost()) + .InScenario("Retry") + .WhenStateIs("Server Error") + .WillSetStateTo("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(429)); + + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingPost()) + .InScenario("Retry") + .WhenStateIs("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(200).WithBody("Success")); + + var request = new SeedBasicAuthOptional.Core.MultipartFormRequest + { + BaseUrl = _baseUrl, + Method = HttpMethod.Post, + Path = "/test", + }; + request.AddJsonPart("object", new { }); + + var response = await _rawClient.SendRequestAsync(request); + Assert.That(response.StatusCode, Is.EqualTo(200)); + + var content = await response.Raw.Content.ReadAsStringAsync(); + Assert.Multiple(() => + { + Assert.That(content, Is.EqualTo("Success")); + Assert.That(_server.LogEntries, Has.Count.EqualTo(MaxRetries)); + }); + } + + [Test] + public async SystemTask SendRequestAsync_ShouldRespectRetryAfterHeader_WithSecondsValue() + { + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) + .InScenario("RetryAfter") + .WillSetStateTo("Success") + .RespondWith( + WireMockResponse.Create().WithStatusCode(429).WithHeader("Retry-After", "1") + ); + + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) + .InScenario("RetryAfter") + .WhenStateIs("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(200).WithBody("Success")); + + var request = new EmptyRequest + { + BaseUrl = _baseUrl, + Method = HttpMethod.Get, + Path = "/test", + }; + + var response = await _rawClient.SendRequestAsync(request); + Assert.That(response.StatusCode, Is.EqualTo(200)); + + var content = await response.Raw.Content.ReadAsStringAsync(); + Assert.Multiple(() => + { + Assert.That(content, Is.EqualTo("Success")); + Assert.That(_server.LogEntries, Has.Count.EqualTo(2)); + }); + } + + [Test] + public async SystemTask SendRequestAsync_ShouldRespectRetryAfterHeader_WithHttpDateValue() + { + var retryAfterDate = DateTimeOffset.UtcNow.AddSeconds(1).ToString("R"); + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) + .InScenario("RetryAfterDate") + .WillSetStateTo("Success") + .RespondWith( + WireMockResponse + .Create() + .WithStatusCode(429) + .WithHeader("Retry-After", retryAfterDate) + ); + + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) + .InScenario("RetryAfterDate") + .WhenStateIs("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(200).WithBody("Success")); + + var request = new EmptyRequest + { + BaseUrl = _baseUrl, + Method = HttpMethod.Get, + Path = "/test", + }; + + var response = await _rawClient.SendRequestAsync(request); + Assert.That(response.StatusCode, Is.EqualTo(200)); + + var content = await response.Raw.Content.ReadAsStringAsync(); + Assert.Multiple(() => + { + Assert.That(content, Is.EqualTo("Success")); + Assert.That(_server.LogEntries, Has.Count.EqualTo(2)); + }); + } + + [Test] + public async SystemTask SendRequestAsync_ShouldRespectXRateLimitResetHeader() + { + var resetTime = DateTimeOffset.UtcNow.AddSeconds(1).ToUnixTimeSeconds().ToString(); + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) + .InScenario("RateLimitReset") + .WillSetStateTo("Success") + .RespondWith( + WireMockResponse + .Create() + .WithStatusCode(429) + .WithHeader("X-RateLimit-Reset", resetTime) + ); + + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) + .InScenario("RateLimitReset") + .WhenStateIs("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(200).WithBody("Success")); + + var request = new EmptyRequest + { + BaseUrl = _baseUrl, + Method = HttpMethod.Get, + Path = "/test", + }; + + var response = await _rawClient.SendRequestAsync(request); + Assert.That(response.StatusCode, Is.EqualTo(200)); + + var content = await response.Raw.Content.ReadAsStringAsync(); + Assert.Multiple(() => + { + Assert.That(content, Is.EqualTo("Success")); + Assert.That(_server.LogEntries, Has.Count.EqualTo(2)); + }); + } + + [Test] + public async SystemTask SendRequestAsync_ShouldPreserveJsonBody_OnRetry() + { + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingPost()) + .InScenario("RetryWithBody") + .WillSetStateTo("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(500)); + + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingPost()) + .InScenario("RetryWithBody") + .WhenStateIs("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(200).WithBody("Success")); + + var request = new JsonRequest + { + BaseUrl = _baseUrl, + Method = HttpMethod.Post, + Path = "/test", + Body = new { key = "value" }, + }; + + var response = await _rawClient.SendRequestAsync(request); + Assert.That(response.StatusCode, Is.EqualTo(200)); + + var content = await response.Raw.Content.ReadAsStringAsync(); + using (Assert.EnterMultipleScope()) + { + Assert.That(content, Is.EqualTo("Success")); + Assert.That(_server.LogEntries, Has.Count.EqualTo(2)); + + // Verify the retried request preserved the JSON body (compare parsed to ignore formatting differences) + var retriedEntry = _server.LogEntries.ElementAt(1); + using var actualJson = JsonDocument.Parse(retriedEntry.RequestMessage.Body!); + Assert.That(actualJson.RootElement.GetProperty("key").GetString(), Is.EqualTo("value")); + } + } + + [Test] + public async SystemTask SendRequestAsync_ShouldPreserveMultipartBody_OnRetry() + { + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingPost()) + .InScenario("RetryMultipart") + .WillSetStateTo("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(500)); + + _server + .Given(WireMockRequest.Create().WithPath("/test").UsingPost()) + .InScenario("RetryMultipart") + .WhenStateIs("Success") + .RespondWith(WireMockResponse.Create().WithStatusCode(200).WithBody("Success")); + + var request = new SeedBasicAuthOptional.Core.MultipartFormRequest + { + BaseUrl = _baseUrl, + Method = HttpMethod.Post, + Path = "/test", + }; + request.AddJsonPart("object", new { key = "value" }); + + var response = await _rawClient.SendRequestAsync(request); + Assert.That(response.StatusCode, Is.EqualTo(200)); + + var content = await response.Raw.Content.ReadAsStringAsync(); + using (Assert.EnterMultipleScope()) + { + Assert.That(content, Is.EqualTo("Success")); + Assert.That(_server.LogEntries, Has.Count.EqualTo(2)); + + // Verify the retried request preserved the multipart body (check key/value presence to ignore formatting differences) + var retriedEntry = _server.LogEntries.ElementAt(1); + Assert.That(retriedEntry.RequestMessage.Body, Does.Contain("\"key\"")); + Assert.That(retriedEntry.RequestMessage.Body, Does.Contain("\"value\"")); + } + } + + [TearDown] + public void TearDown() + { + _server.Dispose(); + _httpClient.Dispose(); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/WithRawResponseTests.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/WithRawResponseTests.cs new file mode 100644 index 000000000000..07deaaab2932 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Core/WithRawResponseTests.cs @@ -0,0 +1,269 @@ +using global::System.Net; +using global::System.Net.Http.Headers; +using NUnit.Framework; +using SeedBasicAuthOptional; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional.Test.Core; + +[TestFixture] +public class WithRawResponseTests +{ + [Test] + public async global::System.Threading.Tasks.Task WithRawResponseTask_DirectAwait_ReturnsData() + { + // Arrange + var expectedData = "test-data"; + var task = CreateWithRawResponseTask(expectedData, HttpStatusCode.OK); + + // Act + var result = await task; + + // Assert + Assert.That(result, Is.EqualTo(expectedData)); + } + + [Test] + public async global::System.Threading.Tasks.Task WithRawResponseTask_WithRawResponse_ReturnsDataAndMetadata() + { + // Arrange + var expectedData = "test-data"; + var expectedStatusCode = HttpStatusCode.Created; + var task = CreateWithRawResponseTask(expectedData, expectedStatusCode); + + // Act + var result = await task.WithRawResponse(); + + // Assert + Assert.That(result.Data, Is.EqualTo(expectedData)); + Assert.That(result.RawResponse.StatusCode, Is.EqualTo(expectedStatusCode)); + Assert.That(result.RawResponse.Url, Is.Not.Null); + } + + [Test] + public async global::System.Threading.Tasks.Task ResponseHeaders_TryGetValue_CaseInsensitive() + { + // Arrange + using var response = CreateHttpResponse(HttpStatusCode.OK); + response.Headers.Add("X-Request-Id", "12345"); + var headers = ResponseHeaders.FromHttpResponseMessage(response); + + // Act & Assert + Assert.That(headers.TryGetValue("X-Request-Id", out var value), Is.True); + Assert.That(value, Is.EqualTo("12345")); + + Assert.That(headers.TryGetValue("x-request-id", out value), Is.True); + Assert.That(value, Is.EqualTo("12345")); + + Assert.That(headers.TryGetValue("X-REQUEST-ID", out value), Is.True); + Assert.That(value, Is.EqualTo("12345")); + } + + [Test] + public async global::System.Threading.Tasks.Task ResponseHeaders_TryGetValues_ReturnsMultipleValues() + { + // Arrange + using var response = CreateHttpResponse(HttpStatusCode.OK); + response.Headers.Add("Set-Cookie", new[] { "cookie1=value1", "cookie2=value2" }); + var headers = ResponseHeaders.FromHttpResponseMessage(response); + + // Act + var success = headers.TryGetValues("Set-Cookie", out var values); + + // Assert + Assert.That(success, Is.True); + Assert.That(values, Is.Not.Null); + Assert.That(values!.Count(), Is.EqualTo(2)); + Assert.That(values, Does.Contain("cookie1=value1")); + Assert.That(values, Does.Contain("cookie2=value2")); + } + + [Test] + public async global::System.Threading.Tasks.Task ResponseHeaders_ContentType_ReturnsValue() + { + // Arrange + using var response = CreateHttpResponse(HttpStatusCode.OK); + response.Content = new StringContent( + "{}", + global::System.Text.Encoding.UTF8, + "application/json" + ); + var headers = ResponseHeaders.FromHttpResponseMessage(response); + + // Act + var contentType = headers.ContentType; + + // Assert + Assert.That(contentType, Is.Not.Null); + Assert.That(contentType, Does.Contain("application/json")); + } + + [Test] + public async global::System.Threading.Tasks.Task ResponseHeaders_ContentLength_ReturnsValue() + { + // Arrange + var content = "test content"; + using var response = CreateHttpResponse(HttpStatusCode.OK); + response.Content = new StringContent(content); + var headers = ResponseHeaders.FromHttpResponseMessage(response); + + // Act + var contentLength = headers.ContentLength; + + // Assert + Assert.That(contentLength, Is.Not.Null); + Assert.That(contentLength, Is.GreaterThan(0)); + } + + [Test] + public async global::System.Threading.Tasks.Task ResponseHeaders_Contains_ReturnsTrueForExistingHeader() + { + // Arrange + using var response = CreateHttpResponse(HttpStatusCode.OK); + response.Headers.Add("X-Custom-Header", "value"); + var headers = ResponseHeaders.FromHttpResponseMessage(response); + + // Act & Assert + Assert.That(headers.Contains("X-Custom-Header"), Is.True); + Assert.That(headers.Contains("x-custom-header"), Is.True); + Assert.That(headers.Contains("NonExistent"), Is.False); + } + + [Test] + public async global::System.Threading.Tasks.Task ResponseHeaders_Enumeration_IncludesAllHeaders() + { + // Arrange + using var response = CreateHttpResponse(HttpStatusCode.OK); + response.Headers.Add("X-Header-1", "value1"); + response.Headers.Add("X-Header-2", "value2"); + response.Content = new StringContent("test"); + var headers = ResponseHeaders.FromHttpResponseMessage(response); + + // Act + var allHeaders = headers.ToList(); + + // Assert + Assert.That(allHeaders.Count, Is.GreaterThan(0)); + Assert.That(allHeaders.Any(h => h.Name == "X-Header-1"), Is.True); + Assert.That(allHeaders.Any(h => h.Name == "X-Header-2"), Is.True); + } + + [Test] + public async global::System.Threading.Tasks.Task WithRawResponseTask_ErrorStatusCode_StillReturnsMetadata() + { + // Arrange + var expectedData = "error-data"; + var task = CreateWithRawResponseTask(expectedData, HttpStatusCode.BadRequest); + + // Act + var result = await task.WithRawResponse(); + + // Assert + Assert.That(result.Data, Is.EqualTo(expectedData)); + Assert.That(result.RawResponse.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); + } + + [Test] + public async global::System.Threading.Tasks.Task WithRawResponseTask_Url_IsPreserved() + { + // Arrange + var expectedUrl = new Uri("https://api.example.com/users/123"); + var task = CreateWithRawResponseTask("data", HttpStatusCode.OK, expectedUrl); + + // Act + var result = await task.WithRawResponse(); + + // Assert + Assert.That(result.RawResponse.Url, Is.EqualTo(expectedUrl)); + } + + [Test] + public async global::System.Threading.Tasks.Task ResponseHeaders_TryGetValue_NonExistentHeader_ReturnsFalse() + { + // Arrange + using var response = CreateHttpResponse(HttpStatusCode.OK); + var headers = ResponseHeaders.FromHttpResponseMessage(response); + + // Act + var success = headers.TryGetValue("X-NonExistent", out var value); + + // Assert + Assert.That(success, Is.False); + Assert.That(value, Is.Null); + } + + [Test] + public async global::System.Threading.Tasks.Task ResponseHeaders_TryGetValues_NonExistentHeader_ReturnsFalse() + { + // Arrange + using var response = CreateHttpResponse(HttpStatusCode.OK); + var headers = ResponseHeaders.FromHttpResponseMessage(response); + + // Act + var success = headers.TryGetValues("X-NonExistent", out var values); + + // Assert + Assert.That(success, Is.False); + Assert.That(values, Is.Null); + } + + [Test] + public async global::System.Threading.Tasks.Task WithRawResponseTask_ImplicitConversion_ToTask() + { + // Arrange + var expectedData = "test-data"; + var task = CreateWithRawResponseTask(expectedData, HttpStatusCode.OK); + + // Act - implicitly convert to Task + global::System.Threading.Tasks.Task regularTask = task; + var result = await regularTask; + + // Assert + Assert.That(result, Is.EqualTo(expectedData)); + } + + [Test] + public void WithRawResponseTask_ImplicitConversion_AssignToTaskVariable() + { + // Arrange + var expectedData = "test-data"; + var wrappedTask = CreateWithRawResponseTask(expectedData, HttpStatusCode.OK); + + // Act - assign to Task variable + global::System.Threading.Tasks.Task regularTask = wrappedTask; + + // Assert + Assert.That(regularTask, Is.Not.Null); + Assert.That(regularTask, Is.InstanceOf>()); + } + + // Helper methods + + private static WithRawResponseTask CreateWithRawResponseTask( + T data, + HttpStatusCode statusCode, + Uri? url = null + ) + { + url ??= new Uri("https://api.example.com/test"); + using var httpResponse = CreateHttpResponse(statusCode); + httpResponse.RequestMessage = new HttpRequestMessage(HttpMethod.Get, url); + + var rawResponse = new RawResponse + { + StatusCode = statusCode, + Url = url, + Headers = ResponseHeaders.FromHttpResponseMessage(httpResponse), + }; + + var withRawResponse = new WithRawResponse { Data = data, RawResponse = rawResponse }; + + var task = global::System.Threading.Tasks.Task.FromResult(withRawResponse); + return new WithRawResponseTask(task); + } + + private static HttpResponseMessage CreateHttpResponse(HttpStatusCode statusCode) + { + return new HttpResponseMessage(statusCode) { Content = new StringContent("") }; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.Custom.props b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.Custom.props new file mode 100644 index 000000000000..aac9b5020d80 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.Custom.props @@ -0,0 +1,6 @@ + + diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.csproj b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.csproj new file mode 100644 index 000000000000..2ffd45f0bd14 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/SeedBasicAuthOptional.Test.csproj @@ -0,0 +1,39 @@ + + + net9.0 + 12 + enable + enable + false + true + true + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/TestClient.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/TestClient.cs new file mode 100644 index 000000000000..378fc6e838e2 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/TestClient.cs @@ -0,0 +1,6 @@ +using NUnit.Framework; + +namespace SeedBasicAuthOptional.Test; + +[TestFixture] +public class TestClient; diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BaseMockServerTest.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BaseMockServerTest.cs new file mode 100644 index 000000000000..c52d8138a3d3 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BaseMockServerTest.cs @@ -0,0 +1,39 @@ +using NUnit.Framework; +using SeedBasicAuthOptional; +using WireMock.Logging; +using WireMock.Server; +using WireMock.Settings; + +namespace SeedBasicAuthOptional.Test.Unit.MockServer; + +public class BaseMockServerTest +{ + protected WireMockServer Server { get; set; } = null!; + + protected SeedBasicAuthOptionalClient Client { get; set; } = null!; + + protected RequestOptions RequestOptions { get; set; } = new(); + + [OneTimeSetUp] + public void GlobalSetup() + { + // Start the WireMock server + Server = WireMockServer.Start( + new WireMockServerSettings { Logger = new WireMockConsoleLogger() } + ); + + // Initialize the Client + Client = new SeedBasicAuthOptionalClient( + "USERNAME", + "PASSWORD", + clientOptions: new ClientOptions { BaseUrl = Server.Urls[0], MaxRetries = 0 } + ); + } + + [OneTimeTearDown] + public void GlobalTeardown() + { + Server.Stop(); + Server.Dispose(); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BasicAuth/GetWithBasicAuthTest.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BasicAuth/GetWithBasicAuthTest.cs new file mode 100644 index 000000000000..15a25ad49511 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BasicAuth/GetWithBasicAuthTest.cs @@ -0,0 +1,50 @@ +using NUnit.Framework; +using SeedBasicAuthOptional.Test.Unit.MockServer; +using SeedBasicAuthOptional.Test.Utils; + +namespace SeedBasicAuthOptional.Test.Unit.MockServer.BasicAuth; + +[TestFixture] +[Parallelizable(ParallelScope.Self)] +public class GetWithBasicAuthTest : BaseMockServerTest +{ + [NUnit.Framework.Test] + public async Task MockServerTest_1() + { + const string mockResponse = """ + true + """; + + Server + .Given(WireMock.RequestBuilders.Request.Create().WithPath("/basic-auth").UsingGet()) + .RespondWith( + WireMock + .ResponseBuilders.Response.Create() + .WithStatusCode(200) + .WithBody(mockResponse) + ); + + var response = await Client.BasicAuth.GetWithBasicAuthAsync(); + JsonAssert.AreEqual(response, mockResponse); + } + + [NUnit.Framework.Test] + public async Task MockServerTest_2() + { + const string mockResponse = """ + true + """; + + Server + .Given(WireMock.RequestBuilders.Request.Create().WithPath("/basic-auth").UsingGet()) + .RespondWith( + WireMock + .ResponseBuilders.Response.Create() + .WithStatusCode(200) + .WithBody(mockResponse) + ); + + var response = await Client.BasicAuth.GetWithBasicAuthAsync(); + JsonAssert.AreEqual(response, mockResponse); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BasicAuth/PostWithBasicAuthTest.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BasicAuth/PostWithBasicAuthTest.cs new file mode 100644 index 000000000000..0eb28eb4a585 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Unit/MockServer/BasicAuth/PostWithBasicAuthTest.cs @@ -0,0 +1,78 @@ +using NUnit.Framework; +using SeedBasicAuthOptional.Test.Unit.MockServer; +using SeedBasicAuthOptional.Test.Utils; + +namespace SeedBasicAuthOptional.Test.Unit.MockServer.BasicAuth; + +[TestFixture] +[Parallelizable(ParallelScope.Self)] +public class PostWithBasicAuthTest : BaseMockServerTest +{ + [NUnit.Framework.Test] + public async Task MockServerTest_1() + { + const string requestJson = """ + { + "key": "value" + } + """; + + const string mockResponse = """ + true + """; + + Server + .Given( + WireMock + .RequestBuilders.Request.Create() + .WithPath("/basic-auth") + .UsingPost() + .WithBodyAsJson(requestJson) + ) + .RespondWith( + WireMock + .ResponseBuilders.Response.Create() + .WithStatusCode(200) + .WithBody(mockResponse) + ); + + var response = await Client.BasicAuth.PostWithBasicAuthAsync( + new Dictionary() { { "key", "value" } } + ); + JsonAssert.AreEqual(response, mockResponse); + } + + [NUnit.Framework.Test] + public async Task MockServerTest_2() + { + const string requestJson = """ + { + "key": "value" + } + """; + + const string mockResponse = """ + true + """; + + Server + .Given( + WireMock + .RequestBuilders.Request.Create() + .WithPath("/basic-auth") + .UsingPost() + .WithBodyAsJson(requestJson) + ) + .RespondWith( + WireMock + .ResponseBuilders.Response.Create() + .WithStatusCode(200) + .WithBody(mockResponse) + ); + + var response = await Client.BasicAuth.PostWithBasicAuthAsync( + new Dictionary() { { "key", "value" } } + ); + JsonAssert.AreEqual(response, mockResponse); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/AdditionalPropertiesComparer.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/AdditionalPropertiesComparer.cs new file mode 100644 index 000000000000..1c5f0cf63e64 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/AdditionalPropertiesComparer.cs @@ -0,0 +1,219 @@ +using global::System.Text.Json; +using NUnit.Framework.Constraints; +using SeedBasicAuthOptional; +using SeedBasicAuthOptional.Core; + +namespace NUnit.Framework; + +/// +/// Extensions for EqualConstraint to handle AdditionalProperties values. +/// +public static class AdditionalPropertiesComparerExtensions +{ + /// + /// Modifies the EqualConstraint to handle AdditionalProperties instances by comparing their + /// serialized JSON representations. This handles the type mismatch between native C# types + /// and JsonElement values that occur when comparing manually constructed objects with + /// deserialized objects. + /// + /// The EqualConstraint to modify. + /// The same constraint instance for method chaining. + public static EqualConstraint UsingAdditionalPropertiesComparer(this EqualConstraint constraint) + { + constraint.Using( + (x, y) => + { + if (x.Count != y.Count) + { + return false; + } + + foreach (var key in x.Keys) + { + if (!y.ContainsKey(key)) + { + return false; + } + + var xElement = JsonUtils.SerializeToElement(x[key]); + var yElement = JsonUtils.SerializeToElement(y[key]); + + if (!JsonElementsAreEqual(xElement, yElement)) + { + return false; + } + } + + return true; + } + ); + + return constraint; + } + + /// + /// Modifies the EqualConstraint to handle Dictionary<string, object?> values by comparing + /// their serialized JSON representations. This handles the type mismatch between native C# types + /// and JsonElement values that occur when comparing manually constructed objects with + /// deserialized objects. + /// + /// The EqualConstraint to modify. + /// The same constraint instance for method chaining. + public static EqualConstraint UsingObjectDictionaryComparer(this EqualConstraint constraint) + { + constraint.Using>( + (x, y) => + { + if (x.Count != y.Count) + { + return false; + } + + foreach (var key in x.Keys) + { + if (!y.ContainsKey(key)) + { + return false; + } + + var xElement = JsonUtils.SerializeToElement(x[key]); + var yElement = JsonUtils.SerializeToElement(y[key]); + + if (!JsonElementsAreEqual(xElement, yElement)) + { + return false; + } + } + + return true; + } + ); + + return constraint; + } + + internal static bool JsonElementsAreEqualPublic(JsonElement x, JsonElement y) => + JsonElementsAreEqual(x, y); + + private static bool JsonElementsAreEqual(JsonElement x, JsonElement y) + { + if (x.ValueKind != y.ValueKind) + { + return false; + } + + return x.ValueKind switch + { + JsonValueKind.Object => CompareJsonObjects(x, y), + JsonValueKind.Array => CompareJsonArrays(x, y), + JsonValueKind.String => x.GetString() == y.GetString(), + JsonValueKind.Number => x.GetDecimal() == y.GetDecimal(), + JsonValueKind.True => true, + JsonValueKind.False => true, + JsonValueKind.Null => true, + _ => false, + }; + } + + private static bool CompareJsonObjects(JsonElement x, JsonElement y) + { + var xProps = new Dictionary(); + var yProps = new Dictionary(); + + foreach (var prop in x.EnumerateObject()) + xProps[prop.Name] = prop.Value; + + foreach (var prop in y.EnumerateObject()) + yProps[prop.Name] = prop.Value; + + if (xProps.Count != yProps.Count) + { + return false; + } + + foreach (var key in xProps.Keys) + { + if (!yProps.ContainsKey(key)) + { + return false; + } + + if (!JsonElementsAreEqual(xProps[key], yProps[key])) + { + return false; + } + } + + return true; + } + + private static bool CompareJsonArrays(JsonElement x, JsonElement y) + { + var xArray = x.EnumerateArray().ToList(); + var yArray = y.EnumerateArray().ToList(); + + if (xArray.Count != yArray.Count) + { + return false; + } + + for (var i = 0; i < xArray.Count; i++) + { + if (!JsonElementsAreEqual(xArray[i], yArray[i])) + { + return false; + } + } + + return true; + } + + /// + /// Modifies the EqualConstraint to handle cross-type comparisons involving JsonElement. + /// When UsingPropertiesComparer() walks object properties and encounters a property typed as + /// 'object', the expected side may be a Dictionary<object, object?> while the actual + /// (deserialized) side is a JsonElement. These typed predicates bridge that gap by serializing + /// the non-JsonElement side and comparing JSON representations. + /// + /// Uses typed Func<TExpected, TActual, bool> predicates instead of a non-generic + /// IComparer/IEqualityComparer so that NUnit's CanCompare type check ensures these only + /// fire when one side is a JsonElement, letting UsingPropertiesComparer() handle all + /// same-type comparisons normally. + /// + /// The EqualConstraint to modify. + /// The same constraint instance for method chaining. + public static EqualConstraint UsingJsonSerializationComparer(this EqualConstraint constraint) + { + // Handle: expected is non-JsonElement, actual is JsonElement + constraint.Using( + (actualJsonElement, expectedObj) => + { + try + { + var expectedElement = JsonUtils.SerializeToElement(expectedObj); + return JsonElementsAreEqualPublic(expectedElement, actualJsonElement); + } + catch + { + return false; + } + } + ); + // Handle reverse: expected is JsonElement, actual is non-JsonElement + constraint.Using( + (actualObj, expectedJsonElement) => + { + try + { + var actualElement = JsonUtils.SerializeToElement(actualObj); + return JsonElementsAreEqualPublic(expectedJsonElement, actualElement); + } + catch + { + return false; + } + } + ); + return constraint; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/JsonAssert.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/JsonAssert.cs new file mode 100644 index 000000000000..cccd122c8ed1 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/JsonAssert.cs @@ -0,0 +1,29 @@ +using global::System.Text.Json; +using NUnit.Framework; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional.Test.Utils; + +internal static class JsonAssert +{ + /// + /// Asserts that the serialized JSON of an object equals the expected JSON string. + /// Uses JsonElement comparison for reliable deep equality of collections and union types. + /// + internal static void AreEqual(object actual, string expectedJson) + { + var actualElement = JsonUtils.SerializeToElement(actual); + var expectedElement = JsonUtils.Deserialize(expectedJson); + Assert.That(actualElement, Is.EqualTo(expectedElement).UsingJsonElementComparer()); + } + + /// + /// Asserts that the given JSON string survives a deserialization/serialization round-trip + /// intact: deserializes to T then re-serializes and compares to the original JSON. + /// + internal static void Roundtrips(string json) + { + var deserialized = JsonUtils.Deserialize(json); + AreEqual(deserialized!, json); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/JsonElementComparer.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/JsonElementComparer.cs new file mode 100644 index 000000000000..a37ef402c1ac --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/JsonElementComparer.cs @@ -0,0 +1,236 @@ +using global::System.Text.Json; +using NUnit.Framework.Constraints; + +namespace NUnit.Framework; + +/// +/// Extensions for EqualConstraint to handle JsonElement objects. +/// +public static class JsonElementComparerExtensions +{ + /// + /// Extension method for comparing JsonElement objects in NUnit tests. + /// Property order doesn't matter, but array order does matter. + /// Includes special handling for DateTime string formats. + /// + /// The Is.EqualTo() constraint instance. + /// A constraint that can compare JsonElements with detailed diffs. + public static EqualConstraint UsingJsonElementComparer(this EqualConstraint constraint) + { + return constraint.Using(new JsonElementComparer()); + } +} + +/// +/// Equality comparer for JsonElement with detailed reporting. +/// Property order doesn't matter, but array order does matter. +/// Now includes special handling for DateTime string formats with improved null handling. +/// +public class JsonElementComparer : IEqualityComparer +{ + private string _failurePath = string.Empty; + + /// + public bool Equals(JsonElement x, JsonElement y) + { + _failurePath = string.Empty; + return CompareJsonElements(x, y, string.Empty); + } + + /// + public int GetHashCode(JsonElement obj) + { + return JsonSerializer.Serialize(obj).GetHashCode(); + } + + private bool CompareJsonElements(JsonElement x, JsonElement y, string path) + { + // If value kinds don't match, they're not equivalent + if (x.ValueKind != y.ValueKind) + { + _failurePath = $"{path}: Expected {x.ValueKind} but got {y.ValueKind}"; + return false; + } + + switch (x.ValueKind) + { + case JsonValueKind.Object: + return CompareJsonObjects(x, y, path); + + case JsonValueKind.Array: + return CompareJsonArraysInOrder(x, y, path); + + case JsonValueKind.String: + string? xStr = x.GetString(); + string? yStr = y.GetString(); + + // Handle null strings + if (xStr is null && yStr is null) + return true; + + if (xStr is null || yStr is null) + { + _failurePath = + $"{path}: Expected {(xStr is null ? "null" : $"\"{xStr}\"")} but got {(yStr is null ? "null" : $"\"{yStr}\"")}"; + return false; + } + + // Check if they are identical strings + if (xStr == yStr) + return true; + + // Try to handle DateTime strings + if (IsLikelyDateTimeString(xStr) && IsLikelyDateTimeString(yStr)) + { + if (AreEquivalentDateTimeStrings(xStr, yStr)) + return true; + } + + _failurePath = $"{path}: Expected \"{xStr}\" but got \"{yStr}\""; + return false; + + case JsonValueKind.Number: + if (x.GetDecimal() != y.GetDecimal()) + { + _failurePath = $"{path}: Expected {x.GetDecimal()} but got {y.GetDecimal()}"; + return false; + } + + return true; + + case JsonValueKind.True: + case JsonValueKind.False: + if (x.GetBoolean() != y.GetBoolean()) + { + _failurePath = $"{path}: Expected {x.GetBoolean()} but got {y.GetBoolean()}"; + return false; + } + + return true; + + case JsonValueKind.Null: + return true; + + default: + _failurePath = $"{path}: Unsupported JsonValueKind {x.ValueKind}"; + return false; + } + } + + private bool IsLikelyDateTimeString(string? str) + { + // Simple heuristic to identify likely ISO date time strings + return str is not null + && (str.Contains("T") && (str.EndsWith("Z") || str.Contains("+") || str.Contains("-"))); + } + + private bool AreEquivalentDateTimeStrings(string str1, string str2) + { + // Try to parse both as DateTime + if (DateTime.TryParse(str1, out DateTime dt1) && DateTime.TryParse(str2, out DateTime dt2)) + { + return dt1 == dt2; + } + + return false; + } + + private bool CompareJsonObjects(JsonElement x, JsonElement y, string path) + { + // Create dictionaries for both JSON objects + var xProps = new Dictionary(); + var yProps = new Dictionary(); + + foreach (var prop in x.EnumerateObject()) + xProps[prop.Name] = prop.Value; + + foreach (var prop in y.EnumerateObject()) + yProps[prop.Name] = prop.Value; + + // Check if all properties in x exist in y + foreach (var key in xProps.Keys) + { + if (!yProps.ContainsKey(key)) + { + _failurePath = $"{path}: Missing property '{key}'"; + return false; + } + } + + // Check if y has extra properties + foreach (var key in yProps.Keys) + { + if (!xProps.ContainsKey(key)) + { + _failurePath = $"{path}: Unexpected property '{key}'"; + return false; + } + } + + // Compare each property value + foreach (var key in xProps.Keys) + { + var propPath = string.IsNullOrEmpty(path) ? key : $"{path}.{key}"; + if (!CompareJsonElements(xProps[key], yProps[key], propPath)) + { + return false; + } + } + + return true; + } + + private bool CompareJsonArraysInOrder(JsonElement x, JsonElement y, string path) + { + var xArray = x.EnumerateArray(); + var yArray = y.EnumerateArray(); + + // Count x elements + var xCount = 0; + var xElements = new List(); + foreach (var item in xArray) + { + xElements.Add(item); + xCount++; + } + + // Count y elements + var yCount = 0; + var yElements = new List(); + foreach (var item in yArray) + { + yElements.Add(item); + yCount++; + } + + // Check if counts match + if (xCount != yCount) + { + _failurePath = $"{path}: Expected {xCount} items but found {yCount}"; + return false; + } + + // Compare elements in order + for (var i = 0; i < xCount; i++) + { + var itemPath = $"{path}[{i}]"; + if (!CompareJsonElements(xElements[i], yElements[i], itemPath)) + { + return false; + } + } + + return true; + } + + /// + public override string ToString() + { + if (!string.IsNullOrEmpty(_failurePath)) + { + return $"JSON comparison failed at {_failurePath}"; + } + + return "JsonElementEqualityComparer"; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/NUnitExtensions.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/NUnitExtensions.cs new file mode 100644 index 000000000000..816f4c010e6e --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/NUnitExtensions.cs @@ -0,0 +1,32 @@ +using NUnit.Framework.Constraints; + +namespace NUnit.Framework; + +/// +/// Extensions for NUnit constraints. +/// +public static class NUnitExtensions +{ + /// + /// Modifies the EqualConstraint to use our own set of default comparers. + /// + /// + /// + public static EqualConstraint UsingDefaults(this EqualConstraint constraint) => + constraint + .UsingPropertiesComparer() + .UsingReadOnlyMemoryComparer() + .UsingReadOnlyMemoryComparer() + .UsingReadOnlyMemoryComparer() + .UsingReadOnlyMemoryComparer() + .UsingReadOnlyMemoryComparer() + .UsingReadOnlyMemoryComparer() + .UsingReadOnlyMemoryComparer() + .UsingReadOnlyMemoryComparer() + .UsingOneOfComparer() + .UsingJsonElementComparer() + .UsingOptionalComparer() + .UsingObjectDictionaryComparer() + .UsingAdditionalPropertiesComparer() + .UsingJsonSerializationComparer(); +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/OneOfComparer.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/OneOfComparer.cs new file mode 100644 index 000000000000..767439174363 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/OneOfComparer.cs @@ -0,0 +1,86 @@ +using NUnit.Framework.Constraints; +using OneOf; + +namespace NUnit.Framework; + +/// +/// Extensions for EqualConstraint to handle OneOf values. +/// +public static class EqualConstraintExtensions +{ + /// + /// Modifies the EqualConstraint to handle OneOf instances by comparing their inner values. + /// This works alongside other comparison modifiers like UsingPropertiesComparer. + /// + /// The EqualConstraint to modify. + /// The same constraint instance for method chaining. + public static EqualConstraint UsingOneOfComparer(this EqualConstraint constraint) + { + // Register a comparer factory for IOneOf types + constraint.Using( + (x, y) => + { + // ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract + if (x.Value is null && y.Value is null) + { + return true; + } + + if (x.Value is null) + { + return false; + } + + var propertiesComparer = new NUnitEqualityComparer(); + var tolerance = Tolerance.Default; + propertiesComparer.CompareProperties = true; + // Add OneOf comparer to handle nested OneOf values (e.g., in Lists) + propertiesComparer.ExternalComparers.Add( + new OneOfEqualityAdapter(propertiesComparer) + ); + return propertiesComparer.AreEqual(x.Value, y.Value, ref tolerance); + } + ); + + return constraint; + } + + /// + /// EqualityAdapter for comparing IOneOf instances within NUnitEqualityComparer. + /// This enables recursive comparison of nested OneOf values. + /// + private class OneOfEqualityAdapter : EqualityAdapter + { + private readonly NUnitEqualityComparer _comparer; + + public OneOfEqualityAdapter(NUnitEqualityComparer comparer) + { + _comparer = comparer; + } + + public override bool CanCompare(object? x, object? y) + { + return x is IOneOf && y is IOneOf; + } + + public override bool AreEqual(object? x, object? y) + { + var oneOfX = (IOneOf?)x; + var oneOfY = (IOneOf?)y; + + // ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract + if (oneOfX?.Value is null && oneOfY?.Value is null) + { + return true; + } + + if (oneOfX?.Value is null || oneOfY?.Value is null) + { + return false; + } + + var tolerance = Tolerance.Default; + return _comparer.AreEqual(oneOfX.Value, oneOfY.Value, ref tolerance); + } + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/OptionalComparer.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/OptionalComparer.cs new file mode 100644 index 000000000000..1cac67cf25d2 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/OptionalComparer.cs @@ -0,0 +1,104 @@ +using NUnit.Framework.Constraints; +using OneOf; +using SeedBasicAuthOptional.Core; + +namespace NUnit.Framework; + +/// +/// Extensions for EqualConstraint to handle Optional values. +/// +public static class OptionalComparerExtensions +{ + /// + /// Modifies the EqualConstraint to handle Optional instances by comparing their IsDefined state and inner values. + /// This works alongside other comparison modifiers like UsingPropertiesComparer. + /// + /// The EqualConstraint to modify. + /// The same constraint instance for method chaining. + public static EqualConstraint UsingOptionalComparer(this EqualConstraint constraint) + { + // Register a comparer factory for IOptional types + constraint.Using( + (x, y) => + { + // Both must have the same IsDefined state + if (x.IsDefined != y.IsDefined) + { + return false; + } + + // If both are undefined, they're equal + if (!x.IsDefined) + { + return true; + } + + // Both are defined, compare their boxed values + var xValue = x.GetBoxedValue(); + var yValue = y.GetBoxedValue(); + + // ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract + if (xValue is null && yValue is null) + { + return true; + } + + if (xValue is null || yValue is null) + { + return false; + } + + // Use NUnit's property comparer for the inner values + var propertiesComparer = new NUnitEqualityComparer(); + var tolerance = Tolerance.Default; + propertiesComparer.CompareProperties = true; + // Add OneOf comparer to handle nested OneOf values (e.g., in Lists within Optional) + propertiesComparer.ExternalComparers.Add( + new OneOfEqualityAdapter(propertiesComparer) + ); + return propertiesComparer.AreEqual(xValue, yValue, ref tolerance); + } + ); + + return constraint; + } + + /// + /// EqualityAdapter for comparing IOneOf instances within NUnitEqualityComparer. + /// This enables recursive comparison of nested OneOf values within Optional types. + /// + private class OneOfEqualityAdapter : EqualityAdapter + { + private readonly NUnitEqualityComparer _comparer; + + public OneOfEqualityAdapter(NUnitEqualityComparer comparer) + { + _comparer = comparer; + } + + public override bool CanCompare(object? x, object? y) + { + return x is IOneOf && y is IOneOf; + } + + public override bool AreEqual(object? x, object? y) + { + var oneOfX = (IOneOf?)x; + var oneOfY = (IOneOf?)y; + + // ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract + if (oneOfX?.Value is null && oneOfY?.Value is null) + { + return true; + } + + if (oneOfX?.Value is null || oneOfY?.Value is null) + { + return false; + } + + var tolerance = Tolerance.Default; + return _comparer.AreEqual(oneOfX.Value, oneOfY.Value, ref tolerance); + } + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/ReadOnlyMemoryComparer.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/ReadOnlyMemoryComparer.cs new file mode 100644 index 000000000000..fc0b595a5e54 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional.Test/Utils/ReadOnlyMemoryComparer.cs @@ -0,0 +1,87 @@ +using NUnit.Framework.Constraints; + +namespace NUnit.Framework; + +/// +/// Extensions for NUnit constraints. +/// +public static class ReadOnlyMemoryComparerExtensions +{ + /// + /// Extension method for comparing ReadOnlyMemory<T> in NUnit tests. + /// + /// The type of elements in the ReadOnlyMemory. + /// The Is.EqualTo() constraint instance. + /// A constraint that can compare ReadOnlyMemory<T>. + public static EqualConstraint UsingReadOnlyMemoryComparer(this EqualConstraint constraint) + where T : IComparable + { + return constraint.Using(new ReadOnlyMemoryComparer()); + } +} + +/// +/// Comparer for ReadOnlyMemory<T>. Compares sequences by value. +/// +/// +/// The type of elements in the ReadOnlyMemory. +/// +public class ReadOnlyMemoryComparer : IComparer> + where T : IComparable +{ + /// + public int Compare(ReadOnlyMemory x, ReadOnlyMemory y) + { + // Check if sequences are equal + var xSpan = x.Span; + var ySpan = y.Span; + + // Optimized case for IEquatable implementations + if (typeof(IEquatable).IsAssignableFrom(typeof(T))) + { + var areEqual = xSpan.SequenceEqual(ySpan); + if (areEqual) + { + return 0; // Sequences are equal + } + } + else + { + // Manual equality check for non-IEquatable types + if (xSpan.Length == ySpan.Length) + { + var areEqual = true; + for (var i = 0; i < xSpan.Length; i++) + { + if (!EqualityComparer.Default.Equals(xSpan[i], ySpan[i])) + { + areEqual = false; + break; + } + } + + if (areEqual) + { + return 0; // Sequences are equal + } + } + } + + // For non-equal sequences, we need to return a consistent ordering + // First compare lengths + if (x.Length != y.Length) + return x.Length.CompareTo(y.Length); + + // Same length but different content - compare first differing element + for (var i = 0; i < x.Length; i++) + { + if (!EqualityComparer.Default.Equals(xSpan[i], ySpan[i])) + { + return xSpan[i].CompareTo(ySpan[i]); + } + } + + // Should never reach here if not equal + return 0; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/BasicAuth/BasicAuthClient.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/BasicAuth/BasicAuthClient.cs new file mode 100644 index 000000000000..d78685d4ec41 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/BasicAuth/BasicAuthClient.cs @@ -0,0 +1,207 @@ +using global::System.Text.Json; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional; + +public partial class BasicAuthClient : IBasicAuthClient +{ + private readonly RawClient _client; + + internal BasicAuthClient(RawClient client) + { + _client = client; + } + + private async Task> GetWithBasicAuthAsyncCore( + RequestOptions? options = null, + CancellationToken cancellationToken = default + ) + { + var _headers = await new SeedBasicAuthOptional.Core.HeadersBuilder.Builder() + .Add(_client.Options.Headers) + .Add(_client.Options.AdditionalHeaders) + .Add(options?.AdditionalHeaders) + .BuildAsync() + .ConfigureAwait(false); + var response = await _client + .SendRequestAsync( + new JsonRequest + { + Method = HttpMethod.Get, + Path = "basic-auth", + Headers = _headers, + Options = options, + }, + cancellationToken + ) + .ConfigureAwait(false); + if (response.StatusCode is >= 200 and < 400) + { + var responseBody = await response + .Raw.Content.ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + try + { + var responseData = JsonUtils.Deserialize(responseBody)!; + return new WithRawResponse() + { + Data = responseData, + RawResponse = new RawResponse() + { + StatusCode = response.Raw.StatusCode, + Url = response.Raw.RequestMessage?.RequestUri ?? new Uri("about:blank"), + Headers = ResponseHeaders.FromHttpResponseMessage(response.Raw), + }, + }; + } + catch (JsonException e) + { + throw new SeedBasicAuthOptionalApiException( + "Failed to deserialize response", + response.StatusCode, + responseBody, + e + ); + } + } + { + var responseBody = await response + .Raw.Content.ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + try + { + switch (response.StatusCode) + { + case 401: + throw new UnauthorizedRequest( + JsonUtils.Deserialize(responseBody) + ); + } + } + catch (JsonException) + { + // unable to map error response, throwing generic error + } + throw new SeedBasicAuthOptionalApiException( + $"Error with status code {response.StatusCode}", + response.StatusCode, + responseBody + ); + } + } + + private async Task> PostWithBasicAuthAsyncCore( + object request, + RequestOptions? options = null, + CancellationToken cancellationToken = default + ) + { + var _headers = await new SeedBasicAuthOptional.Core.HeadersBuilder.Builder() + .Add(_client.Options.Headers) + .Add(_client.Options.AdditionalHeaders) + .Add(options?.AdditionalHeaders) + .BuildAsync() + .ConfigureAwait(false); + var response = await _client + .SendRequestAsync( + new JsonRequest + { + Method = HttpMethod.Post, + Path = "basic-auth", + Body = request, + Headers = _headers, + Options = options, + }, + cancellationToken + ) + .ConfigureAwait(false); + if (response.StatusCode is >= 200 and < 400) + { + var responseBody = await response + .Raw.Content.ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + try + { + var responseData = JsonUtils.Deserialize(responseBody)!; + return new WithRawResponse() + { + Data = responseData, + RawResponse = new RawResponse() + { + StatusCode = response.Raw.StatusCode, + Url = response.Raw.RequestMessage?.RequestUri ?? new Uri("about:blank"), + Headers = ResponseHeaders.FromHttpResponseMessage(response.Raw), + }, + }; + } + catch (JsonException e) + { + throw new SeedBasicAuthOptionalApiException( + "Failed to deserialize response", + response.StatusCode, + responseBody, + e + ); + } + } + { + var responseBody = await response + .Raw.Content.ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + try + { + switch (response.StatusCode) + { + case 401: + throw new UnauthorizedRequest( + JsonUtils.Deserialize(responseBody) + ); + case 400: + throw new BadRequest(JsonUtils.Deserialize(responseBody)); + } + } + catch (JsonException) + { + // unable to map error response, throwing generic error + } + throw new SeedBasicAuthOptionalApiException( + $"Error with status code {response.StatusCode}", + response.StatusCode, + responseBody + ); + } + } + + /// + /// GET request with basic auth scheme + /// + /// + /// await client.BasicAuth.GetWithBasicAuthAsync(); + /// + public WithRawResponseTask GetWithBasicAuthAsync( + RequestOptions? options = null, + CancellationToken cancellationToken = default + ) + { + return new WithRawResponseTask(GetWithBasicAuthAsyncCore(options, cancellationToken)); + } + + /// + /// POST request with basic auth scheme + /// + /// + /// await client.BasicAuth.PostWithBasicAuthAsync( + /// new Dictionary<object, object?>() { { "key", "value" } } + /// ); + /// + public WithRawResponseTask PostWithBasicAuthAsync( + object request, + RequestOptions? options = null, + CancellationToken cancellationToken = default + ) + { + return new WithRawResponseTask( + PostWithBasicAuthAsyncCore(request, options, cancellationToken) + ); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/BasicAuth/IBasicAuthClient.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/BasicAuth/IBasicAuthClient.cs new file mode 100644 index 000000000000..aca0fbc2578b --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/BasicAuth/IBasicAuthClient.cs @@ -0,0 +1,21 @@ +namespace SeedBasicAuthOptional; + +public partial interface IBasicAuthClient +{ + /// + /// GET request with basic auth scheme + /// + WithRawResponseTask GetWithBasicAuthAsync( + RequestOptions? options = null, + CancellationToken cancellationToken = default + ); + + /// + /// POST request with basic auth scheme + /// + WithRawResponseTask PostWithBasicAuthAsync( + object request, + RequestOptions? options = null, + CancellationToken cancellationToken = default + ); +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ApiResponse.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ApiResponse.cs new file mode 100644 index 000000000000..f033f46040d8 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ApiResponse.cs @@ -0,0 +1,13 @@ +using global::System.Net.Http; + +namespace SeedBasicAuthOptional.Core; + +/// +/// The response object returned from the API. +/// +internal record ApiResponse +{ + internal required int StatusCode { get; init; } + + internal required HttpResponseMessage Raw { get; init; } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/BaseRequest.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/BaseRequest.cs new file mode 100644 index 000000000000..ad45d88fcc0e --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/BaseRequest.cs @@ -0,0 +1,67 @@ +using global::System.Net.Http; +using global::System.Net.Http.Headers; +using global::System.Text; + +namespace SeedBasicAuthOptional.Core; + +internal abstract record BaseRequest +{ + internal string? BaseUrl { get; init; } + + internal required HttpMethod Method { get; init; } + + internal required string Path { get; init; } + + internal string? ContentType { get; init; } + + /// + /// The query string for this request (including the leading '?' if non-empty). + /// + internal string? QueryString { get; init; } + + internal Dictionary Headers { get; init; } = + new(StringComparer.OrdinalIgnoreCase); + + internal IRequestOptions? Options { get; init; } + + internal abstract HttpContent? CreateContent(); + + protected static ( + Encoding encoding, + string? charset, + string mediaType + ) ParseContentTypeOrDefault( + string? contentType, + Encoding encodingFallback, + string mediaTypeFallback + ) + { + var encoding = encodingFallback; + var mediaType = mediaTypeFallback; + string? charset = null; + if (string.IsNullOrEmpty(contentType)) + { + return (encoding, charset, mediaType); + } + + if (!MediaTypeHeaderValue.TryParse(contentType, out var mediaTypeHeaderValue)) + { + return (encoding, charset, mediaType); + } + + if (!string.IsNullOrEmpty(mediaTypeHeaderValue.CharSet)) + { + charset = mediaTypeHeaderValue.CharSet; + encoding = Encoding.GetEncoding(mediaTypeHeaderValue.CharSet); + } + + if (!string.IsNullOrEmpty(mediaTypeHeaderValue.MediaType)) + { + mediaType = mediaTypeHeaderValue.MediaType; + } + + return (encoding, charset, mediaType); + } + + protected static Encoding Utf8NoBom => EncodingCache.Utf8NoBom; +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/CollectionItemSerializer.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/CollectionItemSerializer.cs new file mode 100644 index 000000000000..730f1e27b265 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/CollectionItemSerializer.cs @@ -0,0 +1,91 @@ +using global::System.Text.Json; +using global::System.Text.Json.Serialization; + +namespace SeedBasicAuthOptional.Core; + +/// +/// Json collection converter. +/// +/// Type of item to convert. +/// Converter to use for individual items. +internal class CollectionItemSerializer + : JsonConverter> + where TConverterType : JsonConverter, new() +{ + private static readonly TConverterType _converter = new TConverterType(); + + /// + /// Reads a json string and deserializes it into an object. + /// + /// Json reader. + /// Type to convert. + /// Serializer options. + /// Created object. + public override IEnumerable? Read( + ref Utf8JsonReader reader, + global::System.Type typeToConvert, + JsonSerializerOptions options + ) + { + if (reader.TokenType == JsonTokenType.Null) + { + return default; + } + + var jsonSerializerOptions = new JsonSerializerOptions(options); + jsonSerializerOptions.Converters.Clear(); + jsonSerializerOptions.Converters.Add(_converter); + + var returnValue = new List(); + + while (reader.TokenType != JsonTokenType.EndArray) + { + if (reader.TokenType != JsonTokenType.StartArray) + { + var item = (TDatatype)( + JsonSerializer.Deserialize(ref reader, typeof(TDatatype), jsonSerializerOptions) + ?? throw new global::System.Exception( + $"Failed to deserialize collection item of type {typeof(TDatatype)}" + ) + ); + returnValue.Add(item); + } + + reader.Read(); + } + + return returnValue; + } + + /// + /// Writes a json string. + /// + /// Json writer. + /// Value to write. + /// Serializer options. + public override void Write( + Utf8JsonWriter writer, + IEnumerable? value, + JsonSerializerOptions options + ) + { + if (value is null) + { + writer.WriteNullValue(); + return; + } + + var jsonSerializerOptions = new JsonSerializerOptions(options); + jsonSerializerOptions.Converters.Clear(); + jsonSerializerOptions.Converters.Add(_converter); + + writer.WriteStartArray(); + + foreach (var data in value) + { + JsonSerializer.Serialize(writer, data, jsonSerializerOptions); + } + + writer.WriteEndArray(); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Constants.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Constants.cs new file mode 100644 index 000000000000..b7f86c54a04c --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Constants.cs @@ -0,0 +1,7 @@ +namespace SeedBasicAuthOptional.Core; + +internal static class Constants +{ + public const string DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK"; + public const string DateFormat = "yyyy-MM-dd"; +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/DateOnlyConverter.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/DateOnlyConverter.cs new file mode 100644 index 000000000000..cb4e399cca83 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/DateOnlyConverter.cs @@ -0,0 +1,747 @@ +// ReSharper disable All +#pragma warning disable + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using global::System.Diagnostics; +using global::System.Diagnostics.CodeAnalysis; +using global::System.Globalization; +using global::System.Runtime.CompilerServices; +using global::System.Runtime.InteropServices; +using global::System.Text.Json; +using global::System.Text.Json.Serialization; + +// ReSharper disable SuggestVarOrType_SimpleTypes +// ReSharper disable SuggestVarOrType_BuiltInTypes + +namespace SeedBasicAuthOptional.Core +{ + /// + /// Custom converter for handling the data type with the System.Text.Json library. + /// + /// + /// This class backported from: + /// + /// System.Text.Json.Serialization.Converters.DateOnlyConverter + /// + public sealed class DateOnlyConverter : JsonConverter + { + private const int FormatLength = 10; // YYYY-MM-DD + + private const int MaxEscapedFormatLength = + FormatLength * JsonConstants.MaxExpansionFactorWhileEscaping; + + /// + public override DateOnly Read( + ref Utf8JsonReader reader, + global::System.Type typeToConvert, + JsonSerializerOptions options + ) + { + if (reader.TokenType != JsonTokenType.String) + { + ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); + } + + return ReadCore(ref reader); + } + + /// + public override DateOnly ReadAsPropertyName( + ref Utf8JsonReader reader, + global::System.Type typeToConvert, + JsonSerializerOptions options + ) + { + Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); + return ReadCore(ref reader); + } + + private static DateOnly ReadCore(ref Utf8JsonReader reader) + { + if ( + !JsonHelpers.IsInRangeInclusive( + reader.ValueLength(), + FormatLength, + MaxEscapedFormatLength + ) + ) + { + ThrowHelper.ThrowFormatException(DataType.DateOnly); + } + + scoped ReadOnlySpan source; + if (!reader.HasValueSequence && !reader.ValueIsEscaped) + { + source = reader.ValueSpan; + } + else + { + Span stackSpan = stackalloc byte[MaxEscapedFormatLength]; + int bytesWritten = reader.CopyString(stackSpan); + source = stackSpan.Slice(0, bytesWritten); + } + + if (!JsonHelpers.TryParseAsIso(source, out DateOnly value)) + { + ThrowHelper.ThrowFormatException(DataType.DateOnly); + } + + return value; + } + + /// + public override void Write( + Utf8JsonWriter writer, + DateOnly value, + JsonSerializerOptions options + ) + { +#if NET8_0_OR_GREATER + Span buffer = stackalloc byte[FormatLength]; +#else + Span buffer = stackalloc char[FormatLength]; +#endif + // ReSharper disable once RedundantAssignment + bool formattedSuccessfully = value.TryFormat( + buffer, + out int charsWritten, + "O".AsSpan(), + CultureInfo.InvariantCulture + ); + Debug.Assert(formattedSuccessfully && charsWritten == FormatLength); + writer.WriteStringValue(buffer); + } + + /// + public override void WriteAsPropertyName( + Utf8JsonWriter writer, + DateOnly value, + JsonSerializerOptions options + ) + { +#if NET8_0_OR_GREATER + Span buffer = stackalloc byte[FormatLength]; +#else + Span buffer = stackalloc char[FormatLength]; +#endif + // ReSharper disable once RedundantAssignment + bool formattedSuccessfully = value.TryFormat( + buffer, + out int charsWritten, + "O".AsSpan(), + CultureInfo.InvariantCulture + ); + Debug.Assert(formattedSuccessfully && charsWritten == FormatLength); + writer.WritePropertyName(buffer); + } + } + + internal static class JsonConstants + { + // The maximum number of fraction digits the Json DateTime parser allows + public const int DateTimeParseNumFractionDigits = 16; + + // In the worst case, an ASCII character represented as a single utf-8 byte could expand 6x when escaped. + public const int MaxExpansionFactorWhileEscaping = 6; + + // The largest fraction expressible by TimeSpan and DateTime formats + public const int MaxDateTimeFraction = 9_999_999; + + // TimeSpan and DateTime formats allow exactly up to many digits for specifying the fraction after the seconds. + public const int DateTimeNumFractionDigits = 7; + + public const byte UtcOffsetToken = (byte)'Z'; + + public const byte TimePrefix = (byte)'T'; + + public const byte Period = (byte)'.'; + + public const byte Hyphen = (byte)'-'; + + public const byte Colon = (byte)':'; + + public const byte Plus = (byte)'+'; + } + + // ReSharper disable SuggestVarOrType_Elsewhere + // ReSharper disable SuggestVarOrType_SimpleTypes + // ReSharper disable SuggestVarOrType_BuiltInTypes + + internal static class JsonHelpers + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsInRangeInclusive(int value, int lowerBound, int upperBound) => + (uint)(value - lowerBound) <= (uint)(upperBound - lowerBound); + + public static bool IsDigit(byte value) => (uint)(value - '0') <= '9' - '0'; + + [StructLayout(LayoutKind.Auto)] + private struct DateTimeParseData + { + public int Year; + public int Month; + public int Day; + public bool IsCalendarDateOnly; + public int Hour; + public int Minute; + public int Second; + public int Fraction; // This value should never be greater than 9_999_999. + public int OffsetHours; + public int OffsetMinutes; + + // ReSharper disable once NotAccessedField.Local + public byte OffsetToken; + } + + public static bool TryParseAsIso(ReadOnlySpan source, out DateOnly value) + { + if ( + TryParseDateTimeOffset(source, out DateTimeParseData parseData) + && parseData.IsCalendarDateOnly + && TryCreateDateTime(parseData, DateTimeKind.Unspecified, out DateTime dateTime) + ) + { + value = DateOnly.FromDateTime(dateTime); + return true; + } + + value = default; + return false; + } + + /// + /// ISO 8601 date time parser (ISO 8601-1:2019). + /// + /// The date/time to parse in UTF-8 format. + /// The parsed for the given . + /// + /// Supports extended calendar date (5.2.2.1) and complete (5.4.2.1) calendar date/time of day + /// representations with optional specification of seconds and fractional seconds. + /// + /// Times can be explicitly specified as UTC ("Z" - 5.3.3) or offsets from UTC ("+/-hh:mm" 5.3.4.2). + /// If unspecified they are considered to be local per spec. + /// + /// Examples: (TZD is either "Z" or hh:mm offset from UTC) + /// + /// YYYY-MM-DD (e.g. 1997-07-16) + /// YYYY-MM-DDThh:mm (e.g. 1997-07-16T19:20) + /// YYYY-MM-DDThh:mm:ss (e.g. 1997-07-16T19:20:30) + /// YYYY-MM-DDThh:mm:ss.s (e.g. 1997-07-16T19:20:30.45) + /// YYYY-MM-DDThh:mmTZD (e.g. 1997-07-16T19:20+01:00) + /// YYYY-MM-DDThh:mm:ssTZD (e.g. 1997-07-16T19:20:3001:00) + /// YYYY-MM-DDThh:mm:ss.sTZD (e.g. 1997-07-16T19:20:30.45Z) + /// + /// Generally speaking we always require the "extended" option when one exists (3.1.3.5). + /// The extended variants have separator characters between components ('-', ':', '.', etc.). + /// Spaces are not permitted. + /// + /// "true" if successfully parsed. + private static bool TryParseDateTimeOffset( + ReadOnlySpan source, + out DateTimeParseData parseData + ) + { + parseData = default; + + // too short datetime + Debug.Assert(source.Length >= 10); + + // Parse the calendar date + // ----------------------- + // ISO 8601-1:2019 5.2.2.1b "Calendar date complete extended format" + // [dateX] = [year]["-"][month]["-"][day] + // [year] = [YYYY] [0000 - 9999] (4.3.2) + // [month] = [MM] [01 - 12] (4.3.3) + // [day] = [DD] [01 - 28, 29, 30, 31] (4.3.4) + // + // Note: 5.2.2.2 "Representations with reduced precision" allows for + // just [year]["-"][month] (a) and just [year] (b), but we currently + // don't permit it. + + { + uint digit1 = source[0] - (uint)'0'; + uint digit2 = source[1] - (uint)'0'; + uint digit3 = source[2] - (uint)'0'; + uint digit4 = source[3] - (uint)'0'; + + if (digit1 > 9 || digit2 > 9 || digit3 > 9 || digit4 > 9) + { + return false; + } + + parseData.Year = (int)(digit1 * 1000 + digit2 * 100 + digit3 * 10 + digit4); + } + + if ( + source[4] != JsonConstants.Hyphen + || !TryGetNextTwoDigits(source.Slice(start: 5, length: 2), ref parseData.Month) + || source[7] != JsonConstants.Hyphen + || !TryGetNextTwoDigits(source.Slice(start: 8, length: 2), ref parseData.Day) + ) + { + return false; + } + + // We now have YYYY-MM-DD [dateX] + // ReSharper disable once ConvertIfStatementToSwitchStatement + if (source.Length == 10) + { + parseData.IsCalendarDateOnly = true; + return true; + } + + // Parse the time of day + // --------------------- + // + // ISO 8601-1:2019 5.3.1.2b "Local time of day complete extended format" + // [timeX] = ["T"][hour][":"][min][":"][sec] + // [hour] = [hh] [00 - 23] (4.3.8a) + // [minute] = [mm] [00 - 59] (4.3.9a) + // [sec] = [ss] [00 - 59, 60 with a leap second] (4.3.10a) + // + // ISO 8601-1:2019 5.3.3 "UTC of day" + // [timeX]["Z"] + // + // ISO 8601-1:2019 5.3.4.2 "Local time of day with the time shift between + // local timescale and UTC" (Extended format) + // + // [shiftX] = ["+"|"-"][hour][":"][min] + // + // Notes: + // + // "T" is optional per spec, but _only_ when times are used alone. In our + // case, we're reading out a complete date & time and as such require "T". + // (5.4.2.1b). + // + // For [timeX] We allow seconds to be omitted per 5.3.1.3a "Representations + // with reduced precision". 5.3.1.3b allows just specifying the hour, but + // we currently don't permit this. + // + // Decimal fractions are allowed for hours, minutes and seconds (5.3.14). + // We only allow fractions for seconds currently. Lower order components + // can't follow, i.e. you can have T23.3, but not T23.3:04. There must be + // one digit, but the max number of digits is implementation defined. We + // currently allow up to 16 digits of fractional seconds only. While we + // support 16 fractional digits we only parse the first seven, anything + // past that is considered a zero. This is to stay compatible with the + // DateTime implementation which is limited to this resolution. + + if (source.Length < 16) + { + // Source does not have enough characters for YYYY-MM-DDThh:mm + return false; + } + + // Parse THH:MM (e.g. "T10:32") + if ( + source[10] != JsonConstants.TimePrefix + || source[13] != JsonConstants.Colon + || !TryGetNextTwoDigits(source.Slice(start: 11, length: 2), ref parseData.Hour) + || !TryGetNextTwoDigits(source.Slice(start: 14, length: 2), ref parseData.Minute) + ) + { + return false; + } + + // We now have YYYY-MM-DDThh:mm + Debug.Assert(source.Length >= 16); + if (source.Length == 16) + { + return true; + } + + byte curByte = source[16]; + int sourceIndex = 17; + + // Either a TZD ['Z'|'+'|'-'] or a seconds separator [':'] is valid at this point + switch (curByte) + { + case JsonConstants.UtcOffsetToken: + parseData.OffsetToken = JsonConstants.UtcOffsetToken; + return sourceIndex == source.Length; + case JsonConstants.Plus: + case JsonConstants.Hyphen: + parseData.OffsetToken = curByte; + return ParseOffset(ref parseData, source.Slice(sourceIndex)); + case JsonConstants.Colon: + break; + default: + return false; + } + + // Try reading the seconds + if ( + source.Length < 19 + || !TryGetNextTwoDigits(source.Slice(start: 17, length: 2), ref parseData.Second) + ) + { + return false; + } + + // We now have YYYY-MM-DDThh:mm:ss + Debug.Assert(source.Length >= 19); + if (source.Length == 19) + { + return true; + } + + curByte = source[19]; + sourceIndex = 20; + + // Either a TZD ['Z'|'+'|'-'] or a seconds decimal fraction separator ['.'] is valid at this point + switch (curByte) + { + case JsonConstants.UtcOffsetToken: + parseData.OffsetToken = JsonConstants.UtcOffsetToken; + return sourceIndex == source.Length; + case JsonConstants.Plus: + case JsonConstants.Hyphen: + parseData.OffsetToken = curByte; + return ParseOffset(ref parseData, source.Slice(sourceIndex)); + case JsonConstants.Period: + break; + default: + return false; + } + + // Source does not have enough characters for second fractions (i.e. ".s") + // YYYY-MM-DDThh:mm:ss.s + if (source.Length < 21) + { + return false; + } + + // Parse fraction. This value should never be greater than 9_999_999 + int numDigitsRead = 0; + int fractionEnd = Math.Min( + sourceIndex + JsonConstants.DateTimeParseNumFractionDigits, + source.Length + ); + + while (sourceIndex < fractionEnd && IsDigit(curByte = source[sourceIndex])) + { + if (numDigitsRead < JsonConstants.DateTimeNumFractionDigits) + { + parseData.Fraction = parseData.Fraction * 10 + (int)(curByte - (uint)'0'); + numDigitsRead++; + } + + sourceIndex++; + } + + if (parseData.Fraction != 0) + { + while (numDigitsRead < JsonConstants.DateTimeNumFractionDigits) + { + parseData.Fraction *= 10; + numDigitsRead++; + } + } + + // We now have YYYY-MM-DDThh:mm:ss.s + Debug.Assert(sourceIndex <= source.Length); + if (sourceIndex == source.Length) + { + return true; + } + + curByte = source[sourceIndex++]; + + // TZD ['Z'|'+'|'-'] is valid at this point + switch (curByte) + { + case JsonConstants.UtcOffsetToken: + parseData.OffsetToken = JsonConstants.UtcOffsetToken; + return sourceIndex == source.Length; + case JsonConstants.Plus: + case JsonConstants.Hyphen: + parseData.OffsetToken = curByte; + return ParseOffset(ref parseData, source.Slice(sourceIndex)); + default: + return false; + } + + static bool ParseOffset(ref DateTimeParseData parseData, ReadOnlySpan offsetData) + { + // Parse the hours for the offset + if ( + offsetData.Length < 2 + || !TryGetNextTwoDigits(offsetData.Slice(0, 2), ref parseData.OffsetHours) + ) + { + return false; + } + + // We now have YYYY-MM-DDThh:mm:ss.s+|-hh + + if (offsetData.Length == 2) + { + // Just hours offset specified + return true; + } + + // Ensure we have enough for ":mm" + return offsetData.Length == 5 + && offsetData[2] == JsonConstants.Colon + && TryGetNextTwoDigits(offsetData.Slice(3), ref parseData.OffsetMinutes); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + // ReSharper disable once RedundantAssignment + private static bool TryGetNextTwoDigits(ReadOnlySpan source, ref int value) + { + Debug.Assert(source.Length == 2); + + uint digit1 = source[0] - (uint)'0'; + uint digit2 = source[1] - (uint)'0'; + + if (digit1 > 9 || digit2 > 9) + { + value = 0; + return false; + } + + value = (int)(digit1 * 10 + digit2); + return true; + } + + // The following methods are borrowed verbatim from src/Common/src/CoreLib/System/Buffers/Text/Utf8Parser/Utf8Parser.Date.Helpers.cs + + /// + /// Overflow-safe DateTime factory. + /// + private static bool TryCreateDateTime( + DateTimeParseData parseData, + DateTimeKind kind, + out DateTime value + ) + { + if (parseData.Year == 0) + { + value = default; + return false; + } + + Debug.Assert(parseData.Year <= 9999); // All of our callers to date parse the year from fixed 4-digit fields so this value is trusted. + + if ((uint)parseData.Month - 1 >= 12) + { + value = default; + return false; + } + + uint dayMinusOne = (uint)parseData.Day - 1; + if ( + dayMinusOne >= 28 + && dayMinusOne >= DateTime.DaysInMonth(parseData.Year, parseData.Month) + ) + { + value = default; + return false; + } + + if ((uint)parseData.Hour > 23) + { + value = default; + return false; + } + + if ((uint)parseData.Minute > 59) + { + value = default; + return false; + } + + // This needs to allow leap seconds when appropriate. + // See https://github.com/dotnet/runtime/issues/30135. + if ((uint)parseData.Second > 59) + { + value = default; + return false; + } + + Debug.Assert(parseData.Fraction is >= 0 and <= JsonConstants.MaxDateTimeFraction); // All of our callers to date parse the fraction from fixed 7-digit fields so this value is trusted. + + ReadOnlySpan days = DateTime.IsLeapYear(parseData.Year) + ? DaysToMonth366 + : DaysToMonth365; + int yearMinusOne = parseData.Year - 1; + int totalDays = + yearMinusOne * 365 + + yearMinusOne / 4 + - yearMinusOne / 100 + + yearMinusOne / 400 + + days[parseData.Month - 1] + + parseData.Day + - 1; + long ticks = totalDays * TimeSpan.TicksPerDay; + int totalSeconds = parseData.Hour * 3600 + parseData.Minute * 60 + parseData.Second; + ticks += totalSeconds * TimeSpan.TicksPerSecond; + ticks += parseData.Fraction; + value = new DateTime(ticks: ticks, kind: kind); + return true; + } + + private static ReadOnlySpan DaysToMonth365 => + [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]; + private static ReadOnlySpan DaysToMonth366 => + [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]; + } + + internal static class ThrowHelper + { + private const string ExceptionSourceValueToRethrowAsJsonException = + "System.Text.Json.Rethrowable"; + + [DoesNotReturn] + public static void ThrowInvalidOperationException_ExpectedString(JsonTokenType tokenType) + { + throw GetInvalidOperationException("string", tokenType); + } + + public static void ThrowFormatException(DataType dataType) + { + throw new FormatException(SR.Format(SR.UnsupportedFormat, dataType)) + { + Source = ExceptionSourceValueToRethrowAsJsonException, + }; + } + + private static global::System.Exception GetInvalidOperationException( + string message, + JsonTokenType tokenType + ) + { + return GetInvalidOperationException(SR.Format(SR.InvalidCast, tokenType, message)); + } + + private static InvalidOperationException GetInvalidOperationException(string message) + { + return new InvalidOperationException(message) + { + Source = ExceptionSourceValueToRethrowAsJsonException, + }; + } + } + + internal static class Utf8JsonReaderExtensions + { + internal static int ValueLength(this Utf8JsonReader reader) => + reader.HasValueSequence + ? checked((int)reader.ValueSequence.Length) + : reader.ValueSpan.Length; + } + + internal enum DataType + { + TimeOnly, + DateOnly, + } + + [SuppressMessage("ReSharper", "InconsistentNaming")] + internal static class SR + { + private static readonly bool s_usingResourceKeys = + AppContext.TryGetSwitch( + "System.Resources.UseSystemResourceKeys", + out bool usingResourceKeys + ) && usingResourceKeys; + + public static string UnsupportedFormat => Strings.UnsupportedFormat; + + public static string InvalidCast => Strings.InvalidCast; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static string Format(string resourceFormat, object? p1) => + s_usingResourceKeys + ? string.Join(", ", resourceFormat, p1) + : string.Format(resourceFormat, p1); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static string Format(string resourceFormat, object? p1, object? p2) => + s_usingResourceKeys + ? string.Join(", ", resourceFormat, p1, p2) + : string.Format(resourceFormat, p1, p2); + } + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute( + "System.Resources.Tools.StronglyTypedResourceBuilder", + "17.0.0.0" + )] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Strings + { + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute( + "Microsoft.Performance", + "CA1811:AvoidUncalledPrivateCode" + )] + internal Strings() { } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute( + global::System.ComponentModel.EditorBrowsableState.Advanced + )] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if (object.ReferenceEquals(resourceMan, null)) + { + global::System.Resources.ResourceManager temp = + new global::System.Resources.ResourceManager( + "System.Text.Json.Resources.Strings", + typeof(Strings).Assembly + ); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute( + global::System.ComponentModel.EditorBrowsableState.Advanced + )] + internal static global::System.Globalization.CultureInfo Culture + { + get { return resourceCulture; } + set { resourceCulture = value; } + } + + /// + /// Looks up a localized string similar to Cannot get the value of a token type '{0}' as a {1}.. + /// + internal static string InvalidCast + { + get { return ResourceManager.GetString("InvalidCast", resourceCulture); } + } + + /// + /// Looks up a localized string similar to The JSON value is not in a supported {0} format.. + /// + internal static string UnsupportedFormat + { + get { return ResourceManager.GetString("UnsupportedFormat", resourceCulture); } + } + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/DateTimeSerializer.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/DateTimeSerializer.cs new file mode 100644 index 000000000000..27b62074d137 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/DateTimeSerializer.cs @@ -0,0 +1,40 @@ +using global::System.Globalization; +using global::System.Text.Json; +using global::System.Text.Json.Serialization; + +namespace SeedBasicAuthOptional.Core; + +internal class DateTimeSerializer : JsonConverter +{ + public override DateTime Read( + ref Utf8JsonReader reader, + global::System.Type typeToConvert, + JsonSerializerOptions options + ) + { + return DateTime.Parse(reader.GetString()!, null, DateTimeStyles.RoundtripKind); + } + + public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString(Constants.DateTimeFormat)); + } + + public override DateTime ReadAsPropertyName( + ref Utf8JsonReader reader, + global::System.Type typeToConvert, + JsonSerializerOptions options + ) + { + return DateTime.Parse(reader.GetString()!, null, DateTimeStyles.RoundtripKind); + } + + public override void WriteAsPropertyName( + Utf8JsonWriter writer, + DateTime value, + JsonSerializerOptions options + ) + { + writer.WritePropertyName(value.ToString(Constants.DateTimeFormat)); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/EmptyRequest.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/EmptyRequest.cs new file mode 100644 index 000000000000..384327b783ab --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/EmptyRequest.cs @@ -0,0 +1,11 @@ +using global::System.Net.Http; + +namespace SeedBasicAuthOptional.Core; + +/// +/// The request object to send without a request body. +/// +internal record EmptyRequest : BaseRequest +{ + internal override HttpContent? CreateContent() => null; +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/EncodingCache.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/EncodingCache.cs new file mode 100644 index 000000000000..ae6139d66855 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/EncodingCache.cs @@ -0,0 +1,11 @@ +using global::System.Text; + +namespace SeedBasicAuthOptional.Core; + +internal static class EncodingCache +{ + internal static readonly Encoding Utf8NoBom = new UTF8Encoding( + encoderShouldEmitUTF8Identifier: false, + throwOnInvalidBytes: true + ); +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Extensions.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Extensions.cs new file mode 100644 index 000000000000..11f9d385c5ef --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Extensions.cs @@ -0,0 +1,55 @@ +using global::System.Diagnostics.CodeAnalysis; +using global::System.Runtime.Serialization; + +namespace SeedBasicAuthOptional.Core; + +internal static class Extensions +{ + public static string Stringify(this Enum value) + { + var field = value.GetType().GetField(value.ToString()); + if (field is not null) + { + var attribute = (EnumMemberAttribute?) + global::System.Attribute.GetCustomAttribute(field, typeof(EnumMemberAttribute)); + return attribute?.Value ?? value.ToString(); + } + return value.ToString(); + } + + /// + /// Asserts that a condition is true, throwing an exception with the specified message if it is false. + /// + /// The condition to assert. + /// The exception message if the assertion fails. + /// Thrown when the condition is false. + internal static void Assert(this object value, bool condition, string message) + { + if (!condition) + { + throw new global::System.Exception(message); + } + } + + /// + /// Asserts that a value is not null, throwing an exception with the specified message if it is null. + /// + /// The type of the value to assert. + /// The value to assert is not null. + /// The exception message if the assertion fails. + /// The non-null value. + /// Thrown when the value is null. + internal static TValue Assert( + this object _unused, + [NotNull] TValue? value, + string message + ) + where TValue : class + { + if (value is null) + { + throw new global::System.Exception(message); + } + return value; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/FormUrlEncoder.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/FormUrlEncoder.cs new file mode 100644 index 000000000000..954003fc2dc1 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/FormUrlEncoder.cs @@ -0,0 +1,33 @@ +using global::System.Net.Http; + +namespace SeedBasicAuthOptional.Core; + +/// +/// Encodes an object into a form URL-encoded content. +/// +public static class FormUrlEncoder +{ + /// + /// Encodes an object into a form URL-encoded content using Deep Object notation. + /// + /// Object to form URL-encode. You can pass in an object or dictionary, but not lists, strings, or primitives. + /// Throws when passing in a list, a string, or a primitive value. + internal static FormUrlEncodedContent EncodeAsDeepObject(object value) => + new(QueryStringConverter.ToDeepObject(value)); + + /// + /// Encodes an object into a form URL-encoded content using Exploded Form notation. + /// + /// Object to form URL-encode. You can pass in an object or dictionary, but not lists, strings, or primitives. + /// Throws when passing in a list, a string, or a primitive value. + internal static FormUrlEncodedContent EncodeAsExplodedForm(object value) => + new(QueryStringConverter.ToExplodedForm(value)); + + /// + /// Encodes an object into a form URL-encoded content using Form notation without exploding parameters. + /// + /// Object to form URL-encode. You can pass in an object or dictionary, but not lists, strings, or primitives. + /// Throws when passing in a list, a string, or a primitive value. + internal static FormUrlEncodedContent EncodeAsForm(object value) => + new(QueryStringConverter.ToForm(value)); +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HeaderValue.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HeaderValue.cs new file mode 100644 index 000000000000..35d9400e7864 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HeaderValue.cs @@ -0,0 +1,52 @@ +namespace SeedBasicAuthOptional.Core; + +internal sealed class HeaderValue +{ + private readonly Func> _resolver; + + public HeaderValue(string value) + { + _resolver = () => new global::System.Threading.Tasks.ValueTask(value); + } + + public HeaderValue(Func value) + { + _resolver = () => new global::System.Threading.Tasks.ValueTask(value()); + } + + public HeaderValue(Func> value) + { + _resolver = value; + } + + public HeaderValue(Func> value) + { + _resolver = () => new global::System.Threading.Tasks.ValueTask(value()); + } + + public static implicit operator HeaderValue(string value) => new(value); + + public static implicit operator HeaderValue(Func value) => new(value); + + public static implicit operator HeaderValue( + Func> value + ) => new(value); + + public static implicit operator HeaderValue( + Func> value + ) => new(value); + + public static HeaderValue FromString(string value) => new(value); + + public static HeaderValue FromFunc(Func value) => new(value); + + public static HeaderValue FromValueTaskFunc( + Func> value + ) => new(value); + + public static HeaderValue FromTaskFunc( + Func> value + ) => new(value); + + internal global::System.Threading.Tasks.ValueTask ResolveAsync() => _resolver(); +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Headers.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Headers.cs new file mode 100644 index 000000000000..878c9821ffc7 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Headers.cs @@ -0,0 +1,28 @@ +namespace SeedBasicAuthOptional.Core; + +/// +/// Represents the headers sent with the request. +/// +internal sealed class Headers : Dictionary +{ + internal Headers() { } + + /// + /// Initializes a new instance of the Headers class with the specified value. + /// + /// + internal Headers(Dictionary value) + { + foreach (var kvp in value) + { + this[kvp.Key] = kvp.Value; + } + } + + /// + /// Initializes a new instance of the Headers class with the specified value. + /// + /// + internal Headers(IEnumerable> value) + : base(value.ToDictionary(e => e.Key, e => e.Value)) { } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HeadersBuilder.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HeadersBuilder.cs new file mode 100644 index 000000000000..b4e0dbee737e --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HeadersBuilder.cs @@ -0,0 +1,197 @@ +namespace SeedBasicAuthOptional.Core; + +/// +/// Fluent builder for constructing HTTP headers with support for merging from multiple sources. +/// Provides a clean API for building headers with proper precedence handling. +/// +internal static class HeadersBuilder +{ + /// + /// Fluent builder for constructing HTTP headers. + /// + public sealed class Builder + { + private readonly Dictionary _headers; + + /// + /// Initializes a new instance with default capacity. + /// Uses case-insensitive header name comparison. + /// + public Builder() + { + _headers = new Dictionary(StringComparer.OrdinalIgnoreCase); + } + + /// + /// Initializes a new instance with the specified initial capacity. + /// Uses case-insensitive header name comparison. + /// + public Builder(int capacity) + { + _headers = new Dictionary( + capacity, + StringComparer.OrdinalIgnoreCase + ); + } + + /// + /// Adds a header with the specified key and value. + /// If a header with the same key already exists, it will be overwritten. + /// Null values are ignored. + /// + /// The header name. + /// The header value. Null values are ignored. + /// This builder instance for method chaining. + public Builder Add(string key, string? value) + { + if (value is not null) + { + _headers[key] = (value); + } + return this; + } + + /// + /// Adds a header with the specified key and object value. + /// The value will be converted to string using ValueConvert for consistent serialization. + /// If a header with the same key already exists, it will be overwritten. + /// Null values are ignored. + /// + /// The header name. + /// The header value. Null values are ignored. + /// This builder instance for method chaining. + public Builder Add(string key, object? value) + { + if (value is null) + { + return this; + } + + // Use ValueConvert for consistent serialization across headers, query params, and path params + var stringValue = ValueConvert.ToString(value); + if (stringValue is not null) + { + _headers[key] = (stringValue); + } + return this; + } + + /// + /// Adds multiple headers from a Headers dictionary. + /// HeaderValue instances are stored and will be resolved when BuildAsync() is called. + /// Overwrites any existing headers with the same key. + /// Null entries are ignored. + /// + /// The headers to add. Null is treated as empty. + /// This builder instance for method chaining. + public Builder Add(Headers? headers) + { + if (headers is null) + { + return this; + } + + foreach (var header in headers) + { + _headers[header.Key] = header.Value; + } + + return this; + } + + /// + /// Adds multiple headers from a Headers dictionary, excluding the Authorization header. + /// This is useful for endpoints that don't require authentication, to avoid triggering + /// lazy auth token resolution. + /// HeaderValue instances are stored and will be resolved when BuildAsync() is called. + /// Overwrites any existing headers with the same key. + /// Null entries are ignored. + /// + /// The headers to add. Null is treated as empty. + /// This builder instance for method chaining. + public Builder AddWithoutAuth(Headers? headers) + { + if (headers is null) + { + return this; + } + + foreach (var header in headers) + { + if (header.Key.Equals("Authorization", StringComparison.OrdinalIgnoreCase)) + { + continue; + } + _headers[header.Key] = header.Value; + } + + return this; + } + + /// + /// Adds multiple headers from a key-value pair collection. + /// Overwrites any existing headers with the same key. + /// Null values are ignored. + /// + /// The headers to add. Null is treated as empty. + /// This builder instance for method chaining. + public Builder Add(IEnumerable>? headers) + { + if (headers is null) + { + return this; + } + + foreach (var header in headers) + { + if (header.Value is not null) + { + _headers[header.Key] = (header.Value); + } + } + + return this; + } + + /// + /// Adds multiple headers from a dictionary. + /// Overwrites any existing headers with the same key. + /// + /// The headers to add. Null is treated as empty. + /// This builder instance for method chaining. + public Builder Add(Dictionary? headers) + { + if (headers is null) + { + return this; + } + + foreach (var header in headers) + { + _headers[header.Key] = (header.Value); + } + + return this; + } + + /// + /// Asynchronously builds the final headers dictionary containing all merged headers. + /// Resolves all HeaderValue instances that may contain async operations. + /// Returns a case-insensitive dictionary. + /// + /// A task that represents the asynchronous operation, containing a case-insensitive dictionary of headers. + public async global::System.Threading.Tasks.Task> BuildAsync() + { + var headers = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var kvp in _headers) + { + var value = await kvp.Value.ResolveAsync().ConfigureAwait(false); + if (value is not null) + { + headers[kvp.Key] = value; + } + } + return headers; + } + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HttpContentExtensions.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HttpContentExtensions.cs new file mode 100644 index 000000000000..65f5ce6554f6 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HttpContentExtensions.cs @@ -0,0 +1,20 @@ +#if !NET5_0_OR_GREATER +namespace SeedBasicAuthOptional.Core; + +/// +/// Polyfill extension providing a ReadAsStringAsync(CancellationToken) overload +/// for target frameworks older than .NET 5, where only the parameterless +/// ReadAsStringAsync() is available. +/// +internal static class HttpContentExtensions +{ + internal static Task ReadAsStringAsync( + this HttpContent httpContent, + CancellationToken cancellationToken + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return httpContent.ReadAsStringAsync(); + } +} +#endif diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HttpMethodExtensions.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HttpMethodExtensions.cs new file mode 100644 index 000000000000..ad77851023d7 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/HttpMethodExtensions.cs @@ -0,0 +1,8 @@ +using global::System.Net.Http; + +namespace SeedBasicAuthOptional.Core; + +internal static class HttpMethodExtensions +{ + public static readonly HttpMethod Patch = new("PATCH"); +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/IIsRetryableContent.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/IIsRetryableContent.cs new file mode 100644 index 000000000000..e64c9b5a5c89 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/IIsRetryableContent.cs @@ -0,0 +1,6 @@ +namespace SeedBasicAuthOptional.Core; + +public interface IIsRetryableContent +{ + public bool IsRetryable { get; } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/IRequestOptions.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/IRequestOptions.cs new file mode 100644 index 000000000000..0386a81482b4 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/IRequestOptions.cs @@ -0,0 +1,83 @@ +namespace SeedBasicAuthOptional.Core; + +internal interface IRequestOptions +{ + /// + /// The Base URL for the API. + /// + public string? BaseUrl { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } + + /// + /// The http client used to make requests. + /// + public HttpClient? HttpClient { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } + + /// + /// Additional headers to be sent with the request. + /// Headers previously set with matching keys will be overwritten. + /// + public IEnumerable> AdditionalHeaders { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } + + /// + /// The max number of retries to attempt. + /// + public int? MaxRetries { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } + + /// + /// The timeout for the request. + /// + public TimeSpan? Timeout { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } + + /// + /// Additional query parameters sent with the request. + /// + public IEnumerable> AdditionalQueryParameters { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } + + /// + /// Additional body properties sent with the request. + /// This is only applied to JSON requests. + /// + public object? AdditionalBodyProperties { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonAccessAttribute.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonAccessAttribute.cs new file mode 100644 index 000000000000..bef7f86c5d27 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonAccessAttribute.cs @@ -0,0 +1,15 @@ +namespace SeedBasicAuthOptional.Core; + +[global::System.AttributeUsage( + global::System.AttributeTargets.Property | global::System.AttributeTargets.Field +)] +internal class JsonAccessAttribute(JsonAccessType accessType) : global::System.Attribute +{ + internal JsonAccessType AccessType { get; init; } = accessType; +} + +internal enum JsonAccessType +{ + ReadOnly, + WriteOnly, +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonConfiguration.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonConfiguration.cs new file mode 100644 index 000000000000..c984380d74b2 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonConfiguration.cs @@ -0,0 +1,275 @@ +using global::System.Reflection; +using global::System.Text.Encodings.Web; +using global::System.Text.Json; +using global::System.Text.Json.Nodes; +using global::System.Text.Json.Serialization; +using global::System.Text.Json.Serialization.Metadata; + +namespace SeedBasicAuthOptional.Core; + +internal static partial class JsonOptions +{ + internal static readonly JsonSerializerOptions JsonSerializerOptions; + internal static readonly JsonSerializerOptions JsonSerializerOptionsRelaxedEscaping; + + static JsonOptions() + { + var options = new JsonSerializerOptions + { + Converters = + { + new DateTimeSerializer(), +#if USE_PORTABLE_DATE_ONLY + new DateOnlyConverter(), +#endif + new OneOfSerializer(), + new OptionalJsonConverterFactory(), + }, +#if DEBUG + WriteIndented = true, +#endif + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + TypeInfoResolver = new DefaultJsonTypeInfoResolver + { + Modifiers = + { + NullableOptionalModifier, + JsonAccessAndIgnoreModifier, + HandleExtensionDataFields, + }, + }, + }; + ConfigureJsonSerializerOptions(options); + JsonSerializerOptions = options; + + var relaxedOptions = new JsonSerializerOptions(options) + { + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + }; + JsonSerializerOptionsRelaxedEscaping = relaxedOptions; + } + + private static void NullableOptionalModifier(JsonTypeInfo typeInfo) + { + if (typeInfo.Kind != JsonTypeInfoKind.Object) + return; + + foreach (var property in typeInfo.Properties) + { + var propertyInfo = property.AttributeProvider as global::System.Reflection.PropertyInfo; + + if (propertyInfo is null) + continue; + + // Check for ReadOnly JsonAccessAttribute - it overrides Optional/Nullable behavior + var jsonAccessAttribute = propertyInfo.GetCustomAttribute(); + if (jsonAccessAttribute?.AccessType == JsonAccessType.ReadOnly) + { + // ReadOnly means "never serialize", which completely overrides Optional/Nullable. + // Skip Optional/Nullable processing since JsonAccessAndIgnoreModifier + // will set ShouldSerialize = false anyway. + continue; + } + // Note: WriteOnly doesn't conflict with Optional/Nullable since it only + // affects deserialization (Set), not serialization (ShouldSerialize) + + var isOptionalType = + property.PropertyType.IsGenericType + && property.PropertyType.GetGenericTypeDefinition() == typeof(Optional<>); + + var hasOptionalAttribute = + propertyInfo.GetCustomAttribute() is not null; + var hasNullableAttribute = + propertyInfo.GetCustomAttribute() is not null; + + if (isOptionalType && hasOptionalAttribute) + { + var originalGetter = property.Get; + if (originalGetter is not null) + { + var capturedIsNullable = hasNullableAttribute; + + property.ShouldSerialize = (obj, value) => + { + var optionalValue = originalGetter(obj); + if (optionalValue is not IOptional optional) + return false; + + if (!optional.IsDefined) + return false; + + if (!capturedIsNullable) + { + var innerValue = optional.GetBoxedValue(); + if (innerValue is null) + return false; + } + + return true; + }; + } + } + else if (hasNullableAttribute) + { + // Force serialization of nullable properties even when null + property.ShouldSerialize = (obj, value) => true; + } + } + } + + private static void JsonAccessAndIgnoreModifier(JsonTypeInfo typeInfo) + { + if (typeInfo.Kind != JsonTypeInfoKind.Object) + return; + + foreach (var propertyInfo in typeInfo.Properties) + { + var jsonAccessAttribute = propertyInfo + .AttributeProvider?.GetCustomAttributes(typeof(JsonAccessAttribute), true) + .OfType() + .FirstOrDefault(); + + if (jsonAccessAttribute is not null) + { + propertyInfo.IsRequired = false; + switch (jsonAccessAttribute.AccessType) + { + case JsonAccessType.ReadOnly: + propertyInfo.ShouldSerialize = (_, _) => false; + break; + case JsonAccessType.WriteOnly: + propertyInfo.Set = null; + break; + default: + throw new ArgumentOutOfRangeException(); + } + } + + var jsonIgnoreAttribute = propertyInfo + .AttributeProvider?.GetCustomAttributes(typeof(JsonIgnoreAttribute), true) + .OfType() + .FirstOrDefault(); + + if (jsonIgnoreAttribute is not null) + { + propertyInfo.IsRequired = false; + } + } + } + + private static void HandleExtensionDataFields(JsonTypeInfo typeInfo) + { + if ( + typeInfo.Kind == JsonTypeInfoKind.Object + && typeInfo.Properties.All(prop => !prop.IsExtensionData) + ) + { + var extensionProp = typeInfo + .Type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic) + .FirstOrDefault(prop => + prop.GetCustomAttribute() is not null + ); + + if (extensionProp is not null) + { + var jsonPropertyInfo = typeInfo.CreateJsonPropertyInfo( + extensionProp.FieldType, + extensionProp.Name + ); + jsonPropertyInfo.Get = extensionProp.GetValue; + jsonPropertyInfo.Set = extensionProp.SetValue; + jsonPropertyInfo.IsExtensionData = true; + typeInfo.Properties.Add(jsonPropertyInfo); + } + } + } + + static partial void ConfigureJsonSerializerOptions(JsonSerializerOptions defaultOptions); +} + +internal static class JsonUtils +{ + internal static string Serialize(T obj) => + JsonSerializer.Serialize(obj, JsonOptions.JsonSerializerOptions); + + internal static string Serialize(object obj, global::System.Type type) => + JsonSerializer.Serialize(obj, type, JsonOptions.JsonSerializerOptions); + + internal static string SerializeRelaxedEscaping(T obj) => + JsonSerializer.Serialize(obj, JsonOptions.JsonSerializerOptionsRelaxedEscaping); + + internal static string SerializeRelaxedEscaping(object obj, global::System.Type type) => + JsonSerializer.Serialize(obj, type, JsonOptions.JsonSerializerOptionsRelaxedEscaping); + + internal static JsonElement SerializeToElement(T obj) => + JsonSerializer.SerializeToElement(obj, JsonOptions.JsonSerializerOptions); + + internal static JsonElement SerializeToElement(object obj, global::System.Type type) => + JsonSerializer.SerializeToElement(obj, type, JsonOptions.JsonSerializerOptions); + + internal static JsonDocument SerializeToDocument(T obj) => + JsonSerializer.SerializeToDocument(obj, JsonOptions.JsonSerializerOptions); + + internal static JsonNode? SerializeToNode(T obj) => + JsonSerializer.SerializeToNode(obj, JsonOptions.JsonSerializerOptions); + + internal static byte[] SerializeToUtf8Bytes(T obj) => + JsonSerializer.SerializeToUtf8Bytes(obj, JsonOptions.JsonSerializerOptions); + + internal static string SerializeWithAdditionalProperties( + T obj, + object? additionalProperties = null + ) + { + if (additionalProperties is null) + { + return Serialize(obj); + } + var additionalPropertiesJsonNode = SerializeToNode(additionalProperties); + if (additionalPropertiesJsonNode is not JsonObject additionalPropertiesJsonObject) + { + throw new InvalidOperationException( + "The additional properties must serialize to a JSON object." + ); + } + var jsonNode = SerializeToNode(obj); + if (jsonNode is not JsonObject jsonObject) + { + throw new InvalidOperationException( + "The serialized object must be a JSON object to add properties." + ); + } + MergeJsonObjects(jsonObject, additionalPropertiesJsonObject); + return jsonObject.ToJsonString(JsonOptions.JsonSerializerOptions); + } + + private static void MergeJsonObjects(JsonObject baseObject, JsonObject overrideObject) + { + foreach (var property in overrideObject) + { + if (!baseObject.TryGetPropertyValue(property.Key, out JsonNode? existingValue)) + { + baseObject[property.Key] = property.Value is not null + ? JsonNode.Parse(property.Value.ToJsonString()) + : null; + continue; + } + if ( + existingValue is JsonObject nestedBaseObject + && property.Value is JsonObject nestedOverrideObject + ) + { + // If both values are objects, recursively merge them. + MergeJsonObjects(nestedBaseObject, nestedOverrideObject); + continue; + } + // Otherwise, the overrideObject takes precedence. + baseObject[property.Key] = property.Value is not null + ? JsonNode.Parse(property.Value.ToJsonString()) + : null; + } + } + + internal static T Deserialize(string json) => + JsonSerializer.Deserialize(json, JsonOptions.JsonSerializerOptions)!; +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonRequest.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonRequest.cs new file mode 100644 index 000000000000..900a41b8e024 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/JsonRequest.cs @@ -0,0 +1,36 @@ +using global::System.Net.Http; + +namespace SeedBasicAuthOptional.Core; + +/// +/// The request object to be sent for JSON APIs. +/// +internal record JsonRequest : BaseRequest +{ + internal object? Body { get; init; } + + internal override HttpContent? CreateContent() + { + if (Body is null && Options?.AdditionalBodyProperties is null) + { + return null; + } + + var (encoding, charset, mediaType) = ParseContentTypeOrDefault( + ContentType, + Utf8NoBom, + "application/json" + ); + var content = new StringContent( + JsonUtils.SerializeWithAdditionalProperties(Body, Options?.AdditionalBodyProperties), + encoding, + mediaType + ); + if (string.IsNullOrEmpty(charset) && content.Headers.ContentType is not null) + { + content.Headers.ContentType.CharSet = ""; + } + + return content; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/MultipartFormRequest.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/MultipartFormRequest.cs new file mode 100644 index 000000000000..3f61e753b056 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/MultipartFormRequest.cs @@ -0,0 +1,294 @@ +using global::System.Net.Http; +using global::System.Net.Http.Headers; + +namespace SeedBasicAuthOptional.Core; + +/// +/// The request object to be sent for multipart form data. +/// +internal record MultipartFormRequest : BaseRequest +{ + private readonly List> _partAdders = []; + + internal void AddJsonPart(string name, object? value) => AddJsonPart(name, value, null); + + internal void AddJsonPart(string name, object? value, string? contentType) + { + if (value is null) + { + return; + } + + _partAdders.Add(form => + { + var (encoding, charset, mediaType) = ParseContentTypeOrDefault( + contentType, + Utf8NoBom, + "application/json" + ); + var content = new StringContent(JsonUtils.Serialize(value), encoding, mediaType); + if (string.IsNullOrEmpty(charset) && content.Headers.ContentType is not null) + { + content.Headers.ContentType.CharSet = ""; + } + + form.Add(content, name); + }); + } + + internal void AddJsonParts(string name, IEnumerable? value) => + AddJsonParts(name, value, null); + + internal void AddJsonParts(string name, IEnumerable? value, string? contentType) + { + if (value is null) + { + return; + } + + foreach (var item in value) + { + AddJsonPart(name, item, contentType); + } + } + + internal void AddJsonParts(string name, IEnumerable? value) => + AddJsonParts(name, value, null); + + internal void AddJsonParts(string name, IEnumerable? value, string? contentType) + { + if (value is null) + { + return; + } + + foreach (var item in value) + { + AddJsonPart(name, item, contentType); + } + } + + internal void AddStringPart(string name, object? value) => AddStringPart(name, value, null); + + internal void AddStringPart(string name, object? value, string? contentType) + { + if (value is null) + { + return; + } + + AddStringPart(name, ValueConvert.ToString(value), contentType); + } + + internal void AddStringPart(string name, string? value) => AddStringPart(name, value, null); + + internal void AddStringPart(string name, string? value, string? contentType) + { + if (value is null) + { + return; + } + + _partAdders.Add(form => + { + var (encoding, charset, mediaType) = ParseContentTypeOrDefault( + contentType, + Utf8NoBom, + "text/plain" + ); + var content = new StringContent(value, encoding, mediaType); + if (string.IsNullOrEmpty(charset) && content.Headers.ContentType is not null) + { + content.Headers.ContentType.CharSet = ""; + } + + form.Add(content, name); + }); + } + + internal void AddStringParts(string name, IEnumerable? value) => + AddStringParts(name, value, null); + + internal void AddStringParts(string name, IEnumerable? value, string? contentType) + { + if (value is null) + { + return; + } + + AddStringPart(name, ValueConvert.ToString(value), contentType); + } + + internal void AddStringParts(string name, IEnumerable? value) => + AddStringParts(name, value, null); + + internal void AddStringParts(string name, IEnumerable? value, string? contentType) + { + if (value is null) + { + return; + } + + foreach (var item in value) + { + AddStringPart(name, item, contentType); + } + } + + internal void AddStreamPart(string name, Stream? stream, string? fileName) => + AddStreamPart(name, stream, fileName, null); + + internal void AddStreamPart(string name, Stream? stream, string? fileName, string? contentType) + { + if (stream is null) + { + return; + } + + _partAdders.Add(form => + { + var content = new StreamContent(stream) + { + Headers = + { + ContentType = MediaTypeHeaderValue.Parse( + contentType ?? "application/octet-stream" + ), + }, + }; + + if (fileName is not null) + { + form.Add(content, name, fileName); + } + else + { + form.Add(content, name); + } + }); + } + + internal void AddFileParameterPart(string name, Stream? stream) => + AddStreamPart(name, stream, null, null); + + internal void AddFileParameterPart(string name, FileParameter? file) => + AddFileParameterPart(name, file, null); + + internal void AddFileParameterPart( + string name, + FileParameter? file, + string? fallbackContentType + ) => + AddStreamPart(name, file?.Stream, file?.FileName, file?.ContentType ?? fallbackContentType); + + internal void AddFileParameterParts(string name, IEnumerable? files) => + AddFileParameterParts(name, files, null); + + internal void AddFileParameterParts( + string name, + IEnumerable? files, + string? fallbackContentType + ) + { + if (files is null) + { + return; + } + + foreach (var file in files) + { + AddFileParameterPart(name, file, fallbackContentType); + } + } + + internal void AddFormEncodedPart(string name, object? value) => + AddFormEncodedPart(name, value, null); + + internal void AddFormEncodedPart(string name, object? value, string? contentType) + { + if (value is null) + { + return; + } + + _partAdders.Add(form => + { + var content = FormUrlEncoder.EncodeAsForm(value); + if (!string.IsNullOrEmpty(contentType)) + { + content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType); + } + + form.Add(content, name); + }); + } + + internal void AddFormEncodedParts(string name, IEnumerable? value) => + AddFormEncodedParts(name, value, null); + + internal void AddFormEncodedParts(string name, IEnumerable? value, string? contentType) + { + if (value is null) + { + return; + } + + foreach (var item in value) + { + AddFormEncodedPart(name, item, contentType); + } + } + + internal void AddExplodedFormEncodedPart(string name, object? value) => + AddExplodedFormEncodedPart(name, value, null); + + internal void AddExplodedFormEncodedPart(string name, object? value, string? contentType) + { + if (value is null) + { + return; + } + + _partAdders.Add(form => + { + var content = FormUrlEncoder.EncodeAsExplodedForm(value); + if (!string.IsNullOrEmpty(contentType)) + { + content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType); + } + + form.Add(content, name); + }); + } + + internal void AddExplodedFormEncodedParts(string name, IEnumerable? value) => + AddExplodedFormEncodedParts(name, value, null); + + internal void AddExplodedFormEncodedParts( + string name, + IEnumerable? value, + string? contentType + ) + { + if (value is null) + { + return; + } + + foreach (var item in value) + { + AddExplodedFormEncodedPart(name, item, contentType); + } + } + + internal override HttpContent CreateContent() + { + var form = new MultipartFormDataContent(); + foreach (var adder in _partAdders) + { + adder(form); + } + + return form; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/NullableAttribute.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/NullableAttribute.cs new file mode 100644 index 000000000000..c29115cb9074 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/NullableAttribute.cs @@ -0,0 +1,18 @@ +namespace SeedBasicAuthOptional.Core; + +/// +/// Marks a property as nullable in the OpenAPI specification. +/// When applied to Optional properties, this indicates that null values should be +/// written to JSON when the optional is defined with null. +/// +/// +/// For regular (required) properties: +/// - Without [Nullable]: null values are invalid (omit from JSON at runtime) +/// - With [Nullable]: null values are written to JSON +/// +/// For Optional properties (also marked with [Optional]): +/// - Without [Nullable]: Optional.Of(null) → omit from JSON (runtime edge case) +/// - With [Nullable]: Optional.Of(null) → write null to JSON +/// +[global::System.AttributeUsage(global::System.AttributeTargets.Property, AllowMultiple = false)] +public class NullableAttribute : global::System.Attribute { } diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/OneOfSerializer.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/OneOfSerializer.cs new file mode 100644 index 000000000000..9aacfd4e5fa4 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/OneOfSerializer.cs @@ -0,0 +1,145 @@ +using global::System.Reflection; +using global::System.Text.Json; +using global::System.Text.Json.Serialization; +using OneOf; + +namespace SeedBasicAuthOptional.Core; + +internal class OneOfSerializer : JsonConverter +{ + public override IOneOf? Read( + ref Utf8JsonReader reader, + global::System.Type typeToConvert, + JsonSerializerOptions options + ) + { + if (reader.TokenType is JsonTokenType.Null) + return default; + + foreach (var (type, cast) in GetOneOfTypes(typeToConvert)) + { + try + { + var readerCopy = reader; + var result = JsonSerializer.Deserialize(ref readerCopy, type, options); + reader.Skip(); + return (IOneOf)cast.Invoke(null, [result])!; + } + catch (JsonException) { } + } + + throw new JsonException( + $"Cannot deserialize into one of the supported types for {typeToConvert}" + ); + } + + public override void Write(Utf8JsonWriter writer, IOneOf value, JsonSerializerOptions options) + { + JsonSerializer.Serialize(writer, value.Value, options); + } + + public override IOneOf ReadAsPropertyName( + ref Utf8JsonReader reader, + global::System.Type typeToConvert, + JsonSerializerOptions options + ) + { + var stringValue = reader.GetString(); + if (stringValue == null) + throw new JsonException("Cannot deserialize null property name into OneOf type"); + + // Try to deserialize the string value into one of the supported types + foreach (var (type, cast) in GetOneOfTypes(typeToConvert)) + { + try + { + // For primitive types, try direct conversion + if (type == typeof(string)) + { + return (IOneOf)cast.Invoke(null, [stringValue])!; + } + + // For other types, try to deserialize from JSON string + var result = JsonSerializer.Deserialize($"\"{stringValue}\"", type, options); + if (result != null) + { + return (IOneOf)cast.Invoke(null, [result])!; + } + } + catch { } + } + + // If no type-specific deserialization worked, default to string if available + var stringType = GetOneOfTypes(typeToConvert).FirstOrDefault(t => t.type == typeof(string)); + if (stringType != default) + { + return (IOneOf)stringType.cast.Invoke(null, [stringValue])!; + } + + throw new JsonException( + $"Cannot deserialize dictionary key '{stringValue}' into one of the supported types for {typeToConvert}" + ); + } + + public override void WriteAsPropertyName( + Utf8JsonWriter writer, + IOneOf value, + JsonSerializerOptions options + ) + { + // Serialize the underlying value to a string suitable for use as a dictionary key + var stringValue = value.Value?.ToString() ?? "null"; + writer.WritePropertyName(stringValue); + } + + private static (global::System.Type type, MethodInfo cast)[] GetOneOfTypes( + global::System.Type typeToConvert + ) + { + var type = typeToConvert; + if (Nullable.GetUnderlyingType(type) is { } underlyingType) + { + type = underlyingType; + } + + var casts = type.GetRuntimeMethods() + .Where(m => m.IsSpecialName && m.Name == "op_Implicit") + .ToArray(); + while (type is not null) + { + if ( + type.IsGenericType + && (type.Name.StartsWith("OneOf`") || type.Name.StartsWith("OneOfBase`")) + ) + { + var genericArguments = type.GetGenericArguments(); + if (genericArguments.Length == 1) + { + return [(genericArguments[0], casts[0])]; + } + + // if object type is present, make sure it is last + var indexOfObjectType = Array.IndexOf(genericArguments, typeof(object)); + if (indexOfObjectType != -1 && genericArguments.Length - 1 != indexOfObjectType) + { + genericArguments = genericArguments + .OrderBy(t => t == typeof(object) ? 1 : 0) + .ToArray(); + } + + return genericArguments + .Select(t => (t, casts.First(c => c.GetParameters()[0].ParameterType == t))) + .ToArray(); + } + + type = type.BaseType; + } + + throw new InvalidOperationException($"{type} isn't OneOf or OneOfBase"); + } + + public override bool CanConvert(global::System.Type typeToConvert) + { + return typeof(IOneOf).IsAssignableFrom(typeToConvert); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Optional.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Optional.cs new file mode 100644 index 000000000000..0dac756de991 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Optional.cs @@ -0,0 +1,474 @@ +using global::System.Text.Json; +using global::System.Text.Json.Serialization; + +namespace SeedBasicAuthOptional.Core; + +/// +/// Non-generic interface for Optional types to enable reflection-free checks. +/// +public interface IOptional +{ + /// + /// Returns true if the value is defined (set), even if the value is null. + /// + bool IsDefined { get; } + + /// + /// Gets the boxed value. Returns null if undefined or if the value is null. + /// + object? GetBoxedValue(); +} + +/// +/// Represents a field that can be "not set" (undefined) vs "explicitly set" (defined). +/// Use this for HTTP PATCH requests where you need to distinguish between: +/// +/// Undefined: Don't send this field (leave it unchanged on the server) +/// Defined with null: Send null (clear the field on the server) +/// Defined with value: Send the value (update the field on the server) +/// +/// +/// The type of the value. Use nullable types (T?) for fields that can be null. +/// +/// For nullable string fields, use Optional<string?>: +/// +/// public class UpdateUserRequest +/// { +/// public Optional<string?> Name { get; set; } = Optional<string?>.Undefined; +/// } +/// +/// var request = new UpdateUserRequest +/// { +/// Name = "John" // Will send: { "name": "John" } +/// }; +/// +/// var request2 = new UpdateUserRequest +/// { +/// Name = Optional<string?>.Of(null) // Will send: { "name": null } +/// }; +/// +/// var request3 = new UpdateUserRequest(); // Will send: {} (name not included) +/// +/// +public readonly struct Optional : IOptional, IEquatable> +{ + private readonly T _value; + private readonly bool _isDefined; + + private Optional(T value, bool isDefined) + { + _value = value; + _isDefined = isDefined; + } + + /// + /// Creates an undefined value - the field will not be included in the HTTP request. + /// Use this as the default value for optional fields. + /// + /// + /// + /// public Optional<string?> Email { get; set; } = Optional<string?>.Undefined; + /// + /// + public static Optional Undefined => new(default!, false); + + /// + /// Creates a defined value - the field will be included in the HTTP request. + /// The value can be null if T is a nullable type. + /// + /// The value to set. Can be null if T is nullable (e.g., string?, int?). + /// + /// + /// // Set to a value + /// request.Name = Optional<string?>.Of("John"); + /// + /// // Set to null (clears the field) + /// request.Email = Optional<string?>.Of(null); + /// + /// // Or use implicit conversion + /// request.Name = "John"; // Same as Of("John") + /// request.Email = null; // Same as Of(null) + /// + /// + public static Optional Of(T value) => new(value, true); + + /// + /// Returns true if the field is defined (set), even if the value is null. + /// Use this to determine if the field should be included in the HTTP request. + /// + /// + /// + /// if (request.Name.IsDefined) + /// { + /// requestBody["name"] = request.Name.Value; // Include in request (can be null) + /// } + /// + /// + public bool IsDefined => _isDefined; + + /// + /// Returns true if the field is undefined (not set). + /// Use this to check if the field should be excluded from the HTTP request. + /// + /// + /// + /// if (request.Email.IsUndefined) + /// { + /// // Don't include email in the request - leave it unchanged + /// } + /// + /// + public bool IsUndefined => !_isDefined; + + /// + /// Gets the value. The value may be null if T is a nullable type. + /// + /// Thrown if the value is undefined. + /// + /// Always check before accessing Value, or use instead. + /// + /// + /// + /// if (request.Name.IsDefined) + /// { + /// string? name = request.Name.Value; // Safe - can be null if Optional<string?> + /// } + /// + /// // Or check for null explicitly + /// if (request.Email.IsDefined && request.Email.Value is null) + /// { + /// // Email is explicitly set to null (clear it) + /// } + /// + /// + public T Value + { + get + { + if (!_isDefined) + throw new InvalidOperationException("Optional value is undefined"); + return _value; + } + } + + /// + /// Gets the value if defined, otherwise returns the specified default value. + /// Note: If the value is defined as null, this returns null (not the default). + /// + /// The value to return if undefined. + /// The actual value if defined (can be null), otherwise the default value. + /// + /// + /// string name = request.Name.GetValueOrDefault("Anonymous"); + /// // If Name is undefined: returns "Anonymous" + /// // If Name is Of(null): returns null + /// // If Name is Of("John"): returns "John" + /// + /// + public T GetValueOrDefault(T defaultValue = default!) + { + return _isDefined ? _value : defaultValue; + } + + /// + /// Tries to get the value. Returns true if the value is defined (even if null). + /// + /// + /// When this method returns, contains the value if defined, or default(T) if undefined. + /// The value may be null if T is nullable. + /// + /// True if the value is defined; otherwise, false. + /// + /// + /// if (request.Email.TryGetValue(out var email)) + /// { + /// requestBody["email"] = email; // email can be null + /// } + /// else + /// { + /// // Email is undefined - don't include in request + /// } + /// + /// + public bool TryGetValue(out T value) + { + if (_isDefined) + { + value = _value; + return true; + } + value = default!; + return false; + } + + /// + /// Implicitly converts a value to Optional<T>.Of(value). + /// This allows natural assignment: request.Name = "John" instead of request.Name = Optional<string?>.Of("John"). + /// + /// The value to convert (can be null if T is nullable). + public static implicit operator Optional(T value) => Of(value); + + /// + /// Returns a string representation of this Optional value. + /// + /// "Undefined" if not set, or "Defined(value)" if set. + public override string ToString() => _isDefined ? $"Defined({_value})" : "Undefined"; + + /// + /// Gets the boxed value. Returns null if undefined or if the value is null. + /// + public object? GetBoxedValue() + { + if (!_isDefined) + return null; + return _value; + } + + /// + public bool Equals(Optional other) => + _isDefined == other._isDefined && EqualityComparer.Default.Equals(_value, other._value); + + /// + public override bool Equals(object? obj) => obj is Optional other && Equals(other); + + /// + public override int GetHashCode() + { + if (!_isDefined) + return 0; + unchecked + { + int hash = 17; + hash = hash * 31 + 1; // _isDefined = true + hash = hash * 31 + (_value is null ? 0 : _value.GetHashCode()); + return hash; + } + } + + /// + /// Determines whether two Optional values are equal. + /// + /// The first Optional to compare. + /// The second Optional to compare. + /// True if the Optional values are equal; otherwise, false. + public static bool operator ==(Optional left, Optional right) => left.Equals(right); + + /// + /// Determines whether two Optional values are not equal. + /// + /// The first Optional to compare. + /// The second Optional to compare. + /// True if the Optional values are not equal; otherwise, false. + public static bool operator !=(Optional left, Optional right) => !left.Equals(right); +} + +/// +/// Extension methods for Optional to simplify common operations. +/// +public static class OptionalExtensions +{ + /// + /// Adds the value to a dictionary if the optional is defined (even if the value is null). + /// This is useful for building JSON request payloads where null values should be included. + /// + /// The type of the optional value. + /// The optional value to add. + /// The dictionary to add to. + /// The key to use in the dictionary. + /// + /// + /// var dict = new Dictionary<string, object?>(); + /// request.Name.AddTo(dict, "name"); // Adds only if Name.IsDefined + /// request.Email.AddTo(dict, "email"); // Adds only if Email.IsDefined + /// + /// + public static void AddTo( + this Optional optional, + Dictionary dictionary, + string key + ) + { + if (optional.IsDefined) + { + dictionary[key] = optional.Value; + } + } + + /// + /// Executes an action if the optional is defined. + /// + /// The type of the optional value. + /// The optional value. + /// The action to execute with the value. + /// + /// + /// request.Name.IfDefined(name => Console.WriteLine($"Name: {name}")); + /// + /// + public static void IfDefined(this Optional optional, Action action) + { + if (optional.IsDefined) + { + action(optional.Value); + } + } + + /// + /// Maps the value to a new type if the optional is defined, otherwise returns undefined. + /// + /// The type of the original value. + /// The type to map to. + /// The optional value to map. + /// The mapping function. + /// An optional containing the mapped value if defined, otherwise undefined. + /// + /// + /// Optional<string?> name = Optional<string?>.Of("John"); + /// Optional<int> length = name.Map(n => n?.Length ?? 0); // Optional.Of(4) + /// + /// + public static Optional Map( + this Optional optional, + Func mapper + ) + { + return optional.IsDefined + ? Optional.Of(mapper(optional.Value)) + : Optional.Undefined; + } + + /// + /// Adds a nullable value to a dictionary only if it is not null. + /// This is useful for regular nullable properties where null means "omit from request". + /// + /// The type of the value (must be a reference type or Nullable). + /// The nullable value to add. + /// The dictionary to add to. + /// The key to use in the dictionary. + /// + /// + /// var dict = new Dictionary<string, object?>(); + /// request.Description.AddIfNotNull(dict, "description"); // Only adds if not null + /// request.Score.AddIfNotNull(dict, "score"); // Only adds if not null + /// + /// + public static void AddIfNotNull( + this T? value, + Dictionary dictionary, + string key + ) + where T : class + { + if (value is not null) + { + dictionary[key] = value; + } + } + + /// + /// Adds a nullable value type to a dictionary only if it has a value. + /// This is useful for regular nullable properties where null means "omit from request". + /// + /// The underlying value type. + /// The nullable value to add. + /// The dictionary to add to. + /// The key to use in the dictionary. + /// + /// + /// var dict = new Dictionary<string, object?>(); + /// request.Age.AddIfNotNull(dict, "age"); // Only adds if HasValue + /// request.Score.AddIfNotNull(dict, "score"); // Only adds if HasValue + /// + /// + public static void AddIfNotNull( + this T? value, + Dictionary dictionary, + string key + ) + where T : struct + { + if (value.HasValue) + { + dictionary[key] = value.Value; + } + } +} + +/// +/// JSON converter factory for Optional that handles undefined vs null correctly. +/// Uses a TypeInfoResolver to conditionally include/exclude properties based on Optional.IsDefined. +/// +public class OptionalJsonConverterFactory : JsonConverterFactory +{ + public override bool CanConvert(global::System.Type typeToConvert) + { + if (!typeToConvert.IsGenericType) + return false; + + return typeToConvert.GetGenericTypeDefinition() == typeof(Optional<>); + } + + public override JsonConverter? CreateConverter( + global::System.Type typeToConvert, + JsonSerializerOptions options + ) + { + var valueType = typeToConvert.GetGenericArguments()[0]; + var converterType = typeof(OptionalJsonConverter<>).MakeGenericType(valueType); + return (JsonConverter?)global::System.Activator.CreateInstance(converterType); + } +} + +/// +/// JSON converter for Optional that unwraps the value during serialization. +/// The actual property skipping is handled by the OptionalTypeInfoResolver. +/// +public class OptionalJsonConverter : JsonConverter> +{ + public override Optional Read( + ref Utf8JsonReader reader, + global::System.Type typeToConvert, + JsonSerializerOptions options + ) + { + if (reader.TokenType == JsonTokenType.Null) + { + return Optional.Of(default!); + } + + var value = JsonSerializer.Deserialize(ref reader, options); + return Optional.Of(value!); + } + + public override void Write( + Utf8JsonWriter writer, + Optional value, + JsonSerializerOptions options + ) + { + // This will be called by the serializer + // We need to unwrap and serialize the inner value + // The TypeInfoResolver will handle skipping undefined values + + if (value.IsUndefined) + { + // This shouldn't be called for undefined values due to ShouldSerialize + // But if it is, write null and let the resolver filter it + writer.WriteNullValue(); + return; + } + + // Get the inner value + var innerValue = value.Value; + + // Write null directly if the value is null (don't use JsonSerializer.Serialize for null) + if (innerValue is null) + { + writer.WriteNullValue(); + return; + } + + // Serialize the unwrapped value + JsonSerializer.Serialize(writer, innerValue, options); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/OptionalAttribute.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/OptionalAttribute.cs new file mode 100644 index 000000000000..2867248f37dc --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/OptionalAttribute.cs @@ -0,0 +1,17 @@ +namespace SeedBasicAuthOptional.Core; + +/// +/// Marks a property as optional in the OpenAPI specification. +/// Optional properties use the Optional type and can be undefined (not present in JSON). +/// +/// +/// Properties marked with [Optional] should use the Optional type: +/// - Undefined: Optional.Undefined → omitted from JSON +/// - Defined: Optional.Of(value) → written to JSON +/// +/// Combine with [Nullable] to allow null values: +/// - [Optional, Nullable] Optional → can be undefined, null, or a value +/// - [Optional] Optional → can be undefined or a value (null is invalid) +/// +[global::System.AttributeUsage(global::System.AttributeTargets.Property, AllowMultiple = false)] +public class OptionalAttribute : global::System.Attribute { } diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/AdditionalProperties.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/AdditionalProperties.cs new file mode 100644 index 000000000000..cfa2218695c6 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/AdditionalProperties.cs @@ -0,0 +1,353 @@ +using global::System.Collections; +using global::System.Collections.ObjectModel; +using global::System.Text.Json; +using global::System.Text.Json.Nodes; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional; + +public record ReadOnlyAdditionalProperties : ReadOnlyAdditionalProperties +{ + internal ReadOnlyAdditionalProperties() { } + + internal ReadOnlyAdditionalProperties(IDictionary properties) + : base(properties) { } +} + +public record ReadOnlyAdditionalProperties : IReadOnlyDictionary +{ + private readonly Dictionary _extensionData = new(); + private readonly Dictionary _convertedCache = new(); + + internal ReadOnlyAdditionalProperties() + { + _extensionData = new Dictionary(); + _convertedCache = new Dictionary(); + } + + internal ReadOnlyAdditionalProperties(IDictionary properties) + { + _extensionData = new Dictionary(properties.Count); + _convertedCache = new Dictionary(properties.Count); + foreach (var kvp in properties) + { + if (kvp.Value is JsonElement element) + { + _extensionData.Add(kvp.Key, element); + } + else + { + _extensionData[kvp.Key] = JsonUtils.SerializeToElement(kvp.Value); + } + + _convertedCache[kvp.Key] = kvp.Value; + } + } + + private static T ConvertToT(JsonElement value) + { + if (typeof(T) == typeof(JsonElement)) + { + return (T)(object)value; + } + + return value.Deserialize(JsonOptions.JsonSerializerOptions)!; + } + + internal void CopyFromExtensionData(IDictionary extensionData) + { + _extensionData.Clear(); + _convertedCache.Clear(); + foreach (var kvp in extensionData) + { + _extensionData[kvp.Key] = kvp.Value; + if (kvp.Value is T value) + { + _convertedCache[kvp.Key] = value; + } + } + } + + private T GetCached(string key) + { + if (_convertedCache.TryGetValue(key, out var cached)) + { + return cached; + } + + var value = ConvertToT(_extensionData[key]); + _convertedCache[key] = value; + return value; + } + + public IEnumerator> GetEnumerator() + { + return _extensionData + .Select(kvp => new KeyValuePair(kvp.Key, GetCached(kvp.Key))) + .GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + public int Count => _extensionData.Count; + + public bool ContainsKey(string key) => _extensionData.ContainsKey(key); + + public bool TryGetValue(string key, out T value) + { + if (_convertedCache.TryGetValue(key, out value!)) + { + return true; + } + + if (_extensionData.TryGetValue(key, out var element)) + { + value = ConvertToT(element); + _convertedCache[key] = value; + return true; + } + + return false; + } + + public T this[string key] => GetCached(key); + + public IEnumerable Keys => _extensionData.Keys; + + public IEnumerable Values => Keys.Select(GetCached); +} + +public record AdditionalProperties : AdditionalProperties +{ + public AdditionalProperties() { } + + public AdditionalProperties(IDictionary properties) + : base(properties) { } +} + +public record AdditionalProperties : IDictionary +{ + private readonly Dictionary _extensionData; + private readonly Dictionary _convertedCache; + + public AdditionalProperties() + { + _extensionData = new Dictionary(); + _convertedCache = new Dictionary(); + } + + public AdditionalProperties(IDictionary properties) + { + _extensionData = new Dictionary(properties.Count); + _convertedCache = new Dictionary(properties.Count); + foreach (var kvp in properties) + { + _extensionData[kvp.Key] = kvp.Value; + _convertedCache[kvp.Key] = kvp.Value; + } + } + + private static T ConvertToT(object? extensionDataValue) + { + return extensionDataValue switch + { + T value => value, + JsonElement jsonElement => jsonElement.Deserialize( + JsonOptions.JsonSerializerOptions + )!, + JsonNode jsonNode => jsonNode.Deserialize(JsonOptions.JsonSerializerOptions)!, + _ => JsonUtils + .SerializeToElement(extensionDataValue) + .Deserialize(JsonOptions.JsonSerializerOptions)!, + }; + } + + internal void CopyFromExtensionData(IDictionary extensionData) + { + _extensionData.Clear(); + _convertedCache.Clear(); + foreach (var kvp in extensionData) + { + _extensionData[kvp.Key] = kvp.Value; + if (kvp.Value is T value) + { + _convertedCache[kvp.Key] = value; + } + } + } + + internal void CopyToExtensionData(IDictionary extensionData) + { + extensionData.Clear(); + foreach (var kvp in _extensionData) + { + extensionData[kvp.Key] = kvp.Value; + } + } + + public JsonObject ToJsonObject() => + ( + JsonUtils.SerializeToNode(_extensionData) + ?? throw new InvalidOperationException( + "Failed to serialize AdditionalProperties to JSON Node." + ) + ).AsObject(); + + public JsonNode ToJsonNode() => + JsonUtils.SerializeToNode(_extensionData) + ?? throw new InvalidOperationException( + "Failed to serialize AdditionalProperties to JSON Node." + ); + + public JsonElement ToJsonElement() => JsonUtils.SerializeToElement(_extensionData); + + public JsonDocument ToJsonDocument() => JsonUtils.SerializeToDocument(_extensionData); + + public IReadOnlyDictionary ToJsonElementDictionary() + { + return new ReadOnlyDictionary( + _extensionData.ToDictionary( + kvp => kvp.Key, + kvp => + { + if (kvp.Value is JsonElement jsonElement) + { + return jsonElement; + } + + return JsonUtils.SerializeToElement(kvp.Value); + } + ) + ); + } + + public ICollection Keys => _extensionData.Keys; + + public ICollection Values + { + get + { + var values = new T[_extensionData.Count]; + var i = 0; + foreach (var key in Keys) + { + values[i++] = GetCached(key); + } + + return values; + } + } + + private T GetCached(string key) + { + if (_convertedCache.TryGetValue(key, out var value)) + { + return value; + } + + value = ConvertToT(_extensionData[key]); + _convertedCache.Add(key, value); + return value; + } + + private void SetCached(string key, T value) + { + _extensionData[key] = value; + _convertedCache[key] = value; + } + + private void AddCached(string key, T value) + { + _extensionData.Add(key, value); + _convertedCache.Add(key, value); + } + + private bool RemoveCached(string key) + { + var isRemoved = _extensionData.Remove(key); + _convertedCache.Remove(key); + return isRemoved; + } + + public int Count => _extensionData.Count; + public bool IsReadOnly => false; + + public T this[string key] + { + get => GetCached(key); + set => SetCached(key, value); + } + + public void Add(string key, T value) => AddCached(key, value); + + public void Add(KeyValuePair item) => AddCached(item.Key, item.Value); + + public bool Remove(string key) => RemoveCached(key); + + public bool Remove(KeyValuePair item) => RemoveCached(item.Key); + + public bool ContainsKey(string key) => _extensionData.ContainsKey(key); + + public bool Contains(KeyValuePair item) + { + return _extensionData.ContainsKey(item.Key) + && EqualityComparer.Default.Equals(GetCached(item.Key), item.Value); + } + + public bool TryGetValue(string key, out T value) + { + if (_convertedCache.TryGetValue(key, out value!)) + { + return true; + } + + if (_extensionData.TryGetValue(key, out var extensionDataValue)) + { + value = ConvertToT(extensionDataValue); + _convertedCache[key] = value; + return true; + } + + return false; + } + + public void Clear() + { + _extensionData.Clear(); + _convertedCache.Clear(); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + if (array is null) + { + throw new ArgumentNullException(nameof(array)); + } + + if (arrayIndex < 0 || arrayIndex > array.Length) + { + throw new ArgumentOutOfRangeException(nameof(arrayIndex)); + } + + if (array.Length - arrayIndex < _extensionData.Count) + { + throw new ArgumentException( + "The array does not have enough space to copy the elements." + ); + } + + foreach (var kvp in _extensionData) + { + array[arrayIndex++] = new KeyValuePair(kvp.Key, GetCached(kvp.Key)); + } + } + + public IEnumerator> GetEnumerator() + { + return _extensionData + .Select(kvp => new KeyValuePair(kvp.Key, GetCached(kvp.Key))) + .GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/ClientOptions.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/ClientOptions.cs new file mode 100644 index 000000000000..095b5e14bc8c --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/ClientOptions.cs @@ -0,0 +1,84 @@ +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional; + +[Serializable] +public partial class ClientOptions +{ + /// + /// The http headers sent with the request. + /// + internal Headers Headers { get; init; } = new(); + + /// + /// The Base URL for the API. + /// + public string BaseUrl { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } = ""; + + /// + /// The http client used to make requests. + /// + public HttpClient HttpClient { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } = new HttpClient(); + + /// + /// Additional headers to be sent with HTTP requests. + /// Headers with matching keys will be overwritten by headers set on the request. + /// + public IEnumerable> AdditionalHeaders { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } = []; + + /// + /// The max number of retries to attempt. + /// + public int MaxRetries { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } = 2; + + /// + /// The timeout for the request. + /// + public TimeSpan Timeout { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } = TimeSpan.FromSeconds(30); + + /// + /// Clones this and returns a new instance + /// + internal ClientOptions Clone() + { + return new ClientOptions + { + BaseUrl = BaseUrl, + HttpClient = HttpClient, + MaxRetries = MaxRetries, + Timeout = Timeout, + Headers = new Headers(new Dictionary(Headers)), + AdditionalHeaders = AdditionalHeaders, + }; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/FileParameter.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/FileParameter.cs new file mode 100644 index 000000000000..59afdfed2d7d --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/FileParameter.cs @@ -0,0 +1,63 @@ +namespace SeedBasicAuthOptional; + +/// +/// File parameter for uploading files. +/// +public record FileParameter : IDisposable +#if NET6_0_OR_GREATER + , IAsyncDisposable +#endif +{ + private bool _disposed; + + /// + /// The name of the file to be uploaded. + /// + public string? FileName { get; set; } + + /// + /// The content type of the file to be uploaded. + /// + public string? ContentType { get; set; } + + /// + /// The content of the file to be uploaded. + /// + public required Stream Stream { get; set; } + + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + if (disposing) + { + Stream.Dispose(); + } + + _disposed = true; + } + +#if NET6_0_OR_GREATER + /// + public async ValueTask DisposeAsync() + { + if (!_disposed) + { + await Stream.DisposeAsync().ConfigureAwait(false); + _disposed = true; + } + + GC.SuppressFinalize(this); + } +#endif + + public static implicit operator FileParameter(Stream stream) => new() { Stream = stream }; +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/RawResponse.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/RawResponse.cs new file mode 100644 index 000000000000..0bfb9f0a87e1 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/RawResponse.cs @@ -0,0 +1,24 @@ +using global::System.Net; + +namespace SeedBasicAuthOptional; + +/// +/// Contains HTTP response metadata including status code, URL, and headers. +/// +public record RawResponse +{ + /// + /// The HTTP status code of the response. + /// + public required HttpStatusCode StatusCode { get; init; } + + /// + /// The request URL that generated this response. + /// + public required Uri Url { get; init; } + + /// + /// The HTTP response headers. + /// + public required Core.ResponseHeaders Headers { get; init; } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/RequestOptions.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/RequestOptions.cs new file mode 100644 index 000000000000..b31eb653991f --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/RequestOptions.cs @@ -0,0 +1,86 @@ +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional; + +[Serializable] +public partial class RequestOptions : IRequestOptions +{ + /// + /// The Base URL for the API. + /// + public string? BaseUrl { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } + + /// + /// The http client used to make requests. + /// + public HttpClient? HttpClient { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } + + /// + /// Additional headers to be sent with the request. + /// Headers previously set with matching keys will be overwritten. + /// + public IEnumerable> AdditionalHeaders { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } = []; + + /// + /// The max number of retries to attempt. + /// + public int? MaxRetries { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } + + /// + /// The timeout for the request. + /// + public TimeSpan? Timeout { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } + + /// + /// Additional query parameters sent with the request. + /// + public IEnumerable> AdditionalQueryParameters { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } = Enumerable.Empty>(); + + /// + /// Additional body properties sent with the request. + /// This is only applied to JSON requests. + /// + public object? AdditionalBodyProperties { get; +#if NET5_0_OR_GREATER + init; +#else + set; +#endif + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/SeedBasicAuthOptionalApiException.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/SeedBasicAuthOptionalApiException.cs new file mode 100644 index 000000000000..776d063187d7 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/SeedBasicAuthOptionalApiException.cs @@ -0,0 +1,22 @@ +namespace SeedBasicAuthOptional; + +/// +/// This exception type will be thrown for any non-2XX API responses. +/// +public class SeedBasicAuthOptionalApiException( + string message, + int statusCode, + object body, + Exception? innerException = null +) : SeedBasicAuthOptionalException(message, innerException) +{ + /// + /// The error code of the response that triggered the exception. + /// + public int StatusCode => statusCode; + + /// + /// The body of the response that triggered the exception. + /// + public object Body => body; +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/SeedBasicAuthOptionalException.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/SeedBasicAuthOptionalException.cs new file mode 100644 index 000000000000..bf72810d1158 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/SeedBasicAuthOptionalException.cs @@ -0,0 +1,7 @@ +namespace SeedBasicAuthOptional; + +/// +/// Base exception class for all exceptions thrown by the SDK. +/// +public class SeedBasicAuthOptionalException(string message, Exception? innerException = null) + : Exception(message, innerException); diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/Version.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/Version.cs new file mode 100644 index 000000000000..a15c59ea7031 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/Version.cs @@ -0,0 +1,7 @@ +namespace SeedBasicAuthOptional; + +[Serializable] +internal class Version +{ + public const string Current = "0.0.1"; +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/WithRawResponse.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/WithRawResponse.cs new file mode 100644 index 000000000000..f50dbfdb493b --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/WithRawResponse.cs @@ -0,0 +1,18 @@ +namespace SeedBasicAuthOptional; + +/// +/// Wraps a parsed response value with its raw HTTP response metadata. +/// +/// The type of the parsed response data. +public readonly struct WithRawResponse +{ + /// + /// The parsed response data. + /// + public required T Data { get; init; } + + /// + /// The raw HTTP response metadata. + /// + public required RawResponse RawResponse { get; init; } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/WithRawResponseTask.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/WithRawResponseTask.cs new file mode 100644 index 000000000000..ce2975aac51c --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/Public/WithRawResponseTask.cs @@ -0,0 +1,144 @@ +using global::System.Runtime.CompilerServices; + +namespace SeedBasicAuthOptional; + +/// +/// A task-like type that wraps Task<WithRawResponse<T>> and provides dual-mode awaiting: +/// - Direct await yields just T (zero-allocation path for common case) +/// - .WithRawResponse() yields WithRawResponse<T> (when raw response metadata is needed) +/// +/// The type of the parsed response data. +public readonly struct WithRawResponseTask +{ + private readonly global::System.Threading.Tasks.Task> _task; + + /// + /// Creates a new WithRawResponseTask wrapping the given task. + /// + public WithRawResponseTask(global::System.Threading.Tasks.Task> task) + { + _task = task; + } + + /// + /// Returns the underlying task that yields both the data and raw response metadata. + /// + public global::System.Threading.Tasks.Task> WithRawResponse() => _task; + + /// + /// Gets the custom awaiter that unwraps to just T when awaited. + /// + public Awaiter GetAwaiter() => new(_task.GetAwaiter()); + + /// + /// Configures the awaiter to continue on the captured context or not. + /// + public ConfiguredTaskAwaitable ConfigureAwait(bool continueOnCapturedContext) => + new(_task.ConfigureAwait(continueOnCapturedContext)); + + /// + /// Implicitly converts WithRawResponseTask<T> to global::System.Threading.Tasks.Task<T> for backward compatibility. + /// The resulting task will yield just the data when awaited. + /// + public static implicit operator global::System.Threading.Tasks.Task( + WithRawResponseTask task + ) + { + return task._task.ContinueWith( + t => t.Result.Data, + TaskContinuationOptions.ExecuteSynchronously + ); + } + + /// + /// Custom awaiter that unwraps WithRawResponse<T> to just T. + /// + public readonly struct Awaiter : ICriticalNotifyCompletion + { + private readonly TaskAwaiter> _awaiter; + + internal Awaiter(TaskAwaiter> awaiter) + { + _awaiter = awaiter; + } + + /// + /// Gets whether the underlying task has completed. + /// + public bool IsCompleted => _awaiter.IsCompleted; + + /// + /// Gets the result, unwrapping to just the data. + /// + public T GetResult() => _awaiter.GetResult().Data; + + /// + /// Schedules the continuation action. + /// + public void OnCompleted(global::System.Action continuation) => + _awaiter.OnCompleted(continuation); + + /// + /// Schedules the continuation action without capturing the execution context. + /// + public void UnsafeOnCompleted(global::System.Action continuation) => + _awaiter.UnsafeOnCompleted(continuation); + } + + /// + /// Awaitable type returned by ConfigureAwait that unwraps to just T. + /// + public readonly struct ConfiguredTaskAwaitable + { + private readonly ConfiguredTaskAwaitable> _configuredTask; + + internal ConfiguredTaskAwaitable(ConfiguredTaskAwaitable> configuredTask) + { + _configuredTask = configuredTask; + } + + /// + /// Gets the configured awaiter that unwraps to just T. + /// + public ConfiguredAwaiter GetAwaiter() => new(_configuredTask.GetAwaiter()); + + /// + /// Custom configured awaiter that unwraps WithRawResponse<T> to just T. + /// + public readonly struct ConfiguredAwaiter : ICriticalNotifyCompletion + { + private readonly ConfiguredTaskAwaitable< + WithRawResponse + >.ConfiguredTaskAwaiter _awaiter; + + internal ConfiguredAwaiter( + ConfiguredTaskAwaitable>.ConfiguredTaskAwaiter awaiter + ) + { + _awaiter = awaiter; + } + + /// + /// Gets whether the underlying task has completed. + /// + public bool IsCompleted => _awaiter.IsCompleted; + + /// + /// Gets the result, unwrapping to just the data. + /// + public T GetResult() => _awaiter.GetResult().Data; + + /// + /// Schedules the continuation action. + /// + public void OnCompleted(global::System.Action continuation) => + _awaiter.OnCompleted(continuation); + + /// + /// Schedules the continuation action without capturing the execution context. + /// + public void UnsafeOnCompleted(global::System.Action continuation) => + _awaiter.UnsafeOnCompleted(continuation); + } + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/QueryStringBuilder.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/QueryStringBuilder.cs new file mode 100644 index 000000000000..b2eaac863b68 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/QueryStringBuilder.cs @@ -0,0 +1,469 @@ +using global::System.Buffers; +using global::System.Runtime.CompilerServices; +#if !NET6_0_OR_GREATER +using global::System.Text; +#endif + +namespace SeedBasicAuthOptional.Core; + +/// +/// High-performance query string builder with cross-platform optimizations. +/// Uses span-based APIs on .NET 6+ and StringBuilder fallback for older targets. +/// +internal static class QueryStringBuilder +{ +#if NET8_0_OR_GREATER + private static readonly SearchValues UnreservedChars = SearchValues.Create( + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~" + ); +#else + private const string UnreservedChars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"; +#endif + +#if NET7_0_OR_GREATER + private static ReadOnlySpan UpperHexChars => "0123456789ABCDEF"u8; +#else + private static readonly byte[] UpperHexChars = + { + (byte)'0', + (byte)'1', + (byte)'2', + (byte)'3', + (byte)'4', + (byte)'5', + (byte)'6', + (byte)'7', + (byte)'8', + (byte)'9', + (byte)'A', + (byte)'B', + (byte)'C', + (byte)'D', + (byte)'E', + (byte)'F', + }; +#endif + + /// + /// Builds a query string from the provided parameters. + /// +#if NET6_0_OR_GREATER + public static string Build(ReadOnlySpan> parameters) + { + if (parameters.IsEmpty) + return string.Empty; + + var estimatedLength = EstimateLength(parameters); + if (estimatedLength == 0) + return string.Empty; + + var bufferSize = Math.Min(estimatedLength * 3, 8192); + var buffer = ArrayPool.Shared.Rent(bufferSize); + + try + { + var written = BuildCore(parameters, buffer); + return new string(buffer.AsSpan(0, written)); + } + finally + { + ArrayPool.Shared.Return(buffer); + } + } + + private static int EstimateLength(ReadOnlySpan> parameters) + { + var estimatedLength = 0; + foreach (var kvp in parameters) + { + estimatedLength += kvp.Key.Length + kvp.Value.Length + 2; + } + return estimatedLength; + } +#endif + + /// + /// Builds a query string from the provided parameters. + /// + public static string Build(IEnumerable> parameters) + { +#if NET6_0_OR_GREATER + // Try to get span access for collections that support it + if (parameters is ICollection> collection) + { + if (collection.Count == 0) + return string.Empty; + + var array = ArrayPool>.Shared.Rent(collection.Count); + try + { + collection.CopyTo(array, 0); + return Build(array.AsSpan(0, collection.Count)); + } + finally + { + ArrayPool>.Shared.Return(array); + } + } + + // Fallback for non-collection enumerables + using var enumerator = parameters.GetEnumerator(); + if (!enumerator.MoveNext()) + return string.Empty; + + var buffer = ArrayPool.Shared.Rent(4096); + try + { + var position = 0; + var first = true; + + do + { + var kvp = enumerator.Current; + + // Ensure capacity (worst case: 3x for encoding + separators) + var required = (kvp.Key.Length + kvp.Value.Length + 2) * 3; + if (position + required > buffer.Length) + { + var newBuffer = ArrayPool.Shared.Rent(buffer.Length * 2); + buffer.AsSpan(0, position).CopyTo(newBuffer); + ArrayPool.Shared.Return(buffer); + buffer = newBuffer; + } + + buffer[position++] = first ? '?' : '&'; + first = false; + + position += EncodeComponent(kvp.Key.AsSpan(), buffer.AsSpan(position)); + buffer[position++] = '='; + position += EncodeComponent(kvp.Value.AsSpan(), buffer.AsSpan(position)); + } while (enumerator.MoveNext()); + + return first ? string.Empty : new string(buffer.AsSpan(0, position)); + } + finally + { + ArrayPool.Shared.Return(buffer); + } +#else + // netstandard2.0 / net462 fallback using StringBuilder + var sb = new StringBuilder(); + var first = true; + + foreach (var kvp in parameters) + { + sb.Append(first ? '?' : '&'); + first = false; + + AppendEncoded(sb, kvp.Key); + sb.Append('='); + AppendEncoded(sb, kvp.Value); + } + + return sb.ToString(); +#endif + } + +#if NET6_0_OR_GREATER + private static int BuildCore( + ReadOnlySpan> parameters, + Span buffer + ) + { + var position = 0; + var first = true; + + foreach (var kvp in parameters) + { + buffer[position++] = first ? '?' : '&'; + first = false; + + position += EncodeComponent(kvp.Key.AsSpan(), buffer.Slice(position)); + buffer[position++] = '='; + position += EncodeComponent(kvp.Value.AsSpan(), buffer.Slice(position)); + } + + return position; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int EncodeComponent(ReadOnlySpan input, Span output) + { + if (!NeedsEncoding(input)) + { + input.CopyTo(output); + return input.Length; + } + + return EncodeSlow(input, output); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static bool NeedsEncoding(ReadOnlySpan value) + { + return value.ContainsAnyExcept(UnreservedChars); + } + + private static int EncodeSlow(ReadOnlySpan input, Span output) + { + var position = 0; + + foreach (var c in input) + { + if (IsUnreserved(c)) + { + output[position++] = c; + } + else if (c == ' ') + { + output[position++] = '%'; + output[position++] = '2'; + output[position++] = '0'; + } + else if (char.IsAscii(c)) + { + position += EncodeAscii((byte)c, output.Slice(position)); + } + else + { + position += EncodeUtf8(c, output.Slice(position)); + } + } + + return position; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int EncodeAscii(byte value, Span output) + { + output[0] = '%'; + output[1] = (char)UpperHexChars[value >> 4]; + output[2] = (char)UpperHexChars[value & 0xF]; + return 3; + } + + private static int EncodeUtf8(char c, Span output) + { + Span utf8Bytes = stackalloc byte[4]; + Span singleChar = stackalloc char[1] { c }; + var byteCount = global::System.Text.Encoding.UTF8.GetBytes(singleChar, utf8Bytes); + + var position = 0; + for (var i = 0; i < byteCount; i++) + { + output[position++] = '%'; + output[position++] = (char)UpperHexChars[utf8Bytes[i] >> 4]; + output[position++] = (char)UpperHexChars[utf8Bytes[i] & 0xF]; + } + + return position; + } +#else + // netstandard2.0 / net462 StringBuilder-based encoding + private static void AppendEncoded(StringBuilder sb, string value) + { + foreach (var c in value) + { + if (IsUnreserved(c)) + { + sb.Append(c); + } + else if (c == ' ') + { + sb.Append("%20"); + } + else if (c <= 127) + { + AppendPercentEncoded(sb, (byte)c); + } + else + { + var bytes = Encoding.UTF8.GetBytes(new[] { c }); + foreach (var b in bytes) + { + AppendPercentEncoded(sb, b); + } + } + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void AppendPercentEncoded(StringBuilder sb, byte value) + { + sb.Append('%'); + sb.Append((char)UpperHexChars[value >> 4]); + sb.Append((char)UpperHexChars[value & 0xF]); + } +#endif + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static bool IsUnreserved(char c) + { +#if NET8_0_OR_GREATER + return UnreservedChars.Contains(c); +#else + return (c >= 'A' && c <= 'Z') + || (c >= 'a' && c <= 'z') + || (c >= '0' && c <= '9') + || c == '-' + || c == '_' + || c == '.' + || c == '~'; +#endif + } + + /// + /// Fluent builder for constructing query strings with support for simple parameters and deep object notation. + /// + public sealed class Builder + { + private readonly List> _params; + + /// + /// Initializes a new instance with default capacity. + /// + public Builder() + { + _params = new List>(); + } + + /// + /// Initializes a new instance with the specified initial capacity. + /// + public Builder(int capacity) + { + _params = new List>(capacity); + } + + /// + /// Adds a simple parameter. For collections, adds multiple key-value pairs (one per element). + /// + public Builder Add(string key, object? value) + { + if (value is null) + { + return this; + } + + // Handle string separately since it implements IEnumerable + if (value is string stringValue) + { + _params.Add(new KeyValuePair(key, stringValue)); + return this; + } + + // Handle collections (arrays, lists, etc.) - add each element as a separate key-value pair + if ( + value + is global::System.Collections.IEnumerable enumerable + and not global::System.Collections.IDictionary + ) + { + foreach (var item in enumerable) + { + if (item is not null) + { + _params.Add( + new KeyValuePair( + key, + ValueConvert.ToQueryStringValue(item) + ) + ); + } + } + return this; + } + + // Handle scalar values + _params.Add( + new KeyValuePair(key, ValueConvert.ToQueryStringValue(value)) + ); + return this; + } + + /// + /// Sets a parameter, removing any existing parameters with the same key before adding the new value. + /// For collections, removes all existing parameters with the key, then adds multiple key-value pairs (one per element). + /// This allows overriding parameters set earlier in the builder. + /// + public Builder Set(string key, object? value) + { + // Remove all existing parameters with this key + _params.RemoveAll(kv => kv.Key == key); + + // Add the new value(s) + return Add(key, value); + } + + /// + /// Merges additional query parameters with override semantics. + /// Groups parameters by key and calls Set() once per unique key. + /// This ensures that parameters with the same key are properly merged: + /// - If a key appears once, it's added as a single value + /// - If a key appears multiple times, all values are added as an array + /// - All parameters override any existing parameters with the same key + /// + public Builder MergeAdditional( + global::System.Collections.Generic.IEnumerable>? additionalParameters + ) + { + if (additionalParameters is null) + { + return this; + } + + // Group by key to handle multiple values for the same key correctly + var grouped = additionalParameters + .GroupBy(kv => kv.Key) + .Select(g => new global::System.Collections.Generic.KeyValuePair( + g.Key, + g.Count() == 1 ? (object)g.First().Value : g.Select(kv => kv.Value).ToArray() + )); + + foreach (var param in grouped) + { + Set(param.Key, param.Value); + } + + return this; + } + + /// + /// Adds a complex object using deep object notation with a prefix. + /// Deep object notation nests properties with brackets: prefix[key][nested]=value + /// + public Builder AddDeepObject(string prefix, object? value) + { + if (value is not null) + { + _params.AddRange(QueryStringConverter.ToDeepObject(prefix, value)); + } + return this; + } + + /// + /// Adds a complex object using exploded form notation with an optional prefix. + /// Exploded form flattens properties: prefix[key]=value (no deep nesting). + /// + public Builder AddExploded(string prefix, object? value) + { + if (value is not null) + { + _params.AddRange(QueryStringConverter.ToExplodedForm(prefix, value)); + } + return this; + } + + /// + /// Builds the final query string. + /// + public string Build() + { + return QueryStringBuilder.Build(_params); + } + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/QueryStringConverter.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/QueryStringConverter.cs new file mode 100644 index 000000000000..3f9a49404a2c --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/QueryStringConverter.cs @@ -0,0 +1,259 @@ +using global::System.Text.Json; + +namespace SeedBasicAuthOptional.Core; + +/// +/// Converts an object into a query string collection. +/// +internal static class QueryStringConverter +{ + /// + /// Converts an object into a query string collection using Deep Object notation with a prefix. + /// + /// The prefix to prepend to all keys (e.g., "session_settings"). Pass empty string for no prefix. + /// Object to form URL-encode. Can be an object, array of objects, or dictionary. + /// Throws when passing in a string or primitive value. + /// A collection of key value pairs. The keys and values are not URL encoded. + internal static IEnumerable> ToDeepObject( + string prefix, + object value + ) + { + var queryCollection = new List>(); + var json = JsonUtils.SerializeToElement(value); + JsonToDeepObject(json, prefix, queryCollection); + return queryCollection; + } + + /// + /// Converts an object into a query string collection using Deep Object notation. + /// + /// Object to form URL-encode. Can be an object, array of objects, or dictionary. + /// Throws when passing in a string or primitive value. + /// A collection of key value pairs. The keys and values are not URL encoded. + internal static IEnumerable> ToDeepObject(object value) + { + return ToDeepObject("", value); + } + + /// + /// Converts an object into a query string collection using Exploded Form notation with a prefix. + /// + /// The prefix to prepend to all keys. Pass empty string for no prefix. + /// Object to form URL-encode. You can pass in an object or dictionary, but not lists, strings, or primitives. + /// Throws when passing in a list, a string, or a primitive value. + /// A collection of key value pairs. The keys and values are not URL encoded. + internal static IEnumerable> ToExplodedForm( + string prefix, + object value + ) + { + var queryCollection = new List>(); + var json = JsonUtils.SerializeToElement(value); + AssertRootJson(json); + JsonToFormExploded(json, prefix, queryCollection); + return queryCollection; + } + + /// + /// Converts an object into a query string collection using Exploded Form notation. + /// + /// Object to form URL-encode. You can pass in an object or dictionary, but not lists, strings, or primitives. + /// Throws when passing in a list, a string, or a primitive value. + /// A collection of key value pairs. The keys and values are not URL encoded. + internal static IEnumerable> ToExplodedForm(object value) + { + return ToExplodedForm("", value); + } + + /// + /// Converts an object into a query string collection using Form notation without exploding parameters. + /// + /// Object to form URL-encode. You can pass in an object or dictionary, but not lists, strings, or primitives. + /// Throws when passing in a list, a string, or a primitive value. + /// A collection of key value pairs. The keys and values are not URL encoded. + internal static IEnumerable> ToForm(object value) + { + var queryCollection = new List>(); + var json = JsonUtils.SerializeToElement(value); + AssertRootJson(json); + JsonToForm(json, "", queryCollection); + return queryCollection; + } + + private static void AssertRootJson(JsonElement json) + { + switch (json.ValueKind) + { + case JsonValueKind.Object: + break; + case JsonValueKind.Array: + case JsonValueKind.Undefined: + case JsonValueKind.String: + case JsonValueKind.Number: + case JsonValueKind.True: + case JsonValueKind.False: + case JsonValueKind.Null: + default: + throw new global::System.Exception( + $"Only objects can be converted to query string collections. Given type is {json.ValueKind}." + ); + } + } + + private static void JsonToForm( + JsonElement element, + string prefix, + List> parameters + ) + { + switch (element.ValueKind) + { + case JsonValueKind.Object: + foreach (var property in element.EnumerateObject()) + { + var newPrefix = string.IsNullOrEmpty(prefix) + ? property.Name + : $"{prefix}[{property.Name}]"; + + JsonToForm(property.Value, newPrefix, parameters); + } + break; + case JsonValueKind.Array: + var arrayValues = element.EnumerateArray().Select(ValueToString).ToArray(); + parameters.Add( + new KeyValuePair(prefix, string.Join(",", arrayValues)) + ); + break; + case JsonValueKind.Null: + break; + case JsonValueKind.Undefined: + case JsonValueKind.String: + case JsonValueKind.Number: + case JsonValueKind.True: + case JsonValueKind.False: + default: + parameters.Add(new KeyValuePair(prefix, ValueToString(element))); + break; + } + } + + private static void JsonToFormExploded( + JsonElement element, + string prefix, + List> parameters + ) + { + switch (element.ValueKind) + { + case JsonValueKind.Object: + foreach (var property in element.EnumerateObject()) + { + var newPrefix = string.IsNullOrEmpty(prefix) + ? property.Name + : $"{prefix}[{property.Name}]"; + + JsonToFormExploded(property.Value, newPrefix, parameters); + } + + break; + case JsonValueKind.Array: + foreach (var item in element.EnumerateArray()) + { + if ( + item.ValueKind != JsonValueKind.Object + && item.ValueKind != JsonValueKind.Array + ) + { + parameters.Add( + new KeyValuePair(prefix, ValueToString(item)) + ); + } + else + { + JsonToFormExploded(item, prefix, parameters); + } + } + + break; + case JsonValueKind.Null: + break; + case JsonValueKind.Undefined: + case JsonValueKind.String: + case JsonValueKind.Number: + case JsonValueKind.True: + case JsonValueKind.False: + default: + parameters.Add(new KeyValuePair(prefix, ValueToString(element))); + break; + } + } + + private static void JsonToDeepObject( + JsonElement element, + string prefix, + List> parameters + ) + { + switch (element.ValueKind) + { + case JsonValueKind.Object: + foreach (var property in element.EnumerateObject()) + { + var newPrefix = string.IsNullOrEmpty(prefix) + ? property.Name + : $"{prefix}[{property.Name}]"; + + JsonToDeepObject(property.Value, newPrefix, parameters); + } + + break; + case JsonValueKind.Array: + var index = 0; + foreach (var item in element.EnumerateArray()) + { + var newPrefix = $"{prefix}[{index++}]"; + + if ( + item.ValueKind != JsonValueKind.Object + && item.ValueKind != JsonValueKind.Array + ) + { + parameters.Add( + new KeyValuePair(newPrefix, ValueToString(item)) + ); + } + else + { + JsonToDeepObject(item, newPrefix, parameters); + } + } + + break; + case JsonValueKind.Null: + case JsonValueKind.Undefined: + // Skip null and undefined values - don't add parameters for them + break; + case JsonValueKind.String: + case JsonValueKind.Number: + case JsonValueKind.True: + case JsonValueKind.False: + default: + parameters.Add(new KeyValuePair(prefix, ValueToString(element))); + break; + } + } + + private static string ValueToString(JsonElement element) + { + return element.ValueKind switch + { + JsonValueKind.String => element.GetString() ?? "", + JsonValueKind.Number => element.GetRawText(), + JsonValueKind.True => "true", + JsonValueKind.False => "false", + JsonValueKind.Null => "", + _ => element.GetRawText(), + }; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/RawClient.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/RawClient.cs new file mode 100644 index 000000000000..c0c46ffc8168 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/RawClient.cs @@ -0,0 +1,344 @@ +using global::System.Net.Http; +using global::System.Net.Http.Headers; +using global::System.Text; +using SystemTask = global::System.Threading.Tasks.Task; + +namespace SeedBasicAuthOptional.Core; + +/// +/// Utility class for making raw HTTP requests to the API. +/// +internal partial class RawClient(ClientOptions clientOptions) +{ + private const int MaxRetryDelayMs = 60000; + private const double JitterFactor = 0.2; +#if NET6_0_OR_GREATER + // Use Random.Shared for thread-safe random number generation on .NET 6+ +#else + private static readonly object JitterLock = new(); + private static readonly Random JitterRandom = new(); +#endif + internal int BaseRetryDelay { get; set; } = 1000; + + /// + /// The client options applied on every request. + /// + internal readonly ClientOptions Options = clientOptions; + + internal async global::System.Threading.Tasks.Task SendRequestAsync( + global::SeedBasicAuthOptional.Core.BaseRequest request, + CancellationToken cancellationToken = default + ) + { + // Apply the request timeout. + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + var timeout = request.Options?.Timeout ?? Options.Timeout; + cts.CancelAfter(timeout); + + var httpRequest = await CreateHttpRequestAsync(request).ConfigureAwait(false); + // Send the request. + return await SendWithRetriesAsync(httpRequest, request.Options, cts.Token) + .ConfigureAwait(false); + } + + internal async global::System.Threading.Tasks.Task SendRequestAsync( + HttpRequestMessage request, + IRequestOptions? options, + CancellationToken cancellationToken = default + ) + { + // Apply the request timeout. + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + var timeout = options?.Timeout ?? Options.Timeout; + cts.CancelAfter(timeout); + + // Send the request. + return await SendWithRetriesAsync(request, options, cts.Token).ConfigureAwait(false); + } + + private static async global::System.Threading.Tasks.Task CloneRequestAsync( + HttpRequestMessage request + ) + { + var clonedRequest = new HttpRequestMessage(request.Method, request.RequestUri); + clonedRequest.Version = request.Version; + + if (request.Content != null) + { + switch (request.Content) + { + case MultipartContent oldMultipartFormContent: + var originalBoundary = + oldMultipartFormContent + .Headers.ContentType?.Parameters.First(p => + p.Name.Equals("boundary", StringComparison.OrdinalIgnoreCase) + ) + .Value?.Trim('"') + ?? Guid.NewGuid().ToString(); + var newMultipartContent = oldMultipartFormContent switch + { + MultipartFormDataContent => new MultipartFormDataContent(originalBoundary), + _ => new MultipartContent(), + }; + foreach (var content in oldMultipartFormContent) + { + var ms = new MemoryStream(); + await content.CopyToAsync(ms).ConfigureAwait(false); + ms.Position = 0; + var newPart = new StreamContent(ms); + foreach (var header in content.Headers) + { + newPart.Headers.TryAddWithoutValidation(header.Key, header.Value); + } + + newMultipartContent.Add(newPart); + } + + clonedRequest.Content = newMultipartContent; + break; + default: + var bodyStream = new MemoryStream(); + await request.Content.CopyToAsync(bodyStream).ConfigureAwait(false); + bodyStream.Position = 0; + var clonedContent = new StreamContent(bodyStream); + foreach (var header in request.Content.Headers) + { + clonedContent.Headers.TryAddWithoutValidation(header.Key, header.Value); + } + + clonedRequest.Content = clonedContent; + break; + } + } + + foreach (var header in request.Headers) + { + clonedRequest.Headers.TryAddWithoutValidation(header.Key, header.Value); + } + + return clonedRequest; + } + + /// + /// Sends the request with retries, unless the request content is not retryable, + /// such as stream requests and multipart form data with stream content. + /// + private async global::System.Threading.Tasks.Task SendWithRetriesAsync( + HttpRequestMessage request, + IRequestOptions? options, + CancellationToken cancellationToken + ) + { + var httpClient = options?.HttpClient ?? Options.HttpClient; + var maxRetries = options?.MaxRetries ?? Options.MaxRetries; + var response = await httpClient + .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) + .ConfigureAwait(false); + var isRetryableContent = IsRetryableContent(request); + + if (!isRetryableContent) + { + return new global::SeedBasicAuthOptional.Core.ApiResponse + { + StatusCode = (int)response.StatusCode, + Raw = response, + }; + } + + for (var i = 0; i < maxRetries; i++) + { + if (!ShouldRetry(response)) + { + break; + } + + var delayMs = GetRetryDelayFromHeaders(response, i); + await SystemTask.Delay(delayMs, cancellationToken).ConfigureAwait(false); + using var retryRequest = await CloneRequestAsync(request).ConfigureAwait(false); + response = await httpClient + .SendAsync( + retryRequest, + HttpCompletionOption.ResponseHeadersRead, + cancellationToken + ) + .ConfigureAwait(false); + } + + return new global::SeedBasicAuthOptional.Core.ApiResponse + { + StatusCode = (int)response.StatusCode, + Raw = response, + }; + } + + private static bool ShouldRetry(HttpResponseMessage response) + { + var statusCode = (int)response.StatusCode; + return statusCode is 408 or 429 or >= 500; + } + + private static int AddPositiveJitter(int delayMs) + { +#if NET6_0_OR_GREATER + var random = Random.Shared.NextDouble(); +#else + double random; + lock (JitterLock) + { + random = JitterRandom.NextDouble(); + } +#endif + var jitterMultiplier = 1 + random * JitterFactor; + return (int)(delayMs * jitterMultiplier); + } + + private static int AddSymmetricJitter(int delayMs) + { +#if NET6_0_OR_GREATER + var random = Random.Shared.NextDouble(); +#else + double random; + lock (JitterLock) + { + random = JitterRandom.NextDouble(); + } +#endif + var jitterMultiplier = 1 + (random - 0.5) * JitterFactor; + return (int)(delayMs * jitterMultiplier); + } + + private int GetRetryDelayFromHeaders(HttpResponseMessage response, int retryAttempt) + { + if (response.Headers.TryGetValues("Retry-After", out var retryAfterValues)) + { + var retryAfter = retryAfterValues.FirstOrDefault(); + if (!string.IsNullOrEmpty(retryAfter)) + { + if (int.TryParse(retryAfter, out var retryAfterSeconds) && retryAfterSeconds > 0) + { + return Math.Min(retryAfterSeconds * 1000, MaxRetryDelayMs); + } + + if (DateTimeOffset.TryParse(retryAfter, out var retryAfterDate)) + { + var delay = (int)(retryAfterDate - DateTimeOffset.UtcNow).TotalMilliseconds; + if (delay > 0) + { + return Math.Min(delay, MaxRetryDelayMs); + } + } + } + } + + if (response.Headers.TryGetValues("X-RateLimit-Reset", out var rateLimitResetValues)) + { + var rateLimitReset = rateLimitResetValues.FirstOrDefault(); + if ( + !string.IsNullOrEmpty(rateLimitReset) + && long.TryParse(rateLimitReset, out var resetTime) + ) + { + var resetDateTime = DateTimeOffset.FromUnixTimeSeconds(resetTime); + var delay = (int)(resetDateTime - DateTimeOffset.UtcNow).TotalMilliseconds; + if (delay > 0) + { + return AddPositiveJitter(Math.Min(delay, MaxRetryDelayMs)); + } + } + } + + var exponentialDelay = Math.Min(BaseRetryDelay * (1 << retryAttempt), MaxRetryDelayMs); + return AddSymmetricJitter(exponentialDelay); + } + + private static bool IsRetryableContent(HttpRequestMessage request) + { + return request.Content switch + { + IIsRetryableContent c => c.IsRetryable, + StreamContent => false, + MultipartContent content => !content.Any(c => c is StreamContent), + _ => true, + }; + } + + internal async global::System.Threading.Tasks.Task CreateHttpRequestAsync( + global::SeedBasicAuthOptional.Core.BaseRequest request + ) + { + var url = BuildUrl(request); + var httpRequest = new HttpRequestMessage(request.Method, url); + httpRequest.Content = request.CreateContent(); + SetHeaders(httpRequest, request.Headers); + + return httpRequest; + } + + private string BuildUrl(global::SeedBasicAuthOptional.Core.BaseRequest request) + { + var baseUrl = request.Options?.BaseUrl ?? request.BaseUrl ?? Options.BaseUrl; + + var trimmedBaseUrl = baseUrl.TrimEnd('/'); + var trimmedBasePath = request.Path.TrimStart('/'); + var url = $"{trimmedBaseUrl}/{trimmedBasePath}"; + + // Append query string if present + if (!string.IsNullOrEmpty(request.QueryString)) + { + return url + request.QueryString; + } + + return url; + } + + private void SetHeaders(HttpRequestMessage httpRequest, Dictionary? headers) + { + if (headers is null) + { + return; + } + + foreach (var kv in headers) + { + if (kv.Value is null) + { + continue; + } + + httpRequest.Headers.TryAddWithoutValidation(kv.Key, kv.Value); + } + } + + private static (Encoding encoding, string? charset, string mediaType) ParseContentTypeOrDefault( + string? contentType, + Encoding encodingFallback, + string mediaTypeFallback + ) + { + var encoding = encodingFallback; + var mediaType = mediaTypeFallback; + string? charset = null; + if (string.IsNullOrEmpty(contentType)) + { + return (encoding, charset, mediaType); + } + + if (!MediaTypeHeaderValue.TryParse(contentType, out var mediaTypeHeaderValue)) + { + return (encoding, charset, mediaType); + } + + if (!string.IsNullOrEmpty(mediaTypeHeaderValue.CharSet)) + { + charset = mediaTypeHeaderValue.CharSet; + encoding = Encoding.GetEncoding(mediaTypeHeaderValue.CharSet); + } + + if (!string.IsNullOrEmpty(mediaTypeHeaderValue.MediaType)) + { + mediaType = mediaTypeHeaderValue.MediaType; + } + + return (encoding, charset, mediaType); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/RawResponse.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/RawResponse.cs new file mode 100644 index 000000000000..86c0a315bdf0 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/RawResponse.cs @@ -0,0 +1,24 @@ +using global::System.Net; + +namespace SeedBasicAuthOptional.Core; + +/// +/// Contains HTTP response metadata including status code, URL, and headers. +/// +public record RawResponse +{ + /// + /// The HTTP status code of the response. + /// + public required HttpStatusCode StatusCode { get; init; } + + /// + /// The request URL that generated this response. + /// + public required Uri Url { get; init; } + + /// + /// The HTTP response headers. + /// + public required Core.ResponseHeaders Headers { get; init; } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ResponseHeaders.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ResponseHeaders.cs new file mode 100644 index 000000000000..e2c7c2b77595 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ResponseHeaders.cs @@ -0,0 +1,108 @@ +using global::System.Collections; +using global::System.Net.Http.Headers; + +namespace SeedBasicAuthOptional.Core; + +/// +/// Represents HTTP response headers with case-insensitive lookup. +/// +public readonly struct ResponseHeaders : IEnumerable +{ + private readonly HttpResponseHeaders? _headers; + private readonly HttpContentHeaders? _contentHeaders; + + private ResponseHeaders(HttpResponseHeaders headers, HttpContentHeaders? contentHeaders) + { + _headers = headers; + _contentHeaders = contentHeaders; + } + + /// + /// Gets the Content-Type header value, if present. + /// + public string? ContentType => _contentHeaders?.ContentType?.ToString(); + + /// + /// Gets the Content-Length header value, if present. + /// + public long? ContentLength => _contentHeaders?.ContentLength; + + /// + /// Creates a ResponseHeaders instance from an HttpResponseMessage. + /// + public static ResponseHeaders FromHttpResponseMessage(HttpResponseMessage response) + { + return new ResponseHeaders(response.Headers, response.Content?.Headers); + } + + /// + /// Tries to get a single header value. Returns the first value if multiple values exist. + /// + public bool TryGetValue(string name, out string? value) + { + if (TryGetValues(name, out var values) && values is not null) + { + value = values.FirstOrDefault(); + return true; + } + + value = null; + return false; + } + + /// + /// Tries to get all values for a header. + /// + public bool TryGetValues(string name, out IEnumerable? values) + { + if (_headers?.TryGetValues(name, out values) == true) + { + return true; + } + + if (_contentHeaders?.TryGetValues(name, out values) == true) + { + return true; + } + + values = null; + return false; + } + + /// + /// Checks if the headers contain a specific header name. + /// + public bool Contains(string name) + { + return _headers?.Contains(name) == true || _contentHeaders?.Contains(name) == true; + } + + /// + /// Gets an enumerator for all headers. + /// + public IEnumerator GetEnumerator() + { + if (_headers is not null) + { + foreach (var header in _headers) + { + yield return new HttpHeader(header.Key, string.Join(", ", header.Value)); + } + } + + if (_contentHeaders is not null) + { + foreach (var header in _contentHeaders) + { + yield return new HttpHeader(header.Key, string.Join(", ", header.Value)); + } + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); +} + +/// +/// Represents a single HTTP header. +/// +public readonly record struct HttpHeader(string Name, string Value); diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StreamRequest.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StreamRequest.cs new file mode 100644 index 000000000000..6d98922df29b --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StreamRequest.cs @@ -0,0 +1,29 @@ +using global::System.Net.Http; +using global::System.Net.Http.Headers; + +namespace SeedBasicAuthOptional.Core; + +/// +/// The request object to be sent for streaming uploads. +/// +internal record StreamRequest : BaseRequest +{ + internal Stream? Body { get; init; } + + internal override HttpContent? CreateContent() + { + if (Body is null) + { + return null; + } + + var content = new StreamContent(Body) + { + Headers = + { + ContentType = MediaTypeHeaderValue.Parse(ContentType ?? "application/octet-stream"), + }, + }; + return content; + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StringEnum.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StringEnum.cs new file mode 100644 index 000000000000..3d3a3a39d207 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StringEnum.cs @@ -0,0 +1,6 @@ +namespace SeedBasicAuthOptional.Core; + +public interface IStringEnum : IEquatable +{ + public string Value { get; } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StringEnumExtensions.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StringEnumExtensions.cs new file mode 100644 index 000000000000..ec99d9954684 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/StringEnumExtensions.cs @@ -0,0 +1,6 @@ +namespace SeedBasicAuthOptional.Core; + +internal static class StringEnumExtensions +{ + public static string Stringify(this IStringEnum stringEnum) => stringEnum.Value; +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ValueConvert.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ValueConvert.cs new file mode 100644 index 000000000000..0507e04f0e08 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Core/ValueConvert.cs @@ -0,0 +1,114 @@ +using global::System.Globalization; + +namespace SeedBasicAuthOptional.Core; + +/// +/// Convert values to string for path and query parameters. +/// +public static class ValueConvert +{ + internal static string ToPathParameterString(T value) => ToString(value); + + internal static string ToPathParameterString(bool v) => ToString(v); + + internal static string ToPathParameterString(int v) => ToString(v); + + internal static string ToPathParameterString(long v) => ToString(v); + + internal static string ToPathParameterString(float v) => ToString(v); + + internal static string ToPathParameterString(double v) => ToString(v); + + internal static string ToPathParameterString(decimal v) => ToString(v); + + internal static string ToPathParameterString(short v) => ToString(v); + + internal static string ToPathParameterString(ushort v) => ToString(v); + + internal static string ToPathParameterString(uint v) => ToString(v); + + internal static string ToPathParameterString(ulong v) => ToString(v); + + internal static string ToPathParameterString(string v) => ToString(v); + + internal static string ToPathParameterString(char v) => ToString(v); + + internal static string ToPathParameterString(Guid v) => ToString(v); + + internal static string ToQueryStringValue(T value) => value is null ? "" : ToString(value); + + internal static string ToQueryStringValue(bool v) => ToString(v); + + internal static string ToQueryStringValue(int v) => ToString(v); + + internal static string ToQueryStringValue(long v) => ToString(v); + + internal static string ToQueryStringValue(float v) => ToString(v); + + internal static string ToQueryStringValue(double v) => ToString(v); + + internal static string ToQueryStringValue(decimal v) => ToString(v); + + internal static string ToQueryStringValue(short v) => ToString(v); + + internal static string ToQueryStringValue(ushort v) => ToString(v); + + internal static string ToQueryStringValue(uint v) => ToString(v); + + internal static string ToQueryStringValue(ulong v) => ToString(v); + + internal static string ToQueryStringValue(string v) => v is null ? "" : v; + + internal static string ToQueryStringValue(char v) => ToString(v); + + internal static string ToQueryStringValue(Guid v) => ToString(v); + + internal static string ToString(T value) + { + return value switch + { + null => "null", + string str => str, + true => "true", + false => "false", + int i => ToString(i), + long l => ToString(l), + float f => ToString(f), + double d => ToString(d), + decimal dec => ToString(dec), + short s => ToString(s), + ushort u => ToString(u), + uint u => ToString(u), + ulong u => ToString(u), + char c => ToString(c), + Guid guid => ToString(guid), + _ => JsonUtils.SerializeRelaxedEscaping(value, value.GetType()).Trim('"'), + }; + } + + internal static string ToString(bool v) => v ? "true" : "false"; + + internal static string ToString(int v) => v.ToString(CultureInfo.InvariantCulture); + + internal static string ToString(long v) => v.ToString(CultureInfo.InvariantCulture); + + internal static string ToString(float v) => v.ToString(CultureInfo.InvariantCulture); + + internal static string ToString(double v) => v.ToString(CultureInfo.InvariantCulture); + + internal static string ToString(decimal v) => v.ToString(CultureInfo.InvariantCulture); + + internal static string ToString(short v) => v.ToString(CultureInfo.InvariantCulture); + + internal static string ToString(ushort v) => v.ToString(CultureInfo.InvariantCulture); + + internal static string ToString(uint v) => v.ToString(CultureInfo.InvariantCulture); + + internal static string ToString(ulong v) => v.ToString(CultureInfo.InvariantCulture); + + internal static string ToString(char v) => v.ToString(CultureInfo.InvariantCulture); + + internal static string ToString(string v) => v; + + internal static string ToString(Guid v) => v.ToString("D"); +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Exceptions/BadRequest.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Exceptions/BadRequest.cs new file mode 100644 index 000000000000..202ed74919a4 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Exceptions/BadRequest.cs @@ -0,0 +1,7 @@ +namespace SeedBasicAuthOptional; + +/// +/// This exception type will be thrown for any non-2XX API responses. +/// +[Serializable] +public class BadRequest(object body) : SeedBasicAuthOptionalApiException("BadRequest", 400, body); diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Exceptions/UnauthorizedRequest.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Exceptions/UnauthorizedRequest.cs new file mode 100644 index 000000000000..3cb3690f6b3a --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Exceptions/UnauthorizedRequest.cs @@ -0,0 +1,14 @@ +namespace SeedBasicAuthOptional; + +/// +/// This exception type will be thrown for any non-2XX API responses. +/// +[Serializable] +public class UnauthorizedRequest(UnauthorizedRequestErrorBody body) + : SeedBasicAuthOptionalApiException("UnauthorizedRequest", 401, body) +{ + /// + /// The body of the response that triggered the exception. + /// + public new UnauthorizedRequestErrorBody Body => body; +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Types/UnauthorizedRequestErrorBody.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Types/UnauthorizedRequestErrorBody.cs new file mode 100644 index 000000000000..038ffe868775 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/Errors/Types/UnauthorizedRequestErrorBody.cs @@ -0,0 +1,28 @@ +using global::System.Text.Json; +using global::System.Text.Json.Serialization; +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional; + +[Serializable] +public record UnauthorizedRequestErrorBody : IJsonOnDeserialized +{ + [JsonExtensionData] + private readonly IDictionary _extensionData = + new Dictionary(); + + [JsonPropertyName("message")] + public required string Message { get; set; } + + [JsonIgnore] + public ReadOnlyAdditionalProperties AdditionalProperties { get; private set; } = new(); + + void IJsonOnDeserialized.OnDeserialized() => + AdditionalProperties.CopyFromExtensionData(_extensionData); + + /// + public override string ToString() + { + return JsonUtils.Serialize(this); + } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/ISeedBasicAuthOptionalClient.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/ISeedBasicAuthOptionalClient.cs new file mode 100644 index 000000000000..cd6ea32ea6b2 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/ISeedBasicAuthOptionalClient.cs @@ -0,0 +1,6 @@ +namespace SeedBasicAuthOptional; + +public partial interface ISeedBasicAuthOptionalClient +{ + public IBasicAuthClient BasicAuth { get; } +} diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptional.Custom.props b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptional.Custom.props new file mode 100644 index 000000000000..17a84cada530 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptional.Custom.props @@ -0,0 +1,20 @@ + + + + diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptional.csproj b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptional.csproj new file mode 100644 index 000000000000..51c362c8292d --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptional.csproj @@ -0,0 +1,63 @@ + + + net462;net8.0;net9.0;netstandard2.0 + enable + 12 + enable + 0.0.1 + $(Version) + $(Version) + README.md + https://github.com/basic-auth-optional/fern + true + + + + false + + + $(DefineConstants);USE_PORTABLE_DATE_ONLY + true + + + + + + + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + + + <_Parameter1>SeedBasicAuthOptional.Test + + + + + diff --git a/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptionalClient.cs b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptionalClient.cs new file mode 100644 index 000000000000..3f4f6ee0f7d2 --- /dev/null +++ b/seed/csharp-sdk/basic-auth-optional/src/SeedBasicAuthOptional/SeedBasicAuthOptionalClient.cs @@ -0,0 +1,40 @@ +using SeedBasicAuthOptional.Core; + +namespace SeedBasicAuthOptional; + +public partial class SeedBasicAuthOptionalClient : ISeedBasicAuthOptionalClient +{ + private readonly RawClient _client; + + public SeedBasicAuthOptionalClient( + string? username = null, + string? password = null, + ClientOptions? clientOptions = null + ) + { + clientOptions ??= new ClientOptions(); + var platformHeaders = new Headers( + new Dictionary() + { + { "X-Fern-Language", "C#" }, + { "X-Fern-SDK-Name", "SeedBasicAuthOptional" }, + { "X-Fern-SDK-Version", Version.Current }, + { "User-Agent", "Fernbasic-auth-optional/0.0.1" }, + } + ); + foreach (var header in platformHeaders) + { + if (!clientOptions.Headers.ContainsKey(header.Key)) + { + clientOptions.Headers[header.Key] = header.Value; + } + } + var clientOptionsWithAuth = clientOptions.Clone(); + clientOptionsWithAuth.Headers["Authorization"] = + $"Basic {Convert.ToBase64String(global::System.Text.Encoding.UTF8.GetBytes($"{username ?? ""}:{password ?? ""}"))}"; + _client = new RawClient(clientOptionsWithAuth); + BasicAuth = new BasicAuthClient(_client); + } + + public IBasicAuthClient BasicAuth { get; } +} diff --git a/seed/go-sdk/basic-auth-optional/.fern/metadata.json b/seed/go-sdk/basic-auth-optional/.fern/metadata.json new file mode 100644 index 000000000000..1e0f7d8e54e4 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/.fern/metadata.json @@ -0,0 +1,10 @@ +{ + "cliVersion": "DUMMY", + "generatorName": "fernapi/fern-go-sdk", + "generatorVersion": "latest", + "generatorConfig": { + "enableWireTests": false + }, + "originGitCommit": "DUMMY", + "sdkVersion": "v0.0.1" +} \ No newline at end of file diff --git a/seed/go-sdk/basic-auth-optional/.github/workflows/ci.yml b/seed/go-sdk/basic-auth-optional/.github/workflows/ci.yml new file mode 100644 index 000000000000..1097e6a18acc --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/.github/workflows/ci.yml @@ -0,0 +1,62 @@ +name: ci + +on: [push] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + compile: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Set up go + uses: actions/setup-go@v4 + + - name: Compile + run: go build ./... + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Set up go + uses: actions/setup-go@v4 + + - name: Lint + uses: golangci/golangci-lint-action@v9 + with: + version: v2.10.1 + test: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Set up go + uses: actions/setup-go@v4 + + - name: Setup wiremock server + run: | + PROJECT_NAME="wiremock-$(basename $(dirname $(pwd)) | tr -d '.')" + echo "PROJECT_NAME=$PROJECT_NAME" >> $GITHUB_ENV + if [ -f wiremock/docker-compose.test.yml ]; then + docker compose -p "$PROJECT_NAME" -f wiremock/docker-compose.test.yml down + docker compose -p "$PROJECT_NAME" -f wiremock/docker-compose.test.yml up -d + WIREMOCK_PORT=$(docker compose -p "$PROJECT_NAME" -f wiremock/docker-compose.test.yml port wiremock 8080 | cut -d: -f2) + echo "WIREMOCK_URL=http://localhost:$WIREMOCK_PORT" >> $GITHUB_ENV + fi + + - name: Test + run: go test ./... + + - name: Teardown wiremock server + if: always() + run: | + if [ -f wiremock/docker-compose.test.yml ]; then + docker compose -p "$PROJECT_NAME" -f wiremock/docker-compose.test.yml down + fi diff --git a/seed/go-sdk/basic-auth-optional/README.md b/seed/go-sdk/basic-auth-optional/README.md new file mode 100644 index 000000000000..38f9aa12a7b8 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/README.md @@ -0,0 +1,199 @@ +# Seed Go Library + +[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=Seed%2FGo) + +The Seed Go library provides convenient access to the Seed APIs from Go. + +## Table of Contents + +- [Reference](#reference) +- [Usage](#usage) +- [Environments](#environments) +- [Errors](#errors) +- [Request Options](#request-options) +- [Advanced](#advanced) + - [Response Headers](#response-headers) + - [Retries](#retries) + - [Timeouts](#timeouts) + - [Explicit Null](#explicit-null) +- [Contributing](#contributing) + +## Reference + +A full reference for this library is available [here](./reference.md). + +## Usage + +Instantiate and use the client with the following: + +```go +package example + +import ( + context "context" + + client "github.com/basic-auth-optional/fern/client" + option "github.com/basic-auth-optional/fern/option" +) + +func do() { + client := client.NewClient( + option.WithBasicAuth( + "", + "", + ), + ) + request := map[string]any{ + "key": "value", + } + client.BasicAuth.PostWithBasicAuth( + context.TODO(), + request, + ) +} +``` + +## Environments + +You can choose between different environments by using the `option.WithBaseURL` option. You can configure any arbitrary base +URL, which is particularly useful in test environments. + +```go +client := client.NewClient( + option.WithBaseURL("https://example.com"), +) +``` + +## Errors + +Structured error types are returned from API calls that return non-success status codes. These errors are compatible +with the `errors.Is` and `errors.As` APIs, so you can access the error like so: + +```go +response, err := client.BasicAuth.PostWithBasicAuth(...) +if err != nil { + var apiError *core.APIError + if errors.As(err, apiError) { + // Do something with the API error ... + } + return err +} +``` + +## Request Options + +A variety of request options are included to adapt the behavior of the library, which includes configuring +authorization tokens, or providing your own instrumented `*http.Client`. + +These request options can either be +specified on the client so that they're applied on every request, or for an individual request, like so: + +> Providing your own `*http.Client` is recommended. Otherwise, the `http.DefaultClient` will be used, +> and your client will wait indefinitely for a response (unless the per-request, context-based timeout +> is used). + +```go +// Specify default options applied on every request. +client := client.NewClient( + option.WithToken(""), + option.WithHTTPClient( + &http.Client{ + Timeout: 5 * time.Second, + }, + ), +) + +// Specify options for an individual request. +response, err := client.BasicAuth.PostWithBasicAuth( + ..., + option.WithToken(""), +) +``` + +## Advanced + +### Response Headers + +You can access the raw HTTP response data by using the `WithRawResponse` field on the client. This is useful +when you need to examine the response headers received from the API call. (When the endpoint is paginated, +the raw HTTP response data will be included automatically in the Page response object.) + +```go +response, err := client.BasicAuth.WithRawResponse.PostWithBasicAuth(...) +if err != nil { + return err +} +fmt.Printf("Got response headers: %v", response.Header) +fmt.Printf("Got status code: %d", response.StatusCode) +``` + +### Retries + +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured +retry limit (default: 2). + +A request is deemed retryable when any of the following HTTP status codes is returned: + +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) + +If the `Retry-After` header is present in the response, the SDK will prioritize respecting its value exactly +over the default exponential backoff. + +Use the `option.WithMaxAttempts` option to configure this behavior for the entire client or an individual request: + +```go +client := client.NewClient( + option.WithMaxAttempts(1), +) + +response, err := client.BasicAuth.PostWithBasicAuth( + ..., + option.WithMaxAttempts(1), +) +``` + +### Timeouts + +Setting a timeout for each individual request is as simple as using the standard context library. Setting a one second timeout for an individual API call looks like the following: + +```go +ctx, cancel := context.WithTimeout(ctx, time.Second) +defer cancel() + +response, err := client.BasicAuth.PostWithBasicAuth(ctx, ...) +``` + +### Explicit Null + +If you want to send the explicit `null` JSON value through an optional parameter, you can use the setters\ +that come with every object. Calling a setter method for a property will flip a bit in the `explicitFields` +bitfield for that setter's object; during serialization, any property with a flipped bit will have its +omittable status stripped, so zero or `nil` values will be sent explicitly rather than omitted altogether: + +```go +type ExampleRequest struct { + // An optional string parameter. + Name *string `json:"name,omitempty" url:"-"` + + // Private bitmask of fields set to an explicit value and therefore not to be omitted + explicitFields *big.Int `json:"-" url:"-"` +} + +request := &ExampleRequest{} +request.SetName(nil) + +response, err := client.BasicAuth.PostWithBasicAuth(ctx, request, ...) +``` + +## Contributing + +While we value open-source contributions to this SDK, this library is generated programmatically. +Additions made directly to this library would have to be moved over to our generation code, +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening +an issue first to discuss with us! + +On the other hand, contributions to the README are always very welcome! diff --git a/seed/go-sdk/basic-auth-optional/basicauth/client.go b/seed/go-sdk/basic-auth-optional/basicauth/client.go new file mode 100644 index 000000000000..f31be0b83313 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/basicauth/client.go @@ -0,0 +1,65 @@ +// Code generated by Fern. DO NOT EDIT. + +package basicauth + +import ( + context "context" + + core "github.com/basic-auth-optional/fern/core" + internal "github.com/basic-auth-optional/fern/internal" + option "github.com/basic-auth-optional/fern/option" +) + +type Client struct { + WithRawResponse *RawClient + + options *core.RequestOptions + baseURL string + caller *internal.Caller +} + +func NewClient(options *core.RequestOptions) *Client { + return &Client{ + WithRawResponse: NewRawClient(options), + options: options, + baseURL: options.BaseURL, + caller: internal.NewCaller( + &internal.CallerParams{ + Client: options.HTTPClient, + MaxAttempts: options.MaxAttempts, + }, + ), + } +} + +// GET request with basic auth scheme +func (c *Client) GetWithBasicAuth( + ctx context.Context, + opts ...option.RequestOption, +) (bool, error) { + response, err := c.WithRawResponse.GetWithBasicAuth( + ctx, + opts..., + ) + if err != nil { + return false, err + } + return response.Body, nil +} + +// POST request with basic auth scheme +func (c *Client) PostWithBasicAuth( + ctx context.Context, + request any, + opts ...option.RequestOption, +) (bool, error) { + response, err := c.WithRawResponse.PostWithBasicAuth( + ctx, + request, + opts..., + ) + if err != nil { + return false, err + } + return response.Body, nil +} diff --git a/seed/go-sdk/basic-auth-optional/basicauth/raw_client.go b/seed/go-sdk/basic-auth-optional/basicauth/raw_client.go new file mode 100644 index 000000000000..b4aae98cd3e8 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/basicauth/raw_client.go @@ -0,0 +1,114 @@ +// Code generated by Fern. DO NOT EDIT. + +package basicauth + +import ( + context "context" + http "net/http" + + fern "github.com/basic-auth-optional/fern" + core "github.com/basic-auth-optional/fern/core" + internal "github.com/basic-auth-optional/fern/internal" + option "github.com/basic-auth-optional/fern/option" +) + +type RawClient struct { + baseURL string + caller *internal.Caller + options *core.RequestOptions +} + +func NewRawClient(options *core.RequestOptions) *RawClient { + return &RawClient{ + options: options, + baseURL: options.BaseURL, + caller: internal.NewCaller( + &internal.CallerParams{ + Client: options.HTTPClient, + MaxAttempts: options.MaxAttempts, + }, + ), + } +} + +func (r *RawClient) GetWithBasicAuth( + ctx context.Context, + opts ...option.RequestOption, +) (*core.Response[bool], error) { + options := core.NewRequestOptions(opts...) + baseURL := internal.ResolveBaseURL( + options.BaseURL, + r.baseURL, + "", + ) + endpointURL := baseURL + "/basic-auth" + headers := internal.MergeHeaders( + r.options.ToHeader(), + options.ToHeader(), + ) + var response bool + raw, err := r.caller.Call( + ctx, + &internal.CallParams{ + URL: endpointURL, + Method: http.MethodGet, + Headers: headers, + MaxAttempts: options.MaxAttempts, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Response: &response, + ErrorDecoder: internal.NewErrorDecoder(fern.ErrorCodes), + }, + ) + if err != nil { + return nil, err + } + return &core.Response[bool]{ + StatusCode: raw.StatusCode, + Header: raw.Header, + Body: response, + }, nil +} + +func (r *RawClient) PostWithBasicAuth( + ctx context.Context, + request any, + opts ...option.RequestOption, +) (*core.Response[bool], error) { + options := core.NewRequestOptions(opts...) + baseURL := internal.ResolveBaseURL( + options.BaseURL, + r.baseURL, + "", + ) + endpointURL := baseURL + "/basic-auth" + headers := internal.MergeHeaders( + r.options.ToHeader(), + options.ToHeader(), + ) + var response bool + raw, err := r.caller.Call( + ctx, + &internal.CallParams{ + URL: endpointURL, + Method: http.MethodPost, + Headers: headers, + MaxAttempts: options.MaxAttempts, + BodyProperties: options.BodyProperties, + QueryParameters: options.QueryParameters, + Client: options.HTTPClient, + Request: request, + Response: &response, + ErrorDecoder: internal.NewErrorDecoder(fern.ErrorCodes), + }, + ) + if err != nil { + return nil, err + } + return &core.Response[bool]{ + StatusCode: raw.StatusCode, + Header: raw.Header, + Body: response, + }, nil +} diff --git a/seed/go-sdk/basic-auth-optional/client/client.go b/seed/go-sdk/basic-auth-optional/client/client.go new file mode 100644 index 000000000000..ad934300ce9a --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/client/client.go @@ -0,0 +1,33 @@ +// Code generated by Fern. DO NOT EDIT. + +package client + +import ( + basicauth "github.com/basic-auth-optional/fern/basicauth" + core "github.com/basic-auth-optional/fern/core" + internal "github.com/basic-auth-optional/fern/internal" + option "github.com/basic-auth-optional/fern/option" +) + +type Client struct { + BasicAuth *basicauth.Client + + options *core.RequestOptions + baseURL string + caller *internal.Caller +} + +func NewClient(opts ...option.RequestOption) *Client { + options := core.NewRequestOptions(opts...) + return &Client{ + BasicAuth: basicauth.NewClient(options), + options: options, + baseURL: options.BaseURL, + caller: internal.NewCaller( + &internal.CallerParams{ + Client: options.HTTPClient, + MaxAttempts: options.MaxAttempts, + }, + ), + } +} diff --git a/seed/go-sdk/basic-auth-optional/client/client_test.go b/seed/go-sdk/basic-auth-optional/client/client_test.go new file mode 100644 index 000000000000..a67e052e5add --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/client/client_test.go @@ -0,0 +1,45 @@ +// Code generated by Fern. DO NOT EDIT. + +package client + +import ( + option "github.com/basic-auth-optional/fern/option" + assert "github.com/stretchr/testify/assert" + http "net/http" + testing "testing" + time "time" +) + +func TestNewClient(t *testing.T) { + t.Run("default", func(t *testing.T) { + c := NewClient() + assert.Empty(t, c.baseURL) + }) + + t.Run("base url", func(t *testing.T) { + c := NewClient( + option.WithBaseURL("test.co"), + ) + assert.Equal(t, "test.co", c.baseURL) + }) + + t.Run("http client", func(t *testing.T) { + httpClient := &http.Client{ + Timeout: 5 * time.Second, + } + c := NewClient( + option.WithHTTPClient(httpClient), + ) + assert.Empty(t, c.baseURL) + }) + + t.Run("http header", func(t *testing.T) { + header := make(http.Header) + header.Set("X-API-Tenancy", "test") + c := NewClient( + option.WithHTTPHeader(header), + ) + assert.Empty(t, c.baseURL) + assert.Equal(t, "test", c.options.HTTPHeader.Get("X-API-Tenancy")) + }) +} diff --git a/seed/go-sdk/basic-auth-optional/core/api_error.go b/seed/go-sdk/basic-auth-optional/core/api_error.go new file mode 100644 index 000000000000..6168388541b4 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/core/api_error.go @@ -0,0 +1,47 @@ +package core + +import ( + "fmt" + "net/http" +) + +// APIError is a lightweight wrapper around the standard error +// interface that preserves the status code from the RPC, if any. +type APIError struct { + err error + + StatusCode int `json:"-"` + Header http.Header `json:"-"` +} + +// NewAPIError constructs a new API error. +func NewAPIError(statusCode int, header http.Header, err error) *APIError { + return &APIError{ + err: err, + Header: header, + StatusCode: statusCode, + } +} + +// Unwrap returns the underlying error. This also makes the error compatible +// with errors.As and errors.Is. +func (a *APIError) Unwrap() error { + if a == nil { + return nil + } + return a.err +} + +// Error returns the API error's message. +func (a *APIError) Error() string { + if a == nil || (a.err == nil && a.StatusCode == 0) { + return "" + } + if a.err == nil { + return fmt.Sprintf("%d", a.StatusCode) + } + if a.StatusCode == 0 { + return a.err.Error() + } + return fmt.Sprintf("%d: %s", a.StatusCode, a.err.Error()) +} diff --git a/seed/go-sdk/basic-auth-optional/core/http.go b/seed/go-sdk/basic-auth-optional/core/http.go new file mode 100644 index 000000000000..92c435692940 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/core/http.go @@ -0,0 +1,15 @@ +package core + +import "net/http" + +// HTTPClient is an interface for a subset of the *http.Client. +type HTTPClient interface { + Do(*http.Request) (*http.Response, error) +} + +// Response is an HTTP response from an HTTP client. +type Response[T any] struct { + StatusCode int + Header http.Header + Body T +} diff --git a/seed/go-sdk/basic-auth-optional/core/request_option.go b/seed/go-sdk/basic-auth-optional/core/request_option.go new file mode 100644 index 000000000000..0b0f3c2155c2 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/core/request_option.go @@ -0,0 +1,139 @@ +// Code generated by Fern. DO NOT EDIT. + +package core + +import ( + base64 "encoding/base64" + http "net/http" + url "net/url" +) + +// RequestOption adapts the behavior of the client or an individual request. +type RequestOption interface { + applyRequestOptions(*RequestOptions) +} + +// RequestOptions defines all of the possible request options. +// +// This type is primarily used by the generated code and is not meant +// to be used directly; use the option package instead. +type RequestOptions struct { + BaseURL string + HTTPClient HTTPClient + HTTPHeader http.Header + BodyProperties map[string]interface{} + QueryParameters url.Values + MaxAttempts uint + MaxBufSize int + Username string + Password string +} + +// NewRequestOptions returns a new *RequestOptions value. +// +// This function is primarily used by the generated code and is not meant +// to be used directly; use RequestOption instead. +func NewRequestOptions(opts ...RequestOption) *RequestOptions { + options := &RequestOptions{ + HTTPHeader: make(http.Header), + BodyProperties: make(map[string]interface{}), + QueryParameters: make(url.Values), + } + for _, opt := range opts { + opt.applyRequestOptions(options) + } + return options +} + +// ToHeader maps the configured request options into a http.Header used +// for the request(s). +func (r *RequestOptions) ToHeader() http.Header { + header := r.cloneHeader() + if r.Username != "" || r.Password != "" { + header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(r.Username+":"+r.Password))) + } + return header +} + +func (r *RequestOptions) cloneHeader() http.Header { + headers := r.HTTPHeader.Clone() + headers.Set("X-Fern-Language", "Go") + headers.Set("X-Fern-SDK-Name", "github.com/basic-auth-optional/fern") + headers.Set("X-Fern-SDK-Version", "v0.0.1") + headers.Set("User-Agent", "github.com/basic-auth-optional/fern/0.0.1") + return headers +} + +// BaseURLOption implements the RequestOption interface. +type BaseURLOption struct { + BaseURL string +} + +func (b *BaseURLOption) applyRequestOptions(opts *RequestOptions) { + opts.BaseURL = b.BaseURL +} + +// HTTPClientOption implements the RequestOption interface. +type HTTPClientOption struct { + HTTPClient HTTPClient +} + +func (h *HTTPClientOption) applyRequestOptions(opts *RequestOptions) { + opts.HTTPClient = h.HTTPClient +} + +// HTTPHeaderOption implements the RequestOption interface. +type HTTPHeaderOption struct { + HTTPHeader http.Header +} + +func (h *HTTPHeaderOption) applyRequestOptions(opts *RequestOptions) { + opts.HTTPHeader = h.HTTPHeader +} + +// BodyPropertiesOption implements the RequestOption interface. +type BodyPropertiesOption struct { + BodyProperties map[string]interface{} +} + +func (b *BodyPropertiesOption) applyRequestOptions(opts *RequestOptions) { + opts.BodyProperties = b.BodyProperties +} + +// QueryParametersOption implements the RequestOption interface. +type QueryParametersOption struct { + QueryParameters url.Values +} + +func (q *QueryParametersOption) applyRequestOptions(opts *RequestOptions) { + opts.QueryParameters = q.QueryParameters +} + +// MaxAttemptsOption implements the RequestOption interface. +type MaxAttemptsOption struct { + MaxAttempts uint +} + +func (m *MaxAttemptsOption) applyRequestOptions(opts *RequestOptions) { + opts.MaxAttempts = m.MaxAttempts +} + +// MaxBufSizeOption implements the RequestOption interface. +type MaxBufSizeOption struct { + MaxBufSize int +} + +func (m *MaxBufSizeOption) applyRequestOptions(opts *RequestOptions) { + opts.MaxBufSize = m.MaxBufSize +} + +// BasicAuthOption implements the RequestOption interface. +type BasicAuthOption struct { + Username string + Password string +} + +func (b *BasicAuthOption) applyRequestOptions(opts *RequestOptions) { + opts.Username = b.Username + opts.Password = b.Password +} diff --git a/seed/go-sdk/basic-auth-optional/dynamic-snippets/example0/snippet.go b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example0/snippet.go new file mode 100644 index 000000000000..3cf23a6c80a0 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example0/snippet.go @@ -0,0 +1,23 @@ +package example + +import ( + context "context" + + client "github.com/basic-auth-optional/fern/client" + option "github.com/basic-auth-optional/fern/option" +) + +func do() { + client := client.NewClient( + option.WithBaseURL( + "https://api.fern.com", + ), + option.WithBasicAuth( + "", + "", + ), + ) + client.BasicAuth.GetWithBasicAuth( + context.TODO(), + ) +} diff --git a/seed/go-sdk/basic-auth-optional/dynamic-snippets/example1/snippet.go b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example1/snippet.go new file mode 100644 index 000000000000..3cf23a6c80a0 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example1/snippet.go @@ -0,0 +1,23 @@ +package example + +import ( + context "context" + + client "github.com/basic-auth-optional/fern/client" + option "github.com/basic-auth-optional/fern/option" +) + +func do() { + client := client.NewClient( + option.WithBaseURL( + "https://api.fern.com", + ), + option.WithBasicAuth( + "", + "", + ), + ) + client.BasicAuth.GetWithBasicAuth( + context.TODO(), + ) +} diff --git a/seed/go-sdk/basic-auth-optional/dynamic-snippets/example2/snippet.go b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example2/snippet.go new file mode 100644 index 000000000000..3cf23a6c80a0 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example2/snippet.go @@ -0,0 +1,23 @@ +package example + +import ( + context "context" + + client "github.com/basic-auth-optional/fern/client" + option "github.com/basic-auth-optional/fern/option" +) + +func do() { + client := client.NewClient( + option.WithBaseURL( + "https://api.fern.com", + ), + option.WithBasicAuth( + "", + "", + ), + ) + client.BasicAuth.GetWithBasicAuth( + context.TODO(), + ) +} diff --git a/seed/go-sdk/basic-auth-optional/dynamic-snippets/example3/snippet.go b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example3/snippet.go new file mode 100644 index 000000000000..e873a933abb8 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example3/snippet.go @@ -0,0 +1,27 @@ +package example + +import ( + context "context" + + client "github.com/basic-auth-optional/fern/client" + option "github.com/basic-auth-optional/fern/option" +) + +func do() { + client := client.NewClient( + option.WithBaseURL( + "https://api.fern.com", + ), + option.WithBasicAuth( + "", + "", + ), + ) + request := map[string]any{ + "key": "value", + } + client.BasicAuth.PostWithBasicAuth( + context.TODO(), + request, + ) +} diff --git a/seed/go-sdk/basic-auth-optional/dynamic-snippets/example4/snippet.go b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example4/snippet.go new file mode 100644 index 000000000000..e873a933abb8 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example4/snippet.go @@ -0,0 +1,27 @@ +package example + +import ( + context "context" + + client "github.com/basic-auth-optional/fern/client" + option "github.com/basic-auth-optional/fern/option" +) + +func do() { + client := client.NewClient( + option.WithBaseURL( + "https://api.fern.com", + ), + option.WithBasicAuth( + "", + "", + ), + ) + request := map[string]any{ + "key": "value", + } + client.BasicAuth.PostWithBasicAuth( + context.TODO(), + request, + ) +} diff --git a/seed/go-sdk/basic-auth-optional/dynamic-snippets/example5/snippet.go b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example5/snippet.go new file mode 100644 index 000000000000..e873a933abb8 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example5/snippet.go @@ -0,0 +1,27 @@ +package example + +import ( + context "context" + + client "github.com/basic-auth-optional/fern/client" + option "github.com/basic-auth-optional/fern/option" +) + +func do() { + client := client.NewClient( + option.WithBaseURL( + "https://api.fern.com", + ), + option.WithBasicAuth( + "", + "", + ), + ) + request := map[string]any{ + "key": "value", + } + client.BasicAuth.PostWithBasicAuth( + context.TODO(), + request, + ) +} diff --git a/seed/go-sdk/basic-auth-optional/dynamic-snippets/example6/snippet.go b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example6/snippet.go new file mode 100644 index 000000000000..e873a933abb8 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/dynamic-snippets/example6/snippet.go @@ -0,0 +1,27 @@ +package example + +import ( + context "context" + + client "github.com/basic-auth-optional/fern/client" + option "github.com/basic-auth-optional/fern/option" +) + +func do() { + client := client.NewClient( + option.WithBaseURL( + "https://api.fern.com", + ), + option.WithBasicAuth( + "", + "", + ), + ) + request := map[string]any{ + "key": "value", + } + client.BasicAuth.PostWithBasicAuth( + context.TODO(), + request, + ) +} diff --git a/seed/go-sdk/basic-auth-optional/error_codes.go b/seed/go-sdk/basic-auth-optional/error_codes.go new file mode 100644 index 000000000000..a3e89f4ec571 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/error_codes.go @@ -0,0 +1,21 @@ +// Code generated by Fern. DO NOT EDIT. + +package basicauthoptional + +import ( + core "github.com/basic-auth-optional/fern/core" + internal "github.com/basic-auth-optional/fern/internal" +) + +var ErrorCodes internal.ErrorCodes = internal.ErrorCodes{ + 401: func(apiError *core.APIError) error { + return &UnauthorizedRequest{ + APIError: apiError, + } + }, + 400: func(apiError *core.APIError) error { + return &BadRequest{ + APIError: apiError, + } + }, +} diff --git a/seed/go-sdk/basic-auth-optional/errors.go b/seed/go-sdk/basic-auth-optional/errors.go new file mode 100644 index 000000000000..cf0fe91ad9fe --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/errors.go @@ -0,0 +1,44 @@ +// Code generated by Fern. DO NOT EDIT. + +package basicauthoptional + +import ( + json "encoding/json" + core "github.com/basic-auth-optional/fern/core" +) + +type BadRequest struct { + *core.APIError +} + +func (b *BadRequest) UnmarshalJSON(data []byte) error { + b.StatusCode = 400 + return nil +} + +func (b *BadRequest) MarshalJSON() ([]byte, error) { + return nil, nil +} + +type UnauthorizedRequest struct { + *core.APIError + Body *UnauthorizedRequestErrorBody +} + +func (u *UnauthorizedRequest) UnmarshalJSON(data []byte) error { + var body *UnauthorizedRequestErrorBody + if err := json.Unmarshal(data, &body); err != nil { + return err + } + u.StatusCode = 401 + u.Body = body + return nil +} + +func (u *UnauthorizedRequest) MarshalJSON() ([]byte, error) { + return json.Marshal(u.Body) +} + +func (u *UnauthorizedRequest) Unwrap() error { + return u.APIError +} diff --git a/seed/go-sdk/basic-auth-optional/file_param.go b/seed/go-sdk/basic-auth-optional/file_param.go new file mode 100644 index 000000000000..ee41d1ece30b --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/file_param.go @@ -0,0 +1,41 @@ +package basicauthoptional + +import ( + "io" +) + +// FileParam is a file type suitable for multipart/form-data uploads. +type FileParam struct { + io.Reader + filename string + contentType string +} + +// FileParamOption adapts the behavior of the FileParam. No options are +// implemented yet, but this interface allows for future extensibility. +type FileParamOption interface { + apply() +} + +// NewFileParam returns a *FileParam type suitable for multipart/form-data uploads. All file +// upload endpoints accept a simple io.Reader, which is usually created by opening a file +// via os.Open. +// +// However, some endpoints require additional metadata about the file such as a specific +// Content-Type or custom filename. FileParam makes it easier to create the correct type +// signature for these endpoints. +func NewFileParam( + reader io.Reader, + filename string, + contentType string, + opts ...FileParamOption, +) *FileParam { + return &FileParam{ + Reader: reader, + filename: filename, + contentType: contentType, + } +} + +func (f *FileParam) Name() string { return f.filename } +func (f *FileParam) ContentType() string { return f.contentType } diff --git a/seed/go-sdk/basic-auth-optional/go.mod b/seed/go-sdk/basic-auth-optional/go.mod new file mode 100644 index 000000000000..8c580a08cdd9 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/go.mod @@ -0,0 +1,16 @@ +module github.com/basic-auth-optional/fern + +go 1.21 + +toolchain go1.23.8 + +require github.com/google/uuid v1.6.0 + +require github.com/stretchr/testify v1.8.4 + +require gopkg.in/yaml.v3 v3.0.1 // indirect + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect +) diff --git a/seed/go-sdk/basic-auth-optional/go.sum b/seed/go-sdk/basic-auth-optional/go.sum new file mode 100644 index 000000000000..fcca6d128057 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/seed/go-sdk/basic-auth-optional/internal/caller.go b/seed/go-sdk/basic-auth-optional/internal/caller.go new file mode 100644 index 000000000000..665f4ecc2f50 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/caller.go @@ -0,0 +1,311 @@ +package internal + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "reflect" + "strings" + + "github.com/basic-auth-optional/fern/core" +) + +const ( + // contentType specifies the JSON Content-Type header value. + contentType = "application/json" + contentTypeHeader = "Content-Type" + contentTypeFormURLEncoded = "application/x-www-form-urlencoded" +) + +// Caller calls APIs and deserializes their response, if any. +type Caller struct { + client core.HTTPClient + retrier *Retrier +} + +// CallerParams represents the parameters used to constrcut a new *Caller. +type CallerParams struct { + Client core.HTTPClient + MaxAttempts uint +} + +// NewCaller returns a new *Caller backed by the given parameters. +func NewCaller(params *CallerParams) *Caller { + var httpClient core.HTTPClient = http.DefaultClient + if params.Client != nil { + httpClient = params.Client + } + var retryOptions []RetryOption + if params.MaxAttempts > 0 { + retryOptions = append(retryOptions, WithMaxAttempts(params.MaxAttempts)) + } + return &Caller{ + client: httpClient, + retrier: NewRetrier(retryOptions...), + } +} + +// CallParams represents the parameters used to issue an API call. +type CallParams struct { + URL string + Method string + MaxAttempts uint + Headers http.Header + BodyProperties map[string]interface{} + QueryParameters url.Values + Client core.HTTPClient + Request interface{} + Response interface{} + ResponseIsOptional bool + ErrorDecoder ErrorDecoder +} + +// CallResponse is a parsed HTTP response from an API call. +type CallResponse struct { + StatusCode int + Header http.Header +} + +// Call issues an API call according to the given call parameters. +func (c *Caller) Call(ctx context.Context, params *CallParams) (*CallResponse, error) { + url := buildURL(params.URL, params.QueryParameters) + req, err := newRequest( + ctx, + url, + params.Method, + params.Headers, + params.Request, + params.BodyProperties, + ) + if err != nil { + return nil, err + } + + // If the call has been cancelled, don't issue the request. + if err := ctx.Err(); err != nil { + return nil, err + } + + client := c.client + if params.Client != nil { + // Use the HTTP client scoped to the request. + client = params.Client + } + + var retryOptions []RetryOption + if params.MaxAttempts > 0 { + retryOptions = append(retryOptions, WithMaxAttempts(params.MaxAttempts)) + } + + resp, err := c.retrier.Run( + client.Do, + req, + params.ErrorDecoder, + retryOptions..., + ) + if err != nil { + return nil, err + } + + // Close the response body after we're done. + defer func() { _ = resp.Body.Close() }() + + // Check if the call was cancelled before we return the error + // associated with the call and/or unmarshal the response data. + if err := ctx.Err(); err != nil { + return nil, err + } + + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return nil, decodeError(resp, params.ErrorDecoder) + } + + // Mutate the response parameter in-place. + if params.Response != nil { + if writer, ok := params.Response.(io.Writer); ok { + _, err = io.Copy(writer, resp.Body) + } else { + err = json.NewDecoder(resp.Body).Decode(params.Response) + } + if err != nil { + if err == io.EOF { + if params.ResponseIsOptional { + // The response is optional, so we should ignore the + // io.EOF error + return &CallResponse{ + StatusCode: resp.StatusCode, + Header: resp.Header, + }, nil + } + return nil, fmt.Errorf("expected a %T response, but the server responded with nothing", params.Response) + } + return nil, err + } + } + + return &CallResponse{ + StatusCode: resp.StatusCode, + Header: resp.Header, + }, nil +} + +// buildURL constructs the final URL by appending the given query parameters (if any). +func buildURL( + url string, + queryParameters url.Values, +) string { + if len(queryParameters) == 0 { + return url + } + if strings.ContainsRune(url, '?') { + url += "&" + } else { + url += "?" + } + url += queryParameters.Encode() + return url +} + +// newRequest returns a new *http.Request with all of the fields +// required to issue the call. +func newRequest( + ctx context.Context, + url string, + method string, + endpointHeaders http.Header, + request interface{}, + bodyProperties map[string]interface{}, +) (*http.Request, error) { + // Determine the content type from headers, defaulting to JSON. + reqContentType := contentType + if endpointHeaders != nil { + if ct := endpointHeaders.Get(contentTypeHeader); ct != "" { + reqContentType = ct + } + } + requestBody, err := newRequestBody(request, bodyProperties, reqContentType) + if err != nil { + return nil, err + } + req, err := http.NewRequestWithContext(ctx, method, url, requestBody) + if err != nil { + return nil, err + } + req.Header.Set(contentTypeHeader, reqContentType) + for name, values := range endpointHeaders { + req.Header[name] = values + } + return req, nil +} + +// newRequestBody returns a new io.Reader that represents the HTTP request body. +func newRequestBody(request interface{}, bodyProperties map[string]interface{}, reqContentType string) (io.Reader, error) { + if isNil(request) { + if len(bodyProperties) == 0 { + return nil, nil + } + if reqContentType == contentTypeFormURLEncoded { + return newFormURLEncodedBody(bodyProperties), nil + } + requestBytes, err := json.Marshal(bodyProperties) + if err != nil { + return nil, err + } + return bytes.NewReader(requestBytes), nil + } + if body, ok := request.(io.Reader); ok { + return body, nil + } + // Handle form URL encoded content type. + if reqContentType == contentTypeFormURLEncoded { + return newFormURLEncodedRequestBody(request, bodyProperties) + } + requestBytes, err := MarshalJSONWithExtraProperties(request, bodyProperties) + if err != nil { + return nil, err + } + return bytes.NewReader(requestBytes), nil +} + +// newFormURLEncodedBody returns a new io.Reader that represents a form URL encoded body +// from the given body properties map. +func newFormURLEncodedBody(bodyProperties map[string]interface{}) io.Reader { + values := url.Values{} + for key, val := range bodyProperties { + values.Set(key, fmt.Sprintf("%v", val)) + } + return strings.NewReader(values.Encode()) +} + +// newFormURLEncodedRequestBody returns a new io.Reader that represents a form URL encoded body +// from the given request struct and body properties. +func newFormURLEncodedRequestBody(request interface{}, bodyProperties map[string]interface{}) (io.Reader, error) { + values := url.Values{} + // Marshal the request to JSON first to respect any custom MarshalJSON methods, + // then unmarshal into a map to extract the field values. + jsonBytes, err := json.Marshal(request) + if err != nil { + return nil, err + } + var jsonMap map[string]interface{} + if err := json.Unmarshal(jsonBytes, &jsonMap); err != nil { + return nil, err + } + // Convert the JSON map to form URL encoded values. + for key, val := range jsonMap { + if val == nil { + continue + } + values.Set(key, fmt.Sprintf("%v", val)) + } + // Add any extra body properties. + for key, val := range bodyProperties { + values.Set(key, fmt.Sprintf("%v", val)) + } + return strings.NewReader(values.Encode()), nil +} + +// decodeError decodes the error from the given HTTP response. Note that +// it's the caller's responsibility to close the response body. +func decodeError(response *http.Response, errorDecoder ErrorDecoder) error { + if errorDecoder != nil { + // This endpoint has custom errors, so we'll + // attempt to unmarshal the error into a structured + // type based on the status code. + return errorDecoder(response.StatusCode, response.Header, response.Body) + } + // This endpoint doesn't have any custom error + // types, so we just read the body as-is, and + // put it into a normal error. + bytes, err := io.ReadAll(response.Body) + if err != nil && err != io.EOF { + return err + } + if err == io.EOF { + // The error didn't have a response body, + // so all we can do is return an error + // with the status code. + return core.NewAPIError(response.StatusCode, response.Header, nil) + } + return core.NewAPIError(response.StatusCode, response.Header, errors.New(string(bytes))) +} + +// isNil is used to determine if the request value is equal to nil (i.e. an interface +// value that holds a nil concrete value is itself non-nil). +func isNil(value interface{}) bool { + if value == nil { + return true + } + v := reflect.ValueOf(value) + switch v.Kind() { + case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice: + return v.IsNil() + default: + return false + } +} diff --git a/seed/go-sdk/basic-auth-optional/internal/caller_test.go b/seed/go-sdk/basic-auth-optional/internal/caller_test.go new file mode 100644 index 000000000000..50b1ea8969d5 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/caller_test.go @@ -0,0 +1,705 @@ +package internal + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/http/httptest" + "net/url" + "strconv" + "strings" + "testing" + + "github.com/basic-auth-optional/fern/core" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// InternalTestCase represents a single test case. +type InternalTestCase struct { + description string + + // Server-side assertions. + givePathSuffix string + giveMethod string + giveResponseIsOptional bool + giveHeader http.Header + giveErrorDecoder ErrorDecoder + giveRequest *InternalTestRequest + giveQueryParams url.Values + giveBodyProperties map[string]interface{} + + // Client-side assertions. + wantResponse *InternalTestResponse + wantError error +} + +// InternalTestRequest a simple request body. +type InternalTestRequest struct { + Id string `json:"id"` +} + +// InternalTestResponse a simple response body. +type InternalTestResponse struct { + Id string `json:"id"` + ExtraBodyProperties map[string]interface{} `json:"extraBodyProperties,omitempty"` + QueryParameters url.Values `json:"queryParameters,omitempty"` +} + +// InternalTestNotFoundError represents a 404. +type InternalTestNotFoundError struct { + *core.APIError + + Message string `json:"message"` +} + +func TestCall(t *testing.T) { + tests := []*InternalTestCase{ + { + description: "GET success", + giveMethod: http.MethodGet, + giveHeader: http.Header{ + "X-API-Status": []string{"success"}, + }, + giveRequest: &InternalTestRequest{ + Id: "123", + }, + wantResponse: &InternalTestResponse{ + Id: "123", + }, + }, + { + description: "GET success with query", + givePathSuffix: "?limit=1", + giveMethod: http.MethodGet, + giveHeader: http.Header{ + "X-API-Status": []string{"success"}, + }, + giveRequest: &InternalTestRequest{ + Id: "123", + }, + wantResponse: &InternalTestResponse{ + Id: "123", + QueryParameters: url.Values{ + "limit": []string{"1"}, + }, + }, + }, + { + description: "GET not found", + giveMethod: http.MethodGet, + giveHeader: http.Header{ + "X-API-Status": []string{"fail"}, + }, + giveRequest: &InternalTestRequest{ + Id: strconv.Itoa(http.StatusNotFound), + }, + giveErrorDecoder: newTestErrorDecoder(t), + wantError: &InternalTestNotFoundError{ + APIError: core.NewAPIError( + http.StatusNotFound, + http.Header{}, + errors.New(`{"message":"ID \"404\" not found"}`), + ), + }, + }, + { + description: "POST empty body", + giveMethod: http.MethodPost, + giveHeader: http.Header{ + "X-API-Status": []string{"fail"}, + }, + giveRequest: nil, + wantError: core.NewAPIError( + http.StatusBadRequest, + http.Header{}, + errors.New("invalid request"), + ), + }, + { + description: "POST optional response", + giveMethod: http.MethodPost, + giveHeader: http.Header{ + "X-API-Status": []string{"success"}, + }, + giveRequest: &InternalTestRequest{ + Id: "123", + }, + giveResponseIsOptional: true, + }, + { + description: "POST API error", + giveMethod: http.MethodPost, + giveHeader: http.Header{ + "X-API-Status": []string{"fail"}, + }, + giveRequest: &InternalTestRequest{ + Id: strconv.Itoa(http.StatusInternalServerError), + }, + wantError: core.NewAPIError( + http.StatusInternalServerError, + http.Header{}, + errors.New("failed to process request"), + ), + }, + { + description: "POST extra properties", + giveMethod: http.MethodPost, + giveHeader: http.Header{ + "X-API-Status": []string{"success"}, + }, + giveRequest: new(InternalTestRequest), + giveBodyProperties: map[string]interface{}{ + "key": "value", + }, + wantResponse: &InternalTestResponse{ + ExtraBodyProperties: map[string]interface{}{ + "key": "value", + }, + }, + }, + { + description: "GET extra query parameters", + giveMethod: http.MethodGet, + giveHeader: http.Header{ + "X-API-Status": []string{"success"}, + }, + giveQueryParams: url.Values{ + "extra": []string{"true"}, + }, + giveRequest: &InternalTestRequest{ + Id: "123", + }, + wantResponse: &InternalTestResponse{ + Id: "123", + QueryParameters: url.Values{ + "extra": []string{"true"}, + }, + }, + }, + { + description: "GET merge extra query parameters", + givePathSuffix: "?limit=1", + giveMethod: http.MethodGet, + giveHeader: http.Header{ + "X-API-Status": []string{"success"}, + }, + giveRequest: &InternalTestRequest{ + Id: "123", + }, + giveQueryParams: url.Values{ + "extra": []string{"true"}, + }, + wantResponse: &InternalTestResponse{ + Id: "123", + QueryParameters: url.Values{ + "limit": []string{"1"}, + "extra": []string{"true"}, + }, + }, + }, + } + for _, test := range tests { + t.Run(test.description, func(t *testing.T) { + var ( + server = newTestServer(t, test) + client = server.Client() + ) + caller := NewCaller( + &CallerParams{ + Client: client, + }, + ) + var response *InternalTestResponse + _, err := caller.Call( + context.Background(), + &CallParams{ + URL: server.URL + test.givePathSuffix, + Method: test.giveMethod, + Headers: test.giveHeader, + BodyProperties: test.giveBodyProperties, + QueryParameters: test.giveQueryParams, + Request: test.giveRequest, + Response: &response, + ResponseIsOptional: test.giveResponseIsOptional, + ErrorDecoder: test.giveErrorDecoder, + }, + ) + if test.wantError != nil { + assert.EqualError(t, err, test.wantError.Error()) + return + } + require.NoError(t, err) + assert.Equal(t, test.wantResponse, response) + }) + } +} + +func TestMergeHeaders(t *testing.T) { + t.Run("both empty", func(t *testing.T) { + merged := MergeHeaders(make(http.Header), make(http.Header)) + assert.Empty(t, merged) + }) + + t.Run("empty left", func(t *testing.T) { + left := make(http.Header) + + right := make(http.Header) + right.Set("X-API-Version", "0.0.1") + + merged := MergeHeaders(left, right) + assert.Equal(t, "0.0.1", merged.Get("X-API-Version")) + }) + + t.Run("empty right", func(t *testing.T) { + left := make(http.Header) + left.Set("X-API-Version", "0.0.1") + + right := make(http.Header) + + merged := MergeHeaders(left, right) + assert.Equal(t, "0.0.1", merged.Get("X-API-Version")) + }) + + t.Run("single value override", func(t *testing.T) { + left := make(http.Header) + left.Set("X-API-Version", "0.0.0") + + right := make(http.Header) + right.Set("X-API-Version", "0.0.1") + + merged := MergeHeaders(left, right) + assert.Equal(t, []string{"0.0.1"}, merged.Values("X-API-Version")) + }) + + t.Run("multiple value override", func(t *testing.T) { + left := make(http.Header) + left.Set("X-API-Versions", "0.0.0") + + right := make(http.Header) + right.Add("X-API-Versions", "0.0.1") + right.Add("X-API-Versions", "0.0.2") + + merged := MergeHeaders(left, right) + assert.Equal(t, []string{"0.0.1", "0.0.2"}, merged.Values("X-API-Versions")) + }) + + t.Run("disjoint merge", func(t *testing.T) { + left := make(http.Header) + left.Set("X-API-Tenancy", "test") + + right := make(http.Header) + right.Set("X-API-Version", "0.0.1") + + merged := MergeHeaders(left, right) + assert.Equal(t, []string{"test"}, merged.Values("X-API-Tenancy")) + assert.Equal(t, []string{"0.0.1"}, merged.Values("X-API-Version")) + }) +} + +// newTestServer returns a new *httptest.Server configured with the +// given test parameters. +func newTestServer(t *testing.T, tc *InternalTestCase) *httptest.Server { + return httptest.NewServer( + http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, tc.giveMethod, r.Method) + assert.Equal(t, contentType, r.Header.Get(contentTypeHeader)) + for header, value := range tc.giveHeader { + assert.Equal(t, value, r.Header.Values(header)) + } + + request := new(InternalTestRequest) + + bytes, err := io.ReadAll(r.Body) + if tc.giveRequest == nil { + require.Empty(t, bytes) + w.WriteHeader(http.StatusBadRequest) + _, err = w.Write([]byte("invalid request")) + require.NoError(t, err) + return + } + require.NoError(t, err) + require.NoError(t, json.Unmarshal(bytes, request)) + + switch request.Id { + case strconv.Itoa(http.StatusNotFound): + notFoundError := &InternalTestNotFoundError{ + APIError: &core.APIError{ + StatusCode: http.StatusNotFound, + }, + Message: fmt.Sprintf("ID %q not found", request.Id), + } + bytes, err = json.Marshal(notFoundError) + require.NoError(t, err) + + w.WriteHeader(http.StatusNotFound) + _, err = w.Write(bytes) + require.NoError(t, err) + return + + case strconv.Itoa(http.StatusInternalServerError): + w.WriteHeader(http.StatusInternalServerError) + _, err = w.Write([]byte("failed to process request")) + require.NoError(t, err) + return + } + + if tc.giveResponseIsOptional { + w.WriteHeader(http.StatusOK) + return + } + + extraBodyProperties := make(map[string]interface{}) + require.NoError(t, json.Unmarshal(bytes, &extraBodyProperties)) + delete(extraBodyProperties, "id") + + response := &InternalTestResponse{ + Id: request.Id, + ExtraBodyProperties: extraBodyProperties, + QueryParameters: r.URL.Query(), + } + bytes, err = json.Marshal(response) + require.NoError(t, err) + + _, err = w.Write(bytes) + require.NoError(t, err) + }, + ), + ) +} + +func TestIsNil(t *testing.T) { + t.Run("nil interface", func(t *testing.T) { + assert.True(t, isNil(nil)) + }) + + t.Run("nil pointer", func(t *testing.T) { + var ptr *string + assert.True(t, isNil(ptr)) + }) + + t.Run("non-nil pointer", func(t *testing.T) { + s := "test" + assert.False(t, isNil(&s)) + }) + + t.Run("nil slice", func(t *testing.T) { + var slice []string + assert.True(t, isNil(slice)) + }) + + t.Run("non-nil slice", func(t *testing.T) { + slice := []string{} + assert.False(t, isNil(slice)) + }) + + t.Run("nil map", func(t *testing.T) { + var m map[string]string + assert.True(t, isNil(m)) + }) + + t.Run("non-nil map", func(t *testing.T) { + m := make(map[string]string) + assert.False(t, isNil(m)) + }) + + t.Run("string value", func(t *testing.T) { + assert.False(t, isNil("test")) + }) + + t.Run("empty string value", func(t *testing.T) { + assert.False(t, isNil("")) + }) + + t.Run("int value", func(t *testing.T) { + assert.False(t, isNil(42)) + }) + + t.Run("zero int value", func(t *testing.T) { + assert.False(t, isNil(0)) + }) + + t.Run("bool value", func(t *testing.T) { + assert.False(t, isNil(true)) + }) + + t.Run("false bool value", func(t *testing.T) { + assert.False(t, isNil(false)) + }) + + t.Run("struct value", func(t *testing.T) { + type testStruct struct { + Field string + } + assert.False(t, isNil(testStruct{Field: "test"})) + }) + + t.Run("empty struct value", func(t *testing.T) { + type testStruct struct { + Field string + } + assert.False(t, isNil(testStruct{})) + }) +} + +// newTestErrorDecoder returns an error decoder suitable for tests. +func newTestErrorDecoder(t *testing.T) func(int, http.Header, io.Reader) error { + return func(statusCode int, header http.Header, body io.Reader) error { + raw, err := io.ReadAll(body) + require.NoError(t, err) + + var ( + apiError = core.NewAPIError(statusCode, header, errors.New(string(raw))) + decoder = json.NewDecoder(bytes.NewReader(raw)) + ) + if statusCode == http.StatusNotFound { + value := new(InternalTestNotFoundError) + value.APIError = apiError + require.NoError(t, decoder.Decode(value)) + + return value + } + return apiError + } +} + +// FormURLEncodedTestRequest is a test struct for form URL encoding tests. +type FormURLEncodedTestRequest struct { + ClientID string `json:"client_id"` + ClientSecret string `json:"client_secret"` + GrantType string `json:"grant_type,omitempty"` + Scope *string `json:"scope,omitempty"` + NilPointer *string `json:"nil_pointer,omitempty"` +} + +func TestNewFormURLEncodedBody(t *testing.T) { + t.Run("simple key-value pairs", func(t *testing.T) { + bodyProperties := map[string]interface{}{ + "client_id": "test_client_id", + "client_secret": "test_client_secret", + "grant_type": "client_credentials", + } + reader := newFormURLEncodedBody(bodyProperties) + body, err := io.ReadAll(reader) + require.NoError(t, err) + + // Parse the body and verify values + values, err := url.ParseQuery(string(body)) + require.NoError(t, err) + + assert.Equal(t, "test_client_id", values.Get("client_id")) + assert.Equal(t, "test_client_secret", values.Get("client_secret")) + assert.Equal(t, "client_credentials", values.Get("grant_type")) + + // Verify it's not JSON + bodyStr := string(body) + assert.False(t, strings.HasPrefix(strings.TrimSpace(bodyStr), "{"), + "Body should not be JSON, got: %s", bodyStr) + }) + + t.Run("special characters requiring URL encoding", func(t *testing.T) { + bodyProperties := map[string]interface{}{ + "value_with_space": "hello world", + "value_with_ampersand": "a&b", + "value_with_equals": "a=b", + "value_with_plus": "a+b", + } + reader := newFormURLEncodedBody(bodyProperties) + body, err := io.ReadAll(reader) + require.NoError(t, err) + + // Parse the body and verify values are correctly decoded + values, err := url.ParseQuery(string(body)) + require.NoError(t, err) + + assert.Equal(t, "hello world", values.Get("value_with_space")) + assert.Equal(t, "a&b", values.Get("value_with_ampersand")) + assert.Equal(t, "a=b", values.Get("value_with_equals")) + assert.Equal(t, "a+b", values.Get("value_with_plus")) + }) + + t.Run("empty map", func(t *testing.T) { + bodyProperties := map[string]interface{}{} + reader := newFormURLEncodedBody(bodyProperties) + body, err := io.ReadAll(reader) + require.NoError(t, err) + assert.Empty(t, string(body)) + }) +} + +func TestNewFormURLEncodedRequestBody(t *testing.T) { + t.Run("struct with json tags", func(t *testing.T) { + scope := "read write" + request := &FormURLEncodedTestRequest{ + ClientID: "test_client_id", + ClientSecret: "test_client_secret", + GrantType: "client_credentials", + Scope: &scope, + NilPointer: nil, + } + reader, err := newFormURLEncodedRequestBody(request, nil) + require.NoError(t, err) + + body, err := io.ReadAll(reader) + require.NoError(t, err) + + // Parse the body and verify values + values, err := url.ParseQuery(string(body)) + require.NoError(t, err) + + assert.Equal(t, "test_client_id", values.Get("client_id")) + assert.Equal(t, "test_client_secret", values.Get("client_secret")) + assert.Equal(t, "client_credentials", values.Get("grant_type")) + assert.Equal(t, "read write", values.Get("scope")) + // nil_pointer should not be present (nil pointer with omitempty) + assert.Empty(t, values.Get("nil_pointer")) + + // Verify it's not JSON + bodyStr := string(body) + assert.False(t, strings.HasPrefix(strings.TrimSpace(bodyStr), "{"), + "Body should not be JSON, got: %s", bodyStr) + }) + + t.Run("struct with omitempty and zero values", func(t *testing.T) { + request := &FormURLEncodedTestRequest{ + ClientID: "test_client_id", + ClientSecret: "test_client_secret", + GrantType: "", // empty string with omitempty should be omitted + Scope: nil, + NilPointer: nil, + } + reader, err := newFormURLEncodedRequestBody(request, nil) + require.NoError(t, err) + + body, err := io.ReadAll(reader) + require.NoError(t, err) + + values, err := url.ParseQuery(string(body)) + require.NoError(t, err) + + assert.Equal(t, "test_client_id", values.Get("client_id")) + assert.Equal(t, "test_client_secret", values.Get("client_secret")) + // grant_type should not be present (empty string with omitempty) + assert.Empty(t, values.Get("grant_type")) + assert.Empty(t, values.Get("scope")) + }) + + t.Run("struct with extra body properties", func(t *testing.T) { + request := &FormURLEncodedTestRequest{ + ClientID: "test_client_id", + ClientSecret: "test_client_secret", + } + bodyProperties := map[string]interface{}{ + "extra_param": "extra_value", + } + reader, err := newFormURLEncodedRequestBody(request, bodyProperties) + require.NoError(t, err) + + body, err := io.ReadAll(reader) + require.NoError(t, err) + + values, err := url.ParseQuery(string(body)) + require.NoError(t, err) + + assert.Equal(t, "test_client_id", values.Get("client_id")) + assert.Equal(t, "test_client_secret", values.Get("client_secret")) + assert.Equal(t, "extra_value", values.Get("extra_param")) + }) + + t.Run("special characters in struct fields", func(t *testing.T) { + scope := "read&write=all+permissions" + request := &FormURLEncodedTestRequest{ + ClientID: "client with spaces", + ClientSecret: "secret&with=special+chars", + Scope: &scope, + } + reader, err := newFormURLEncodedRequestBody(request, nil) + require.NoError(t, err) + + body, err := io.ReadAll(reader) + require.NoError(t, err) + + values, err := url.ParseQuery(string(body)) + require.NoError(t, err) + + assert.Equal(t, "client with spaces", values.Get("client_id")) + assert.Equal(t, "secret&with=special+chars", values.Get("client_secret")) + assert.Equal(t, "read&write=all+permissions", values.Get("scope")) + }) +} + +func TestNewRequestBodyFormURLEncoded(t *testing.T) { + t.Run("selects form encoding when content-type is form-urlencoded", func(t *testing.T) { + request := &FormURLEncodedTestRequest{ + ClientID: "test_client_id", + ClientSecret: "test_client_secret", + GrantType: "client_credentials", + } + reader, err := newRequestBody(request, nil, contentTypeFormURLEncoded) + require.NoError(t, err) + + body, err := io.ReadAll(reader) + require.NoError(t, err) + + // Verify it's form-urlencoded, not JSON + bodyStr := string(body) + assert.False(t, strings.HasPrefix(strings.TrimSpace(bodyStr), "{"), + "Body should not be JSON when Content-Type is form-urlencoded, got: %s", bodyStr) + + // Parse and verify values + values, err := url.ParseQuery(bodyStr) + require.NoError(t, err) + + assert.Equal(t, "test_client_id", values.Get("client_id")) + assert.Equal(t, "test_client_secret", values.Get("client_secret")) + assert.Equal(t, "client_credentials", values.Get("grant_type")) + }) + + t.Run("selects JSON encoding when content-type is application/json", func(t *testing.T) { + request := &FormURLEncodedTestRequest{ + ClientID: "test_client_id", + ClientSecret: "test_client_secret", + } + reader, err := newRequestBody(request, nil, contentType) + require.NoError(t, err) + + body, err := io.ReadAll(reader) + require.NoError(t, err) + + // Verify it's JSON + bodyStr := string(body) + assert.True(t, strings.HasPrefix(strings.TrimSpace(bodyStr), "{"), + "Body should be JSON when Content-Type is application/json, got: %s", bodyStr) + + // Parse and verify it's valid JSON + var parsed map[string]interface{} + err = json.Unmarshal(body, &parsed) + require.NoError(t, err) + + assert.Equal(t, "test_client_id", parsed["client_id"]) + assert.Equal(t, "test_client_secret", parsed["client_secret"]) + }) + + t.Run("form encoding with body properties only (nil request)", func(t *testing.T) { + bodyProperties := map[string]interface{}{ + "client_id": "test_client_id", + "client_secret": "test_client_secret", + } + reader, err := newRequestBody(nil, bodyProperties, contentTypeFormURLEncoded) + require.NoError(t, err) + + body, err := io.ReadAll(reader) + require.NoError(t, err) + + values, err := url.ParseQuery(string(body)) + require.NoError(t, err) + + assert.Equal(t, "test_client_id", values.Get("client_id")) + assert.Equal(t, "test_client_secret", values.Get("client_secret")) + }) +} diff --git a/seed/go-sdk/basic-auth-optional/internal/error_decoder.go b/seed/go-sdk/basic-auth-optional/internal/error_decoder.go new file mode 100644 index 000000000000..ebafe345fa1e --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/error_decoder.go @@ -0,0 +1,64 @@ +package internal + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + + "github.com/basic-auth-optional/fern/core" +) + +// ErrorCodes maps HTTP status codes to error constructors. +type ErrorCodes map[int]func(*core.APIError) error + +// ErrorDecoder decodes *http.Response errors and returns a +// typed API error (e.g. *core.APIError). +type ErrorDecoder func(statusCode int, header http.Header, body io.Reader) error + +// NewErrorDecoder returns a new ErrorDecoder backed by the given error codes. +// errorCodesOverrides is optional and will be merged with the default error codes, +// with overrides taking precedence. +func NewErrorDecoder(errorCodes ErrorCodes, errorCodesOverrides ...ErrorCodes) ErrorDecoder { + // Merge default error codes with overrides + mergedErrorCodes := make(ErrorCodes) + + // Start with default error codes + for statusCode, errorFunc := range errorCodes { + mergedErrorCodes[statusCode] = errorFunc + } + + // Apply overrides if provided + if len(errorCodesOverrides) > 0 && errorCodesOverrides[0] != nil { + for statusCode, errorFunc := range errorCodesOverrides[0] { + mergedErrorCodes[statusCode] = errorFunc + } + } + + return func(statusCode int, header http.Header, body io.Reader) error { + raw, err := io.ReadAll(body) + if err != nil { + return fmt.Errorf("failed to read error from response body: %w", err) + } + apiError := core.NewAPIError( + statusCode, + header, + errors.New(string(raw)), + ) + newErrorFunc, ok := mergedErrorCodes[statusCode] + if !ok { + // This status code isn't recognized, so we return + // the API error as-is. + return apiError + } + customError := newErrorFunc(apiError) + if err := json.NewDecoder(bytes.NewReader(raw)).Decode(customError); err != nil { + // If we fail to decode the error, we return the + // API error as-is. + return apiError + } + return customError + } +} diff --git a/seed/go-sdk/basic-auth-optional/internal/error_decoder_test.go b/seed/go-sdk/basic-auth-optional/internal/error_decoder_test.go new file mode 100644 index 000000000000..85b529f99ec1 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/error_decoder_test.go @@ -0,0 +1,59 @@ +package internal + +import ( + "bytes" + "errors" + "net/http" + "testing" + + "github.com/basic-auth-optional/fern/core" + "github.com/stretchr/testify/assert" +) + +func TestErrorDecoder(t *testing.T) { + decoder := NewErrorDecoder( + ErrorCodes{ + http.StatusNotFound: func(apiError *core.APIError) error { + return &InternalTestNotFoundError{APIError: apiError} + }, + }) + + tests := []struct { + description string + giveStatusCode int + giveHeader http.Header + giveBody string + wantError error + }{ + { + description: "unrecognized status code", + giveStatusCode: http.StatusInternalServerError, + giveHeader: http.Header{}, + giveBody: "Internal Server Error", + wantError: core.NewAPIError(http.StatusInternalServerError, http.Header{}, errors.New("Internal Server Error")), + }, + { + description: "not found with valid JSON", + giveStatusCode: http.StatusNotFound, + giveHeader: http.Header{}, + giveBody: `{"message": "Resource not found"}`, + wantError: &InternalTestNotFoundError{ + APIError: core.NewAPIError(http.StatusNotFound, http.Header{}, errors.New(`{"message": "Resource not found"}`)), + Message: "Resource not found", + }, + }, + { + description: "not found with invalid JSON", + giveStatusCode: http.StatusNotFound, + giveHeader: http.Header{}, + giveBody: `Resource not found`, + wantError: core.NewAPIError(http.StatusNotFound, http.Header{}, errors.New("Resource not found")), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + assert.Equal(t, tt.wantError, decoder(tt.giveStatusCode, tt.giveHeader, bytes.NewReader([]byte(tt.giveBody)))) + }) + } +} diff --git a/seed/go-sdk/basic-auth-optional/internal/explicit_fields.go b/seed/go-sdk/basic-auth-optional/internal/explicit_fields.go new file mode 100644 index 000000000000..4bdf34fc2b7c --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/explicit_fields.go @@ -0,0 +1,116 @@ +package internal + +import ( + "math/big" + "reflect" + "strings" +) + +// HandleExplicitFields processes a struct to remove `omitempty` from +// fields that have been explicitly set (as indicated by their corresponding bit in explicitFields). +// Note that `marshaler` should be an embedded struct to avoid infinite recursion. +// Returns an interface{} that can be passed to json.Marshal. +func HandleExplicitFields(marshaler interface{}, explicitFields *big.Int) interface{} { + val := reflect.ValueOf(marshaler) + typ := reflect.TypeOf(marshaler) + + // Handle pointer types + if val.Kind() == reflect.Ptr { + if val.IsNil() { + return nil + } + val = val.Elem() + typ = typ.Elem() + } + + // Only handle struct types + if val.Kind() != reflect.Struct { + return marshaler + } + + // Handle embedded struct pattern + var sourceVal reflect.Value + var sourceType reflect.Type + + // Check if this is an embedded struct pattern + if typ.NumField() == 1 && typ.Field(0).Anonymous { + // This is likely an embedded struct, get the embedded value + embeddedField := val.Field(0) + sourceVal = embeddedField + sourceType = embeddedField.Type() + } else { + // Regular struct + sourceVal = val + sourceType = typ + } + + // If no explicit fields set, use standard marshaling + if explicitFields == nil || explicitFields.Sign() == 0 { + return marshaler + } + + // Create a new struct type with modified tags + fields := make([]reflect.StructField, 0, sourceType.NumField()) + + for i := 0; i < sourceType.NumField(); i++ { + field := sourceType.Field(i) + + // Skip unexported fields and the explicitFields field itself + if !field.IsExported() || field.Name == "explicitFields" { + continue + } + + // Check if this field has been explicitly set + fieldBit := big.NewInt(1) + fieldBit.Lsh(fieldBit, uint(i)) + if big.NewInt(0).And(explicitFields, fieldBit).Sign() != 0 { + // Remove omitempty from the json tag + tag := field.Tag.Get("json") + if tag != "" && tag != "-" { + // Parse the json tag, remove omitempty from options + parts := strings.Split(tag, ",") + if len(parts) > 1 { + var newParts []string + newParts = append(newParts, parts[0]) // Keep the field name + for _, part := range parts[1:] { + if strings.TrimSpace(part) != "omitempty" { + newParts = append(newParts, part) + } + } + tag = strings.Join(newParts, ",") + } + + // Reconstruct the struct tag + newTag := `json:"` + tag + `"` + if urlTag := field.Tag.Get("url"); urlTag != "" { + newTag += ` url:"` + urlTag + `"` + } + + field.Tag = reflect.StructTag(newTag) + } + } + + fields = append(fields, field) + } + + // Create new struct type with modified tags + newType := reflect.StructOf(fields) + newVal := reflect.New(newType).Elem() + + // Copy field values from original struct to new struct + fieldIndex := 0 + for i := 0; i < sourceType.NumField(); i++ { + originalField := sourceType.Field(i) + + // Skip unexported fields and the explicitFields field itself + if !originalField.IsExported() || originalField.Name == "explicitFields" { + continue + } + + originalValue := sourceVal.Field(i) + newVal.Field(fieldIndex).Set(originalValue) + fieldIndex++ + } + + return newVal.Interface() +} diff --git a/seed/go-sdk/basic-auth-optional/internal/explicit_fields_test.go b/seed/go-sdk/basic-auth-optional/internal/explicit_fields_test.go new file mode 100644 index 000000000000..f44beec447d6 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/explicit_fields_test.go @@ -0,0 +1,645 @@ +package internal + +import ( + "encoding/json" + "math/big" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type testExplicitFieldsStruct struct { + Name *string `json:"name,omitempty"` + Code *string `json:"code,omitempty"` + Count *int `json:"count,omitempty"` + Enabled *bool `json:"enabled,omitempty"` + Tags []string `json:"tags,omitempty"` + unexported string `json:"-"` //nolint:unused + explicitFields *big.Int `json:"-"` +} + +var ( + testFieldName = big.NewInt(1 << 0) + testFieldCode = big.NewInt(1 << 1) + testFieldCount = big.NewInt(1 << 2) + testFieldEnabled = big.NewInt(1 << 3) + testFieldTags = big.NewInt(1 << 4) +) + +func (t *testExplicitFieldsStruct) require(field *big.Int) { + if t.explicitFields == nil { + t.explicitFields = big.NewInt(0) + } + t.explicitFields.Or(t.explicitFields, field) +} + +func (t *testExplicitFieldsStruct) SetName(name *string) { + t.Name = name + t.require(testFieldName) +} + +func (t *testExplicitFieldsStruct) SetCode(code *string) { + t.Code = code + t.require(testFieldCode) +} + +func (t *testExplicitFieldsStruct) SetCount(count *int) { + t.Count = count + t.require(testFieldCount) +} + +func (t *testExplicitFieldsStruct) SetEnabled(enabled *bool) { + t.Enabled = enabled + t.require(testFieldEnabled) +} + +func (t *testExplicitFieldsStruct) SetTags(tags []string) { + t.Tags = tags + t.require(testFieldTags) +} + +func (t *testExplicitFieldsStruct) MarshalJSON() ([]byte, error) { + type embed testExplicitFieldsStruct + var marshaler = struct { + embed + }{ + embed: embed(*t), + } + return json.Marshal(HandleExplicitFields(marshaler, t.explicitFields)) +} + +type testStructWithoutExplicitFields struct { + Name *string `json:"name,omitempty"` + Code *string `json:"code,omitempty"` +} + +func TestHandleExplicitFields(t *testing.T) { + tests := []struct { + desc string + giveInput interface{} + wantBytes []byte + wantError string + }{ + { + desc: "nil input", + giveInput: nil, + wantBytes: []byte(`null`), + }, + { + desc: "non-struct input", + giveInput: "string", + wantBytes: []byte(`"string"`), + }, + { + desc: "slice input", + giveInput: []string{"a", "b"}, + wantBytes: []byte(`["a","b"]`), + }, + { + desc: "map input", + giveInput: map[string]interface{}{"key": "value"}, + wantBytes: []byte(`{"key":"value"}`), + }, + { + desc: "struct without explicitFields field", + giveInput: &testStructWithoutExplicitFields{ + Name: stringPtr("test"), + Code: nil, + }, + wantBytes: []byte(`{"name":"test"}`), + }, + { + desc: "struct with no explicit fields set", + giveInput: &testExplicitFieldsStruct{ + Name: stringPtr("test"), + Code: nil, + }, + wantBytes: []byte(`{"name":"test"}`), + }, + { + desc: "struct with explicit nil field", + giveInput: func() *testExplicitFieldsStruct { + s := &testExplicitFieldsStruct{ + Name: stringPtr("test"), + } + s.SetCode(nil) + return s + }(), + wantBytes: []byte(`{"name":"test","code":null}`), + }, + { + desc: "struct with explicit non-nil field", + giveInput: func() *testExplicitFieldsStruct { + s := &testExplicitFieldsStruct{} + s.SetName(stringPtr("explicit")) + s.SetCode(stringPtr("also-explicit")) + return s + }(), + wantBytes: []byte(`{"name":"explicit","code":"also-explicit"}`), + }, + { + desc: "struct with mixed explicit and implicit fields", + giveInput: func() *testExplicitFieldsStruct { + s := &testExplicitFieldsStruct{ + Name: stringPtr("implicit"), + Count: intPtr(42), + } + s.SetCode(nil) // explicit nil + return s + }(), + wantBytes: []byte(`{"name":"implicit","code":null,"count":42}`), + }, + { + desc: "struct with multiple explicit nil fields", + giveInput: func() *testExplicitFieldsStruct { + s := &testExplicitFieldsStruct{ + Name: stringPtr("test"), + } + s.SetCode(nil) + s.SetCount(nil) + return s + }(), + wantBytes: []byte(`{"name":"test","code":null,"count":null}`), + }, + { + desc: "struct with slice field", + giveInput: func() *testExplicitFieldsStruct { + s := &testExplicitFieldsStruct{ + Tags: []string{"tag1", "tag2"}, + } + s.SetTags(nil) // explicit nil slice + return s + }(), + wantBytes: []byte(`{"tags":null}`), + }, + { + desc: "struct with boolean field", + giveInput: func() *testExplicitFieldsStruct { + s := &testExplicitFieldsStruct{} + s.SetEnabled(boolPtr(false)) // explicit false + return s + }(), + wantBytes: []byte(`{"enabled":false}`), + }, + { + desc: "struct with all fields explicit", + giveInput: func() *testExplicitFieldsStruct { + s := &testExplicitFieldsStruct{} + s.SetName(stringPtr("test")) + s.SetCode(nil) + s.SetCount(intPtr(0)) + s.SetEnabled(boolPtr(false)) + s.SetTags([]string{}) + return s + }(), + wantBytes: []byte(`{"name":"test","code":null,"count":0,"enabled":false,"tags":[]}`), + }, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + var explicitFields *big.Int + if s, ok := tt.giveInput.(*testExplicitFieldsStruct); ok { + explicitFields = s.explicitFields + } + bytes, err := json.Marshal(HandleExplicitFields(tt.giveInput, explicitFields)) + if tt.wantError != "" { + require.EqualError(t, err, tt.wantError) + assert.Nil(t, tt.wantBytes) + return + } + require.NoError(t, err) + assert.JSONEq(t, string(tt.wantBytes), string(bytes)) + + // Verify it's valid JSON + var value interface{} + require.NoError(t, json.Unmarshal(bytes, &value)) + }) + } +} + +func TestHandleExplicitFieldsCustomMarshaler(t *testing.T) { + t.Run("custom marshaler with explicit fields", func(t *testing.T) { + s := &testExplicitFieldsStruct{} + s.SetName(nil) + s.SetCode(stringPtr("test-code")) + + bytes, err := s.MarshalJSON() + require.NoError(t, err) + assert.JSONEq(t, `{"name":null,"code":"test-code"}`, string(bytes)) + }) + + t.Run("custom marshaler with no explicit fields", func(t *testing.T) { + s := &testExplicitFieldsStruct{ + Name: stringPtr("implicit"), + Code: stringPtr("also-implicit"), + } + + bytes, err := s.MarshalJSON() + require.NoError(t, err) + assert.JSONEq(t, `{"name":"implicit","code":"also-implicit"}`, string(bytes)) + }) +} + +func TestHandleExplicitFieldsPointerHandling(t *testing.T) { + t.Run("nil pointer", func(t *testing.T) { + var s *testExplicitFieldsStruct + bytes, err := json.Marshal(HandleExplicitFields(s, nil)) + require.NoError(t, err) + assert.Equal(t, []byte(`null`), bytes) + }) + + t.Run("pointer to struct", func(t *testing.T) { + s := &testExplicitFieldsStruct{} + s.SetName(nil) + + bytes, err := json.Marshal(HandleExplicitFields(s, s.explicitFields)) + require.NoError(t, err) + assert.JSONEq(t, `{"name":null}`, string(bytes)) + }) +} + +func TestHandleExplicitFieldsEmbeddedStruct(t *testing.T) { + t.Run("embedded struct with explicit fields", func(t *testing.T) { + // Create a struct similar to what MarshalJSON creates + s := &testExplicitFieldsStruct{} + s.SetName(nil) + s.SetCode(stringPtr("test-code")) + + type embed testExplicitFieldsStruct + var marshaler = struct { + embed + }{ + embed: embed(*s), + } + + bytes, err := json.Marshal(HandleExplicitFields(marshaler, s.explicitFields)) + require.NoError(t, err) + // Should include both explicit fields (name as null, code as "test-code") + assert.JSONEq(t, `{"name":null,"code":"test-code"}`, string(bytes)) + }) + + t.Run("embedded struct with no explicit fields", func(t *testing.T) { + s := &testExplicitFieldsStruct{ + Name: stringPtr("implicit"), + Code: stringPtr("also-implicit"), + } + + type embed testExplicitFieldsStruct + var marshaler = struct { + embed + }{ + embed: embed(*s), + } + + bytes, err := json.Marshal(HandleExplicitFields(marshaler, s.explicitFields)) + require.NoError(t, err) + // Should only include non-nil fields (omitempty behavior) + assert.JSONEq(t, `{"name":"implicit","code":"also-implicit"}`, string(bytes)) + }) + + t.Run("embedded struct with mixed fields", func(t *testing.T) { + s := &testExplicitFieldsStruct{ + Count: intPtr(42), // implicit field + } + s.SetName(nil) // explicit nil + s.SetCode(stringPtr("explicit")) // explicit value + + type embed testExplicitFieldsStruct + var marshaler = struct { + embed + }{ + embed: embed(*s), + } + + bytes, err := json.Marshal(HandleExplicitFields(marshaler, s.explicitFields)) + require.NoError(t, err) + // Should include explicit null, explicit value, and implicit value + assert.JSONEq(t, `{"name":null,"code":"explicit","count":42}`, string(bytes)) + }) +} + +func TestHandleExplicitFieldsTagHandling(t *testing.T) { + type testStructWithComplexTags struct { + Field1 *string `json:"field1,omitempty" url:"field1,omitempty"` + Field2 *string `json:"field2,omitempty,string" url:"field2"` + Field3 *string `json:"-"` + Field4 *string `json:"field4"` + explicitFields *big.Int `json:"-"` + } + + s := &testStructWithComplexTags{ + Field1: stringPtr("test1"), + Field4: stringPtr("test4"), + explicitFields: big.NewInt(1), // Only first field is explicit + } + + bytes, err := json.Marshal(HandleExplicitFields(s, s.explicitFields)) + require.NoError(t, err) + + // Field1 should have omitempty removed, Field2 should keep omitempty, Field4 should be included + assert.JSONEq(t, `{"field1":"test1","field4":"test4"}`, string(bytes)) +} + +// Test types for nested struct explicit fields testing +type testNestedStruct struct { + NestedName *string `json:"nested_name,omitempty"` + NestedCode *string `json:"nested_code,omitempty"` + explicitFields *big.Int `json:"-"` +} + +type testParentStruct struct { + ParentName *string `json:"parent_name,omitempty"` + Nested *testNestedStruct `json:"nested,omitempty"` + explicitFields *big.Int `json:"-"` +} + +var ( + nestedFieldName = big.NewInt(1 << 0) + nestedFieldCode = big.NewInt(1 << 1) +) + +var ( + parentFieldName = big.NewInt(1 << 0) + parentFieldNested = big.NewInt(1 << 1) +) + +func (n *testNestedStruct) require(field *big.Int) { + if n.explicitFields == nil { + n.explicitFields = big.NewInt(0) + } + n.explicitFields.Or(n.explicitFields, field) +} + +func (n *testNestedStruct) SetNestedName(name *string) { + n.NestedName = name + n.require(nestedFieldName) +} + +func (n *testNestedStruct) SetNestedCode(code *string) { + n.NestedCode = code + n.require(nestedFieldCode) +} + +func (n *testNestedStruct) MarshalJSON() ([]byte, error) { + type embed testNestedStruct + var marshaler = struct { + embed + }{ + embed: embed(*n), + } + return json.Marshal(HandleExplicitFields(marshaler, n.explicitFields)) +} + +func (p *testParentStruct) require(field *big.Int) { + if p.explicitFields == nil { + p.explicitFields = big.NewInt(0) + } + p.explicitFields.Or(p.explicitFields, field) +} + +func (p *testParentStruct) SetParentName(name *string) { + p.ParentName = name + p.require(parentFieldName) +} + +func (p *testParentStruct) SetNested(nested *testNestedStruct) { + p.Nested = nested + p.require(parentFieldNested) +} + +func (p *testParentStruct) MarshalJSON() ([]byte, error) { + type embed testParentStruct + var marshaler = struct { + embed + }{ + embed: embed(*p), + } + return json.Marshal(HandleExplicitFields(marshaler, p.explicitFields)) +} + +func TestHandleExplicitFieldsNestedStruct(t *testing.T) { + tests := []struct { + desc string + setupFunc func() *testParentStruct + wantBytes []byte + }{ + { + desc: "nested struct with explicit nil in nested object", + setupFunc: func() *testParentStruct { + nested := &testNestedStruct{ + NestedName: stringPtr("implicit-nested"), + } + nested.SetNestedCode(nil) // explicit nil + + return &testParentStruct{ + ParentName: stringPtr("implicit-parent"), + Nested: nested, + } + }, + wantBytes: []byte(`{"parent_name":"implicit-parent","nested":{"nested_name":"implicit-nested","nested_code":null}}`), + }, + { + desc: "parent with explicit nil nested struct", + setupFunc: func() *testParentStruct { + parent := &testParentStruct{ + ParentName: stringPtr("implicit-parent"), + } + parent.SetNested(nil) // explicit nil nested struct + return parent + }, + wantBytes: []byte(`{"parent_name":"implicit-parent","nested":null}`), + }, + { + desc: "all explicit fields in nested structure", + setupFunc: func() *testParentStruct { + nested := &testNestedStruct{} + nested.SetNestedName(stringPtr("explicit-nested")) + nested.SetNestedCode(nil) // explicit nil + + parent := &testParentStruct{} + parent.SetParentName(nil) // explicit nil + parent.SetNested(nested) // explicit nested struct + + return parent + }, + wantBytes: []byte(`{"parent_name":null,"nested":{"nested_name":"explicit-nested","nested_code":null}}`), + }, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + parent := tt.setupFunc() + bytes, err := parent.MarshalJSON() + require.NoError(t, err) + assert.JSONEq(t, string(tt.wantBytes), string(bytes)) + + // Verify it's valid JSON + var value interface{} + require.NoError(t, json.Unmarshal(bytes, &value)) + }) + } +} + +// Test for setter method documentation and behavior +func TestSetterMethodsDocumentation(t *testing.T) { + t.Run("setter prevents omitempty for nil values", func(t *testing.T) { + s := &testExplicitFieldsStruct{} + + // Use setter to explicitly set nil - this should prevent omitempty + s.SetName(nil) + s.SetCode(nil) + + bytes, err := s.MarshalJSON() + require.NoError(t, err) + + // Both fields should be included as null, not omitted + assert.JSONEq(t, `{"name":null,"code":null}`, string(bytes)) + }) + + t.Run("setter prevents omitempty for empty slice", func(t *testing.T) { + s := &testExplicitFieldsStruct{} + + // Use setter to explicitly set empty slice + s.SetTags([]string{}) + + bytes, err := s.MarshalJSON() + require.NoError(t, err) + + // Empty slice should be included as [], not omitted + assert.JSONEq(t, `{"tags":[]}`, string(bytes)) + }) + + t.Run("setter prevents omitempty for zero values", func(t *testing.T) { + s := &testExplicitFieldsStruct{} + + // Use setter to explicitly set zero values + s.SetCount(intPtr(0)) + s.SetEnabled(boolPtr(false)) + + bytes, err := s.MarshalJSON() + require.NoError(t, err) + + // Zero values should be included, not omitted + assert.JSONEq(t, `{"count":0,"enabled":false}`, string(bytes)) + }) + + t.Run("direct assignment is omitted when nil", func(t *testing.T) { + s := &testExplicitFieldsStruct{ + Name: nil, // Direct assignment, not using setter + Code: nil, // Direct assignment, not using setter + } + + bytes, err := s.MarshalJSON() + require.NoError(t, err) + + // Fields not set via setter should be omitted when nil + assert.JSONEq(t, `{}`, string(bytes)) + }) + + t.Run("mix of setter and direct assignment", func(t *testing.T) { + s := &testExplicitFieldsStruct{ + Name: stringPtr("direct"), // Direct assignment + Count: intPtr(42), // Direct assignment + } + s.SetCode(nil) // Setter with nil + s.SetEnabled(boolPtr(false)) // Setter with zero value + + bytes, err := s.MarshalJSON() + require.NoError(t, err) + + // Direct assignments included if non-nil, setter fields always included + assert.JSONEq(t, `{"name":"direct","code":null,"count":42,"enabled":false}`, string(bytes)) + }) +} + +// Test for complex scenarios with multiple setters +func TestComplexSetterScenarios(t *testing.T) { + t.Run("multiple setter calls on same field", func(t *testing.T) { + s := &testExplicitFieldsStruct{} + + // Call setter multiple times - last one should win + s.SetName(stringPtr("first")) + s.SetName(stringPtr("second")) + s.SetName(nil) // Final value is nil + + bytes, err := s.MarshalJSON() + require.NoError(t, err) + + // Should serialize the last set value (nil) + assert.JSONEq(t, `{"name":null}`, string(bytes)) + }) + + t.Run("setter after direct assignment", func(t *testing.T) { + s := &testExplicitFieldsStruct{ + Name: stringPtr("direct"), + } + + // Override with setter + s.SetName(nil) + + bytes, err := s.MarshalJSON() + require.NoError(t, err) + + // Setter should mark field as explicit, so nil is serialized + assert.JSONEq(t, `{"name":null}`, string(bytes)) + }) + + t.Run("all fields set via setters", func(t *testing.T) { + s := &testExplicitFieldsStruct{} + s.SetName(nil) + s.SetCode(stringPtr("")) // Empty string + s.SetCount(intPtr(0)) // Zero + s.SetEnabled(boolPtr(false)) // False + s.SetTags(nil) // Nil slice + + bytes, err := s.MarshalJSON() + require.NoError(t, err) + + // All fields should be present even with nil/zero values + assert.JSONEq(t, `{"name":null,"code":"","count":0,"enabled":false,"tags":null}`, string(bytes)) + }) +} + +// Test for backwards compatibility +func TestBackwardsCompatibility(t *testing.T) { + t.Run("struct without setters behaves normally", func(t *testing.T) { + s := &testStructWithoutExplicitFields{ + Name: stringPtr("test"), + Code: nil, // This should be omitted + } + + bytes, err := json.Marshal(s) + require.NoError(t, err) + + // Without setters, omitempty works normally + assert.JSONEq(t, `{"name":"test"}`, string(bytes)) + }) + + t.Run("struct with explicit fields works with standard json.Marshal", func(t *testing.T) { + s := &testExplicitFieldsStruct{ + Name: stringPtr("test"), + } + s.SetCode(nil) + + // Using the custom MarshalJSON + bytes, err := s.MarshalJSON() + require.NoError(t, err) + + assert.JSONEq(t, `{"name":"test","code":null}`, string(bytes)) + }) +} + +// Helper functions +func stringPtr(s string) *string { + return &s +} + +func intPtr(i int) *int { + return &i +} + +func boolPtr(b bool) *bool { + return &b +} diff --git a/seed/go-sdk/basic-auth-optional/internal/extra_properties.go b/seed/go-sdk/basic-auth-optional/internal/extra_properties.go new file mode 100644 index 000000000000..540c3fd89eeb --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/extra_properties.go @@ -0,0 +1,141 @@ +package internal + +import ( + "bytes" + "encoding/json" + "fmt" + "reflect" + "strings" +) + +// MarshalJSONWithExtraProperty marshals the given value to JSON, including the extra property. +func MarshalJSONWithExtraProperty(marshaler interface{}, key string, value interface{}) ([]byte, error) { + return MarshalJSONWithExtraProperties(marshaler, map[string]interface{}{key: value}) +} + +// MarshalJSONWithExtraProperties marshals the given value to JSON, including any extra properties. +func MarshalJSONWithExtraProperties(marshaler interface{}, extraProperties map[string]interface{}) ([]byte, error) { + bytes, err := json.Marshal(marshaler) + if err != nil { + return nil, err + } + if len(extraProperties) == 0 { + return bytes, nil + } + keys, err := getKeys(marshaler) + if err != nil { + return nil, err + } + for _, key := range keys { + if _, ok := extraProperties[key]; ok { + return nil, fmt.Errorf("cannot add extra property %q because it is already defined on the type", key) + } + } + extraBytes, err := json.Marshal(extraProperties) + if err != nil { + return nil, err + } + if isEmptyJSON(bytes) { + if isEmptyJSON(extraBytes) { + return bytes, nil + } + return extraBytes, nil + } + result := bytes[:len(bytes)-1] + result = append(result, ',') + result = append(result, extraBytes[1:len(extraBytes)-1]...) + result = append(result, '}') + return result, nil +} + +// ExtractExtraProperties extracts any extra properties from the given value. +func ExtractExtraProperties(bytes []byte, value interface{}, exclude ...string) (map[string]interface{}, error) { + val := reflect.ValueOf(value) + for val.Kind() == reflect.Ptr { + if val.IsNil() { + return nil, fmt.Errorf("value must be non-nil to extract extra properties") + } + val = val.Elem() + } + if err := json.Unmarshal(bytes, &value); err != nil { + return nil, err + } + var extraProperties map[string]interface{} + if err := json.Unmarshal(bytes, &extraProperties); err != nil { + return nil, err + } + for i := 0; i < val.Type().NumField(); i++ { + key := jsonKey(val.Type().Field(i)) + if key == "" || key == "-" { + continue + } + delete(extraProperties, key) + } + for _, key := range exclude { + delete(extraProperties, key) + } + if len(extraProperties) == 0 { + return nil, nil + } + return extraProperties, nil +} + +// getKeys returns the keys associated with the given value. The value must be a +// a struct or a map with string keys. +func getKeys(value interface{}) ([]string, error) { + val := reflect.ValueOf(value) + if val.Kind() == reflect.Ptr { + val = val.Elem() + } + if !val.IsValid() { + return nil, nil + } + switch val.Kind() { + case reflect.Struct: + return getKeysForStructType(val.Type()), nil + case reflect.Map: + var keys []string + if val.Type().Key().Kind() != reflect.String { + return nil, fmt.Errorf("cannot extract keys from %T; only structs and maps with string keys are supported", value) + } + for _, key := range val.MapKeys() { + keys = append(keys, key.String()) + } + return keys, nil + default: + return nil, fmt.Errorf("cannot extract keys from %T; only structs and maps with string keys are supported", value) + } +} + +// getKeysForStructType returns all the keys associated with the given struct type, +// visiting embedded fields recursively. +func getKeysForStructType(structType reflect.Type) []string { + if structType.Kind() == reflect.Pointer { + structType = structType.Elem() + } + if structType.Kind() != reflect.Struct { + return nil + } + var keys []string + for i := 0; i < structType.NumField(); i++ { + field := structType.Field(i) + if field.Anonymous { + keys = append(keys, getKeysForStructType(field.Type)...) + continue + } + keys = append(keys, jsonKey(field)) + } + return keys +} + +// jsonKey returns the JSON key from the struct tag of the given field, +// excluding the omitempty flag (if any). +func jsonKey(field reflect.StructField) string { + return strings.TrimSuffix(field.Tag.Get("json"), ",omitempty") +} + +// isEmptyJSON returns true if the given data is empty, the empty JSON object, or +// an explicit null. +func isEmptyJSON(data []byte) bool { + return len(data) <= 2 || bytes.Equal(data, []byte("null")) +} diff --git a/seed/go-sdk/basic-auth-optional/internal/extra_properties_test.go b/seed/go-sdk/basic-auth-optional/internal/extra_properties_test.go new file mode 100644 index 000000000000..aa2510ee5121 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/extra_properties_test.go @@ -0,0 +1,228 @@ +package internal + +import ( + "encoding/json" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type testMarshaler struct { + Name string `json:"name"` + BirthDate time.Time `json:"birthDate"` + CreatedAt time.Time `json:"created_at"` +} + +func (t *testMarshaler) MarshalJSON() ([]byte, error) { + type embed testMarshaler + var marshaler = struct { + embed + BirthDate string `json:"birthDate"` + CreatedAt string `json:"created_at"` + }{ + embed: embed(*t), + BirthDate: t.BirthDate.Format("2006-01-02"), + CreatedAt: t.CreatedAt.Format(time.RFC3339), + } + return MarshalJSONWithExtraProperty(marshaler, "type", "test") +} + +func TestMarshalJSONWithExtraProperties(t *testing.T) { + tests := []struct { + desc string + giveMarshaler interface{} + giveExtraProperties map[string]interface{} + wantBytes []byte + wantError string + }{ + { + desc: "invalid type", + giveMarshaler: []string{"invalid"}, + giveExtraProperties: map[string]interface{}{"key": "overwrite"}, + wantError: `cannot extract keys from []string; only structs and maps with string keys are supported`, + }, + { + desc: "invalid key type", + giveMarshaler: map[int]interface{}{42: "value"}, + giveExtraProperties: map[string]interface{}{"key": "overwrite"}, + wantError: `cannot extract keys from map[int]interface {}; only structs and maps with string keys are supported`, + }, + { + desc: "invalid map overwrite", + giveMarshaler: map[string]interface{}{"key": "value"}, + giveExtraProperties: map[string]interface{}{"key": "overwrite"}, + wantError: `cannot add extra property "key" because it is already defined on the type`, + }, + { + desc: "invalid struct overwrite", + giveMarshaler: new(testMarshaler), + giveExtraProperties: map[string]interface{}{"birthDate": "2000-01-01"}, + wantError: `cannot add extra property "birthDate" because it is already defined on the type`, + }, + { + desc: "invalid struct overwrite embedded type", + giveMarshaler: new(testMarshaler), + giveExtraProperties: map[string]interface{}{"name": "bob"}, + wantError: `cannot add extra property "name" because it is already defined on the type`, + }, + { + desc: "nil", + giveMarshaler: nil, + giveExtraProperties: nil, + wantBytes: []byte(`null`), + }, + { + desc: "empty", + giveMarshaler: map[string]interface{}{}, + giveExtraProperties: map[string]interface{}{}, + wantBytes: []byte(`{}`), + }, + { + desc: "no extra properties", + giveMarshaler: map[string]interface{}{"key": "value"}, + giveExtraProperties: map[string]interface{}{}, + wantBytes: []byte(`{"key":"value"}`), + }, + { + desc: "only extra properties", + giveMarshaler: map[string]interface{}{}, + giveExtraProperties: map[string]interface{}{"key": "value"}, + wantBytes: []byte(`{"key":"value"}`), + }, + { + desc: "single extra property", + giveMarshaler: map[string]interface{}{"key": "value"}, + giveExtraProperties: map[string]interface{}{"extra": "property"}, + wantBytes: []byte(`{"key":"value","extra":"property"}`), + }, + { + desc: "multiple extra properties", + giveMarshaler: map[string]interface{}{"key": "value"}, + giveExtraProperties: map[string]interface{}{"one": 1, "two": 2}, + wantBytes: []byte(`{"key":"value","one":1,"two":2}`), + }, + { + desc: "nested properties", + giveMarshaler: map[string]interface{}{"key": "value"}, + giveExtraProperties: map[string]interface{}{ + "user": map[string]interface{}{ + "age": 42, + "name": "alice", + }, + }, + wantBytes: []byte(`{"key":"value","user":{"age":42,"name":"alice"}}`), + }, + { + desc: "multiple nested properties", + giveMarshaler: map[string]interface{}{"key": "value"}, + giveExtraProperties: map[string]interface{}{ + "metadata": map[string]interface{}{ + "ip": "127.0.0.1", + }, + "user": map[string]interface{}{ + "age": 42, + "name": "alice", + }, + }, + wantBytes: []byte(`{"key":"value","metadata":{"ip":"127.0.0.1"},"user":{"age":42,"name":"alice"}}`), + }, + { + desc: "custom marshaler", + giveMarshaler: &testMarshaler{ + Name: "alice", + BirthDate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), + CreatedAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), + }, + giveExtraProperties: map[string]interface{}{ + "extra": "property", + }, + wantBytes: []byte(`{"name":"alice","birthDate":"2000-01-01","created_at":"2024-01-01T00:00:00Z","type":"test","extra":"property"}`), + }, + } + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + bytes, err := MarshalJSONWithExtraProperties(tt.giveMarshaler, tt.giveExtraProperties) + if tt.wantError != "" { + require.EqualError(t, err, tt.wantError) + assert.Nil(t, tt.wantBytes) + return + } + require.NoError(t, err) + assert.Equal(t, tt.wantBytes, bytes) + + value := make(map[string]interface{}) + require.NoError(t, json.Unmarshal(bytes, &value)) + }) + } +} + +func TestExtractExtraProperties(t *testing.T) { + t.Run("none", func(t *testing.T) { + type user struct { + Name string `json:"name"` + } + value := &user{ + Name: "alice", + } + extraProperties, err := ExtractExtraProperties([]byte(`{"name": "alice"}`), value) + require.NoError(t, err) + assert.Nil(t, extraProperties) + }) + + t.Run("non-nil pointer", func(t *testing.T) { + type user struct { + Name string `json:"name"` + } + value := &user{ + Name: "alice", + } + extraProperties, err := ExtractExtraProperties([]byte(`{"name": "alice", "age": 42}`), value) + require.NoError(t, err) + assert.Equal(t, map[string]interface{}{"age": float64(42)}, extraProperties) + }) + + t.Run("nil pointer", func(t *testing.T) { + type user struct { + Name string `json:"name"` + } + var value *user + _, err := ExtractExtraProperties([]byte(`{"name": "alice", "age": 42}`), value) + assert.EqualError(t, err, "value must be non-nil to extract extra properties") + }) + + t.Run("non-zero value", func(t *testing.T) { + type user struct { + Name string `json:"name"` + } + value := user{ + Name: "alice", + } + extraProperties, err := ExtractExtraProperties([]byte(`{"name": "alice", "age": 42}`), value) + require.NoError(t, err) + assert.Equal(t, map[string]interface{}{"age": float64(42)}, extraProperties) + }) + + t.Run("zero value", func(t *testing.T) { + type user struct { + Name string `json:"name"` + } + var value user + extraProperties, err := ExtractExtraProperties([]byte(`{"name": "alice", "age": 42}`), value) + require.NoError(t, err) + assert.Equal(t, map[string]interface{}{"age": float64(42)}, extraProperties) + }) + + t.Run("exclude", func(t *testing.T) { + type user struct { + Name string `json:"name"` + } + value := &user{ + Name: "alice", + } + extraProperties, err := ExtractExtraProperties([]byte(`{"name": "alice", "age": 42}`), value, "age") + require.NoError(t, err) + assert.Nil(t, extraProperties) + }) +} diff --git a/seed/go-sdk/basic-auth-optional/internal/http.go b/seed/go-sdk/basic-auth-optional/internal/http.go new file mode 100644 index 000000000000..77863752bb58 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/http.go @@ -0,0 +1,71 @@ +package internal + +import ( + "fmt" + "net/http" + "net/url" + "reflect" +) + +// HTTPClient is an interface for a subset of the *http.Client. +type HTTPClient interface { + Do(*http.Request) (*http.Response, error) +} + +// ResolveBaseURL resolves the base URL from the given arguments, +// preferring the first non-empty value. +func ResolveBaseURL(values ...string) string { + for _, value := range values { + if value != "" { + return value + } + } + return "" +} + +// EncodeURL encodes the given arguments into the URL, escaping +// values as needed. Pointer arguments are dereferenced before processing. +func EncodeURL(urlFormat string, args ...interface{}) string { + escapedArgs := make([]interface{}, 0, len(args)) + for _, arg := range args { + // Dereference the argument if it's a pointer + value := dereferenceArg(arg) + escapedArgs = append(escapedArgs, url.PathEscape(fmt.Sprintf("%v", value))) + } + return fmt.Sprintf(urlFormat, escapedArgs...) +} + +// dereferenceArg dereferences a pointer argument if necessary, returning the underlying value. +// If the argument is not a pointer or is nil, it returns the argument as-is. +func dereferenceArg(arg interface{}) interface{} { + if arg == nil { + return arg + } + + v := reflect.ValueOf(arg) + + // Keep dereferencing until we get to a non-pointer value or hit nil + for v.Kind() == reflect.Ptr { + if v.IsNil() { + return nil + } + v = v.Elem() + } + + return v.Interface() +} + +// MergeHeaders merges the given headers together, where the right +// takes precedence over the left. +func MergeHeaders(left, right http.Header) http.Header { + for key, values := range right { + if len(values) > 1 { + left[key] = values + continue + } + if value := right.Get(key); value != "" { + left.Set(key, value) + } + } + return left +} diff --git a/seed/go-sdk/basic-auth-optional/internal/query.go b/seed/go-sdk/basic-auth-optional/internal/query.go new file mode 100644 index 000000000000..9b567f7a5563 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/query.go @@ -0,0 +1,358 @@ +package internal + +import ( + "encoding/base64" + "fmt" + "net/url" + "reflect" + "strings" + "time" + + "github.com/google/uuid" +) + +// RFC3339Milli is a time format string for RFC 3339 with millisecond precision. +// Go's time.RFC3339 omits fractional seconds and time.RFC3339Nano trims trailing +// zeros, so neither produces the fixed ".000" millisecond suffix that many APIs expect. +const RFC3339Milli = "2006-01-02T15:04:05.000Z07:00" + +var ( + bytesType = reflect.TypeOf([]byte{}) + queryEncoderType = reflect.TypeOf(new(QueryEncoder)).Elem() + timeType = reflect.TypeOf(time.Time{}) + uuidType = reflect.TypeOf(uuid.UUID{}) +) + +// QueryEncoder is an interface implemented by any type that wishes to encode +// itself into URL values in a non-standard way. +type QueryEncoder interface { + EncodeQueryValues(key string, v *url.Values) error +} + +// prepareValue handles common validation and unwrapping logic for both functions +func prepareValue(v interface{}) (reflect.Value, url.Values, error) { + values := make(url.Values) + val := reflect.ValueOf(v) + for val.Kind() == reflect.Ptr { + if val.IsNil() { + return reflect.Value{}, values, nil + } + val = val.Elem() + } + + if v == nil { + return reflect.Value{}, values, nil + } + + if val.Kind() != reflect.Struct { + return reflect.Value{}, nil, fmt.Errorf("query: Values() expects struct input. Got %v", val.Kind()) + } + + err := reflectValue(values, val, "") + if err != nil { + return reflect.Value{}, nil, err + } + + return val, values, nil +} + +// QueryValues encodes url.Values from request objects. +// +// Note: This type is inspired by Google's query encoding library, but +// supports far less customization and is tailored to fit this SDK's use case. +// +// Ref: https://github.com/google/go-querystring +func QueryValues(v interface{}) (url.Values, error) { + _, values, err := prepareValue(v) + return values, err +} + +// QueryValuesWithDefaults encodes url.Values from request objects +// and default values, merging the defaults into the request. +// It's expected that the values of defaults are wire names. +func QueryValuesWithDefaults(v interface{}, defaults map[string]interface{}) (url.Values, error) { + val, values, err := prepareValue(v) + if err != nil { + return values, err + } + if !val.IsValid() { + return values, nil + } + + // apply defaults to zero-value fields directly on the original struct + valType := val.Type() + for i := 0; i < val.NumField(); i++ { + field := val.Field(i) + fieldType := valType.Field(i) + fieldName := fieldType.Name + + if fieldType.PkgPath != "" && !fieldType.Anonymous { + // Skip unexported fields. + continue + } + + // check if field is zero value and we have a default for it + if field.CanSet() && field.IsZero() { + tag := fieldType.Tag.Get("url") + if tag == "" || tag == "-" { + continue + } + wireName, _ := parseTag(tag) + if wireName == "" { + wireName = fieldName + } + if defaultVal, exists := defaults[wireName]; exists { + values.Set(wireName, valueString(reflect.ValueOf(defaultVal), tagOptions{}, reflect.StructField{})) + } + } + } + + return values, err +} + +// reflectValue populates the values parameter from the struct fields in val. +// Embedded structs are followed recursively (using the rules defined in the +// Values function documentation) breadth-first. +func reflectValue(values url.Values, val reflect.Value, scope string) error { + typ := val.Type() + for i := 0; i < typ.NumField(); i++ { + sf := typ.Field(i) + if sf.PkgPath != "" && !sf.Anonymous { + // Skip unexported fields. + continue + } + + sv := val.Field(i) + tag := sf.Tag.Get("url") + if tag == "" || tag == "-" { + continue + } + + name, opts := parseTag(tag) + if name == "" { + name = sf.Name + } + + if scope != "" { + name = scope + "[" + name + "]" + } + + if opts.Contains("omitempty") && isEmptyValue(sv) { + continue + } + + if sv.Type().Implements(queryEncoderType) { + // If sv is a nil pointer and the custom encoder is defined on a non-pointer + // method receiver, set sv to the zero value of the underlying type + if !reflect.Indirect(sv).IsValid() && sv.Type().Elem().Implements(queryEncoderType) { + sv = reflect.New(sv.Type().Elem()) + } + + m := sv.Interface().(QueryEncoder) + if err := m.EncodeQueryValues(name, &values); err != nil { + return err + } + continue + } + + // Recursively dereference pointers, but stop at nil pointers. + for sv.Kind() == reflect.Ptr { + if sv.IsNil() { + break + } + sv = sv.Elem() + } + + if sv.Type() == uuidType || sv.Type() == bytesType || sv.Type() == timeType { + values.Add(name, valueString(sv, opts, sf)) + continue + } + + if sv.Kind() == reflect.Slice || sv.Kind() == reflect.Array { + if sv.Len() == 0 { + // Skip if slice or array is empty. + continue + } + for i := 0; i < sv.Len(); i++ { + value := sv.Index(i) + if isStructPointer(value) && !value.IsNil() { + if err := reflectValue(values, value.Elem(), name); err != nil { + return err + } + } else { + values.Add(name, valueString(value, opts, sf)) + } + } + continue + } + + if sv.Kind() == reflect.Map { + if err := reflectMap(values, sv, name); err != nil { + return err + } + continue + } + + if sv.Kind() == reflect.Struct { + if err := reflectValue(values, sv, name); err != nil { + return err + } + continue + } + + values.Add(name, valueString(sv, opts, sf)) + } + + return nil +} + +// reflectMap handles map types specifically, generating query parameters in the format key[mapkey]=value +func reflectMap(values url.Values, val reflect.Value, scope string) error { + if val.IsNil() { + return nil + } + + iter := val.MapRange() + for iter.Next() { + k := iter.Key() + v := iter.Value() + + key := fmt.Sprint(k.Interface()) + paramName := scope + "[" + key + "]" + + for v.Kind() == reflect.Ptr { + if v.IsNil() { + break + } + v = v.Elem() + } + + for v.Kind() == reflect.Interface { + v = v.Elem() + } + + if v.Kind() == reflect.Map { + if err := reflectMap(values, v, paramName); err != nil { + return err + } + continue + } + + if v.Kind() == reflect.Struct { + if err := reflectValue(values, v, paramName); err != nil { + return err + } + continue + } + + if v.Kind() == reflect.Slice || v.Kind() == reflect.Array { + if v.Len() == 0 { + continue + } + for i := 0; i < v.Len(); i++ { + value := v.Index(i) + if isStructPointer(value) && !value.IsNil() { + if err := reflectValue(values, value.Elem(), paramName); err != nil { + return err + } + } else { + values.Add(paramName, valueString(value, tagOptions{}, reflect.StructField{})) + } + } + continue + } + + values.Add(paramName, valueString(v, tagOptions{}, reflect.StructField{})) + } + + return nil +} + +// valueString returns the string representation of a value. +func valueString(v reflect.Value, opts tagOptions, sf reflect.StructField) string { + for v.Kind() == reflect.Ptr { + if v.IsNil() { + return "" + } + v = v.Elem() + } + + if v.Type() == timeType { + t := v.Interface().(time.Time) + if format := sf.Tag.Get("format"); format == "date" { + return t.Format("2006-01-02") + } + return t.Format(RFC3339Milli) + } + + if v.Type() == uuidType { + u := v.Interface().(uuid.UUID) + return u.String() + } + + if v.Type() == bytesType { + b := v.Interface().([]byte) + return base64.StdEncoding.EncodeToString(b) + } + + return fmt.Sprint(v.Interface()) +} + +// isEmptyValue checks if a value should be considered empty for the purposes +// of omitting fields with the "omitempty" option. +func isEmptyValue(v reflect.Value) bool { + type zeroable interface { + IsZero() bool + } + + if !v.IsZero() { + if z, ok := v.Interface().(zeroable); ok { + return z.IsZero() + } + } + + switch v.Kind() { + case reflect.Array, reflect.Map, reflect.Slice, reflect.String: + return v.Len() == 0 + case reflect.Bool: + return !v.Bool() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return v.Int() == 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return v.Uint() == 0 + case reflect.Float32, reflect.Float64: + return v.Float() == 0 + case reflect.Interface, reflect.Ptr: + return v.IsNil() + case reflect.Invalid, reflect.Complex64, reflect.Complex128, reflect.Chan, reflect.Func, reflect.Struct, reflect.UnsafePointer: + return false + } + + return false +} + +// isStructPointer returns true if the given reflect.Value is a pointer to a struct. +func isStructPointer(v reflect.Value) bool { + return v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct +} + +// tagOptions is the string following a comma in a struct field's "url" tag, or +// the empty string. It does not include the leading comma. +type tagOptions []string + +// parseTag splits a struct field's url tag into its name and comma-separated +// options. +func parseTag(tag string) (string, tagOptions) { + s := strings.Split(tag, ",") + return s[0], s[1:] +} + +// Contains checks whether the tagOptions contains the specified option. +func (o tagOptions) Contains(option string) bool { + for _, s := range o { + if s == option { + return true + } + } + return false +} diff --git a/seed/go-sdk/basic-auth-optional/internal/query_test.go b/seed/go-sdk/basic-auth-optional/internal/query_test.go new file mode 100644 index 000000000000..5b463e297350 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/query_test.go @@ -0,0 +1,395 @@ +package internal + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestQueryValues(t *testing.T) { + t.Run("empty optional", func(t *testing.T) { + type nested struct { + Value *string `json:"value,omitempty" url:"value,omitempty"` + } + type example struct { + Nested *nested `json:"nested,omitempty" url:"nested,omitempty"` + } + + values, err := QueryValues(&example{}) + require.NoError(t, err) + assert.Empty(t, values) + }) + + t.Run("empty required", func(t *testing.T) { + type nested struct { + Value *string `json:"value,omitempty" url:"value,omitempty"` + } + type example struct { + Required string `json:"required" url:"required"` + Nested *nested `json:"nested,omitempty" url:"nested,omitempty"` + } + + values, err := QueryValues(&example{}) + require.NoError(t, err) + assert.Equal(t, "required=", values.Encode()) + }) + + t.Run("allow multiple", func(t *testing.T) { + type example struct { + Values []string `json:"values" url:"values"` + } + + values, err := QueryValues( + &example{ + Values: []string{"foo", "bar", "baz"}, + }, + ) + require.NoError(t, err) + assert.Equal(t, "values=foo&values=bar&values=baz", values.Encode()) + }) + + t.Run("nested object", func(t *testing.T) { + type nested struct { + Value *string `json:"value,omitempty" url:"value,omitempty"` + } + type example struct { + Required string `json:"required" url:"required"` + Nested *nested `json:"nested,omitempty" url:"nested,omitempty"` + } + + nestedValue := "nestedValue" + values, err := QueryValues( + &example{ + Required: "requiredValue", + Nested: &nested{ + Value: &nestedValue, + }, + }, + ) + require.NoError(t, err) + assert.Equal(t, "nested%5Bvalue%5D=nestedValue&required=requiredValue", values.Encode()) + }) + + t.Run("url unspecified", func(t *testing.T) { + type example struct { + Required string `json:"required" url:"required"` + NotFound string `json:"notFound"` + } + + values, err := QueryValues( + &example{ + Required: "requiredValue", + NotFound: "notFound", + }, + ) + require.NoError(t, err) + assert.Equal(t, "required=requiredValue", values.Encode()) + }) + + t.Run("url ignored", func(t *testing.T) { + type example struct { + Required string `json:"required" url:"required"` + NotFound string `json:"notFound" url:"-"` + } + + values, err := QueryValues( + &example{ + Required: "requiredValue", + NotFound: "notFound", + }, + ) + require.NoError(t, err) + assert.Equal(t, "required=requiredValue", values.Encode()) + }) + + t.Run("datetime", func(t *testing.T) { + type example struct { + DateTime time.Time `json:"dateTime" url:"dateTime"` + } + + values, err := QueryValues( + &example{ + DateTime: time.Date(1994, 3, 16, 12, 34, 56, 0, time.UTC), + }, + ) + require.NoError(t, err) + assert.Equal(t, "dateTime=1994-03-16T12%3A34%3A56.000Z", values.Encode()) + }) + + t.Run("date", func(t *testing.T) { + type example struct { + Date time.Time `json:"date" url:"date" format:"date"` + } + + values, err := QueryValues( + &example{ + Date: time.Date(1994, 3, 16, 12, 34, 56, 0, time.UTC), + }, + ) + require.NoError(t, err) + assert.Equal(t, "date=1994-03-16", values.Encode()) + }) + + t.Run("optional time", func(t *testing.T) { + type example struct { + Date *time.Time `json:"date,omitempty" url:"date,omitempty" format:"date"` + } + + values, err := QueryValues( + &example{}, + ) + require.NoError(t, err) + assert.Empty(t, values.Encode()) + }) + + t.Run("omitempty with non-pointer zero value", func(t *testing.T) { + type enum string + + type example struct { + Enum enum `json:"enum,omitempty" url:"enum,omitempty"` + } + + values, err := QueryValues( + &example{}, + ) + require.NoError(t, err) + assert.Empty(t, values.Encode()) + }) + + t.Run("object array", func(t *testing.T) { + type object struct { + Key string `json:"key" url:"key"` + Value string `json:"value" url:"value"` + } + type example struct { + Objects []*object `json:"objects,omitempty" url:"objects,omitempty"` + } + + values, err := QueryValues( + &example{ + Objects: []*object{ + { + Key: "hello", + Value: "world", + }, + { + Key: "foo", + Value: "bar", + }, + }, + }, + ) + require.NoError(t, err) + assert.Equal(t, "objects%5Bkey%5D=hello&objects%5Bkey%5D=foo&objects%5Bvalue%5D=world&objects%5Bvalue%5D=bar", values.Encode()) + }) + + t.Run("map", func(t *testing.T) { + type request struct { + Metadata map[string]interface{} `json:"metadata" url:"metadata"` + } + values, err := QueryValues( + &request{ + Metadata: map[string]interface{}{ + "foo": "bar", + "baz": "qux", + }, + }, + ) + require.NoError(t, err) + assert.Equal(t, "metadata%5Bbaz%5D=qux&metadata%5Bfoo%5D=bar", values.Encode()) + }) + + t.Run("nested map", func(t *testing.T) { + type request struct { + Metadata map[string]interface{} `json:"metadata" url:"metadata"` + } + values, err := QueryValues( + &request{ + Metadata: map[string]interface{}{ + "inner": map[string]interface{}{ + "foo": "bar", + }, + }, + }, + ) + require.NoError(t, err) + assert.Equal(t, "metadata%5Binner%5D%5Bfoo%5D=bar", values.Encode()) + }) + + t.Run("nested map array", func(t *testing.T) { + type request struct { + Metadata map[string]interface{} `json:"metadata" url:"metadata"` + } + values, err := QueryValues( + &request{ + Metadata: map[string]interface{}{ + "inner": []string{ + "one", + "two", + "three", + }, + }, + }, + ) + require.NoError(t, err) + assert.Equal(t, "metadata%5Binner%5D=one&metadata%5Binner%5D=two&metadata%5Binner%5D=three", values.Encode()) + }) +} + +func TestQueryValuesWithDefaults(t *testing.T) { + t.Run("apply defaults to zero values", func(t *testing.T) { + type example struct { + Name string `json:"name" url:"name"` + Age int `json:"age" url:"age"` + Enabled bool `json:"enabled" url:"enabled"` + } + + defaults := map[string]interface{}{ + "name": "default-name", + "age": 25, + "enabled": true, + } + + values, err := QueryValuesWithDefaults(&example{}, defaults) + require.NoError(t, err) + assert.Equal(t, "age=25&enabled=true&name=default-name", values.Encode()) + }) + + t.Run("preserve non-zero values over defaults", func(t *testing.T) { + type example struct { + Name string `json:"name" url:"name"` + Age int `json:"age" url:"age"` + Enabled bool `json:"enabled" url:"enabled"` + } + + defaults := map[string]interface{}{ + "name": "default-name", + "age": 25, + "enabled": true, + } + + values, err := QueryValuesWithDefaults(&example{ + Name: "actual-name", + Age: 30, + // Enabled remains false (zero value), should get default + }, defaults) + require.NoError(t, err) + assert.Equal(t, "age=30&enabled=true&name=actual-name", values.Encode()) + }) + + t.Run("ignore defaults for fields not in struct", func(t *testing.T) { + type example struct { + Name string `json:"name" url:"name"` + Age int `json:"age" url:"age"` + } + + defaults := map[string]interface{}{ + "name": "default-name", + "age": 25, + "nonexistent": "should-be-ignored", + } + + values, err := QueryValuesWithDefaults(&example{}, defaults) + require.NoError(t, err) + assert.Equal(t, "age=25&name=default-name", values.Encode()) + }) + + t.Run("type conversion for compatible defaults", func(t *testing.T) { + type example struct { + Count int64 `json:"count" url:"count"` + Rate float64 `json:"rate" url:"rate"` + Message string `json:"message" url:"message"` + } + + defaults := map[string]interface{}{ + "count": int(100), // int -> int64 conversion + "rate": float32(2.5), // float32 -> float64 conversion + "message": "hello", // string -> string (no conversion needed) + } + + values, err := QueryValuesWithDefaults(&example{}, defaults) + require.NoError(t, err) + assert.Equal(t, "count=100&message=hello&rate=2.5", values.Encode()) + }) + + t.Run("mixed with pointer fields and omitempty", func(t *testing.T) { + type example struct { + Required string `json:"required" url:"required"` + Optional *string `json:"optional,omitempty" url:"optional,omitempty"` + Count int `json:"count,omitempty" url:"count,omitempty"` + } + + defaultOptional := "default-optional" + defaults := map[string]interface{}{ + "required": "default-required", + "optional": &defaultOptional, // pointer type + "count": 42, + } + + values, err := QueryValuesWithDefaults(&example{ + Required: "custom-required", // should override default + // Optional is nil, should get default + // Count is 0, should get default + }, defaults) + require.NoError(t, err) + assert.Equal(t, "count=42&optional=default-optional&required=custom-required", values.Encode()) + }) + + t.Run("override non-zero defaults with explicit zero values", func(t *testing.T) { + type example struct { + Name *string `json:"name" url:"name"` + Age *int `json:"age" url:"age"` + Enabled *bool `json:"enabled" url:"enabled"` + } + + defaults := map[string]interface{}{ + "name": "default-name", + "age": 25, + "enabled": true, + } + + // first, test that a properly empty request is overridden: + { + values, err := QueryValuesWithDefaults(&example{}, defaults) + require.NoError(t, err) + assert.Equal(t, "age=25&enabled=true&name=default-name", values.Encode()) + } + + // second, test that a request that contains zeros is not overridden: + var ( + name = "" + age = 0 + enabled = false + ) + values, err := QueryValuesWithDefaults(&example{ + Name: &name, // explicit empty string should override default + Age: &age, // explicit zero should override default + Enabled: &enabled, // explicit false should override default + }, defaults) + require.NoError(t, err) + assert.Equal(t, "age=0&enabled=false&name=", values.Encode()) + }) + + t.Run("nil input returns empty values", func(t *testing.T) { + defaults := map[string]any{ + "name": "default-name", + "age": 25, + } + + // Test with nil + values, err := QueryValuesWithDefaults(nil, defaults) + require.NoError(t, err) + assert.Empty(t, values) + + // Test with nil pointer + type example struct { + Name string `json:"name" url:"name"` + } + var nilPtr *example + values, err = QueryValuesWithDefaults(nilPtr, defaults) + require.NoError(t, err) + assert.Empty(t, values) + }) +} diff --git a/seed/go-sdk/basic-auth-optional/internal/retrier.go b/seed/go-sdk/basic-auth-optional/internal/retrier.go new file mode 100644 index 000000000000..02fd1fb7d3f1 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/retrier.go @@ -0,0 +1,239 @@ +package internal + +import ( + "crypto/rand" + "math/big" + "net/http" + "strconv" + "time" +) + +const ( + defaultRetryAttempts = 2 + minRetryDelay = 1000 * time.Millisecond + maxRetryDelay = 60000 * time.Millisecond +) + +// RetryOption adapts the behavior the *Retrier. +type RetryOption func(*retryOptions) + +// RetryFunc is a retryable HTTP function call (i.e. *http.Client.Do). +type RetryFunc func(*http.Request) (*http.Response, error) + +// WithMaxAttempts configures the maximum number of attempts +// of the *Retrier. +func WithMaxAttempts(attempts uint) RetryOption { + return func(opts *retryOptions) { + opts.attempts = attempts + } +} + +// Retrier retries failed requests a configurable number of times with an +// exponential back-off between each retry. +type Retrier struct { + attempts uint +} + +// NewRetrier constructs a new *Retrier with the given options, if any. +func NewRetrier(opts ...RetryOption) *Retrier { + options := new(retryOptions) + for _, opt := range opts { + opt(options) + } + attempts := uint(defaultRetryAttempts) + if options.attempts > 0 { + attempts = options.attempts + } + return &Retrier{ + attempts: attempts, + } +} + +// Run issues the request and, upon failure, retries the request if possible. +// +// The request will be retried as long as the request is deemed retryable and the +// number of retry attempts has not grown larger than the configured retry limit. +func (r *Retrier) Run( + fn RetryFunc, + request *http.Request, + errorDecoder ErrorDecoder, + opts ...RetryOption, +) (*http.Response, error) { + options := new(retryOptions) + for _, opt := range opts { + opt(options) + } + maxRetryAttempts := r.attempts + if options.attempts > 0 { + maxRetryAttempts = options.attempts + } + var ( + retryAttempt uint + previousError error + ) + return r.run( + fn, + request, + errorDecoder, + maxRetryAttempts, + retryAttempt, + previousError, + ) +} + +func (r *Retrier) run( + fn RetryFunc, + request *http.Request, + errorDecoder ErrorDecoder, + maxRetryAttempts uint, + retryAttempt uint, + previousError error, +) (*http.Response, error) { + if retryAttempt >= maxRetryAttempts { + return nil, previousError + } + + // If the call has been cancelled, don't issue the request. + if err := request.Context().Err(); err != nil { + return nil, err + } + + // Reset the request body for retries since the body may have already been read. + if retryAttempt > 0 && request.GetBody != nil { + requestBody, err := request.GetBody() + if err != nil { + return nil, err + } + request.Body = requestBody + } + + response, err := fn(request) + if err != nil { + return nil, err + } + + if r.shouldRetry(response) { + defer func() { _ = response.Body.Close() }() + + delay, err := r.retryDelay(response, retryAttempt) + if err != nil { + return nil, err + } + + time.Sleep(delay) + + return r.run( + fn, + request, + errorDecoder, + maxRetryAttempts, + retryAttempt+1, + decodeError(response, errorDecoder), + ) + } + + return response, nil +} + +// shouldRetry returns true if the request should be retried based on the given +// response status code. +func (r *Retrier) shouldRetry(response *http.Response) bool { + return response.StatusCode == http.StatusTooManyRequests || + response.StatusCode == http.StatusRequestTimeout || + response.StatusCode >= http.StatusInternalServerError +} + +// retryDelay calculates the delay time based on response headers, +// falling back to exponential backoff if no headers are present. +func (r *Retrier) retryDelay(response *http.Response, retryAttempt uint) (time.Duration, error) { + // Check for Retry-After header first (RFC 7231), applying no jitter + if retryAfter := response.Header.Get("Retry-After"); retryAfter != "" { + // Parse as number of seconds... + if seconds, err := strconv.Atoi(retryAfter); err == nil { + delay := time.Duration(seconds) * time.Second + if delay > 0 { + if delay > maxRetryDelay { + delay = maxRetryDelay + } + return delay, nil + } + } + + // ...or as an HTTP date; both are valid + if retryTime, err := time.Parse(time.RFC1123, retryAfter); err == nil { + delay := time.Until(retryTime) + if delay > 0 { + if delay > maxRetryDelay { + delay = maxRetryDelay + } + return delay, nil + } + } + } + + // Then check for industry-standard X-RateLimit-Reset header, applying positive jitter + if rateLimitReset := response.Header.Get("X-RateLimit-Reset"); rateLimitReset != "" { + if resetTimestamp, err := strconv.ParseInt(rateLimitReset, 10, 64); err == nil { + // Assume Unix timestamp in seconds + resetTime := time.Unix(resetTimestamp, 0) + delay := time.Until(resetTime) + if delay > 0 { + if delay > maxRetryDelay { + delay = maxRetryDelay + } + return r.addPositiveJitter(delay) + } + } + } + + // Fall back to exponential backoff + return r.exponentialBackoff(retryAttempt) +} + +// exponentialBackoff calculates the delay time based on the retry attempt +// and applies symmetric jitter (±10% around the delay). +func (r *Retrier) exponentialBackoff(retryAttempt uint) (time.Duration, error) { + if retryAttempt > 63 { // 2^63+ would overflow uint64 + retryAttempt = 63 + } + + delay := minRetryDelay << retryAttempt + if delay > maxRetryDelay { + delay = maxRetryDelay + } + + return r.addSymmetricJitter(delay) +} + +// addJitterWithRange applies jitter to the given delay. +// minPercent and maxPercent define the jitter range (e.g., 100, 120 for +0% to +20%). +func (r *Retrier) addJitterWithRange(delay time.Duration, minPercent, maxPercent int) (time.Duration, error) { + jitterRange := big.NewInt(int64(delay * time.Duration(maxPercent-minPercent) / 100)) + jitter, err := rand.Int(rand.Reader, jitterRange) + if err != nil { + return 0, err + } + + jitteredDelay := delay + time.Duration(jitter.Int64()) + delay*time.Duration(minPercent-100)/100 + if jitteredDelay < minRetryDelay { + jitteredDelay = minRetryDelay + } + if jitteredDelay > maxRetryDelay { + jitteredDelay = maxRetryDelay + } + return jitteredDelay, nil +} + +// addPositiveJitter applies positive jitter to the given delay (100%-120% range). +func (r *Retrier) addPositiveJitter(delay time.Duration) (time.Duration, error) { + return r.addJitterWithRange(delay, 100, 120) +} + +// addSymmetricJitter applies symmetric jitter to the given delay (90%-110% range). +func (r *Retrier) addSymmetricJitter(delay time.Duration) (time.Duration, error) { + return r.addJitterWithRange(delay, 90, 110) +} + +type retryOptions struct { + attempts uint +} diff --git a/seed/go-sdk/basic-auth-optional/internal/retrier_test.go b/seed/go-sdk/basic-auth-optional/internal/retrier_test.go new file mode 100644 index 000000000000..c45822871638 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/retrier_test.go @@ -0,0 +1,352 @@ +package internal + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/basic-auth-optional/fern/core" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type RetryTestCase struct { + description string + + giveAttempts uint + giveStatusCodes []int + giveResponse *InternalTestResponse + + wantResponse *InternalTestResponse + wantError *core.APIError +} + +func TestRetrier(t *testing.T) { + tests := []*RetryTestCase{ + { + description: "retry request succeeds after multiple failures", + giveAttempts: 3, + giveStatusCodes: []int{ + http.StatusServiceUnavailable, + http.StatusServiceUnavailable, + http.StatusOK, + }, + giveResponse: &InternalTestResponse{ + Id: "1", + }, + wantResponse: &InternalTestResponse{ + Id: "1", + }, + }, + { + description: "retry request fails if MaxAttempts is exceeded", + giveAttempts: 3, + giveStatusCodes: []int{ + http.StatusRequestTimeout, + http.StatusRequestTimeout, + http.StatusRequestTimeout, + http.StatusOK, + }, + wantError: &core.APIError{ + StatusCode: http.StatusRequestTimeout, + }, + }, + { + description: "retry durations increase exponentially and stay within the min and max delay values", + giveAttempts: 4, + giveStatusCodes: []int{ + http.StatusServiceUnavailable, + http.StatusServiceUnavailable, + http.StatusServiceUnavailable, + http.StatusOK, + }, + }, + { + description: "retry does not occur on status code 404", + giveAttempts: 2, + giveStatusCodes: []int{http.StatusNotFound, http.StatusOK}, + wantError: &core.APIError{ + StatusCode: http.StatusNotFound, + }, + }, + { + description: "retries occur on status code 429", + giveAttempts: 2, + giveStatusCodes: []int{http.StatusTooManyRequests, http.StatusOK}, + }, + { + description: "retries occur on status code 408", + giveAttempts: 2, + giveStatusCodes: []int{http.StatusRequestTimeout, http.StatusOK}, + }, + { + description: "retries occur on status code 500", + giveAttempts: 2, + giveStatusCodes: []int{http.StatusInternalServerError, http.StatusOK}, + }, + } + + for _, tc := range tests { + t.Run(tc.description, func(t *testing.T) { + var ( + test = tc + server = newTestRetryServer(t, test) + client = server.Client() + ) + + t.Parallel() + + caller := NewCaller( + &CallerParams{ + Client: client, + }, + ) + + var response *InternalTestResponse + _, err := caller.Call( + context.Background(), + &CallParams{ + URL: server.URL, + Method: http.MethodGet, + Request: &InternalTestRequest{}, + Response: &response, + MaxAttempts: test.giveAttempts, + ResponseIsOptional: true, + }, + ) + + if test.wantError != nil { + require.IsType(t, err, &core.APIError{}) + expectedErrorCode := test.wantError.StatusCode + actualErrorCode := err.(*core.APIError).StatusCode + assert.Equal(t, expectedErrorCode, actualErrorCode) + return + } + + require.NoError(t, err) + assert.Equal(t, test.wantResponse, response) + }) + } +} + +// newTestRetryServer returns a new *httptest.Server configured with the +// given test parameters, suitable for testing retries. +func newTestRetryServer(t *testing.T, tc *RetryTestCase) *httptest.Server { + var index int + timestamps := make([]time.Time, 0, len(tc.giveStatusCodes)) + + return httptest.NewServer( + http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + timestamps = append(timestamps, time.Now()) + if index > 0 && index < len(expectedRetryDurations) { + // Ensure that the duration between retries increases exponentially, + // and that it is within the minimum and maximum retry delay values. + actualDuration := timestamps[index].Sub(timestamps[index-1]) + expectedDurationMin := expectedRetryDurations[index-1] * 50 / 100 + expectedDurationMax := expectedRetryDurations[index-1] * 150 / 100 + assert.True( + t, + actualDuration >= expectedDurationMin && actualDuration <= expectedDurationMax, + "expected duration to be in range [%v, %v], got %v", + expectedDurationMin, + expectedDurationMax, + actualDuration, + ) + assert.LessOrEqual( + t, + actualDuration, + maxRetryDelay, + "expected duration to be less than the maxRetryDelay (%v), got %v", + maxRetryDelay, + actualDuration, + ) + assert.GreaterOrEqual( + t, + actualDuration, + minRetryDelay, + "expected duration to be greater than the minRetryDelay (%v), got %v", + minRetryDelay, + actualDuration, + ) + } + + request := new(InternalTestRequest) + bytes, err := io.ReadAll(r.Body) + require.NoError(t, err) + require.NoError(t, json.Unmarshal(bytes, request)) + require.LessOrEqual(t, index, len(tc.giveStatusCodes)) + + statusCode := tc.giveStatusCodes[index] + + w.WriteHeader(statusCode) + + if tc.giveResponse != nil && statusCode == http.StatusOK { + bytes, err = json.Marshal(tc.giveResponse) + require.NoError(t, err) + _, err = w.Write(bytes) + require.NoError(t, err) + } + + index++ + }, + ), + ) +} + +// expectedRetryDurations holds an array of calculated retry durations, +// where the index of the array should correspond to the retry attempt. +// +// Values are calculated based off of `minRetryDelay * 2^i`. +var expectedRetryDurations = []time.Duration{ + 1000 * time.Millisecond, // 500ms * 2^1 = 1000ms + 2000 * time.Millisecond, // 500ms * 2^2 = 2000ms + 4000 * time.Millisecond, // 500ms * 2^3 = 4000ms + 8000 * time.Millisecond, // 500ms * 2^4 = 8000ms +} + +func TestRetryWithRequestBody(t *testing.T) { + // This test verifies that POST requests with a body are properly retried. + // The request body should be re-sent on each retry attempt. + expectedBody := `{"id":"test-id"}` + var requestBodies []string + var requestCount int + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requestCount++ + bodyBytes, err := io.ReadAll(r.Body) + require.NoError(t, err) + requestBodies = append(requestBodies, string(bodyBytes)) + + if requestCount == 1 { + // First request - return retryable error + w.WriteHeader(http.StatusServiceUnavailable) + return + } + // Second request - return success + w.WriteHeader(http.StatusOK) + response := &InternalTestResponse{Id: "success"} + bytes, _ := json.Marshal(response) + _, _ = w.Write(bytes) + })) + defer server.Close() + + caller := NewCaller(&CallerParams{ + Client: server.Client(), + }) + + var response *InternalTestResponse + _, err := caller.Call( + context.Background(), + &CallParams{ + URL: server.URL, + Method: http.MethodPost, + Request: &InternalTestRequest{Id: "test-id"}, + Response: &response, + MaxAttempts: 2, + ResponseIsOptional: true, + }, + ) + + require.NoError(t, err) + require.Equal(t, 2, requestCount, "Expected exactly 2 requests") + require.Len(t, requestBodies, 2, "Expected 2 request bodies to be captured") + + // Both requests should have the same non-empty body + assert.Equal(t, expectedBody, requestBodies[0], "First request body should match expected") + assert.Equal(t, expectedBody, requestBodies[1], "Second request body should match expected (retry should re-send body)") +} + +func TestRetryDelayTiming(t *testing.T) { + tests := []struct { + name string + headerName string + headerValueFunc func() string + expectedMinMs int64 + expectedMaxMs int64 + }{ + { + name: "retry-after with seconds value", + headerName: "retry-after", + headerValueFunc: func() string { + return "1" + }, + expectedMinMs: 500, + expectedMaxMs: 1500, + }, + { + name: "retry-after with HTTP date", + headerName: "retry-after", + headerValueFunc: func() string { + return time.Now().Add(3 * time.Second).Format(time.RFC1123) + }, + expectedMinMs: 1500, + expectedMaxMs: 4500, + }, + { + name: "x-ratelimit-reset with future timestamp", + headerName: "x-ratelimit-reset", + headerValueFunc: func() string { + return fmt.Sprintf("%d", time.Now().Add(3*time.Second).Unix()) + }, + expectedMinMs: 1500, + expectedMaxMs: 4500, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + var timestamps []time.Time + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + timestamps = append(timestamps, time.Now()) + if len(timestamps) == 1 { + // First request - return retryable error with header + w.Header().Set(tt.headerName, tt.headerValueFunc()) + w.WriteHeader(http.StatusTooManyRequests) + } else { + // Second request - return success + w.WriteHeader(http.StatusOK) + response := &InternalTestResponse{Id: "success"} + bytes, _ := json.Marshal(response) + _, _ = w.Write(bytes) + } + })) + defer server.Close() + + caller := NewCaller(&CallerParams{ + Client: server.Client(), + }) + + var response *InternalTestResponse + _, err := caller.Call( + context.Background(), + &CallParams{ + URL: server.URL, + Method: http.MethodGet, + Request: &InternalTestRequest{}, + Response: &response, + MaxAttempts: 2, + ResponseIsOptional: true, + }, + ) + + require.NoError(t, err) + require.Len(t, timestamps, 2, "Expected exactly 2 requests") + + actualDelayMs := timestamps[1].Sub(timestamps[0]).Milliseconds() + + assert.GreaterOrEqual(t, actualDelayMs, tt.expectedMinMs, + "Actual delay %dms should be >= expected min %dms", actualDelayMs, tt.expectedMinMs) + assert.LessOrEqual(t, actualDelayMs, tt.expectedMaxMs, + "Actual delay %dms should be <= expected max %dms", actualDelayMs, tt.expectedMaxMs) + }) + } +} diff --git a/seed/go-sdk/basic-auth-optional/internal/stringer.go b/seed/go-sdk/basic-auth-optional/internal/stringer.go new file mode 100644 index 000000000000..312801851e0e --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/stringer.go @@ -0,0 +1,13 @@ +package internal + +import "encoding/json" + +// StringifyJSON returns a pretty JSON string representation of +// the given value. +func StringifyJSON(value interface{}) (string, error) { + bytes, err := json.MarshalIndent(value, "", " ") + if err != nil { + return "", err + } + return string(bytes), nil +} diff --git a/seed/go-sdk/basic-auth-optional/internal/time.go b/seed/go-sdk/basic-auth-optional/internal/time.go new file mode 100644 index 000000000000..57f901a35ed8 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/internal/time.go @@ -0,0 +1,165 @@ +package internal + +import ( + "encoding/json" + "fmt" + "time" +) + +const dateFormat = "2006-01-02" + +// DateTime wraps time.Time and adapts its JSON representation +// to conform to a RFC3339 date (e.g. 2006-01-02). +// +// Ref: https://ijmacd.github.io/rfc3339-iso8601 +type Date struct { + t *time.Time +} + +// NewDate returns a new *Date. If the given time.Time +// is nil, nil will be returned. +func NewDate(t time.Time) *Date { + return &Date{t: &t} +} + +// NewOptionalDate returns a new *Date. If the given time.Time +// is nil, nil will be returned. +func NewOptionalDate(t *time.Time) *Date { + if t == nil { + return nil + } + return &Date{t: t} +} + +// Time returns the Date's underlying time, if any. If the +// date is nil, the zero value is returned. +func (d *Date) Time() time.Time { + if d == nil || d.t == nil { + return time.Time{} + } + return *d.t +} + +// TimePtr returns a pointer to the Date's underlying time.Time, if any. +func (d *Date) TimePtr() *time.Time { + if d == nil || d.t == nil { + return nil + } + if d.t.IsZero() { + return nil + } + return d.t +} + +func (d *Date) MarshalJSON() ([]byte, error) { + if d == nil || d.t == nil { + return nil, nil + } + return json.Marshal(d.t.Format(dateFormat)) +} + +func (d *Date) UnmarshalJSON(data []byte) error { + var raw string + if err := json.Unmarshal(data, &raw); err != nil { + return err + } + + parsedTime, err := time.Parse(dateFormat, raw) + if err != nil { + return err + } + + *d = Date{t: &parsedTime} + return nil +} + +// DateTime wraps time.Time and adapts its JSON representation +// to conform to a RFC3339 date-time (e.g. 2017-07-21T17:32:28Z). +// +// Ref: https://ijmacd.github.io/rfc3339-iso8601 +type DateTime struct { + t *time.Time +} + +// NewDateTime returns a new *DateTime. +func NewDateTime(t time.Time) *DateTime { + return &DateTime{t: &t} +} + +// NewOptionalDateTime returns a new *DateTime. If the given time.Time +// is nil, nil will be returned. +func NewOptionalDateTime(t *time.Time) *DateTime { + if t == nil { + return nil + } + return &DateTime{t: t} +} + +// Time returns the DateTime's underlying time, if any. If the +// date-time is nil, the zero value is returned. +func (d *DateTime) Time() time.Time { + if d == nil || d.t == nil { + return time.Time{} + } + return *d.t +} + +// TimePtr returns a pointer to the DateTime's underlying time.Time, if any. +func (d *DateTime) TimePtr() *time.Time { + if d == nil || d.t == nil { + return nil + } + if d.t.IsZero() { + return nil + } + return d.t +} + +func (d *DateTime) MarshalJSON() ([]byte, error) { + if d == nil || d.t == nil { + return nil, nil + } + return json.Marshal(d.t.Format(time.RFC3339)) +} + +func (d *DateTime) UnmarshalJSON(data []byte) error { + var raw string + if err := json.Unmarshal(data, &raw); err != nil { + // If the value is not a string, check if it is a number (unix epoch seconds). + var epoch int64 + if numErr := json.Unmarshal(data, &epoch); numErr == nil { + t := time.Unix(epoch, 0).UTC() + *d = DateTime{t: &t} + return nil + } + return err + } + + // Try RFC3339Nano first (superset of RFC3339, supports fractional seconds). + parsedTime, err := time.Parse(time.RFC3339Nano, raw) + if err == nil { + *d = DateTime{t: &parsedTime} + return nil + } + rfc3339NanoErr := err + + // Fall back to ISO 8601 without timezone (assume UTC). + parsedTime, err = time.Parse("2006-01-02T15:04:05", raw) + if err == nil { + parsedTime = parsedTime.UTC() + *d = DateTime{t: &parsedTime} + return nil + } + iso8601Err := err + + // Fall back to date-only format. + parsedTime, err = time.Parse("2006-01-02", raw) + if err == nil { + parsedTime = parsedTime.UTC() + *d = DateTime{t: &parsedTime} + return nil + } + dateOnlyErr := err + + return fmt.Errorf("unable to parse datetime string %q: tried RFC3339Nano (%v), ISO8601 (%v), date-only (%v)", raw, rfc3339NanoErr, iso8601Err, dateOnlyErr) +} diff --git a/seed/go-sdk/basic-auth-optional/option/request_option.go b/seed/go-sdk/basic-auth-optional/option/request_option.go new file mode 100644 index 000000000000..cf173ed05305 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/option/request_option.go @@ -0,0 +1,81 @@ +// Code generated by Fern. DO NOT EDIT. + +package option + +import ( + core "github.com/basic-auth-optional/fern/core" + http "net/http" + url "net/url" +) + +// RequestOption adapts the behavior of an individual request. +type RequestOption = core.RequestOption + +// WithBaseURL sets the base URL, overriding the default +// environment, if any. +func WithBaseURL(baseURL string) *core.BaseURLOption { + return &core.BaseURLOption{ + BaseURL: baseURL, + } +} + +// WithHTTPClient uses the given HTTPClient to issue the request. +func WithHTTPClient(httpClient core.HTTPClient) *core.HTTPClientOption { + return &core.HTTPClientOption{ + HTTPClient: httpClient, + } +} + +// WithHTTPHeader adds the given http.Header to the request. +func WithHTTPHeader(httpHeader http.Header) *core.HTTPHeaderOption { + return &core.HTTPHeaderOption{ + // Clone the headers so they can't be modified after the option call. + HTTPHeader: httpHeader.Clone(), + } +} + +// WithBodyProperties adds the given body properties to the request. +func WithBodyProperties(bodyProperties map[string]interface{}) *core.BodyPropertiesOption { + copiedBodyProperties := make(map[string]interface{}, len(bodyProperties)) + for key, value := range bodyProperties { + copiedBodyProperties[key] = value + } + return &core.BodyPropertiesOption{ + BodyProperties: copiedBodyProperties, + } +} + +// WithQueryParameters adds the given query parameters to the request. +func WithQueryParameters(queryParameters url.Values) *core.QueryParametersOption { + copiedQueryParameters := make(url.Values, len(queryParameters)) + for key, values := range queryParameters { + copiedQueryParameters[key] = values + } + return &core.QueryParametersOption{ + QueryParameters: copiedQueryParameters, + } +} + +// WithMaxAttempts configures the maximum number of retry attempts. +func WithMaxAttempts(attempts uint) *core.MaxAttemptsOption { + return &core.MaxAttemptsOption{ + MaxAttempts: attempts, + } +} + +// WithMaxStreamBufSize configures the maximum buffer size for streaming responses. +// This controls the maximum size of a single message (in bytes) that the stream +// can process. By default, this is set to 1MB. +func WithMaxStreamBufSize(size int) *core.MaxBufSizeOption { + return &core.MaxBufSizeOption{ + MaxBufSize: size, + } +} + +// WithBasicAuth sets the 'Authorization: Basic ' request header. +func WithBasicAuth(username, password string) *core.BasicAuthOption { + return &core.BasicAuthOption{ + Username: username, + Password: password, + } +} diff --git a/seed/go-sdk/basic-auth-optional/pointer.go b/seed/go-sdk/basic-auth-optional/pointer.go new file mode 100644 index 000000000000..9be282560124 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/pointer.go @@ -0,0 +1,137 @@ +package basicauthoptional + +import ( + "time" + + "github.com/google/uuid" +) + +// Bool returns a pointer to the given bool value. +func Bool(b bool) *bool { + return &b +} + +// Byte returns a pointer to the given byte value. +func Byte(b byte) *byte { + return &b +} + +// Bytes returns a pointer to the given []byte value. +func Bytes(b []byte) *[]byte { + return &b +} + +// Complex64 returns a pointer to the given complex64 value. +func Complex64(c complex64) *complex64 { + return &c +} + +// Complex128 returns a pointer to the given complex128 value. +func Complex128(c complex128) *complex128 { + return &c +} + +// Float32 returns a pointer to the given float32 value. +func Float32(f float32) *float32 { + return &f +} + +// Float64 returns a pointer to the given float64 value. +func Float64(f float64) *float64 { + return &f +} + +// Int returns a pointer to the given int value. +func Int(i int) *int { + return &i +} + +// Int8 returns a pointer to the given int8 value. +func Int8(i int8) *int8 { + return &i +} + +// Int16 returns a pointer to the given int16 value. +func Int16(i int16) *int16 { + return &i +} + +// Int32 returns a pointer to the given int32 value. +func Int32(i int32) *int32 { + return &i +} + +// Int64 returns a pointer to the given int64 value. +func Int64(i int64) *int64 { + return &i +} + +// Rune returns a pointer to the given rune value. +func Rune(r rune) *rune { + return &r +} + +// String returns a pointer to the given string value. +func String(s string) *string { + return &s +} + +// Uint returns a pointer to the given uint value. +func Uint(u uint) *uint { + return &u +} + +// Uint8 returns a pointer to the given uint8 value. +func Uint8(u uint8) *uint8 { + return &u +} + +// Uint16 returns a pointer to the given uint16 value. +func Uint16(u uint16) *uint16 { + return &u +} + +// Uint32 returns a pointer to the given uint32 value. +func Uint32(u uint32) *uint32 { + return &u +} + +// Uint64 returns a pointer to the given uint64 value. +func Uint64(u uint64) *uint64 { + return &u +} + +// Uintptr returns a pointer to the given uintptr value. +func Uintptr(u uintptr) *uintptr { + return &u +} + +// UUID returns a pointer to the given uuid.UUID value. +func UUID(u uuid.UUID) *uuid.UUID { + return &u +} + +// Time returns a pointer to the given time.Time value. +func Time(t time.Time) *time.Time { + return &t +} + +// MustParseDate attempts to parse the given string as a +// date time.Time, and panics upon failure. +func MustParseDate(date string) time.Time { + t, err := time.Parse("2006-01-02", date) + if err != nil { + panic(err) + } + return t +} + +// MustParseDateTime attempts to parse the given string as a +// datetime time.Time, and panics upon failure. +func MustParseDateTime(datetime string) time.Time { + t, err := time.Parse(time.RFC3339, datetime) + if err != nil { + panic(err) + } + return t +} diff --git a/seed/go-sdk/basic-auth-optional/pointer_test.go b/seed/go-sdk/basic-auth-optional/pointer_test.go new file mode 100644 index 000000000000..3769e25c530a --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/pointer_test.go @@ -0,0 +1,211 @@ +package basicauthoptional + +import ( + "testing" + "time" + + "github.com/google/uuid" + "github.com/stretchr/testify/assert" +) + +func TestBool(t *testing.T) { + value := true + ptr := Bool(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestByte(t *testing.T) { + value := byte(42) + ptr := Byte(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestComplex64(t *testing.T) { + value := complex64(1 + 2i) + ptr := Complex64(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestComplex128(t *testing.T) { + value := complex128(1 + 2i) + ptr := Complex128(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestFloat32(t *testing.T) { + value := float32(3.14) + ptr := Float32(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestFloat64(t *testing.T) { + value := 3.14159 + ptr := Float64(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestInt(t *testing.T) { + value := 42 + ptr := Int(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestInt8(t *testing.T) { + value := int8(42) + ptr := Int8(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestInt16(t *testing.T) { + value := int16(42) + ptr := Int16(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestInt32(t *testing.T) { + value := int32(42) + ptr := Int32(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestInt64(t *testing.T) { + value := int64(42) + ptr := Int64(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestRune(t *testing.T) { + value := 'A' + ptr := Rune(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestString(t *testing.T) { + value := "hello" + ptr := String(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestUint(t *testing.T) { + value := uint(42) + ptr := Uint(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestUint8(t *testing.T) { + value := uint8(42) + ptr := Uint8(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestUint16(t *testing.T) { + value := uint16(42) + ptr := Uint16(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestUint32(t *testing.T) { + value := uint32(42) + ptr := Uint32(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestUint64(t *testing.T) { + value := uint64(42) + ptr := Uint64(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestUintptr(t *testing.T) { + value := uintptr(42) + ptr := Uintptr(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestUUID(t *testing.T) { + value := uuid.New() + ptr := UUID(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestTime(t *testing.T) { + value := time.Now() + ptr := Time(value) + assert.NotNil(t, ptr) + assert.Equal(t, value, *ptr) +} + +func TestMustParseDate(t *testing.T) { + t.Run("valid date", func(t *testing.T) { + result := MustParseDate("2024-01-15") + expected, _ := time.Parse("2006-01-02", "2024-01-15") + assert.Equal(t, expected, result) + }) + + t.Run("invalid date panics", func(t *testing.T) { + assert.Panics(t, func() { + MustParseDate("invalid-date") + }) + }) +} + +func TestMustParseDateTime(t *testing.T) { + t.Run("valid datetime", func(t *testing.T) { + result := MustParseDateTime("2024-01-15T10:30:00Z") + expected, _ := time.Parse(time.RFC3339, "2024-01-15T10:30:00Z") + assert.Equal(t, expected, result) + }) + + t.Run("invalid datetime panics", func(t *testing.T) { + assert.Panics(t, func() { + MustParseDateTime("invalid-datetime") + }) + }) +} + +func TestPointerHelpersWithZeroValues(t *testing.T) { + t.Run("zero bool", func(t *testing.T) { + ptr := Bool(false) + assert.NotNil(t, ptr) + assert.Equal(t, false, *ptr) + }) + + t.Run("zero int", func(t *testing.T) { + ptr := Int(0) + assert.NotNil(t, ptr) + assert.Equal(t, 0, *ptr) + }) + + t.Run("empty string", func(t *testing.T) { + ptr := String("") + assert.NotNil(t, ptr) + assert.Equal(t, "", *ptr) + }) + + t.Run("zero time", func(t *testing.T) { + zeroTime := time.Time{} + ptr := Time(zeroTime) + assert.NotNil(t, ptr) + assert.Equal(t, zeroTime, *ptr) + }) +} diff --git a/seed/go-sdk/basic-auth-optional/reference.md b/seed/go-sdk/basic-auth-optional/reference.md new file mode 100644 index 000000000000..d7481978d71f --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/reference.md @@ -0,0 +1,105 @@ +# Reference +## BasicAuth +
client.BasicAuth.GetWithBasicAuth() -> bool +
+
+ +#### 📝 Description + +
+
+ +
+
+ +GET request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```go +client.BasicAuth.GetWithBasicAuth( + context.TODO(), + ) +} +``` +
+
+
+
+ + +
+
+
+ +
client.BasicAuth.PostWithBasicAuth(request) -> bool +
+
+ +#### 📝 Description + +
+
+ +
+
+ +POST request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```go +request := map[string]any{ + "key": "value", + } +client.BasicAuth.PostWithBasicAuth( + context.TODO(), + request, + ) +} +``` +
+
+
+
+ +#### ⚙️ Parameters + +
+
+ +
+
+ +**request:** `any` + +
+
+
+
+ + +
+
+
+ diff --git a/seed/go-sdk/basic-auth-optional/snippet.json b/seed/go-sdk/basic-auth-optional/snippet.json new file mode 100644 index 000000000000..b5e8b81b2d5f --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/snippet.json @@ -0,0 +1,26 @@ +{ + "endpoints": [ + { + "id": { + "path": "/basic-auth", + "method": "GET", + "identifier_override": "endpoint_basic-auth.getWithBasicAuth" + }, + "snippet": { + "type": "go", + "client": "import (\n\tcontext \"context\"\n\tfernclient \"github.com/basic-auth-optional/fern/client\"\n\toption \"github.com/basic-auth-optional/fern/option\"\n)\n\nclient := fernclient.NewClient(\n\toption.WithBasicAuth(\n\t\t\"\u003cYOUR_USERNAME\u003e\",\n\t\t\"\u003cYOUR_PASSWORD\u003e\",\n\t),\n)\nresponse, err := client.BasicAuth.GetWithBasicAuth(\n\tcontext.TODO(),\n)\n" + } + }, + { + "id": { + "path": "/basic-auth", + "method": "POST", + "identifier_override": "endpoint_basic-auth.postWithBasicAuth" + }, + "snippet": { + "type": "go", + "client": "import (\n\tcontext \"context\"\n\tfernclient \"github.com/basic-auth-optional/fern/client\"\n\toption \"github.com/basic-auth-optional/fern/option\"\n)\n\nclient := fernclient.NewClient(\n\toption.WithBasicAuth(\n\t\t\"\u003cYOUR_USERNAME\u003e\",\n\t\t\"\u003cYOUR_PASSWORD\u003e\",\n\t),\n)\nresponse, err := client.BasicAuth.PostWithBasicAuth(\n\tcontext.TODO(),\n\tmap[string]interface{}{\n\t\t\"key\": \"value\",\n\t},\n)\n" + } + } + ] +} \ No newline at end of file diff --git a/seed/go-sdk/basic-auth-optional/types.go b/seed/go-sdk/basic-auth-optional/types.go new file mode 100644 index 000000000000..fc9f46088535 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/types.go @@ -0,0 +1,94 @@ +// Code generated by Fern. DO NOT EDIT. + +package basicauthoptional + +import ( + json "encoding/json" + fmt "fmt" + internal "github.com/basic-auth-optional/fern/internal" + big "math/big" +) + +var ( + unauthorizedRequestErrorBodyFieldMessage = big.NewInt(1 << 0) +) + +type UnauthorizedRequestErrorBody struct { + Message string `json:"message" url:"message"` + + // Private bitmask of fields set to an explicit value and therefore not to be omitted + explicitFields *big.Int `json:"-" url:"-"` + + extraProperties map[string]interface{} + rawJSON json.RawMessage +} + +func (u *UnauthorizedRequestErrorBody) GetMessage() string { + if u == nil { + return "" + } + return u.Message +} + +func (u *UnauthorizedRequestErrorBody) GetExtraProperties() map[string]interface{} { + if u == nil { + return nil + } + return u.extraProperties +} + +func (u *UnauthorizedRequestErrorBody) require(field *big.Int) { + if u.explicitFields == nil { + u.explicitFields = big.NewInt(0) + } + u.explicitFields.Or(u.explicitFields, field) +} + +// SetMessage sets the Message field and marks it as non-optional; +// this prevents an empty or null value for this field from being omitted during serialization. +func (u *UnauthorizedRequestErrorBody) SetMessage(message string) { + u.Message = message + u.require(unauthorizedRequestErrorBodyFieldMessage) +} + +func (u *UnauthorizedRequestErrorBody) UnmarshalJSON(data []byte) error { + type unmarshaler UnauthorizedRequestErrorBody + var value unmarshaler + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *u = UnauthorizedRequestErrorBody(value) + extraProperties, err := internal.ExtractExtraProperties(data, *u) + if err != nil { + return err + } + u.extraProperties = extraProperties + u.rawJSON = json.RawMessage(data) + return nil +} + +func (u *UnauthorizedRequestErrorBody) MarshalJSON() ([]byte, error) { + type embed UnauthorizedRequestErrorBody + var marshaler = struct { + embed + }{ + embed: embed(*u), + } + explicitMarshaler := internal.HandleExplicitFields(marshaler, u.explicitFields) + return json.Marshal(explicitMarshaler) +} + +func (u *UnauthorizedRequestErrorBody) String() string { + if u == nil { + return "" + } + if len(u.rawJSON) > 0 { + if value, err := internal.StringifyJSON(u.rawJSON); err == nil { + return value + } + } + if value, err := internal.StringifyJSON(u); err == nil { + return value + } + return fmt.Sprintf("%#v", u) +} diff --git a/seed/go-sdk/basic-auth-optional/types_test.go b/seed/go-sdk/basic-auth-optional/types_test.go new file mode 100644 index 000000000000..b02b34054488 --- /dev/null +++ b/seed/go-sdk/basic-auth-optional/types_test.go @@ -0,0 +1,153 @@ +// Code generated by Fern. DO NOT EDIT. + +package basicauthoptional + +import ( + json "encoding/json" + assert "github.com/stretchr/testify/assert" + require "github.com/stretchr/testify/require" + testing "testing" +) + +func TestSettersUnauthorizedRequestErrorBody(t *testing.T) { + t.Run("SetMessage", func(t *testing.T) { + obj := &UnauthorizedRequestErrorBody{} + var fernTestValueMessage string + obj.SetMessage(fernTestValueMessage) + assert.Equal(t, fernTestValueMessage, obj.Message) + assert.NotNil(t, obj.explicitFields) + }) + +} + +func TestGettersUnauthorizedRequestErrorBody(t *testing.T) { + t.Run("GetMessage", func(t *testing.T) { + t.Parallel() + // Arrange + obj := &UnauthorizedRequestErrorBody{} + var expected string + obj.Message = expected + + // Act & Assert + assert.Equal(t, expected, obj.GetMessage(), "getter should return the property value") + }) + + t.Run("GetMessage_NilReceiver", func(t *testing.T) { + t.Parallel() + var obj *UnauthorizedRequestErrorBody + // Should not panic - getters should handle nil receiver gracefully + defer func() { + if r := recover(); r != nil { + t.Errorf("Getter panicked on nil receiver: %v", r) + } + }() + _ = obj.GetMessage() // Should return zero value + }) + +} + +func TestSettersMarkExplicitUnauthorizedRequestErrorBody(t *testing.T) { + t.Run("SetMessage_MarksExplicit", func(t *testing.T) { + t.Parallel() + // Arrange + obj := &UnauthorizedRequestErrorBody{} + var fernTestValueMessage string + + // Act + obj.SetMessage(fernTestValueMessage) + + // Assert - object with explicitly set field can be marshaled/unmarshaled + bytes, err := json.Marshal(obj) + require.NoError(t, err, "marshaling should succeed for test setup") + + // This test ensures JSON marshaling and unmarshaling succeed when the field has a zero/nil value + // Detect if marshaled JSON is an object or primitive to use correct unmarshal target + if len(bytes) > 0 && bytes[0] == '{' { + // JSON object - unmarshal into map + var unmarshaled map[string]interface{} + err = json.Unmarshal(bytes, &unmarshaled) + require.NoError(t, err, "unmarshaling should succeed for test verification") + } else { + // JSON primitive (string, number, boolean, null) - unmarshal into interface{} + var unmarshaled interface{} + err = json.Unmarshal(bytes, &unmarshaled) + require.NoError(t, err, "unmarshaling should succeed for test verification") + } + + // Note: This does not explicitly assert the presence of a specific JSON field + // It verifies that setting a field via setter allows successful JSON round-trip + }) + +} + +func TestJSONMarshalingUnauthorizedRequestErrorBody(t *testing.T) { + t.Run("MarshalUnmarshal", func(t *testing.T) { + t.Parallel() + // Arrange + obj := &UnauthorizedRequestErrorBody{} + + // Act - Marshal to JSON + data, err := json.Marshal(obj) + require.NoError(t, err, "marshaling should succeed") + assert.NotNil(t, data, "marshaled data should not be nil") + assert.NotEmpty(t, data, "marshaled data should not be empty") + + // Unmarshal back and verify round-trip + var unmarshaled UnauthorizedRequestErrorBody + err = json.Unmarshal(data, &unmarshaled) + assert.NoError(t, err, "round-trip unmarshal should succeed") + }) + + t.Run("UnmarshalInvalidJSON", func(t *testing.T) { + t.Parallel() + var obj UnauthorizedRequestErrorBody + err := json.Unmarshal([]byte(`{invalid json}`), &obj) + assert.Error(t, err, "unmarshaling invalid JSON should return an error") + }) + + t.Run("UnmarshalEmptyObject", func(t *testing.T) { + t.Parallel() + var obj UnauthorizedRequestErrorBody + err := json.Unmarshal([]byte(`{}`), &obj) + assert.NoError(t, err, "unmarshaling empty object should succeed") + }) +} + +func TestStringUnauthorizedRequestErrorBody(t *testing.T) { + t.Run("StringMethod", func(t *testing.T) { + t.Parallel() + obj := &UnauthorizedRequestErrorBody{} + result := obj.String() + assert.NotEmpty(t, result, "String() should return a non-empty representation") + }) + + t.Run("StringMethod_NilReceiver", func(t *testing.T) { + t.Parallel() + var obj *UnauthorizedRequestErrorBody + result := obj.String() + assert.Equal(t, "", result, "String() should return for nil receiver") + }) +} + +func TestExtraPropertiesUnauthorizedRequestErrorBody(t *testing.T) { + t.Run("GetExtraProperties", func(t *testing.T) { + t.Parallel() + obj := &UnauthorizedRequestErrorBody{} + // Should not panic when calling GetExtraProperties() + defer func() { + if r := recover(); r != nil { + t.Errorf("GetExtraProperties() panicked: %v", r) + } + }() + extraProps := obj.GetExtraProperties() + // Result can be nil or an empty/non-empty map + _ = extraProps + }) + + t.Run("GetExtraProperties_NilReceiver", func(t *testing.T) { + t.Parallel() + var obj *UnauthorizedRequestErrorBody + extraProps := obj.GetExtraProperties() + assert.Nil(t, extraProps, "nil receiver should return nil without panicking") + }) +} diff --git a/seed/java-sdk/basic-auth-optional/.fern/metadata.json b/seed/java-sdk/basic-auth-optional/.fern/metadata.json new file mode 100644 index 000000000000..6077ef8b5f57 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/.fern/metadata.json @@ -0,0 +1,7 @@ +{ + "cliVersion": "DUMMY", + "generatorName": "fernapi/fern-java-sdk", + "generatorVersion": "latest", + "originGitCommit": "DUMMY", + "sdkVersion": "0.0.1" +} \ No newline at end of file diff --git a/seed/java-sdk/basic-auth-optional/.github/workflows/ci.yml b/seed/java-sdk/basic-auth-optional/.github/workflows/ci.yml new file mode 100644 index 000000000000..09c8c666ad73 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/.github/workflows/ci.yml @@ -0,0 +1,65 @@ +name: ci + +on: [push] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + compile: + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Set up Java + id: setup-jre + uses: actions/setup-java@v1 + with: + java-version: "11" + architecture: x64 + + - name: Compile + run: ./gradlew compileJava + + test: + needs: [ compile ] + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Set up Java + id: setup-jre + uses: actions/setup-java@v1 + with: + java-version: "11" + architecture: x64 + + - name: Test + run: ./gradlew test + publish: + needs: [ compile, test ] + if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Set up Java + id: setup-jre + uses: actions/setup-java@v1 + with: + java-version: "11" + architecture: x64 + + - name: Publish to maven + run: | + ./gradlew publish + env: + MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }} + MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }} + MAVEN_PUBLISH_REGISTRY_URL: "" diff --git a/seed/java-sdk/basic-auth-optional/.gitignore b/seed/java-sdk/basic-auth-optional/.gitignore new file mode 100644 index 000000000000..d4199abc2cd4 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/.gitignore @@ -0,0 +1,24 @@ +*.class +.project +.gradle +? +.classpath +.checkstyle +.settings +.node +build + +# IntelliJ +*.iml +*.ipr +*.iws +.idea/ +out/ + +# Eclipse/IntelliJ APT +generated_src/ +generated_testSrc/ +generated/ + +bin +build \ No newline at end of file diff --git a/seed/java-sdk/basic-auth-optional/README.md b/seed/java-sdk/basic-auth-optional/README.md new file mode 100644 index 000000000000..922d19866fad --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/README.md @@ -0,0 +1,216 @@ +# Seed Java Library + +[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=Seed%2FJava) +[![Maven Central](https://img.shields.io/maven-central/v/com.fern/basic-auth-optional)](https://central.sonatype.com/artifact/com.fern/basic-auth-optional) + +The Seed Java library provides convenient access to the Seed APIs from Java. + +## Table of Contents + +- [Installation](#installation) +- [Reference](#reference) +- [Usage](#usage) +- [Base Url](#base-url) +- [Exception Handling](#exception-handling) +- [Advanced](#advanced) + - [Custom Client](#custom-client) + - [Retries](#retries) + - [Timeouts](#timeouts) + - [Custom Headers](#custom-headers) + - [Access Raw Response Data](#access-raw-response-data) +- [Contributing](#contributing) + +## Installation + +### Gradle + +Add the dependency in your `build.gradle` file: + +```groovy +dependencies { + implementation 'com.fern:basic-auth-optional:0.0.1' +} +``` + +### Maven + +Add the dependency in your `pom.xml` file: + +```xml + + com.fern + basic-auth-optional + 0.0.1 + +``` + +## Reference + +A full reference for this library is available [here](./reference.md). + +## Usage + +Instantiate and use the client with the following: + +```java +package com.example.usage; + +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; +import java.util.HashMap; + +public class Example { + public static void main(String[] args) { + SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient + .builder() + .credentials("", "") + .build(); + + client.basicAuth().postWithBasicAuth(new + HashMap() {{put("key", "value"); + }}); + } +} +``` + +## Base Url + +You can set a custom base URL when constructing the client. + +```java +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; + +SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient + .builder() + .url("https://example.com") + .build(); +``` + +## Exception Handling + +When the API returns a non-success status code (4xx or 5xx response), an API exception will be thrown. + +```java +import com.seed.basicAuthOptional.core.SeedBasicAuthOptionalApiException; + +try{ + client.basicAuth().postWithBasicAuth(...); +} catch (SeedBasicAuthOptionalApiException e){ + // Do something with the API exception... +} +``` + +## Advanced + +### Custom Client + +This SDK is built to work with any instance of `OkHttpClient`. By default, if no client is provided, the SDK will construct one. +However, you can pass your own client like so: + +```java +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; +import okhttp3.OkHttpClient; + +OkHttpClient customClient = ...; + +SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient + .builder() + .httpClient(customClient) + .build(); +``` + +### Retries + +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured +retry limit (default: 2). Before defaulting to exponential backoff, the SDK will first attempt to respect +the `Retry-After` header (as either in seconds or as an HTTP date), and then the `X-RateLimit-Reset` header +(as a Unix timestamp in epoch seconds); failing both of those, it will fall back to exponential backoff. + +A request is deemed retryable when any of the following HTTP status codes is returned: + +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) + +Use the `maxRetries` client option to configure this behavior. + +```java +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; + +SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient + .builder() + .maxRetries(1) + .build(); +``` + +### Timeouts + +The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level. +```java +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; +import com.seed.basicAuthOptional.core.RequestOptions; + +// Client level +SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient + .builder() + .timeout(60) + .build(); + +// Request level +client.basicAuth().postWithBasicAuth( + ..., + RequestOptions + .builder() + .timeout(60) + .build() +); +``` + +### Custom Headers + +The SDK allows you to add custom headers to requests. You can configure headers at the client level or at the request level. + +```java +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; +import com.seed.basicAuthOptional.core.RequestOptions; + +// Client level +SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient + .builder() + .addHeader("X-Custom-Header", "custom-value") + .addHeader("X-Request-Id", "abc-123") + .build(); +; + +// Request level +client.basicAuth().postWithBasicAuth( + ..., + RequestOptions + .builder() + .addHeader("X-Request-Header", "request-value") + .build() +); +``` + +### Access Raw Response Data + +The SDK provides access to raw response data, including headers, through the `withRawResponse()` method. +The `withRawResponse()` method returns a raw client that wraps all responses with `body()` and `headers()` methods. +(A normal client's `response` is identical to a raw client's `response.body()`.) + +```java +SeedBasicAuthOptionalHttpResponse response = client.basicAuth().withRawResponse().postWithBasicAuth(...); + +System.out.println(response.body()); +System.out.println(response.headers().get("X-My-Header")); +``` + +## Contributing + +While we value open-source contributions to this SDK, this library is generated programmatically. +Additions made directly to this library would have to be moved over to our generation code, +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening +an issue first to discuss with us! + +On the other hand, contributions to the README are always very welcome! diff --git a/seed/java-sdk/basic-auth-optional/build.gradle b/seed/java-sdk/basic-auth-optional/build.gradle new file mode 100644 index 000000000000..05c973c0771b --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/build.gradle @@ -0,0 +1,102 @@ +plugins { + id 'java-library' + id 'maven-publish' + id 'com.diffplug.spotless' version '6.11.0' +} + +repositories { + mavenCentral() + maven { + url 'https://s01.oss.sonatype.org/content/repositories/releases/' + } +} + +dependencies { + api 'com.squareup.okhttp3:okhttp:5.2.1' + api 'com.fasterxml.jackson.core:jackson-databind:2.18.6' + api 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.18.6' + api 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.18.6' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2' +} + + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +tasks.withType(Javadoc) { + failOnError false + options.addStringOption('Xdoclint:none', '-quiet') +} + +spotless { + java { + palantirJavaFormat() + } +} + + +java { + withSourcesJar() + withJavadocJar() +} + + +group = 'com.fern' + +version = '0.0.1' + +jar { + dependsOn(":generatePomFileForMavenPublication") + archiveBaseName = "basic-auth-optional" +} + +sourcesJar { + archiveBaseName = "basic-auth-optional" +} + +javadocJar { + archiveBaseName = "basic-auth-optional" +} + +test { + useJUnitPlatform() + testLogging { + showStandardStreams = true + } +} + +publishing { + publications { + maven(MavenPublication) { + groupId = 'com.fern' + artifactId = 'basic-auth-optional' + version = '0.0.1' + from components.java + pom { + licenses { + license { + name = 'The MIT License (MIT)' + url = 'https://mit-license.org/' + } + } + scm { + connection = 'scm:git:git://github.com/basic-auth-optional/fern.git' + developerConnection = 'scm:git:git://github.com/basic-auth-optional/fern.git' + url = 'https://github.com/basic-auth-optional/fern' + } + } + } + } + repositories { + maven { + url "$System.env.MAVEN_PUBLISH_REGISTRY_URL" + credentials { + username "$System.env.MAVEN_USERNAME" + password "$System.env.MAVEN_PASSWORD" + } + } + } +} + diff --git a/seed/java-sdk/basic-auth-optional/reference.md b/seed/java-sdk/basic-auth-optional/reference.md new file mode 100644 index 000000000000..babd62f53641 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/reference.md @@ -0,0 +1,97 @@ +# Reference +## BasicAuth +
client.basicAuth.getWithBasicAuth() -> Boolean +
+
+ +#### 📝 Description + +
+
+ +
+
+ +GET request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```java +client.basicAuth().getWithBasicAuth(); +``` +
+
+
+
+ + +
+
+
+ +
client.basicAuth.postWithBasicAuth(request) -> Boolean +
+
+ +#### 📝 Description + +
+
+ +
+
+ +POST request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```java +client.basicAuth().postWithBasicAuth(new +HashMap() {{put("key", "value"); +}}); +``` +
+
+
+
+ +#### ⚙️ Parameters + +
+
+ +
+
+ +**request:** `Object` + +
+
+
+
+ + +
+
+
+ diff --git a/seed/java-sdk/basic-auth-optional/sample-app/build.gradle b/seed/java-sdk/basic-auth-optional/sample-app/build.gradle new file mode 100644 index 000000000000..4ee8f227b7af --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/sample-app/build.gradle @@ -0,0 +1,19 @@ +plugins { + id 'java-library' +} + +repositories { + mavenCentral() + maven { + url 'https://s01.oss.sonatype.org/content/repositories/releases/' + } +} + +dependencies { + implementation rootProject +} + + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + diff --git a/seed/java-sdk/basic-auth-optional/sample-app/src/main/java/sample/App.java b/seed/java-sdk/basic-auth-optional/sample-app/src/main/java/sample/App.java new file mode 100644 index 000000000000..80d66d9e3dff --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/sample-app/src/main/java/sample/App.java @@ -0,0 +1,13 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ + +package sample; + +import java.lang.String; + +public final class App { + public static void main(String[] args) { + // import com.seed.basicAuthOptional.AsyncSeedBasicAuthOptionalClient + } +} diff --git a/seed/java-sdk/basic-auth-optional/settings.gradle b/seed/java-sdk/basic-auth-optional/settings.gradle new file mode 100644 index 000000000000..149e774d7b70 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/settings.gradle @@ -0,0 +1,3 @@ +rootProject.name = 'basic-auth-optional' + +include 'sample-app' \ No newline at end of file diff --git a/seed/java-sdk/basic-auth-optional/snippet.json b/seed/java-sdk/basic-auth-optional/snippet.json new file mode 100644 index 000000000000..5af56df74dc6 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/snippet.json @@ -0,0 +1,96 @@ +{ + "endpoints": [ + { + "example_identifier": "c7d7b6ea", + "id": { + "method": "GET", + "path": "/basic-auth", + "identifier_override": "endpoint_basic-auth.getWithBasicAuth" + }, + "snippet": { + "type": "java", + "sync_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().getWithBasicAuth();\n }\n}\n", + "async_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().getWithBasicAuth();\n }\n}\n" + } + }, + { + "example_identifier": "14b143fa", + "id": { + "method": "GET", + "path": "/basic-auth", + "identifier_override": "endpoint_basic-auth.getWithBasicAuth" + }, + "snippet": { + "type": "java", + "sync_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().getWithBasicAuth();\n }\n}\n", + "async_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().getWithBasicAuth();\n }\n}\n" + } + }, + { + "example_identifier": "adda2170", + "id": { + "method": "GET", + "path": "/basic-auth", + "identifier_override": "endpoint_basic-auth.getWithBasicAuth" + }, + "snippet": { + "type": "java", + "sync_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().getWithBasicAuth();\n }\n}\n", + "async_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().getWithBasicAuth();\n }\n}\n" + } + }, + { + "example_identifier": "8ae9427b", + "id": { + "method": "POST", + "path": "/basic-auth", + "identifier_override": "endpoint_basic-auth.postWithBasicAuth" + }, + "snippet": { + "type": "java", + "sync_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\nimport java.util.HashMap;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().postWithBasicAuth(new \n HashMap() {{put(\"key\", \"value\");\n }});\n }\n}\n", + "async_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\nimport java.util.HashMap;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().postWithBasicAuth(new \n HashMap() {{put(\"key\", \"value\");\n }});\n }\n}\n" + } + }, + { + "example_identifier": "91843eb7", + "id": { + "method": "POST", + "path": "/basic-auth", + "identifier_override": "endpoint_basic-auth.postWithBasicAuth" + }, + "snippet": { + "type": "java", + "sync_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\nimport java.util.HashMap;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().postWithBasicAuth(new \n HashMap() {{put(\"key\", \"value\");\n }});\n }\n}\n", + "async_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\nimport java.util.HashMap;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().postWithBasicAuth(new \n HashMap() {{put(\"key\", \"value\");\n }});\n }\n}\n" + } + }, + { + "example_identifier": "aabdeee5", + "id": { + "method": "POST", + "path": "/basic-auth", + "identifier_override": "endpoint_basic-auth.postWithBasicAuth" + }, + "snippet": { + "type": "java", + "sync_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\nimport java.util.HashMap;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().postWithBasicAuth(new \n HashMap() {{put(\"key\", \"value\");\n }});\n }\n}\n", + "async_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\nimport java.util.HashMap;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().postWithBasicAuth(new \n HashMap() {{put(\"key\", \"value\");\n }});\n }\n}\n" + } + }, + { + "example_identifier": "7eabe5c4", + "id": { + "method": "POST", + "path": "/basic-auth", + "identifier_override": "endpoint_basic-auth.postWithBasicAuth" + }, + "snippet": { + "type": "java", + "sync_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\nimport java.util.HashMap;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().postWithBasicAuth(new \n HashMap() {{put(\"key\", \"value\");\n }});\n }\n}\n", + "async_client": "package com.example.usage;\n\nimport com.seed.basicAuthOptional.SeedBasicAuthOptionalClient;\nimport java.util.HashMap;\n\npublic class Example {\n public static void main(String[] args) {\n SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient\n .builder()\n .credentials(\"\", \"\")\n .build();\n\n client.basicAuth().postWithBasicAuth(new \n HashMap() {{put(\"key\", \"value\");\n }});\n }\n}\n" + } + } + ], + "types": {} +} \ No newline at end of file diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/AsyncSeedBasicAuthOptionalClient.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/AsyncSeedBasicAuthOptionalClient.java new file mode 100644 index 000000000000..95a59cee84d7 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/AsyncSeedBasicAuthOptionalClient.java @@ -0,0 +1,28 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional; + +import com.seed.basicAuthOptional.core.ClientOptions; +import com.seed.basicAuthOptional.core.Suppliers; +import com.seed.basicAuthOptional.resources.basicauth.AsyncBasicAuthClient; +import java.util.function.Supplier; + +public class AsyncSeedBasicAuthOptionalClient { + protected final ClientOptions clientOptions; + + protected final Supplier basicAuthClient; + + public AsyncSeedBasicAuthOptionalClient(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + this.basicAuthClient = Suppliers.memoize(() -> new AsyncBasicAuthClient(clientOptions)); + } + + public AsyncBasicAuthClient basicAuth() { + return this.basicAuthClient.get(); + } + + public static AsyncSeedBasicAuthOptionalClientBuilder builder() { + return new AsyncSeedBasicAuthOptionalClientBuilder(); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/AsyncSeedBasicAuthOptionalClientBuilder.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/AsyncSeedBasicAuthOptionalClientBuilder.java new file mode 100644 index 000000000000..bbdbd6022006 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/AsyncSeedBasicAuthOptionalClientBuilder.java @@ -0,0 +1,230 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional; + +import com.seed.basicAuthOptional.core.ClientOptions; +import com.seed.basicAuthOptional.core.Environment; +import com.seed.basicAuthOptional.core.LogConfig; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import okhttp3.OkHttpClient; + +public class AsyncSeedBasicAuthOptionalClientBuilder { + private Optional timeout = Optional.empty(); + + private Optional maxRetries = Optional.empty(); + + private final Map customHeaders = new HashMap<>(); + + private String username = null; + + private String password = null; + + private Environment environment; + + private OkHttpClient httpClient; + + private Optional logging = Optional.empty(); + + public AsyncSeedBasicAuthOptionalClientBuilder credentials(String username, String password) { + this.username = username; + this.password = password; + return this; + } + + public AsyncSeedBasicAuthOptionalClientBuilder url(String url) { + this.environment = Environment.custom(url); + return this; + } + + /** + * Sets the timeout (in seconds) for the client. Defaults to 60 seconds. + */ + public AsyncSeedBasicAuthOptionalClientBuilder timeout(int timeout) { + this.timeout = Optional.of(timeout); + return this; + } + + /** + * Sets the maximum number of retries for the client. Defaults to 2 retries. + */ + public AsyncSeedBasicAuthOptionalClientBuilder maxRetries(int maxRetries) { + this.maxRetries = Optional.of(maxRetries); + return this; + } + + /** + * Sets the underlying OkHttp client + */ + public AsyncSeedBasicAuthOptionalClientBuilder httpClient(OkHttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /** + * Configure logging for the SDK. Silent by default — no log output unless explicitly configured. + */ + public AsyncSeedBasicAuthOptionalClientBuilder logging(LogConfig logging) { + this.logging = Optional.of(logging); + return this; + } + + /** + * Add a custom header to be sent with all requests. + * For headers that need to be computed dynamically or conditionally, use the setAdditional() method override instead. + * + * @param name The header name + * @param value The header value + * @return This builder for method chaining + */ + public AsyncSeedBasicAuthOptionalClientBuilder addHeader(String name, String value) { + this.customHeaders.put(name, value); + return this; + } + + protected ClientOptions buildClientOptions() { + ClientOptions.Builder builder = ClientOptions.builder(); + setEnvironment(builder); + setAuthentication(builder); + setHttpClient(builder); + setTimeouts(builder); + setRetries(builder); + setLogging(builder); + for (Map.Entry header : this.customHeaders.entrySet()) { + builder.addHeader(header.getKey(), header.getValue()); + } + setAdditional(builder); + return builder.build(); + } + + /** + * Sets the environment configuration for the client. + * Override this method to modify URLs or add environment-specific logic. + * + * @param builder The ClientOptions.Builder to configure + */ + protected void setEnvironment(ClientOptions.Builder builder) { + builder.environment(this.environment); + } + + /** + * Override this method to customize authentication. + * This method is called during client options construction to set up authentication headers. + * + * @param builder The ClientOptions.Builder to configure + * + * Example: + *
{@code
+     * @Override
+     * protected void setAuthentication(ClientOptions.Builder builder) {
+     *     super.setAuthentication(builder); // Keep existing auth
+     *     builder.addHeader("X-API-Key", this.apiKey);
+     * }
+     * }
+ */ + protected void setAuthentication(ClientOptions.Builder builder) { + if (this.username != null && this.password != null) { + String unencodedToken = this.username + ":" + this.password; + String encodedToken = Base64.getEncoder().encodeToString(unencodedToken.getBytes()); + builder.addHeader("Authorization", "Basic " + encodedToken); + } + } + + /** + * Sets the request timeout configuration. + * Override this method to customize timeout behavior. + * + * @param builder The ClientOptions.Builder to configure + */ + protected void setTimeouts(ClientOptions.Builder builder) { + if (this.timeout.isPresent()) { + builder.timeout(this.timeout.get()); + } + } + + /** + * Sets the retry configuration for failed requests. + * Override this method to implement custom retry strategies. + * + * @param builder The ClientOptions.Builder to configure + */ + protected void setRetries(ClientOptions.Builder builder) { + if (this.maxRetries.isPresent()) { + builder.maxRetries(this.maxRetries.get()); + } + } + + /** + * Sets the OkHttp client configuration. + * Override this method to customize HTTP client behavior (interceptors, connection pools, etc). + * + * @param builder The ClientOptions.Builder to configure + */ + protected void setHttpClient(ClientOptions.Builder builder) { + if (this.httpClient != null) { + builder.httpClient(this.httpClient); + } + } + + /** + * Sets the logging configuration for the SDK. + * Override this method to customize logging behavior. + * + * @param builder The ClientOptions.Builder to configure + */ + protected void setLogging(ClientOptions.Builder builder) { + if (this.logging.isPresent()) { + builder.logging(this.logging.get()); + } + } + + /** + * Override this method to add any additional configuration to the client. + * This method is called at the end of the configuration chain, allowing you to add + * custom headers, modify settings, or perform any other client customization. + * + * @param builder The ClientOptions.Builder to configure + * + * Example: + *
{@code
+     * @Override
+     * protected void setAdditional(ClientOptions.Builder builder) {
+     *     builder.addHeader("X-Request-ID", () -> UUID.randomUUID().toString());
+     *     builder.addHeader("X-Client-Version", "1.0.0");
+     * }
+     * }
+ */ + protected void setAdditional(ClientOptions.Builder builder) {} + + /** + * Override this method to add custom validation logic before the client is built. + * This method is called at the beginning of the build() method to ensure the configuration is valid. + * Throw an exception to prevent client creation if validation fails. + * + * Example: + *
{@code
+     * @Override
+     * protected void validateConfiguration() {
+     *     super.validateConfiguration(); // Run parent validations
+     *     if (tenantId == null || tenantId.isEmpty()) {
+     *         throw new IllegalStateException("tenantId is required");
+     *     }
+     * }
+     * }
+ */ + protected void validateConfiguration() {} + + public AsyncSeedBasicAuthOptionalClient build() { + if (this.username == null) { + throw new RuntimeException("Please provide username"); + } + if (this.password == null) { + throw new RuntimeException("Please provide password"); + } + validateConfiguration(); + return new AsyncSeedBasicAuthOptionalClient(buildClientOptions()); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/SeedBasicAuthOptionalClient.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/SeedBasicAuthOptionalClient.java new file mode 100644 index 000000000000..ef34233b2c64 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/SeedBasicAuthOptionalClient.java @@ -0,0 +1,28 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional; + +import com.seed.basicAuthOptional.core.ClientOptions; +import com.seed.basicAuthOptional.core.Suppliers; +import com.seed.basicAuthOptional.resources.basicauth.BasicAuthClient; +import java.util.function.Supplier; + +public class SeedBasicAuthOptionalClient { + protected final ClientOptions clientOptions; + + protected final Supplier basicAuthClient; + + public SeedBasicAuthOptionalClient(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + this.basicAuthClient = Suppliers.memoize(() -> new BasicAuthClient(clientOptions)); + } + + public BasicAuthClient basicAuth() { + return this.basicAuthClient.get(); + } + + public static SeedBasicAuthOptionalClientBuilder builder() { + return new SeedBasicAuthOptionalClientBuilder(); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/SeedBasicAuthOptionalClientBuilder.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/SeedBasicAuthOptionalClientBuilder.java new file mode 100644 index 000000000000..af498a27313b --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/SeedBasicAuthOptionalClientBuilder.java @@ -0,0 +1,230 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional; + +import com.seed.basicAuthOptional.core.ClientOptions; +import com.seed.basicAuthOptional.core.Environment; +import com.seed.basicAuthOptional.core.LogConfig; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import okhttp3.OkHttpClient; + +public class SeedBasicAuthOptionalClientBuilder { + private Optional timeout = Optional.empty(); + + private Optional maxRetries = Optional.empty(); + + private final Map customHeaders = new HashMap<>(); + + private String username = null; + + private String password = null; + + private Environment environment; + + private OkHttpClient httpClient; + + private Optional logging = Optional.empty(); + + public SeedBasicAuthOptionalClientBuilder credentials(String username, String password) { + this.username = username; + this.password = password; + return this; + } + + public SeedBasicAuthOptionalClientBuilder url(String url) { + this.environment = Environment.custom(url); + return this; + } + + /** + * Sets the timeout (in seconds) for the client. Defaults to 60 seconds. + */ + public SeedBasicAuthOptionalClientBuilder timeout(int timeout) { + this.timeout = Optional.of(timeout); + return this; + } + + /** + * Sets the maximum number of retries for the client. Defaults to 2 retries. + */ + public SeedBasicAuthOptionalClientBuilder maxRetries(int maxRetries) { + this.maxRetries = Optional.of(maxRetries); + return this; + } + + /** + * Sets the underlying OkHttp client + */ + public SeedBasicAuthOptionalClientBuilder httpClient(OkHttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /** + * Configure logging for the SDK. Silent by default — no log output unless explicitly configured. + */ + public SeedBasicAuthOptionalClientBuilder logging(LogConfig logging) { + this.logging = Optional.of(logging); + return this; + } + + /** + * Add a custom header to be sent with all requests. + * For headers that need to be computed dynamically or conditionally, use the setAdditional() method override instead. + * + * @param name The header name + * @param value The header value + * @return This builder for method chaining + */ + public SeedBasicAuthOptionalClientBuilder addHeader(String name, String value) { + this.customHeaders.put(name, value); + return this; + } + + protected ClientOptions buildClientOptions() { + ClientOptions.Builder builder = ClientOptions.builder(); + setEnvironment(builder); + setAuthentication(builder); + setHttpClient(builder); + setTimeouts(builder); + setRetries(builder); + setLogging(builder); + for (Map.Entry header : this.customHeaders.entrySet()) { + builder.addHeader(header.getKey(), header.getValue()); + } + setAdditional(builder); + return builder.build(); + } + + /** + * Sets the environment configuration for the client. + * Override this method to modify URLs or add environment-specific logic. + * + * @param builder The ClientOptions.Builder to configure + */ + protected void setEnvironment(ClientOptions.Builder builder) { + builder.environment(this.environment); + } + + /** + * Override this method to customize authentication. + * This method is called during client options construction to set up authentication headers. + * + * @param builder The ClientOptions.Builder to configure + * + * Example: + *
{@code
+     * @Override
+     * protected void setAuthentication(ClientOptions.Builder builder) {
+     *     super.setAuthentication(builder); // Keep existing auth
+     *     builder.addHeader("X-API-Key", this.apiKey);
+     * }
+     * }
+ */ + protected void setAuthentication(ClientOptions.Builder builder) { + if (this.username != null && this.password != null) { + String unencodedToken = this.username + ":" + this.password; + String encodedToken = Base64.getEncoder().encodeToString(unencodedToken.getBytes()); + builder.addHeader("Authorization", "Basic " + encodedToken); + } + } + + /** + * Sets the request timeout configuration. + * Override this method to customize timeout behavior. + * + * @param builder The ClientOptions.Builder to configure + */ + protected void setTimeouts(ClientOptions.Builder builder) { + if (this.timeout.isPresent()) { + builder.timeout(this.timeout.get()); + } + } + + /** + * Sets the retry configuration for failed requests. + * Override this method to implement custom retry strategies. + * + * @param builder The ClientOptions.Builder to configure + */ + protected void setRetries(ClientOptions.Builder builder) { + if (this.maxRetries.isPresent()) { + builder.maxRetries(this.maxRetries.get()); + } + } + + /** + * Sets the OkHttp client configuration. + * Override this method to customize HTTP client behavior (interceptors, connection pools, etc). + * + * @param builder The ClientOptions.Builder to configure + */ + protected void setHttpClient(ClientOptions.Builder builder) { + if (this.httpClient != null) { + builder.httpClient(this.httpClient); + } + } + + /** + * Sets the logging configuration for the SDK. + * Override this method to customize logging behavior. + * + * @param builder The ClientOptions.Builder to configure + */ + protected void setLogging(ClientOptions.Builder builder) { + if (this.logging.isPresent()) { + builder.logging(this.logging.get()); + } + } + + /** + * Override this method to add any additional configuration to the client. + * This method is called at the end of the configuration chain, allowing you to add + * custom headers, modify settings, or perform any other client customization. + * + * @param builder The ClientOptions.Builder to configure + * + * Example: + *
{@code
+     * @Override
+     * protected void setAdditional(ClientOptions.Builder builder) {
+     *     builder.addHeader("X-Request-ID", () -> UUID.randomUUID().toString());
+     *     builder.addHeader("X-Client-Version", "1.0.0");
+     * }
+     * }
+ */ + protected void setAdditional(ClientOptions.Builder builder) {} + + /** + * Override this method to add custom validation logic before the client is built. + * This method is called at the beginning of the build() method to ensure the configuration is valid. + * Throw an exception to prevent client creation if validation fails. + * + * Example: + *
{@code
+     * @Override
+     * protected void validateConfiguration() {
+     *     super.validateConfiguration(); // Run parent validations
+     *     if (tenantId == null || tenantId.isEmpty()) {
+     *         throw new IllegalStateException("tenantId is required");
+     *     }
+     * }
+     * }
+ */ + protected void validateConfiguration() {} + + public SeedBasicAuthOptionalClient build() { + if (this.username == null) { + throw new RuntimeException("Please provide username"); + } + if (this.password == null) { + throw new RuntimeException("Please provide password"); + } + validateConfiguration(); + return new SeedBasicAuthOptionalClient(buildClientOptions()); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ClientOptions.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ClientOptions.java new file mode 100644 index 000000000000..624aaa57c948 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ClientOptions.java @@ -0,0 +1,221 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; +import okhttp3.OkHttpClient; + +public final class ClientOptions { + private final Environment environment; + + private final Map headers; + + private final Map> headerSuppliers; + + private final OkHttpClient httpClient; + + private final int timeout; + + private final int maxRetries; + + private final Optional logging; + + private ClientOptions( + Environment environment, + Map headers, + Map> headerSuppliers, + OkHttpClient httpClient, + int timeout, + int maxRetries, + Optional logging) { + this.environment = environment; + this.headers = new HashMap<>(); + this.headers.putAll(headers); + this.headers.putAll(new HashMap() { + { + put("User-Agent", "com.fern:basic-auth-optional/0.0.1"); + put("X-Fern-Language", "JAVA"); + put("X-Fern-SDK-Name", "com.seed.fern:basic-auth-optional-sdk"); + } + }); + this.headerSuppliers = headerSuppliers; + this.httpClient = httpClient; + this.timeout = timeout; + this.maxRetries = maxRetries; + this.logging = logging; + } + + public Environment environment() { + return this.environment; + } + + public Map headers(RequestOptions requestOptions) { + Map values = new HashMap<>(this.headers); + headerSuppliers.forEach((key, supplier) -> { + values.put(key, supplier.get()); + }); + if (requestOptions != null) { + values.putAll(requestOptions.getHeaders()); + } + return values; + } + + public int timeout(RequestOptions requestOptions) { + if (requestOptions == null) { + return this.timeout; + } + return requestOptions.getTimeout().orElse(this.timeout); + } + + public OkHttpClient httpClient() { + return this.httpClient; + } + + public OkHttpClient httpClientWithTimeout(RequestOptions requestOptions) { + if (requestOptions == null) { + return this.httpClient; + } + return this.httpClient + .newBuilder() + .callTimeout(requestOptions.getTimeout().get(), requestOptions.getTimeoutTimeUnit()) + .connectTimeout(0, TimeUnit.SECONDS) + .writeTimeout(0, TimeUnit.SECONDS) + .readTimeout(0, TimeUnit.SECONDS) + .build(); + } + + public int maxRetries() { + return this.maxRetries; + } + + public Optional logging() { + return this.logging; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private Environment environment; + + private final Map headers = new HashMap<>(); + + private final Map> headerSuppliers = new HashMap<>(); + + private int maxRetries = 2; + + private Optional timeout = Optional.empty(); + + private OkHttpClient httpClient = null; + + private Optional logging = Optional.empty(); + + public Builder environment(Environment environment) { + this.environment = environment; + return this; + } + + public Builder addHeader(String key, String value) { + this.headers.put(key, value); + return this; + } + + public Builder addHeader(String key, Supplier value) { + this.headerSuppliers.put(key, value); + return this; + } + + /** + * Override the timeout in seconds. Defaults to 60 seconds. + */ + public Builder timeout(int timeout) { + this.timeout = Optional.of(timeout); + return this; + } + + /** + * Override the timeout in seconds. Defaults to 60 seconds. + */ + public Builder timeout(Optional timeout) { + this.timeout = timeout; + return this; + } + + /** + * Override the maximum number of retries. Defaults to 2 retries. + */ + public Builder maxRetries(int maxRetries) { + this.maxRetries = maxRetries; + return this; + } + + public Builder httpClient(OkHttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /** + * Configure logging for the SDK. Silent by default — no log output unless explicitly configured. + */ + public Builder logging(LogConfig logging) { + this.logging = Optional.of(logging); + return this; + } + + public ClientOptions build() { + OkHttpClient.Builder httpClientBuilder = + this.httpClient != null ? this.httpClient.newBuilder() : new OkHttpClient.Builder(); + + if (this.httpClient != null) { + timeout.ifPresent(timeout -> httpClientBuilder + .callTimeout(timeout, TimeUnit.SECONDS) + .connectTimeout(0, TimeUnit.SECONDS) + .writeTimeout(0, TimeUnit.SECONDS) + .readTimeout(0, TimeUnit.SECONDS)); + } else { + httpClientBuilder + .callTimeout(this.timeout.orElse(60), TimeUnit.SECONDS) + .connectTimeout(0, TimeUnit.SECONDS) + .writeTimeout(0, TimeUnit.SECONDS) + .readTimeout(0, TimeUnit.SECONDS) + .addInterceptor(new RetryInterceptor(this.maxRetries)); + } + + Logger logger = Logger.from(this.logging); + httpClientBuilder.addInterceptor(new LoggingInterceptor(logger)); + + this.httpClient = httpClientBuilder.build(); + this.timeout = Optional.of(httpClient.callTimeoutMillis() / 1000); + + return new ClientOptions( + environment, + headers, + headerSuppliers, + httpClient, + this.timeout.get(), + this.maxRetries, + this.logging); + } + + /** + * Create a new Builder initialized with values from an existing ClientOptions + */ + public static Builder from(ClientOptions clientOptions) { + Builder builder = new Builder(); + builder.environment = clientOptions.environment(); + builder.timeout = Optional.of(clientOptions.timeout(null)); + builder.httpClient = clientOptions.httpClient(); + builder.headers.putAll(clientOptions.headers); + builder.headerSuppliers.putAll(clientOptions.headerSuppliers); + builder.maxRetries = clientOptions.maxRetries(); + builder.logging = clientOptions.logging(); + return builder; + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ConsoleLogger.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ConsoleLogger.java new file mode 100644 index 000000000000..39dc69b9e6c7 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ConsoleLogger.java @@ -0,0 +1,51 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.util.logging.Level; + +/** + * Default logger implementation that writes to the console using {@link java.util.logging.Logger}. + * + *

Uses the "fern" logger name with a simple format of "LEVEL - message". + */ +public final class ConsoleLogger implements ILogger { + + private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger("fern"); + + static { + if (logger.getHandlers().length == 0) { + java.util.logging.ConsoleHandler handler = new java.util.logging.ConsoleHandler(); + handler.setFormatter(new java.util.logging.SimpleFormatter() { + @Override + public String format(java.util.logging.LogRecord record) { + return record.getLevel() + " - " + record.getMessage() + System.lineSeparator(); + } + }); + logger.addHandler(handler); + logger.setUseParentHandlers(false); + logger.setLevel(Level.ALL); + } + } + + @Override + public void debug(String message) { + logger.log(Level.FINE, message); + } + + @Override + public void info(String message) { + logger.log(Level.INFO, message); + } + + @Override + public void warn(String message) { + logger.log(Level.WARNING, message); + } + + @Override + public void error(String message) { + logger.log(Level.SEVERE, message); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/DateTimeDeserializer.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/DateTimeDeserializer.java new file mode 100644 index 000000000000..a7e955a61c48 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/DateTimeDeserializer.java @@ -0,0 +1,55 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.module.SimpleModule; +import java.io.IOException; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAccessor; +import java.time.temporal.TemporalQueries; + +/** + * Custom deserializer that handles converting ISO8601 dates into {@link OffsetDateTime} objects. + */ +class DateTimeDeserializer extends JsonDeserializer { + private static final SimpleModule MODULE; + + static { + MODULE = new SimpleModule().addDeserializer(OffsetDateTime.class, new DateTimeDeserializer()); + } + + /** + * Gets a module wrapping this deserializer as an adapter for the Jackson ObjectMapper. + * + * @return A {@link SimpleModule} to be plugged onto Jackson ObjectMapper. + */ + public static SimpleModule getModule() { + return MODULE; + } + + @Override + public OffsetDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException { + JsonToken token = parser.currentToken(); + if (token == JsonToken.VALUE_NUMBER_INT) { + return OffsetDateTime.ofInstant(Instant.ofEpochSecond(parser.getValueAsLong()), ZoneOffset.UTC); + } else { + TemporalAccessor temporal = DateTimeFormatter.ISO_DATE_TIME.parseBest( + parser.getValueAsString(), OffsetDateTime::from, LocalDateTime::from); + + if (temporal.query(TemporalQueries.offset()) == null) { + return LocalDateTime.from(temporal).atOffset(ZoneOffset.UTC); + } else { + return OffsetDateTime.from(temporal); + } + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/DoubleSerializer.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/DoubleSerializer.java new file mode 100644 index 000000000000..3b1b8ea645de --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/DoubleSerializer.java @@ -0,0 +1,43 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.module.SimpleModule; +import java.io.IOException; + +/** + * Custom serializer that writes integer-valued doubles without a decimal point. + * For example, {@code 24000.0} is serialized as {@code 24000} instead of {@code 24000.0}. + * Non-integer values like {@code 3.14} are serialized normally. + */ +class DoubleSerializer extends JsonSerializer { + private static final SimpleModule MODULE; + + static { + MODULE = new SimpleModule() + .addSerializer(Double.class, new DoubleSerializer()) + .addSerializer(double.class, new DoubleSerializer()); + } + + /** + * Gets a module wrapping this serializer as an adapter for the Jackson ObjectMapper. + * + * @return A {@link SimpleModule} to be plugged onto Jackson ObjectMapper. + */ + public static SimpleModule getModule() { + return MODULE; + } + + @Override + public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + if (value != null && value == Math.floor(value) && !Double.isInfinite(value) && !Double.isNaN(value)) { + gen.writeNumber(value.longValue()); + } else { + gen.writeNumber(value); + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Environment.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Environment.java new file mode 100644 index 000000000000..d08e09cdc430 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Environment.java @@ -0,0 +1,20 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +public final class Environment { + private final String url; + + private Environment(String url) { + this.url = url; + } + + public String getUrl() { + return this.url; + } + + public static Environment custom(String url) { + return new Environment(url); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/FileStream.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/FileStream.java new file mode 100644 index 000000000000..0bd2a5083d09 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/FileStream.java @@ -0,0 +1,60 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.io.InputStream; +import java.util.Objects; +import okhttp3.MediaType; +import okhttp3.RequestBody; +import org.jetbrains.annotations.Nullable; + +/** + * Represents a file stream with associated metadata for file uploads. + */ +public class FileStream { + private final InputStream inputStream; + private final String fileName; + private final MediaType contentType; + + /** + * Constructs a FileStream with the given input stream and optional metadata. + * + * @param inputStream The input stream of the file content. Must not be null. + * @param fileName The name of the file, or null if unknown. + * @param contentType The MIME type of the file content, or null if unknown. + * @throws NullPointerException if inputStream is null + */ + public FileStream(InputStream inputStream, @Nullable String fileName, @Nullable MediaType contentType) { + this.inputStream = Objects.requireNonNull(inputStream, "Input stream cannot be null"); + this.fileName = fileName; + this.contentType = contentType; + } + + public FileStream(InputStream inputStream) { + this(inputStream, null, null); + } + + public InputStream getInputStream() { + return inputStream; + } + + @Nullable + public String getFileName() { + return fileName; + } + + @Nullable + public MediaType getContentType() { + return contentType; + } + + /** + * Creates a RequestBody suitable for use with OkHttp client. + * + * @return A RequestBody instance representing this file stream. + */ + public RequestBody toRequestBody() { + return new InputStreamRequestBody(contentType, inputStream); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ILogger.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ILogger.java new file mode 100644 index 000000000000..ee1b7a2e2af5 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ILogger.java @@ -0,0 +1,38 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +/** + * Interface for custom logger implementations. + * + *

Implement this interface to provide a custom logging backend for the SDK. + * The SDK will call the appropriate method based on the log level. + * + *

Example: + *

{@code
+ * public class MyCustomLogger implements ILogger {
+ *     public void debug(String message) {
+ *         System.out.println("[DBG] " + message);
+ *     }
+ *     public void info(String message) {
+ *         System.out.println("[INF] " + message);
+ *     }
+ *     public void warn(String message) {
+ *         System.out.println("[WRN] " + message);
+ *     }
+ *     public void error(String message) {
+ *         System.out.println("[ERR] " + message);
+ *     }
+ * }
+ * }
+ */ +public interface ILogger { + void debug(String message); + + void info(String message); + + void warn(String message); + + void error(String message); +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/InputStreamRequestBody.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/InputStreamRequestBody.java new file mode 100644 index 000000000000..f275b7eac734 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/InputStreamRequestBody.java @@ -0,0 +1,74 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Objects; +import okhttp3.MediaType; +import okhttp3.RequestBody; +import okio.BufferedSink; +import okio.Okio; +import okio.Source; +import org.jetbrains.annotations.Nullable; + +/** + * A custom implementation of OkHttp's RequestBody that wraps an InputStream. + * This class allows streaming of data from an InputStream directly to an HTTP request body, + * which is useful for file uploads or sending large amounts of data without loading it all into memory. + */ +public class InputStreamRequestBody extends RequestBody { + private final InputStream inputStream; + private final MediaType contentType; + + /** + * Constructs an InputStreamRequestBody with the specified content type and input stream. + * + * @param contentType the MediaType of the content, or null if not known + * @param inputStream the InputStream containing the data to be sent + * @throws NullPointerException if inputStream is null + */ + public InputStreamRequestBody(@Nullable MediaType contentType, InputStream inputStream) { + this.contentType = contentType; + this.inputStream = Objects.requireNonNull(inputStream, "inputStream == null"); + } + + /** + * Returns the content type of this request body. + * + * @return the MediaType of the content, or null if not specified + */ + @Nullable + @Override + public MediaType contentType() { + return contentType; + } + + /** + * Returns the content length of this request body, if known. + * This method attempts to determine the length using the InputStream's available() method, + * which may not always accurately reflect the total length of the stream. + * + * @return the content length, or -1 if the length is unknown + * @throws IOException if an I/O error occurs + */ + @Override + public long contentLength() throws IOException { + return inputStream.available() == 0 ? -1 : inputStream.available(); + } + + /** + * Writes the content of the InputStream to the given BufferedSink. + * This method is responsible for transferring the data from the InputStream to the network request. + * + * @param sink the BufferedSink to write the content to + * @throws IOException if an I/O error occurs during writing + */ + @Override + public void writeTo(BufferedSink sink) throws IOException { + try (Source source = Okio.source(inputStream)) { + sink.writeAll(source); + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LogConfig.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LogConfig.java new file mode 100644 index 000000000000..832ecacde621 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LogConfig.java @@ -0,0 +1,98 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +/** + * Configuration for SDK logging. + * + *

Use the builder to configure logging behavior: + *

{@code
+ * LogConfig config = LogConfig.builder()
+ *     .level(LogLevel.DEBUG)
+ *     .silent(false)
+ *     .build();
+ * }
+ * + *

Or with a custom logger: + *

{@code
+ * LogConfig config = LogConfig.builder()
+ *     .level(LogLevel.DEBUG)
+ *     .logger(new MyCustomLogger())
+ *     .silent(false)
+ *     .build();
+ * }
+ * + *

Defaults: + *

    + *
  • {@code level} — {@link LogLevel#INFO}
  • + *
  • {@code logger} — {@link ConsoleLogger} (writes to stderr via java.util.logging)
  • + *
  • {@code silent} — {@code true} (no output unless explicitly enabled)
  • + *
+ */ +public final class LogConfig { + + private final LogLevel level; + private final ILogger logger; + private final boolean silent; + + private LogConfig(LogLevel level, ILogger logger, boolean silent) { + this.level = level; + this.logger = logger; + this.silent = silent; + } + + public LogLevel level() { + return level; + } + + public ILogger logger() { + return logger; + } + + public boolean silent() { + return silent; + } + + public static Builder builder() { + return new Builder(); + } + + public static final class Builder { + private LogLevel level = LogLevel.INFO; + private ILogger logger = new ConsoleLogger(); + private boolean silent = true; + + private Builder() {} + + /** + * Set the minimum log level. Only messages at this level or above will be logged. + * Defaults to {@link LogLevel#INFO}. + */ + public Builder level(LogLevel level) { + this.level = level; + return this; + } + + /** + * Set a custom logger implementation. Defaults to {@link ConsoleLogger}. + */ + public Builder logger(ILogger logger) { + this.logger = logger; + return this; + } + + /** + * Set whether logging is silent (disabled). Defaults to {@code true}. + * Set to {@code false} to enable log output. + */ + public Builder silent(boolean silent) { + this.silent = silent; + return this; + } + + public LogConfig build() { + return new LogConfig(level, logger, silent); + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LogLevel.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LogLevel.java new file mode 100644 index 000000000000..d69cb5350c2f --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LogLevel.java @@ -0,0 +1,36 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +/** + * Log levels for SDK logging configuration. + * Silent by default — no log output unless explicitly configured. + */ +public enum LogLevel { + DEBUG(1), + INFO(2), + WARN(3), + ERROR(4); + + private final int value; + + LogLevel(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + /** + * Parse a log level from a string (case-insensitive). + * + * @param level the level string (debug, info, warn, error) + * @return the corresponding LogLevel + * @throws IllegalArgumentException if the string does not match any level + */ + public static LogLevel fromString(String level) { + return LogLevel.valueOf(level.toUpperCase()); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Logger.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Logger.java new file mode 100644 index 000000000000..e94da6c2d149 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Logger.java @@ -0,0 +1,97 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +/** + * SDK logger that filters messages based on level and silent mode. + * + *

Silent by default — no log output unless explicitly configured. + * Create via {@link LogConfig} or directly: + *

{@code
+ * Logger logger = new Logger(LogLevel.DEBUG, new ConsoleLogger(), false);
+ * logger.debug("request sent");
+ * }
+ */ +public final class Logger { + + private static final Logger DEFAULT = new Logger(LogLevel.INFO, new ConsoleLogger(), true); + + private final LogLevel level; + private final ILogger logger; + private final boolean silent; + + public Logger(LogLevel level, ILogger logger, boolean silent) { + this.level = level; + this.logger = logger; + this.silent = silent; + } + + /** + * Returns a default silent logger (no output). + */ + public static Logger getDefault() { + return DEFAULT; + } + + /** + * Creates a Logger from a {@link LogConfig}. If config is {@code null}, returns the default silent logger. + */ + public static Logger from(LogConfig config) { + if (config == null) { + return DEFAULT; + } + return new Logger(config.level(), config.logger(), config.silent()); + } + + /** + * Creates a Logger from an {@code Optional}. If empty, returns the default silent logger. + */ + public static Logger from(java.util.Optional config) { + return config.map(Logger::from).orElse(DEFAULT); + } + + private boolean shouldLog(LogLevel messageLevel) { + return !silent && level.getValue() <= messageLevel.getValue(); + } + + public boolean isDebug() { + return shouldLog(LogLevel.DEBUG); + } + + public boolean isInfo() { + return shouldLog(LogLevel.INFO); + } + + public boolean isWarn() { + return shouldLog(LogLevel.WARN); + } + + public boolean isError() { + return shouldLog(LogLevel.ERROR); + } + + public void debug(String message) { + if (isDebug()) { + logger.debug(message); + } + } + + public void info(String message) { + if (isInfo()) { + logger.info(message); + } + } + + public void warn(String message) { + if (isWarn()) { + logger.warn(message); + } + } + + public void error(String message) { + if (isError()) { + logger.error(message); + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LoggingInterceptor.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LoggingInterceptor.java new file mode 100644 index 000000000000..f3d23a6328bf --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/LoggingInterceptor.java @@ -0,0 +1,104 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.io.IOException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; + +/** + * OkHttp interceptor that logs HTTP requests and responses. + * + *

Logs request method, URL, and headers (with sensitive values redacted) at debug level. + * Logs response status at debug level, and 4xx/5xx responses at error level. + * Does nothing if the logger is silent. + */ +public final class LoggingInterceptor implements Interceptor { + + private static final Set SENSITIVE_HEADERS = new HashSet<>(Arrays.asList( + "authorization", + "www-authenticate", + "x-api-key", + "api-key", + "apikey", + "x-api-token", + "x-auth-token", + "auth-token", + "proxy-authenticate", + "proxy-authorization", + "cookie", + "set-cookie", + "x-csrf-token", + "x-xsrf-token", + "x-session-token", + "x-access-token")); + + private final Logger logger; + + public LoggingInterceptor(Logger logger) { + this.logger = logger; + } + + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + + if (logger.isDebug()) { + StringBuilder sb = new StringBuilder(); + sb.append("HTTP Request: ").append(request.method()).append(" ").append(request.url()); + sb.append(" headers={"); + boolean first = true; + for (String name : request.headers().names()) { + if (!first) { + sb.append(", "); + } + sb.append(name).append("="); + if (SENSITIVE_HEADERS.contains(name.toLowerCase())) { + sb.append("[REDACTED]"); + } else { + sb.append(request.header(name)); + } + first = false; + } + sb.append("}"); + sb.append(" has_body=").append(request.body() != null); + logger.debug(sb.toString()); + } + + Response response = chain.proceed(request); + + if (logger.isDebug()) { + StringBuilder sb = new StringBuilder(); + sb.append("HTTP Response: status=").append(response.code()); + sb.append(" url=").append(response.request().url()); + sb.append(" headers={"); + boolean first = true; + for (String name : response.headers().names()) { + if (!first) { + sb.append(", "); + } + sb.append(name).append("="); + if (SENSITIVE_HEADERS.contains(name.toLowerCase())) { + sb.append("[REDACTED]"); + } else { + sb.append(response.header(name)); + } + first = false; + } + sb.append("}"); + logger.debug(sb.toString()); + } + + if (response.code() >= 400 && logger.isError()) { + logger.error("HTTP Error: status=" + response.code() + " url=" + + response.request().url()); + } + + return response; + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/MediaTypes.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/MediaTypes.java new file mode 100644 index 000000000000..03f7318b2b78 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/MediaTypes.java @@ -0,0 +1,13 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import okhttp3.MediaType; + +public final class MediaTypes { + + public static final MediaType APPLICATION_JSON = MediaType.parse("application/json"); + + private MediaTypes() {} +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Nullable.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Nullable.java new file mode 100644 index 000000000000..b63355bfd81d --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Nullable.java @@ -0,0 +1,140 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.util.Optional; +import java.util.function.Function; + +public final class Nullable { + + private final Either, Null> value; + + private Nullable() { + this.value = Either.left(Optional.empty()); + } + + private Nullable(T value) { + if (value == null) { + this.value = Either.right(Null.INSTANCE); + } else { + this.value = Either.left(Optional.of(value)); + } + } + + public static Nullable ofNull() { + return new Nullable<>(null); + } + + public static Nullable of(T value) { + return new Nullable<>(value); + } + + public static Nullable empty() { + return new Nullable<>(); + } + + public static Nullable ofOptional(Optional value) { + if (value.isPresent()) { + return of(value.get()); + } else { + return empty(); + } + } + + public boolean isNull() { + return this.value.isRight(); + } + + public boolean isEmpty() { + return this.value.isLeft() && !this.value.getLeft().isPresent(); + } + + public T get() { + if (this.isNull()) { + return null; + } + + return this.value.getLeft().get(); + } + + public Nullable map(Function mapper) { + if (this.isNull()) { + return Nullable.ofNull(); + } + + return Nullable.ofOptional(this.value.getLeft().map(mapper)); + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof Nullable)) { + return false; + } + + if (((Nullable) other).isNull() && this.isNull()) { + return true; + } + + return this.value.getLeft().equals(((Nullable) other).value.getLeft()); + } + + private static final class Either { + private L left = null; + private R right = null; + + private Either(L left, R right) { + if (left != null && right != null) { + throw new IllegalArgumentException("Left and right argument cannot both be non-null."); + } + + if (left == null && right == null) { + throw new IllegalArgumentException("Left and right argument cannot both be null."); + } + + if (left != null) { + this.left = left; + } + + if (right != null) { + this.right = right; + } + } + + public static Either left(L left) { + return new Either<>(left, null); + } + + public static Either right(R right) { + return new Either<>(null, right); + } + + public boolean isLeft() { + return this.left != null; + } + + public boolean isRight() { + return this.right != null; + } + + public L getLeft() { + if (!this.isLeft()) { + throw new IllegalArgumentException("Cannot get left from right Either."); + } + return this.left; + } + + public R getRight() { + if (!this.isRight()) { + throw new IllegalArgumentException("Cannot get right from left Either."); + } + return this.right; + } + } + + private static final class Null { + private static final Null INSTANCE = new Null(); + + private Null() {} + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/NullableNonemptyFilter.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/NullableNonemptyFilter.java new file mode 100644 index 000000000000..14661637cf0e --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/NullableNonemptyFilter.java @@ -0,0 +1,22 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.util.Optional; + +public final class NullableNonemptyFilter { + @Override + public boolean equals(Object o) { + boolean isOptionalEmpty = isOptionalEmpty(o); + + return isOptionalEmpty; + } + + private boolean isOptionalEmpty(Object o) { + if (o instanceof Optional) { + return !((Optional) o).isPresent(); + } + return false; + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ObjectMappers.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ObjectMappers.java new file mode 100644 index 000000000000..61d53769ebab --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ObjectMappers.java @@ -0,0 +1,46 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import java.io.IOException; + +public final class ObjectMappers { + public static final ObjectMapper JSON_MAPPER = JsonMapper.builder() + .addModule(new Jdk8Module()) + .addModule(new JavaTimeModule()) + .addModule(DateTimeDeserializer.getModule()) + .addModule(DoubleSerializer.getModule()) + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .build(); + + private ObjectMappers() {} + + public static String stringify(Object o) { + try { + return JSON_MAPPER + .setSerializationInclusion(JsonInclude.Include.ALWAYS) + .writerWithDefaultPrettyPrinter() + .writeValueAsString(o); + } catch (IOException e) { + return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode()); + } + } + + public static Object parseErrorBody(String responseBodyString) { + try { + return JSON_MAPPER.readValue(responseBodyString, Object.class); + } catch (JsonProcessingException ignored) { + return responseBodyString; + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/QueryStringMapper.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/QueryStringMapper.java new file mode 100644 index 000000000000..b98a454ddbd5 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/QueryStringMapper.java @@ -0,0 +1,142 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import okhttp3.HttpUrl; +import okhttp3.MultipartBody; + +public class QueryStringMapper { + + private static final ObjectMapper MAPPER = ObjectMappers.JSON_MAPPER; + + public static void addQueryParameter(HttpUrl.Builder httpUrl, String key, Object value, boolean arraysAsRepeats) { + JsonNode valueNode = MAPPER.valueToTree(value); + + List> flat; + if (valueNode.isObject()) { + flat = flattenObject((ObjectNode) valueNode, arraysAsRepeats); + } else if (valueNode.isArray()) { + flat = flattenArray((ArrayNode) valueNode, "", arraysAsRepeats); + } else { + if (valueNode.isTextual()) { + httpUrl.addQueryParameter(key, valueNode.textValue()); + } else { + httpUrl.addQueryParameter(key, valueNode.toString()); + } + return; + } + + for (Map.Entry field : flat) { + if (field.getValue().isTextual()) { + httpUrl.addQueryParameter(key + field.getKey(), field.getValue().textValue()); + } else { + httpUrl.addQueryParameter(key + field.getKey(), field.getValue().toString()); + } + } + } + + public static void addFormDataPart( + MultipartBody.Builder multipartBody, String key, Object value, boolean arraysAsRepeats) { + JsonNode valueNode = MAPPER.valueToTree(value); + + List> flat; + if (valueNode.isObject()) { + flat = flattenObject((ObjectNode) valueNode, arraysAsRepeats); + } else if (valueNode.isArray()) { + flat = flattenArray((ArrayNode) valueNode, "", arraysAsRepeats); + } else { + if (valueNode.isTextual()) { + multipartBody.addFormDataPart(key, valueNode.textValue()); + } else { + multipartBody.addFormDataPart(key, valueNode.toString()); + } + return; + } + + for (Map.Entry field : flat) { + if (field.getValue().isTextual()) { + multipartBody.addFormDataPart( + key + field.getKey(), field.getValue().textValue()); + } else { + multipartBody.addFormDataPart( + key + field.getKey(), field.getValue().toString()); + } + } + } + + public static List> flattenObject(ObjectNode object, boolean arraysAsRepeats) { + List> flat = new ArrayList<>(); + + Iterator> fields = object.fields(); + while (fields.hasNext()) { + Map.Entry field = fields.next(); + + String key = "[" + field.getKey() + "]"; + + if (field.getValue().isObject()) { + List> flatField = + flattenObject((ObjectNode) field.getValue(), arraysAsRepeats); + addAll(flat, flatField, key); + } else if (field.getValue().isArray()) { + List> flatField = + flattenArray((ArrayNode) field.getValue(), key, arraysAsRepeats); + addAll(flat, flatField, ""); + } else { + flat.add(new AbstractMap.SimpleEntry<>(key, field.getValue())); + } + } + + return flat; + } + + private static List> flattenArray( + ArrayNode array, String key, boolean arraysAsRepeats) { + List> flat = new ArrayList<>(); + + Iterator elements = array.elements(); + + int index = 0; + while (elements.hasNext()) { + JsonNode element = elements.next(); + + String indexKey = key + "[" + index + "]"; + + if (arraysAsRepeats) { + indexKey = key; + } + + if (element.isObject()) { + List> flatField = flattenObject((ObjectNode) element, arraysAsRepeats); + addAll(flat, flatField, indexKey); + } else if (element.isArray()) { + List> flatField = flattenArray((ArrayNode) element, "", arraysAsRepeats); + addAll(flat, flatField, indexKey); + } else { + flat.add(new AbstractMap.SimpleEntry<>(indexKey, element)); + } + + index++; + } + + return flat; + } + + private static void addAll( + List> target, List> source, String prefix) { + for (Map.Entry entry : source) { + Map.Entry entryToAdd = + new AbstractMap.SimpleEntry<>(prefix + entry.getKey(), entry.getValue()); + target.add(entryToAdd); + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/RequestOptions.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/RequestOptions.java new file mode 100644 index 000000000000..b8d2775e7b69 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/RequestOptions.java @@ -0,0 +1,118 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +public final class RequestOptions { + private final Optional timeout; + + private final TimeUnit timeoutTimeUnit; + + private final Map headers; + + private final Map> headerSuppliers; + + private final Map queryParameters; + + private final Map> queryParameterSuppliers; + + private RequestOptions( + Optional timeout, + TimeUnit timeoutTimeUnit, + Map headers, + Map> headerSuppliers, + Map queryParameters, + Map> queryParameterSuppliers) { + this.timeout = timeout; + this.timeoutTimeUnit = timeoutTimeUnit; + this.headers = headers; + this.headerSuppliers = headerSuppliers; + this.queryParameters = queryParameters; + this.queryParameterSuppliers = queryParameterSuppliers; + } + + public Optional getTimeout() { + return timeout; + } + + public TimeUnit getTimeoutTimeUnit() { + return timeoutTimeUnit; + } + + public Map getHeaders() { + Map headers = new HashMap<>(); + headers.putAll(this.headers); + this.headerSuppliers.forEach((key, supplier) -> { + headers.put(key, supplier.get()); + }); + return headers; + } + + public Map getQueryParameters() { + Map queryParameters = new HashMap<>(this.queryParameters); + this.queryParameterSuppliers.forEach((key, supplier) -> { + queryParameters.put(key, supplier.get()); + }); + return queryParameters; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private Optional timeout = Optional.empty(); + + private TimeUnit timeoutTimeUnit = TimeUnit.SECONDS; + + private final Map headers = new HashMap<>(); + + private final Map> headerSuppliers = new HashMap<>(); + + private final Map queryParameters = new HashMap<>(); + + private final Map> queryParameterSuppliers = new HashMap<>(); + + public Builder timeout(Integer timeout) { + this.timeout = Optional.of(timeout); + return this; + } + + public Builder timeout(Integer timeout, TimeUnit timeoutTimeUnit) { + this.timeout = Optional.of(timeout); + this.timeoutTimeUnit = timeoutTimeUnit; + return this; + } + + public Builder addHeader(String key, String value) { + this.headers.put(key, value); + return this; + } + + public Builder addHeader(String key, Supplier value) { + this.headerSuppliers.put(key, value); + return this; + } + + public Builder addQueryParameter(String key, String value) { + this.queryParameters.put(key, value); + return this; + } + + public Builder addQueryParameter(String key, Supplier value) { + this.queryParameterSuppliers.put(key, value); + return this; + } + + public RequestOptions build() { + return new RequestOptions( + timeout, timeoutTimeUnit, headers, headerSuppliers, queryParameters, queryParameterSuppliers); + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ResponseBodyInputStream.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ResponseBodyInputStream.java new file mode 100644 index 000000000000..670948b26d62 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ResponseBodyInputStream.java @@ -0,0 +1,45 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.io.FilterInputStream; +import java.io.IOException; +import okhttp3.Response; + +/** + * A custom InputStream that wraps the InputStream from the OkHttp Response and ensures that the + * OkHttp Response object is properly closed when the stream is closed. + * + * This class extends FilterInputStream and takes an OkHttp Response object as a parameter. + * It retrieves the InputStream from the Response and overrides the close method to close + * both the InputStream and the Response object, ensuring proper resource management and preventing + * premature closure of the underlying HTTP connection. + */ +public class ResponseBodyInputStream extends FilterInputStream { + private final Response response; + + /** + * Constructs a ResponseBodyInputStream that wraps the InputStream from the given OkHttp + * Response object. + * + * @param response the OkHttp Response object from which the InputStream is retrieved + * @throws IOException if an I/O error occurs while retrieving the InputStream + */ + public ResponseBodyInputStream(Response response) throws IOException { + super(response.body().byteStream()); + this.response = response; + } + + /** + * Closes the InputStream and the associated OkHttp Response object. This ensures that the + * underlying HTTP connection is properly closed after the stream is no longer needed. + * + * @throws IOException if an I/O error occurs + */ + @Override + public void close() throws IOException { + super.close(); + response.close(); // Ensure the response is closed when the stream is closed + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ResponseBodyReader.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ResponseBodyReader.java new file mode 100644 index 000000000000..e00c92fe46a3 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/ResponseBodyReader.java @@ -0,0 +1,44 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.io.FilterReader; +import java.io.IOException; +import okhttp3.Response; + +/** + * A custom Reader that wraps the Reader from the OkHttp Response and ensures that the + * OkHttp Response object is properly closed when the reader is closed. + * + * This class extends FilterReader and takes an OkHttp Response object as a parameter. + * It retrieves the Reader from the Response and overrides the close method to close + * both the Reader and the Response object, ensuring proper resource management and preventing + * premature closure of the underlying HTTP connection. + */ +public class ResponseBodyReader extends FilterReader { + private final Response response; + + /** + * Constructs a ResponseBodyReader that wraps the Reader from the given OkHttp Response object. + * + * @param response the OkHttp Response object from which the Reader is retrieved + * @throws IOException if an I/O error occurs while retrieving the Reader + */ + public ResponseBodyReader(Response response) throws IOException { + super(response.body().charStream()); + this.response = response; + } + + /** + * Closes the Reader and the associated OkHttp Response object. This ensures that the + * underlying HTTP connection is properly closed after the reader is no longer needed. + * + * @throws IOException if an I/O error occurs + */ + @Override + public void close() throws IOException { + super.close(); + response.close(); // Ensure the response is closed when the reader is closed + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/RetryInterceptor.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/RetryInterceptor.java new file mode 100644 index 000000000000..8a352c0f9b0c --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/RetryInterceptor.java @@ -0,0 +1,181 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.io.IOException; +import java.time.Duration; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.Optional; +import java.util.Random; +import okhttp3.Interceptor; +import okhttp3.Response; + +public class RetryInterceptor implements Interceptor { + + private static final Duration INITIAL_RETRY_DELAY = Duration.ofMillis(1000); + private static final Duration MAX_RETRY_DELAY = Duration.ofMillis(60000); + private static final double JITTER_FACTOR = 0.2; + + private final int maxRetries; + private final Random random = new Random(); + + public RetryInterceptor(int maxRetries) { + this.maxRetries = maxRetries; + } + + @Override + public Response intercept(Chain chain) throws IOException { + Response response = chain.proceed(chain.request()); + + if (shouldRetry(response.code())) { + return retryChain(response, chain); + } + + return response; + } + + private Response retryChain(Response response, Chain chain) throws IOException { + ExponentialBackoff backoff = new ExponentialBackoff(this.maxRetries); + Optional nextBackoff = backoff.nextBackoff(response); + while (nextBackoff.isPresent()) { + try { + Thread.sleep(nextBackoff.get().toMillis()); + } catch (InterruptedException e) { + throw new IOException("Interrupted while trying request", e); + } + response.close(); + response = chain.proceed(chain.request()); + if (shouldRetry(response.code())) { + nextBackoff = backoff.nextBackoff(response); + } else { + return response; + } + } + + return response; + } + + /** + * Calculates the retry delay from response headers, with fallback to exponential backoff. + * Priority: Retry-After > X-RateLimit-Reset > Exponential Backoff + */ + private Duration getRetryDelayFromHeaders(Response response, int retryAttempt) { + // Check for Retry-After header first (RFC 7231), with no jitter + String retryAfter = response.header("Retry-After"); + if (retryAfter != null) { + // Parse as number of seconds... + Optional secondsDelay = tryParseLong(retryAfter) + .map(seconds -> seconds * 1000) + .filter(delayMs -> delayMs > 0) + .map(delayMs -> Math.min(delayMs, MAX_RETRY_DELAY.toMillis())) + .map(Duration::ofMillis); + if (secondsDelay.isPresent()) { + return secondsDelay.get(); + } + + // ...or as an HTTP date; both are valid + Optional dateDelay = tryParseHttpDate(retryAfter) + .map(resetTime -> resetTime.toInstant().toEpochMilli() - System.currentTimeMillis()) + .filter(delayMs -> delayMs > 0) + .map(delayMs -> Math.min(delayMs, MAX_RETRY_DELAY.toMillis())) + .map(Duration::ofMillis); + if (dateDelay.isPresent()) { + return dateDelay.get(); + } + } + + // Then check for industry-standard X-RateLimit-Reset header, with positive jitter + String rateLimitReset = response.header("X-RateLimit-Reset"); + if (rateLimitReset != null) { + // Assume Unix timestamp in epoch seconds + Optional rateLimitDelay = tryParseLong(rateLimitReset) + .map(resetTimeSeconds -> (resetTimeSeconds * 1000) - System.currentTimeMillis()) + .filter(delayMs -> delayMs > 0) + .map(delayMs -> Math.min(delayMs, MAX_RETRY_DELAY.toMillis())) + .map(this::addPositiveJitter) + .map(Duration::ofMillis); + if (rateLimitDelay.isPresent()) { + return rateLimitDelay.get(); + } + } + + // Fall back to exponential backoff, with symmetric jitter + long baseDelay = INITIAL_RETRY_DELAY.toMillis() * (1L << retryAttempt); // 2^retryAttempt + long cappedDelay = Math.min(baseDelay, MAX_RETRY_DELAY.toMillis()); + return Duration.ofMillis(addSymmetricJitter(cappedDelay)); + } + + /** + * Attempts to parse a string as a long, returning empty Optional on failure. + */ + private Optional tryParseLong(String value) { + if (value == null) { + return Optional.empty(); + } + try { + return Optional.of(Long.parseLong(value)); + } catch (NumberFormatException e) { + return Optional.empty(); + } + } + + /** + * Attempts to parse a string as an HTTP date (RFC 1123), returning empty Optional on failure. + */ + private Optional tryParseHttpDate(String value) { + if (value == null) { + return Optional.empty(); + } + try { + return Optional.of(ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME)); + } catch (DateTimeParseException e) { + return Optional.empty(); + } + } + + /** + * Adds positive jitter (100-120% of original value) to prevent thundering herd. + * Used for X-RateLimit-Reset header delays. + */ + private long addPositiveJitter(long delayMs) { + double jitterMultiplier = 1.0 + (random.nextDouble() * JITTER_FACTOR); + return (long) (delayMs * jitterMultiplier); + } + + /** + * Adds symmetric jitter (90-110% of original value) to prevent thundering herd. + * Used for exponential backoff delays. + */ + private long addSymmetricJitter(long delayMs) { + double jitterMultiplier = 1.0 + ((random.nextDouble() - 0.5) * JITTER_FACTOR); + return (long) (delayMs * jitterMultiplier); + } + + private static boolean shouldRetry(int statusCode) { + return statusCode == 408 || statusCode == 429 || statusCode >= 500; + } + + private final class ExponentialBackoff { + + private final int maxNumRetries; + + private int retryNumber = 0; + + ExponentialBackoff(int maxNumRetries) { + this.maxNumRetries = maxNumRetries; + } + + public Optional nextBackoff(Response response) { + if (retryNumber >= maxNumRetries) { + return Optional.empty(); + } + + Duration delay = getRetryDelayFromHeaders(response, retryNumber); + retryNumber += 1; + return Optional.of(delay); + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Rfc2822DateTimeDeserializer.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Rfc2822DateTimeDeserializer.java new file mode 100644 index 000000000000..6af139c15b88 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Rfc2822DateTimeDeserializer.java @@ -0,0 +1,25 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import java.io.IOException; +import java.time.OffsetDateTime; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +/** + * Custom deserializer that handles converting RFC 2822 (RFC 1123) dates into {@link OffsetDateTime} objects. + * This is used for fields with format "date-time-rfc-2822", such as Twilio's dateCreated, dateSent, dateUpdated. + */ +public class Rfc2822DateTimeDeserializer extends JsonDeserializer { + + @Override + public OffsetDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException { + String raw = parser.getValueAsString(); + return ZonedDateTime.parse(raw, DateTimeFormatter.RFC_1123_DATE_TIME).toOffsetDateTime(); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalApiException.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalApiException.java new file mode 100644 index 000000000000..6b9c5f36f9f6 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalApiException.java @@ -0,0 +1,73 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import okhttp3.Response; + +/** + * This exception type will be thrown for any non-2XX API responses. + */ +public class SeedBasicAuthOptionalApiException extends SeedBasicAuthOptionalException { + /** + * The error code of the response that triggered the exception. + */ + private final int statusCode; + + /** + * The body of the response that triggered the exception. + */ + private final Object body; + + private final Map> headers; + + public SeedBasicAuthOptionalApiException(String message, int statusCode, Object body) { + super(message); + this.statusCode = statusCode; + this.body = body; + this.headers = new HashMap<>(); + } + + public SeedBasicAuthOptionalApiException(String message, int statusCode, Object body, Response rawResponse) { + super(message); + this.statusCode = statusCode; + this.body = body; + this.headers = new HashMap<>(); + rawResponse.headers().forEach(header -> { + String key = header.component1(); + String value = header.component2(); + this.headers.computeIfAbsent(key, _str -> new ArrayList<>()).add(value); + }); + } + + /** + * @return the statusCode + */ + public int statusCode() { + return this.statusCode; + } + + /** + * @return the body + */ + public Object body() { + return this.body; + } + + /** + * @return the headers + */ + public Map> headers() { + return this.headers; + } + + @Override + public String toString() { + return "SeedBasicAuthOptionalApiException{" + "message: " + getMessage() + ", statusCode: " + statusCode + + ", body: " + ObjectMappers.stringify(body) + "}"; + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalException.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalException.java new file mode 100644 index 000000000000..89d1190864bd --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalException.java @@ -0,0 +1,17 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +/** + * This class serves as the base exception for all errors in the SDK. + */ +public class SeedBasicAuthOptionalException extends RuntimeException { + public SeedBasicAuthOptionalException(String message) { + super(message); + } + + public SeedBasicAuthOptionalException(String message, Exception e) { + super(message, e); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalHttpResponse.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalHttpResponse.java new file mode 100644 index 000000000000..8568ad9a54ab --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SeedBasicAuthOptionalHttpResponse.java @@ -0,0 +1,37 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import okhttp3.Response; + +public final class SeedBasicAuthOptionalHttpResponse { + + private final T body; + + private final Map> headers; + + public SeedBasicAuthOptionalHttpResponse(T body, Response rawResponse) { + this.body = body; + + Map> headers = new HashMap<>(); + rawResponse.headers().forEach(header -> { + String key = header.component1(); + String value = header.component2(); + headers.computeIfAbsent(key, _str -> new ArrayList<>()).add(value); + }); + this.headers = headers; + } + + public T body() { + return this.body; + } + + public Map> headers() { + return headers; + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SseEvent.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SseEvent.java new file mode 100644 index 000000000000..cc2b31a29af3 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SseEvent.java @@ -0,0 +1,114 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.Objects; +import java.util.Optional; + +/** + * Represents a Server-Sent Event with all standard fields. + * Used for event-level discrimination where the discriminator is at the SSE envelope level. + * + * @param The type of the data field + */ +@JsonInclude(JsonInclude.Include.NON_ABSENT) +@JsonIgnoreProperties(ignoreUnknown = true) +public final class SseEvent { + private final String event; + private final T data; + private final String id; + private final Long retry; + + private SseEvent(String event, T data, String id, Long retry) { + this.event = event; + this.data = data; + this.id = id; + this.retry = retry; + } + + @JsonProperty("event") + public Optional getEvent() { + return Optional.ofNullable(event); + } + + @JsonProperty("data") + public T getData() { + return data; + } + + @JsonProperty("id") + public Optional getId() { + return Optional.ofNullable(id); + } + + @JsonProperty("retry") + public Optional getRetry() { + return Optional.ofNullable(retry); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SseEvent sseEvent = (SseEvent) o; + return Objects.equals(event, sseEvent.event) + && Objects.equals(data, sseEvent.data) + && Objects.equals(id, sseEvent.id) + && Objects.equals(retry, sseEvent.retry); + } + + @Override + public int hashCode() { + return Objects.hash(event, data, id, retry); + } + + @Override + public String toString() { + return "SseEvent{" + "event='" + + event + '\'' + ", data=" + + data + ", id='" + + id + '\'' + ", retry=" + + retry + '}'; + } + + public static Builder builder() { + return new Builder<>(); + } + + public static final class Builder { + private String event; + private T data; + private String id; + private Long retry; + + private Builder() {} + + public Builder event(String event) { + this.event = event; + return this; + } + + public Builder data(T data) { + this.data = data; + return this; + } + + public Builder id(String id) { + this.id = id; + return this; + } + + public Builder retry(Long retry) { + this.retry = retry; + return this; + } + + public SseEvent build() { + return new SseEvent<>(event, data, id, retry); + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SseEventParser.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SseEventParser.java new file mode 100644 index 000000000000..cef6eb181ecd --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/SseEventParser.java @@ -0,0 +1,228 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeName; +import com.fasterxml.jackson.core.type.TypeReference; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** + * Utility class for parsing Server-Sent Events with support for discriminated unions. + *

+ * Handles two discrimination patterns: + *

    + *
  1. Data-level discrimination: The discriminator (e.g., 'type') is inside the JSON data payload. + * Jackson's polymorphic deserialization handles this automatically.
  2. + *
  3. Event-level discrimination: The discriminator (e.g., 'event') is at the SSE envelope level. + * This requires constructing the full SSE envelope for Jackson to process.
  4. + *
+ */ +public final class SseEventParser { + + private static final Set SSE_ENVELOPE_FIELDS = new HashSet<>(Arrays.asList("event", "data", "id", "retry")); + + private SseEventParser() { + // Utility class + } + + /** + * Parse an SSE event using event-level discrimination. + *

+ * Constructs the full SSE envelope object with event, data, id, and retry fields, + * then deserializes it to the target union type. + * + * @param eventType The SSE event type (from event: field) + * @param data The SSE data content (from data: field) + * @param id The SSE event ID (from id: field), may be null + * @param retry The SSE retry value (from retry: field), may be null + * @param unionClass The target union class + * @param discriminatorProperty The property name used for discrimination (e.g., "event") + * @param The target type + * @return The deserialized object + */ + public static T parseEventLevelUnion( + String eventType, String data, String id, Long retry, Class unionClass, String discriminatorProperty) { + try { + // Determine if data should be parsed as JSON based on the variant's expected type + Object parsedData = parseDataForVariant(eventType, data, unionClass, discriminatorProperty); + + // Construct the SSE envelope object + Map envelope = new HashMap<>(); + envelope.put(discriminatorProperty, eventType); + envelope.put("data", parsedData); + if (id != null) { + envelope.put("id", id); + } + if (retry != null) { + envelope.put("retry", retry); + } + + // Serialize to JSON and deserialize to target type + String envelopeJson = ObjectMappers.JSON_MAPPER.writeValueAsString(envelope); + return ObjectMappers.JSON_MAPPER.readValue(envelopeJson, unionClass); + } catch (Exception e) { + throw new RuntimeException("Failed to parse SSE event with event-level discrimination", e); + } + } + + /** + * Parse an SSE event using data-level discrimination. + *

+ * Simply parses the data field as JSON and deserializes it to the target type. + * Jackson's polymorphic deserialization handles the discrimination automatically. + * + * @param data The SSE data content (from data: field) + * @param valueType The target type + * @param The target type + * @return The deserialized object + */ + public static T parseDataLevelUnion(String data, Class valueType) { + try { + return ObjectMappers.JSON_MAPPER.readValue(data, valueType); + } catch (Exception e) { + throw new RuntimeException("Failed to parse SSE data with data-level discrimination", e); + } + } + + /** + * Determines if the given discriminator property indicates event-level discrimination. + * Event-level discrimination occurs when the discriminator is an SSE envelope field. + * + * @param discriminatorProperty The discriminator property name + * @return true if event-level discrimination, false otherwise + */ + public static boolean isEventLevelDiscrimination(String discriminatorProperty) { + return SSE_ENVELOPE_FIELDS.contains(discriminatorProperty); + } + + /** + * Attempts to find the discriminator property from the union class's Jackson annotations. + * + * @param unionClass The union class to inspect + * @return The discriminator property name, or empty if not found + */ + public static Optional findDiscriminatorProperty(Class unionClass) { + try { + // Look for JsonTypeInfo on the class itself + JsonTypeInfo typeInfo = unionClass.getAnnotation(JsonTypeInfo.class); + if (typeInfo != null && !typeInfo.property().isEmpty()) { + return Optional.of(typeInfo.property()); + } + + // Look for inner Value interface with JsonTypeInfo + for (Class innerClass : unionClass.getDeclaredClasses()) { + typeInfo = innerClass.getAnnotation(JsonTypeInfo.class); + if (typeInfo != null && !typeInfo.property().isEmpty()) { + return Optional.of(typeInfo.property()); + } + } + } catch (Exception e) { + // Ignore reflection errors + } + return Optional.empty(); + } + + /** + * Parse the data field based on what the matching variant expects. + * If the variant expects a String for its data field, returns the raw string. + * Otherwise, parses the data as JSON. + */ + private static Object parseDataForVariant( + String eventType, String data, Class unionClass, String discriminatorProperty) { + if (data == null || data.isEmpty()) { + return data; + } + + try { + // Try to find the variant class that matches this event type + Class variantClass = findVariantClass(unionClass, eventType, discriminatorProperty); + if (variantClass != null) { + // Check if the variant expects a String for the data field + Field dataField = findField(variantClass, "data"); + if (dataField != null && String.class.equals(dataField.getType())) { + // Variant expects String - return raw data + return data; + } + } + + // Try to parse as JSON + return ObjectMappers.JSON_MAPPER.readValue(data, new TypeReference>() {}); + } catch (Exception e) { + // If JSON parsing fails, return as string + return data; + } + } + + /** + * Find the variant class that matches the given discriminator value. + */ + private static Class findVariantClass( + Class unionClass, String discriminatorValue, String discriminatorProperty) { + try { + // Look for JsonSubTypes annotation + JsonSubTypes subTypes = findJsonSubTypes(unionClass); + if (subTypes == null) { + return null; + } + + for (JsonSubTypes.Type subType : subTypes.value()) { + JsonTypeName typeName = subType.value().getAnnotation(JsonTypeName.class); + if (typeName != null && typeName.value().equals(discriminatorValue)) { + return subType.value(); + } + // Also check the name attribute of @JsonSubTypes.Type + if (subType.name().equals(discriminatorValue)) { + return subType.value(); + } + } + } catch (Exception e) { + // Ignore reflection errors + } + return null; + } + + /** + * Find JsonSubTypes annotation on the class or its inner classes. + */ + private static JsonSubTypes findJsonSubTypes(Class unionClass) { + // Check the class itself + JsonSubTypes subTypes = unionClass.getAnnotation(JsonSubTypes.class); + if (subTypes != null) { + return subTypes; + } + + // Check inner classes (for Fern-style unions with inner Value interface) + for (Class innerClass : unionClass.getDeclaredClasses()) { + subTypes = innerClass.getAnnotation(JsonSubTypes.class); + if (subTypes != null) { + return subTypes; + } + } + return null; + } + + /** + * Find a field by name in a class, including private fields. + */ + private static Field findField(Class clazz, String fieldName) { + try { + return clazz.getDeclaredField(fieldName); + } catch (NoSuchFieldException e) { + // Check superclass + Class superClass = clazz.getSuperclass(); + if (superClass != null && superClass != Object.class) { + return findField(superClass, fieldName); + } + return null; + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Stream.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Stream.java new file mode 100644 index 000000000000..c66e45614552 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Stream.java @@ -0,0 +1,513 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.io.Closeable; +import java.io.IOException; +import java.io.Reader; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Scanner; + +/** + * The {@code Stream} class implements {@link Iterable} to provide a simple mechanism for reading and parsing + * objects of a given type from data streamed via a {@link Reader} using a specified delimiter. + *

+ * {@code Stream} assumes that data is being pushed to the provided {@link Reader} asynchronously and utilizes a + * {@code Scanner} to block during iteration if the next object is not available. + * Iterable stream for parsing JSON and Server-Sent Events (SSE) data. + * Supports both newline-delimited JSON and SSE with optional stream termination. + * + * @param The type of objects in the stream. + */ +public final class Stream implements Iterable, Closeable { + + private static final String NEWLINE = "\n"; + private static final String DATA_PREFIX = "data:"; + + public enum StreamType { + JSON, + SSE, + SSE_EVENT_DISCRIMINATED + } + + private final Class valueType; + private final Scanner scanner; + private final StreamType streamType; + private final String messageTerminator; + private final String streamTerminator; + private final Reader sseReader; + private final String discriminatorProperty; + private boolean isClosed = false; + + /** + * Constructs a new {@code Stream} with the specified value type, reader, and delimiter. + * + * @param valueType The class of the objects in the stream. + * @param reader The reader that provides the streamed data. + * @param delimiter The delimiter used to separate elements in the stream. + */ + public Stream(Class valueType, Reader reader, String delimiter) { + this.valueType = valueType; + this.scanner = new Scanner(reader).useDelimiter(delimiter); + this.streamType = StreamType.JSON; + this.messageTerminator = delimiter; + this.streamTerminator = null; + this.sseReader = null; + this.discriminatorProperty = null; + } + + private Stream(Class valueType, StreamType type, Reader reader, String terminator) { + this(valueType, type, reader, terminator, null); + } + + private Stream( + Class valueType, StreamType type, Reader reader, String terminator, String discriminatorProperty) { + this.valueType = valueType; + this.streamType = type; + this.discriminatorProperty = discriminatorProperty; + if (type == StreamType.JSON) { + this.scanner = new Scanner(reader).useDelimiter(terminator); + this.messageTerminator = terminator; + this.streamTerminator = null; + this.sseReader = null; + } else { + this.scanner = null; + this.messageTerminator = NEWLINE; + this.streamTerminator = terminator; + this.sseReader = reader; + } + } + + public static Stream fromJson(Class valueType, Reader reader, String delimiter) { + return new Stream<>(valueType, reader, delimiter); + } + + public static Stream fromJson(Class valueType, Reader reader) { + return new Stream<>(valueType, reader, NEWLINE); + } + + public static Stream fromSse(Class valueType, Reader sseReader) { + return new Stream<>(valueType, StreamType.SSE, sseReader, null); + } + + public static Stream fromSse(Class valueType, Reader sseReader, String streamTerminator) { + return new Stream<>(valueType, StreamType.SSE, sseReader, streamTerminator); + } + + /** + * Creates a stream from SSE data with event-level discrimination support. + * Use this when the SSE payload is a discriminated union where the discriminator + * is an SSE envelope field (e.g., 'event'). + * + * @param valueType The class of the objects in the stream. + * @param sseReader The reader that provides the SSE data. + * @param discriminatorProperty The property name used for discrimination (e.g., "event"). + * @param The type of objects in the stream. + * @return A new Stream instance configured for SSE with event-level discrimination. + */ + public static Stream fromSseWithEventDiscrimination( + Class valueType, Reader sseReader, String discriminatorProperty) { + return new Stream<>(valueType, StreamType.SSE_EVENT_DISCRIMINATED, sseReader, null, discriminatorProperty); + } + + /** + * Creates a stream from SSE data with event-level discrimination support and a stream terminator. + * + * @param valueType The class of the objects in the stream. + * @param sseReader The reader that provides the SSE data. + * @param discriminatorProperty The property name used for discrimination (e.g., "event"). + * @param streamTerminator The terminator string that signals end of stream (e.g., "[DONE]"). + * @param The type of objects in the stream. + * @return A new Stream instance configured for SSE with event-level discrimination. + */ + public static Stream fromSseWithEventDiscrimination( + Class valueType, Reader sseReader, String discriminatorProperty, String streamTerminator) { + return new Stream<>( + valueType, StreamType.SSE_EVENT_DISCRIMINATED, sseReader, streamTerminator, discriminatorProperty); + } + + @Override + public void close() throws IOException { + if (!isClosed) { + isClosed = true; + if (scanner != null) { + scanner.close(); + } + if (sseReader != null) { + sseReader.close(); + } + } + } + + private boolean isStreamClosed() { + return isClosed; + } + + /** + * Returns an iterator over the elements in this stream that blocks during iteration when the next object is + * not yet available. + * + * @return An iterator that can be used to traverse the elements in the stream. + */ + @Override + public Iterator iterator() { + switch (streamType) { + case SSE: + return new SSEIterator(); + case SSE_EVENT_DISCRIMINATED: + return new SSEEventDiscriminatedIterator(); + case JSON: + default: + return new JsonIterator(); + } + } + + private final class JsonIterator implements Iterator { + + /** + * Returns {@code true} if there are more elements in the stream. + *

+ * Will block and wait for input if the stream has not ended and the next object is not yet available. + * + * @return {@code true} if there are more elements, {@code false} otherwise. + */ + @Override + public boolean hasNext() { + if (isStreamClosed()) { + return false; + } + return scanner.hasNext(); + } + + /** + * Returns the next element in the stream. + *

+ * Will block and wait for input if the stream has not ended and the next object is not yet available. + * + * @return The next element in the stream. + * @throws NoSuchElementException If there are no more elements in the stream. + */ + @Override + public T next() { + if (isStreamClosed()) { + throw new NoSuchElementException("Stream is closed"); + } + + if (!scanner.hasNext()) { + throw new NoSuchElementException(); + } else { + try { + T parsedResponse = + ObjectMappers.JSON_MAPPER.readValue(scanner.next().trim(), valueType); + return parsedResponse; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + } + + private final class SSEIterator implements Iterator { + private Scanner sseScanner; + private T nextItem; + private boolean hasNextItem = false; + private boolean endOfStream = false; + private StringBuilder eventDataBuffer = new StringBuilder(); + private String currentEventType = null; + + private SSEIterator() { + if (sseReader != null && !isStreamClosed()) { + this.sseScanner = new Scanner(sseReader); + } else { + this.endOfStream = true; + } + } + + @Override + public boolean hasNext() { + if (isStreamClosed() || endOfStream) { + return false; + } + + if (hasNextItem) { + return true; + } + + return readNextMessage(); + } + + @Override + public T next() { + if (!hasNext()) { + throw new NoSuchElementException("No more elements in stream"); + } + + T result = nextItem; + nextItem = null; + hasNextItem = false; + return result; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + private boolean readNextMessage() { + if (sseScanner == null || isStreamClosed()) { + endOfStream = true; + return false; + } + + try { + while (sseScanner.hasNextLine()) { + String line = sseScanner.nextLine(); + + if (line.trim().isEmpty()) { + if (eventDataBuffer.length() > 0) { + try { + nextItem = ObjectMappers.JSON_MAPPER.readValue(eventDataBuffer.toString(), valueType); + hasNextItem = true; + eventDataBuffer.setLength(0); + currentEventType = null; + return true; + } catch (Exception parseEx) { + System.err.println("Failed to parse SSE event: " + parseEx.getMessage()); + eventDataBuffer.setLength(0); + currentEventType = null; + continue; + } + } + continue; + } + + if (line.startsWith(DATA_PREFIX)) { + String dataContent = line.substring(DATA_PREFIX.length()); + if (dataContent.startsWith(" ")) { + dataContent = dataContent.substring(1); + } + + if (eventDataBuffer.length() == 0 + && streamTerminator != null + && dataContent.trim().equals(streamTerminator)) { + endOfStream = true; + return false; + } + + if (eventDataBuffer.length() > 0) { + eventDataBuffer.append('\n'); + } + eventDataBuffer.append(dataContent); + } else if (line.startsWith("event:")) { + String eventValue = line.length() > 6 ? line.substring(6) : ""; + if (eventValue.startsWith(" ")) { + eventValue = eventValue.substring(1); + } + currentEventType = eventValue; + } else if (line.startsWith("id:")) { + // Event ID field (ignored) + } else if (line.startsWith("retry:")) { + // Retry field (ignored) + } else if (line.startsWith(":")) { + // Comment line (ignored) + } + } + + if (eventDataBuffer.length() > 0) { + try { + nextItem = ObjectMappers.JSON_MAPPER.readValue(eventDataBuffer.toString(), valueType); + hasNextItem = true; + eventDataBuffer.setLength(0); + currentEventType = null; + return true; + } catch (Exception parseEx) { + System.err.println("Failed to parse final SSE event: " + parseEx.getMessage()); + eventDataBuffer.setLength(0); + currentEventType = null; + } + } + + endOfStream = true; + return false; + + } catch (Exception e) { + System.err.println("Failed to parse SSE stream: " + e.getMessage()); + endOfStream = true; + return false; + } + } + } + + /** + * Iterator for SSE streams with event-level discrimination. + * Uses SseEventParser to construct the full SSE envelope for Jackson deserialization. + */ + private final class SSEEventDiscriminatedIterator implements Iterator { + private Scanner sseScanner; + private T nextItem; + private boolean hasNextItem = false; + private boolean endOfStream = false; + private StringBuilder eventDataBuffer = new StringBuilder(); + private String currentEventType = null; + private String currentEventId = null; + private Long currentRetry = null; + + private SSEEventDiscriminatedIterator() { + if (sseReader != null && !isStreamClosed()) { + this.sseScanner = new Scanner(sseReader); + } else { + this.endOfStream = true; + } + } + + @Override + public boolean hasNext() { + if (isStreamClosed() || endOfStream) { + return false; + } + + if (hasNextItem) { + return true; + } + + return readNextMessage(); + } + + @Override + public T next() { + if (!hasNext()) { + throw new NoSuchElementException("No more elements in stream"); + } + + T result = nextItem; + nextItem = null; + hasNextItem = false; + return result; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + private boolean readNextMessage() { + if (sseScanner == null || isStreamClosed()) { + endOfStream = true; + return false; + } + + try { + while (sseScanner.hasNextLine()) { + String line = sseScanner.nextLine(); + + if (line.trim().isEmpty()) { + if (eventDataBuffer.length() > 0 || currentEventType != null) { + try { + // Use SseEventParser for event-level discrimination + nextItem = SseEventParser.parseEventLevelUnion( + currentEventType, + eventDataBuffer.toString(), + currentEventId, + currentRetry, + valueType, + discriminatorProperty); + hasNextItem = true; + resetEventState(); + return true; + } catch (Exception parseEx) { + System.err.println("Failed to parse SSE event: " + parseEx.getMessage()); + resetEventState(); + continue; + } + } + continue; + } + + if (line.startsWith(DATA_PREFIX)) { + String dataContent = line.substring(DATA_PREFIX.length()); + if (dataContent.startsWith(" ")) { + dataContent = dataContent.substring(1); + } + + if (eventDataBuffer.length() == 0 + && streamTerminator != null + && dataContent.trim().equals(streamTerminator)) { + endOfStream = true; + return false; + } + + if (eventDataBuffer.length() > 0) { + eventDataBuffer.append('\n'); + } + eventDataBuffer.append(dataContent); + } else if (line.startsWith("event:")) { + String eventValue = line.length() > 6 ? line.substring(6) : ""; + if (eventValue.startsWith(" ")) { + eventValue = eventValue.substring(1); + } + currentEventType = eventValue; + } else if (line.startsWith("id:")) { + String idValue = line.length() > 3 ? line.substring(3) : ""; + if (idValue.startsWith(" ")) { + idValue = idValue.substring(1); + } + currentEventId = idValue; + } else if (line.startsWith("retry:")) { + String retryValue = line.length() > 6 ? line.substring(6) : ""; + if (retryValue.startsWith(" ")) { + retryValue = retryValue.substring(1); + } + try { + currentRetry = Long.parseLong(retryValue.trim()); + } catch (NumberFormatException e) { + // Ignore invalid retry values + } + } else if (line.startsWith(":")) { + // Comment line (ignored) + } + } + + // Handle any remaining buffered data at end of stream + if (eventDataBuffer.length() > 0 || currentEventType != null) { + try { + nextItem = SseEventParser.parseEventLevelUnion( + currentEventType, + eventDataBuffer.toString(), + currentEventId, + currentRetry, + valueType, + discriminatorProperty); + hasNextItem = true; + resetEventState(); + return true; + } catch (Exception parseEx) { + System.err.println("Failed to parse final SSE event: " + parseEx.getMessage()); + resetEventState(); + } + } + + endOfStream = true; + return false; + + } catch (Exception e) { + System.err.println("Failed to parse SSE stream: " + e.getMessage()); + endOfStream = true; + return false; + } + } + + private void resetEventState() { + eventDataBuffer.setLength(0); + currentEventType = null; + currentEventId = null; + currentRetry = null; + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Suppliers.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Suppliers.java new file mode 100644 index 000000000000..8ce71e045dd7 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/core/Suppliers.java @@ -0,0 +1,23 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Supplier; + +public final class Suppliers { + private Suppliers() {} + + public static Supplier memoize(Supplier delegate) { + AtomicReference value = new AtomicReference<>(); + return () -> { + T val = value.get(); + if (val == null) { + val = value.updateAndGet(cur -> cur == null ? Objects.requireNonNull(delegate.get()) : cur); + } + return val; + }; + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/AsyncBasicAuthClient.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/AsyncBasicAuthClient.java new file mode 100644 index 000000000000..b1848a284e92 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/AsyncBasicAuthClient.java @@ -0,0 +1,54 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.resources.basicauth; + +import com.seed.basicAuthOptional.core.ClientOptions; +import com.seed.basicAuthOptional.core.RequestOptions; +import java.util.concurrent.CompletableFuture; + +public class AsyncBasicAuthClient { + protected final ClientOptions clientOptions; + + private final AsyncRawBasicAuthClient rawClient; + + public AsyncBasicAuthClient(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + this.rawClient = new AsyncRawBasicAuthClient(clientOptions); + } + + /** + * Get responses with HTTP metadata like headers + */ + public AsyncRawBasicAuthClient withRawResponse() { + return this.rawClient; + } + + /** + * GET request with basic auth scheme + */ + public CompletableFuture getWithBasicAuth() { + return this.rawClient.getWithBasicAuth().thenApply(response -> response.body()); + } + + /** + * GET request with basic auth scheme + */ + public CompletableFuture getWithBasicAuth(RequestOptions requestOptions) { + return this.rawClient.getWithBasicAuth(requestOptions).thenApply(response -> response.body()); + } + + /** + * POST request with basic auth scheme + */ + public CompletableFuture postWithBasicAuth(Object request) { + return this.rawClient.postWithBasicAuth(request).thenApply(response -> response.body()); + } + + /** + * POST request with basic auth scheme + */ + public CompletableFuture postWithBasicAuth(Object request, RequestOptions requestOptions) { + return this.rawClient.postWithBasicAuth(request, requestOptions).thenApply(response -> response.body()); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/AsyncRawBasicAuthClient.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/AsyncRawBasicAuthClient.java new file mode 100644 index 000000000000..df6fb35363d3 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/AsyncRawBasicAuthClient.java @@ -0,0 +1,192 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.resources.basicauth; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.seed.basicAuthOptional.core.ClientOptions; +import com.seed.basicAuthOptional.core.MediaTypes; +import com.seed.basicAuthOptional.core.ObjectMappers; +import com.seed.basicAuthOptional.core.RequestOptions; +import com.seed.basicAuthOptional.core.SeedBasicAuthOptionalApiException; +import com.seed.basicAuthOptional.core.SeedBasicAuthOptionalException; +import com.seed.basicAuthOptional.core.SeedBasicAuthOptionalHttpResponse; +import com.seed.basicAuthOptional.resources.errors.errors.BadRequest; +import com.seed.basicAuthOptional.resources.errors.errors.UnauthorizedRequest; +import com.seed.basicAuthOptional.resources.errors.types.UnauthorizedRequestErrorBody; +import java.io.IOException; +import java.util.concurrent.CompletableFuture; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.Headers; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.ResponseBody; +import org.jetbrains.annotations.NotNull; + +public class AsyncRawBasicAuthClient { + protected final ClientOptions clientOptions; + + public AsyncRawBasicAuthClient(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + } + + /** + * GET request with basic auth scheme + */ + public CompletableFuture> getWithBasicAuth() { + return getWithBasicAuth(null); + } + + /** + * GET request with basic auth scheme + */ + public CompletableFuture> getWithBasicAuth( + RequestOptions requestOptions) { + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("basic-auth"); + if (requestOptions != null) { + requestOptions.getQueryParameters().forEach((_key, _value) -> { + httpUrl.addQueryParameter(_key, _value); + }); + } + Request okhttpRequest = new Request.Builder() + .url(httpUrl.build()) + .method("GET", null) + .headers(Headers.of(clientOptions.headers(requestOptions))) + .addHeader("Accept", "application/json") + .build(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + CompletableFuture> future = new CompletableFuture<>(); + client.newCall(okhttpRequest).enqueue(new Callback() { + @Override + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { + try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SeedBasicAuthOptionalHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, boolean.class), response)); + return; + } + try { + if (response.code() == 401) { + future.completeExceptionally(new UnauthorizedRequest( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnauthorizedRequestErrorBody.class), + response)); + return; + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SeedBasicAuthOptionalApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + return; + } catch (IOException e) { + future.completeExceptionally( + new SeedBasicAuthOptionalException("Network error executing HTTP request", e)); + } + } + + @Override + public void onFailure(@NotNull Call call, @NotNull IOException e) { + future.completeExceptionally( + new SeedBasicAuthOptionalException("Network error executing HTTP request", e)); + } + }); + return future; + } + + /** + * POST request with basic auth scheme + */ + public CompletableFuture> postWithBasicAuth(Object request) { + return postWithBasicAuth(request, null); + } + + /** + * POST request with basic auth scheme + */ + public CompletableFuture> postWithBasicAuth( + Object request, RequestOptions requestOptions) { + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("basic-auth"); + if (requestOptions != null) { + requestOptions.getQueryParameters().forEach((_key, _value) -> { + httpUrl.addQueryParameter(_key, _value); + }); + } + RequestBody body; + try { + body = RequestBody.create( + ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); + } catch (JsonProcessingException e) { + throw new SeedBasicAuthOptionalException("Failed to serialize request", e); + } + Request okhttpRequest = new Request.Builder() + .url(httpUrl.build()) + .method("POST", body) + .headers(Headers.of(clientOptions.headers(requestOptions))) + .addHeader("Content-Type", "application/json") + .addHeader("Accept", "application/json") + .build(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + CompletableFuture> future = new CompletableFuture<>(); + client.newCall(okhttpRequest).enqueue(new Callback() { + @Override + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { + try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SeedBasicAuthOptionalHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, boolean.class), response)); + return; + } + try { + switch (response.code()) { + case 400: + future.completeExceptionally(new BadRequest( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), + response)); + return; + case 401: + future.completeExceptionally(new UnauthorizedRequest( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnauthorizedRequestErrorBody.class), + response)); + return; + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SeedBasicAuthOptionalApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + return; + } catch (IOException e) { + future.completeExceptionally( + new SeedBasicAuthOptionalException("Network error executing HTTP request", e)); + } + } + + @Override + public void onFailure(@NotNull Call call, @NotNull IOException e) { + future.completeExceptionally( + new SeedBasicAuthOptionalException("Network error executing HTTP request", e)); + } + }); + return future; + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/BasicAuthClient.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/BasicAuthClient.java new file mode 100644 index 000000000000..e47ae4afa069 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/BasicAuthClient.java @@ -0,0 +1,53 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.resources.basicauth; + +import com.seed.basicAuthOptional.core.ClientOptions; +import com.seed.basicAuthOptional.core.RequestOptions; + +public class BasicAuthClient { + protected final ClientOptions clientOptions; + + private final RawBasicAuthClient rawClient; + + public BasicAuthClient(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + this.rawClient = new RawBasicAuthClient(clientOptions); + } + + /** + * Get responses with HTTP metadata like headers + */ + public RawBasicAuthClient withRawResponse() { + return this.rawClient; + } + + /** + * GET request with basic auth scheme + */ + public boolean getWithBasicAuth() { + return this.rawClient.getWithBasicAuth().body(); + } + + /** + * GET request with basic auth scheme + */ + public boolean getWithBasicAuth(RequestOptions requestOptions) { + return this.rawClient.getWithBasicAuth(requestOptions).body(); + } + + /** + * POST request with basic auth scheme + */ + public boolean postWithBasicAuth(Object request) { + return this.rawClient.postWithBasicAuth(request).body(); + } + + /** + * POST request with basic auth scheme + */ + public boolean postWithBasicAuth(Object request, RequestOptions requestOptions) { + return this.rawClient.postWithBasicAuth(request, requestOptions).body(); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/RawBasicAuthClient.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/RawBasicAuthClient.java new file mode 100644 index 000000000000..efe4d4e7afcc --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/basicauth/RawBasicAuthClient.java @@ -0,0 +1,151 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.resources.basicauth; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.seed.basicAuthOptional.core.ClientOptions; +import com.seed.basicAuthOptional.core.MediaTypes; +import com.seed.basicAuthOptional.core.ObjectMappers; +import com.seed.basicAuthOptional.core.RequestOptions; +import com.seed.basicAuthOptional.core.SeedBasicAuthOptionalApiException; +import com.seed.basicAuthOptional.core.SeedBasicAuthOptionalException; +import com.seed.basicAuthOptional.core.SeedBasicAuthOptionalHttpResponse; +import com.seed.basicAuthOptional.resources.errors.errors.BadRequest; +import com.seed.basicAuthOptional.resources.errors.errors.UnauthorizedRequest; +import com.seed.basicAuthOptional.resources.errors.types.UnauthorizedRequestErrorBody; +import java.io.IOException; +import okhttp3.Headers; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.ResponseBody; + +public class RawBasicAuthClient { + protected final ClientOptions clientOptions; + + public RawBasicAuthClient(ClientOptions clientOptions) { + this.clientOptions = clientOptions; + } + + /** + * GET request with basic auth scheme + */ + public SeedBasicAuthOptionalHttpResponse getWithBasicAuth() { + return getWithBasicAuth(null); + } + + /** + * GET request with basic auth scheme + */ + public SeedBasicAuthOptionalHttpResponse getWithBasicAuth(RequestOptions requestOptions) { + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("basic-auth"); + if (requestOptions != null) { + requestOptions.getQueryParameters().forEach((_key, _value) -> { + httpUrl.addQueryParameter(_key, _value); + }); + } + Request okhttpRequest = new Request.Builder() + .url(httpUrl.build()) + .method("GET", null) + .headers(Headers.of(clientOptions.headers(requestOptions))) + .addHeader("Accept", "application/json") + .build(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { + ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SeedBasicAuthOptionalHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, boolean.class), response); + } + try { + if (response.code() == 401) { + throw new UnauthorizedRequest( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UnauthorizedRequestErrorBody.class), + response); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SeedBasicAuthOptionalApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SeedBasicAuthOptionalException("Network error executing HTTP request", e); + } + } + + /** + * POST request with basic auth scheme + */ + public SeedBasicAuthOptionalHttpResponse postWithBasicAuth(Object request) { + return postWithBasicAuth(request, null); + } + + /** + * POST request with basic auth scheme + */ + public SeedBasicAuthOptionalHttpResponse postWithBasicAuth(Object request, RequestOptions requestOptions) { + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("basic-auth"); + if (requestOptions != null) { + requestOptions.getQueryParameters().forEach((_key, _value) -> { + httpUrl.addQueryParameter(_key, _value); + }); + } + RequestBody body; + try { + body = RequestBody.create( + ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); + } catch (JsonProcessingException e) { + throw new SeedBasicAuthOptionalException("Failed to serialize request", e); + } + Request okhttpRequest = new Request.Builder() + .url(httpUrl.build()) + .method("POST", body) + .headers(Headers.of(clientOptions.headers(requestOptions))) + .addHeader("Content-Type", "application/json") + .addHeader("Accept", "application/json") + .build(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { + ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SeedBasicAuthOptionalHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, boolean.class), response); + } + try { + switch (response.code()) { + case 400: + throw new BadRequest( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response); + case 401: + throw new UnauthorizedRequest( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnauthorizedRequestErrorBody.class), + response); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SeedBasicAuthOptionalApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SeedBasicAuthOptionalException("Network error executing HTTP request", e); + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/errors/BadRequest.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/errors/BadRequest.java new file mode 100644 index 000000000000..6d972bfb7584 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/errors/BadRequest.java @@ -0,0 +1,17 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.resources.errors.errors; + +import com.seed.basicAuthOptional.core.SeedBasicAuthOptionalApiException; +import okhttp3.Response; + +public final class BadRequest extends SeedBasicAuthOptionalApiException { + public BadRequest(Object body) { + super("BadRequest", 400, body); + } + + public BadRequest(Object body, Response rawResponse) { + super("BadRequest", 400, body, rawResponse); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/errors/UnauthorizedRequest.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/errors/UnauthorizedRequest.java new file mode 100644 index 000000000000..1cfa9d69e590 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/errors/UnauthorizedRequest.java @@ -0,0 +1,33 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.resources.errors.errors; + +import com.seed.basicAuthOptional.core.SeedBasicAuthOptionalApiException; +import com.seed.basicAuthOptional.resources.errors.types.UnauthorizedRequestErrorBody; +import okhttp3.Response; + +public final class UnauthorizedRequest extends SeedBasicAuthOptionalApiException { + /** + * The body of the response that triggered the exception. + */ + private final UnauthorizedRequestErrorBody body; + + public UnauthorizedRequest(UnauthorizedRequestErrorBody body) { + super("UnauthorizedRequest", 401, body); + this.body = body; + } + + public UnauthorizedRequest(UnauthorizedRequestErrorBody body, Response rawResponse) { + super("UnauthorizedRequest", 401, body, rawResponse); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public UnauthorizedRequestErrorBody body() { + return this.body; + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/types/UnauthorizedRequestErrorBody.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/types/UnauthorizedRequestErrorBody.java new file mode 100644 index 000000000000..8f457b0b5f26 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/seed/basicAuthOptional/resources/errors/types/UnauthorizedRequestErrorBody.java @@ -0,0 +1,118 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.resources.errors.types; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.seed.basicAuthOptional.core.ObjectMappers; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import org.jetbrains.annotations.NotNull; + +@JsonInclude(JsonInclude.Include.NON_ABSENT) +@JsonDeserialize(builder = UnauthorizedRequestErrorBody.Builder.class) +public final class UnauthorizedRequestErrorBody { + private final String message; + + private final Map additionalProperties; + + private UnauthorizedRequestErrorBody(String message, Map additionalProperties) { + this.message = message; + this.additionalProperties = additionalProperties; + } + + @JsonProperty("message") + public String getMessage() { + return message; + } + + @java.lang.Override + public boolean equals(Object other) { + if (this == other) return true; + return other instanceof UnauthorizedRequestErrorBody && equalTo((UnauthorizedRequestErrorBody) other); + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + private boolean equalTo(UnauthorizedRequestErrorBody other) { + return message.equals(other.message); + } + + @java.lang.Override + public int hashCode() { + return Objects.hash(this.message); + } + + @java.lang.Override + public String toString() { + return ObjectMappers.stringify(this); + } + + public static MessageStage builder() { + return new Builder(); + } + + public interface MessageStage { + _FinalStage message(@NotNull String message); + + Builder from(UnauthorizedRequestErrorBody other); + } + + public interface _FinalStage { + UnauthorizedRequestErrorBody build(); + + _FinalStage additionalProperty(String key, Object value); + + _FinalStage additionalProperties(Map additionalProperties); + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static final class Builder implements MessageStage, _FinalStage { + private String message; + + @JsonAnySetter + private Map additionalProperties = new HashMap<>(); + + private Builder() {} + + @java.lang.Override + public Builder from(UnauthorizedRequestErrorBody other) { + message(other.getMessage()); + return this; + } + + @java.lang.Override + @JsonSetter("message") + public _FinalStage message(@NotNull String message) { + this.message = Objects.requireNonNull(message, "message must not be null"); + return this; + } + + @java.lang.Override + public UnauthorizedRequestErrorBody build() { + return new UnauthorizedRequestErrorBody(message, additionalProperties); + } + + @java.lang.Override + public Builder additionalProperty(String key, Object value) { + this.additionalProperties.put(key, value); + return this; + } + + @java.lang.Override + public Builder additionalProperties(Map additionalProperties) { + this.additionalProperties.putAll(additionalProperties); + return this; + } + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example0.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example0.java new file mode 100644 index 000000000000..14a6d42e9b10 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example0.java @@ -0,0 +1,14 @@ +package com.snippets; + +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; + +public class Example0 { + public static void main(String[] args) { + SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient.builder() + .credentials("", "") + .url("https://api.fern.com") + .build(); + + client.basicAuth().getWithBasicAuth(); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example1.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example1.java new file mode 100644 index 000000000000..7abf04040fe8 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example1.java @@ -0,0 +1,14 @@ +package com.snippets; + +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; + +public class Example1 { + public static void main(String[] args) { + SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient.builder() + .credentials("", "") + .url("https://api.fern.com") + .build(); + + client.basicAuth().getWithBasicAuth(); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example2.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example2.java new file mode 100644 index 000000000000..6d344d42946a --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example2.java @@ -0,0 +1,14 @@ +package com.snippets; + +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; + +public class Example2 { + public static void main(String[] args) { + SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient.builder() + .credentials("", "") + .url("https://api.fern.com") + .build(); + + client.basicAuth().getWithBasicAuth(); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example3.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example3.java new file mode 100644 index 000000000000..08e6ada4fa3f --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example3.java @@ -0,0 +1,19 @@ +package com.snippets; + +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; +import java.util.HashMap; + +public class Example3 { + public static void main(String[] args) { + SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient.builder() + .credentials("", "") + .url("https://api.fern.com") + .build(); + + client.basicAuth().postWithBasicAuth(new HashMap() { + { + put("key", "value"); + } + }); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example4.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example4.java new file mode 100644 index 000000000000..850a77a9e793 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example4.java @@ -0,0 +1,19 @@ +package com.snippets; + +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; +import java.util.HashMap; + +public class Example4 { + public static void main(String[] args) { + SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient.builder() + .credentials("", "") + .url("https://api.fern.com") + .build(); + + client.basicAuth().postWithBasicAuth(new HashMap() { + { + put("key", "value"); + } + }); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example5.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example5.java new file mode 100644 index 000000000000..5ce319c7e0f3 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example5.java @@ -0,0 +1,19 @@ +package com.snippets; + +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; +import java.util.HashMap; + +public class Example5 { + public static void main(String[] args) { + SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient.builder() + .credentials("", "") + .url("https://api.fern.com") + .build(); + + client.basicAuth().postWithBasicAuth(new HashMap() { + { + put("key", "value"); + } + }); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example6.java b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example6.java new file mode 100644 index 000000000000..7f74dbb65983 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/main/java/com/snippets/Example6.java @@ -0,0 +1,19 @@ +package com.snippets; + +import com.seed.basicAuthOptional.SeedBasicAuthOptionalClient; +import java.util.HashMap; + +public class Example6 { + public static void main(String[] args) { + SeedBasicAuthOptionalClient client = SeedBasicAuthOptionalClient.builder() + .credentials("", "") + .url("https://api.fern.com") + .build(); + + client.basicAuth().postWithBasicAuth(new HashMap() { + { + put("key", "value"); + } + }); + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/StreamTest.java b/seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/StreamTest.java new file mode 100644 index 000000000000..701acd19efff --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/StreamTest.java @@ -0,0 +1,120 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional; + +import static org.junit.jupiter.api.Assertions.*; + +import com.seed.basicAuthOptional.core.ObjectMappers; +import com.seed.basicAuthOptional.core.Stream; +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.junit.jupiter.api.Test; + +public final class StreamTest { + @Test + public void testJsonStream() { + List> messages = + Arrays.asList(createMap("message", "hello"), createMap("message", "world")); + List jsonStrings = messages.stream().map(StreamTest::mapToJson).collect(Collectors.toList()); + String input = String.join("\n", jsonStrings); + StringReader jsonInput = new StringReader(input); + Stream jsonStream = Stream.fromJson(Map.class, jsonInput); + int expectedMessages = 2; + int actualMessages = 0; + for (Map jsonObject : jsonStream) { + actualMessages++; + assertTrue(jsonObject.containsKey("message")); + } + assertEquals(expectedMessages, actualMessages); + } + + @Test + public void testSseStream() { + List> events = Arrays.asList(createMap("event", "start"), createMap("event", "end")); + List sseStrings = events.stream().map(StreamTest::mapToSse).collect(Collectors.toList()); + String input = String.join("\n" + "\n", sseStrings); + StringReader sseInput = new StringReader(input); + Stream sseStream = Stream.fromSse(Map.class, sseInput); + int expectedEvents = 2; + int actualEvents = 0; + for (Map eventData : sseStream) { + actualEvents++; + assertTrue(eventData.containsKey("event")); + } + assertEquals(expectedEvents, actualEvents); + } + + @Test + public void testSseStreamWithTerminator() { + List> events = Arrays.asList(createMap("message", "first"), createMap("message", "second")); + List sseStrings = + new ArrayList<>(events.stream().map(StreamTest::mapToSse).collect(Collectors.toList())); + sseStrings.add("data: [DONE]"); + String input = String.join("\n" + "\n", sseStrings); + StringReader sseInput = new StringReader(input); + Stream sseStream = Stream.fromSse(Map.class, sseInput, "[DONE]"); + int expectedEvents = 2; + int actualEvents = 0; + for (Map eventData : sseStream) { + actualEvents++; + assertTrue(eventData.containsKey("message")); + } + assertEquals(expectedEvents, actualEvents); + } + + @Test + public void testSseEventDiscriminatedStream() { + List sseStrings = Arrays.asList( + mapToSseWithEvent("start", createMap("status", "pending")), + mapToSseWithEvent("end", createMap("status", "complete"))); + String input = String.join("\n" + "\n", sseStrings); + StringReader sseInput = new StringReader(input); + Stream sseStream = Stream.fromSseWithEventDiscrimination(Map.class, sseInput, "event"); + int expectedEvents = 2; + int actualEvents = 0; + for (Map eventData : sseStream) { + actualEvents++; + // Event-level discrimination includes the event field in the parsed result + assertTrue(eventData.containsKey("event")); + assertTrue(eventData.containsKey("data")); + } + assertEquals(expectedEvents, actualEvents); + } + + @Test + public void testStreamResourceManagement() throws IOException { + StringReader testInput = new StringReader("{\"test\":\"data\"}"); + Stream testStream = Stream.fromJson(Map.class, testInput); + testStream.close(); + assertFalse(testStream.iterator().hasNext()); + } + + private static String mapToJson(Map map) { + try { + return ObjectMappers.JSON_MAPPER.writeValueAsString(map); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private static String mapToSse(Map map) { + return "data: " + mapToJson(map); + } + + private static String mapToSseWithEvent(String eventType, Map data) { + return "event: " + eventType + "\n" + "data: " + mapToJson(data); + } + + private static Map createMap(String key, String value) { + Map map = new HashMap<>(); + map.put(key, value); + return map; + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/TestClient.java b/seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/TestClient.java new file mode 100644 index 000000000000..4fb6b4eab796 --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/TestClient.java @@ -0,0 +1,11 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional; + +public final class TestClient { + public void test() { + // Add tests here and mark this file in .fernignore + assert true; + } +} diff --git a/seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/core/QueryStringMapperTest.java b/seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/core/QueryStringMapperTest.java new file mode 100644 index 000000000000..bbce88cad69a --- /dev/null +++ b/seed/java-sdk/basic-auth-optional/src/test/java/com/seed/basicAuthOptional/core/QueryStringMapperTest.java @@ -0,0 +1,339 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.seed.basicAuthOptional.core; + +import java.time.Instant; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import okhttp3.HttpUrl; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public final class QueryStringMapperTest { + @Test + public void testObjectWithQuotedString_indexedArrays() { + Map map = new HashMap() { + { + put("hello", "\"world\""); + } + }; + + String expectedQueryString = "withquoted%5Bhello%5D=%22world%22"; + + String actualQueryString = queryString( + new HashMap() { + { + put("withquoted", map); + } + }, + false); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testObjectWithQuotedString_arraysAsRepeats() { + Map map = new HashMap() { + { + put("hello", "\"world\""); + } + }; + + String expectedQueryString = "withquoted%5Bhello%5D=%22world%22"; + + String actualQueryString = queryString( + new HashMap() { + { + put("withquoted", map); + } + }, + true); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testObject_indexedArrays() { + Map map = new HashMap() { + { + put("foo", "bar"); + put("baz", "qux"); + } + }; + + String expectedQueryString = "metadata%5Bfoo%5D=bar&metadata%5Bbaz%5D=qux"; + + String actualQueryString = queryString( + new HashMap() { + { + put("metadata", map); + } + }, + false); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testObject_arraysAsRepeats() { + Map map = new HashMap() { + { + put("foo", "bar"); + put("baz", "qux"); + } + }; + + String expectedQueryString = "metadata%5Bfoo%5D=bar&metadata%5Bbaz%5D=qux"; + + String actualQueryString = queryString( + new HashMap() { + { + put("metadata", map); + } + }, + true); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testNestedObject_indexedArrays() { + Map> nestedMap = new HashMap>() { + { + put("mapkey1", new HashMap() { + { + put("mapkey1mapkey1", "mapkey1mapkey1value"); + put("mapkey1mapkey2", "mapkey1mapkey2value"); + } + }); + put("mapkey2", new HashMap() { + { + put("mapkey2mapkey1", "mapkey2mapkey1value"); + } + }); + } + }; + + String expectedQueryString = + "nested%5Bmapkey2%5D%5Bmapkey2mapkey1%5D=mapkey2mapkey1value&nested%5Bmapkey1%5D%5Bmapkey1mapkey1" + + "%5D=mapkey1mapkey1value&nested%5Bmapkey1%5D%5Bmapkey1mapkey2%5D=mapkey1mapkey2value"; + + String actualQueryString = queryString( + new HashMap() { + { + put("nested", nestedMap); + } + }, + false); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testNestedObject_arraysAsRepeats() { + Map> nestedMap = new HashMap>() { + { + put("mapkey1", new HashMap() { + { + put("mapkey1mapkey1", "mapkey1mapkey1value"); + put("mapkey1mapkey2", "mapkey1mapkey2value"); + } + }); + put("mapkey2", new HashMap() { + { + put("mapkey2mapkey1", "mapkey2mapkey1value"); + } + }); + } + }; + + String expectedQueryString = + "nested%5Bmapkey2%5D%5Bmapkey2mapkey1%5D=mapkey2mapkey1value&nested%5Bmapkey1%5D%5Bmapkey1mapkey1" + + "%5D=mapkey1mapkey1value&nested%5Bmapkey1%5D%5Bmapkey1mapkey2%5D=mapkey1mapkey2value"; + + String actualQueryString = queryString( + new HashMap() { + { + put("nested", nestedMap); + } + }, + true); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testDateTime_indexedArrays() { + OffsetDateTime dateTime = + OffsetDateTime.ofInstant(Instant.ofEpochSecond(1740412107L), ZoneId.of("America/New_York")); + + String expectedQueryString = "datetime=2025-02-24T10%3A48%3A27-05%3A00"; + + String actualQueryString = queryString( + new HashMap() { + { + put("datetime", dateTime); + } + }, + false); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testDateTime_arraysAsRepeats() { + OffsetDateTime dateTime = + OffsetDateTime.ofInstant(Instant.ofEpochSecond(1740412107L), ZoneId.of("America/New_York")); + + String expectedQueryString = "datetime=2025-02-24T10%3A48%3A27-05%3A00"; + + String actualQueryString = queryString( + new HashMap() { + { + put("datetime", dateTime); + } + }, + true); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testObjectArray_indexedArrays() { + List> mapArray = new ArrayList>() { + { + add(new HashMap() { + { + put("key", "hello"); + put("value", "world"); + } + }); + add(new HashMap() { + { + put("key", "foo"); + put("value", "bar"); + } + }); + add(new HashMap<>()); + } + }; + + String expectedQueryString = "objects%5B0%5D%5Bvalue%5D=world&objects%5B0%5D%5Bkey%5D=hello&objects%5B1%5D" + + "%5Bvalue%5D=bar&objects%5B1%5D%5Bkey%5D=foo"; + + String actualQueryString = queryString( + new HashMap() { + { + put("objects", mapArray); + } + }, + false); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testObjectArray_arraysAsRepeats() { + List> mapArray = new ArrayList>() { + { + add(new HashMap() { + { + put("key", "hello"); + put("value", "world"); + } + }); + add(new HashMap() { + { + put("key", "foo"); + put("value", "bar"); + } + }); + add(new HashMap<>()); + } + }; + + String expectedQueryString = + "objects%5Bvalue%5D=world&objects%5Bkey%5D=hello&objects%5Bvalue" + "%5D=bar&objects%5Bkey%5D=foo"; + + String actualQueryString = queryString( + new HashMap() { + { + put("objects", mapArray); + } + }, + true); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testObjectWithArray_indexedArrays() { + Map objectWithArray = new HashMap() { + { + put("id", "abc123"); + put("contactIds", new ArrayList() { + { + add("id1"); + add("id2"); + add("id3"); + } + }); + } + }; + + String expectedQueryString = + "objectwitharray%5Bid%5D=abc123&objectwitharray%5BcontactIds%5D%5B0%5D=id1&objectwitharray" + + "%5BcontactIds%5D%5B1%5D=id2&objectwitharray%5BcontactIds%5D%5B2%5D=id3"; + + String actualQueryString = queryString( + new HashMap() { + { + put("objectwitharray", objectWithArray); + } + }, + false); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + @Test + public void testObjectWithArray_arraysAsRepeats() { + Map objectWithArray = new HashMap() { + { + put("id", "abc123"); + put("contactIds", new ArrayList() { + { + add("id1"); + add("id2"); + add("id3"); + } + }); + } + }; + + String expectedQueryString = "objectwitharray%5Bid%5D=abc123&objectwitharray%5BcontactIds" + + "%5D=id1&objectwitharray%5BcontactIds%5D=id2&objectwitharray%5BcontactIds%5D=id3"; + + String actualQueryString = queryString( + new HashMap() { + { + put("objectwitharray", objectWithArray); + } + }, + true); + + Assertions.assertEquals(expectedQueryString, actualQueryString); + } + + private static String queryString(Map params, boolean arraysAsRepeats) { + HttpUrl.Builder httpUrl = HttpUrl.parse("http://www.fakewebsite.com/").newBuilder(); + params.forEach((paramName, paramValue) -> + QueryStringMapper.addQueryParameter(httpUrl, paramName, paramValue, arraysAsRepeats)); + return httpUrl.build().encodedQuery(); + } +} diff --git a/seed/php-sdk/basic-auth-optional/.fern/metadata.json b/seed/php-sdk/basic-auth-optional/.fern/metadata.json new file mode 100644 index 000000000000..37f759a1679d --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/.fern/metadata.json @@ -0,0 +1,7 @@ +{ + "cliVersion": "DUMMY", + "generatorName": "fernapi/fern-php-sdk", + "generatorVersion": "latest", + "originGitCommit": "DUMMY", + "sdkVersion": "0.0.1" +} \ No newline at end of file diff --git a/seed/php-sdk/basic-auth-optional/.github/workflows/ci.yml b/seed/php-sdk/basic-auth-optional/.github/workflows/ci.yml new file mode 100644 index 000000000000..678eb6c9e141 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/.github/workflows/ci.yml @@ -0,0 +1,52 @@ +name: ci + +on: [push] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + compile: + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.1" + + - name: Install tools + run: | + composer install + + - name: Build + run: | + composer build + + - name: Analyze + run: | + composer analyze + + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.1" + + - name: Install tools + run: | + composer install + + - name: Run Tests + run: | + composer test diff --git a/seed/php-sdk/basic-auth-optional/.gitignore b/seed/php-sdk/basic-auth-optional/.gitignore new file mode 100644 index 000000000000..31a1aeb14f35 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/.gitignore @@ -0,0 +1,5 @@ +.idea +.php-cs-fixer.cache +.phpunit.result.cache +composer.lock +vendor/ \ No newline at end of file diff --git a/seed/php-sdk/basic-auth-optional/README.md b/seed/php-sdk/basic-auth-optional/README.md new file mode 100644 index 000000000000..269cd0bb42b1 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/README.md @@ -0,0 +1,145 @@ +# Seed PHP Library + +[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=Seed%2FPHP) +[![php shield](https://img.shields.io/badge/php-packagist-pink)](https://packagist.org/packages/seed/seed) + +The Seed PHP library provides convenient access to the Seed APIs from PHP. + +## Table of Contents + +- [Requirements](#requirements) +- [Installation](#installation) +- [Usage](#usage) +- [Exception Handling](#exception-handling) +- [Advanced](#advanced) + - [Custom Client](#custom-client) + - [Retries](#retries) + - [Timeouts](#timeouts) +- [Contributing](#contributing) + +## Requirements + +This SDK requires PHP ^8.1. + +## Installation + +```sh +composer require seed/seed +``` + +## Usage + +Instantiate and use the client with the following: + +```php +', + password: '', +); +$client->basicAuth->postWithBasicAuth( + [ + 'key' => "value", + ], +); + +``` + +## Exception Handling + +When the API returns a non-success status code (4xx or 5xx response), an exception will be thrown. + +```php +use Seed\Exceptions\SeedApiException; +use Seed\Exceptions\SeedException; + +try { + $response = $client->basicAuth->postWithBasicAuth(...); +} catch (SeedApiException $e) { + echo 'API Exception occurred: ' . $e->getMessage() . "\n"; + echo 'Status Code: ' . $e->getCode() . "\n"; + echo 'Response Body: ' . $e->getBody() . "\n"; + // Optionally, rethrow the exception or handle accordingly. +} +``` + +## Advanced + +### Custom Client + +This SDK is built to work with any HTTP client that implements the [PSR-18](https://www.php-fig.org/psr/psr-18/) `ClientInterface`. +By default, if no client is provided, the SDK will use `php-http/discovery` to find an installed HTTP client. +However, you can pass your own client that adheres to `ClientInterface`: + +```php +use Seed\SeedClient; + +// Pass any PSR-18 compatible HTTP client implementation. +// For example, using Guzzle: +$customClient = new \GuzzleHttp\Client([ + 'timeout' => 5.0, +]); + +$client = new SeedClient(options: [ + 'client' => $customClient +]); + +// Or using Symfony HttpClient: +// $customClient = (new \Symfony\Component\HttpClient\Psr18Client()) +// ->withOptions(['timeout' => 5.0]); +// +// $client = new SeedClient(options: [ +// 'client' => $customClient +// ]); +``` + +### Retries + +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured +retry limit (default: 2). + +A request is deemed retryable when any of the following HTTP status codes is returned: + +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) + +Use the `maxRetries` request option to configure this behavior. + +```php +$response = $client->basicAuth->postWithBasicAuth( + ..., + options: [ + 'maxRetries' => 0 // Override maxRetries at the request level + ] +); +``` + +### Timeouts + +The SDK defaults to a 30 second timeout. Use the `timeout` option to configure this behavior. + +```php +$response = $client->basicAuth->postWithBasicAuth( + ..., + options: [ + 'timeout' => 3.0 // Override timeout at the request level + ] +); +``` + +## Contributing + +While we value open-source contributions to this SDK, this library is generated programmatically. +Additions made directly to this library would have to be moved over to our generation code, +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening +an issue first to discuss with us! + +On the other hand, contributions to the README are always very welcome! diff --git a/seed/php-sdk/basic-auth-optional/composer.json b/seed/php-sdk/basic-auth-optional/composer.json new file mode 100644 index 000000000000..ad30960a8764 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/composer.json @@ -0,0 +1,46 @@ +{ + "name": "seed/seed", + "version": "0.0.1", + "description": "Seed PHP Library", + "keywords": [ + "seed", + "api", + "sdk" + ], + "license": [], + "require": { + "php": "^8.1", + "ext-json": "*", + "psr/http-client": "^1.0", + "psr/http-client-implementation": "^1.0", + "psr/http-factory": "^1.0", + "psr/http-factory-implementation": "^1.0", + "psr/http-message": "^1.1 || ^2.0", + "php-http/discovery": "^1.0", + "php-http/multipart-stream-builder": "^1.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.0", + "friendsofphp/php-cs-fixer": "3.5.0", + "phpstan/phpstan": "^1.12", + "guzzlehttp/guzzle": "^7.4" + }, + "autoload": { + "psr-4": { + "Seed\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Seed\\Tests\\": "tests/" + } + }, + "scripts": { + "build": [ + "@php -l src", + "@php -l tests" + ], + "test": "phpunit", + "analyze": "phpstan analyze src tests --memory-limit=1G" + } +} \ No newline at end of file diff --git a/seed/php-sdk/basic-auth-optional/phpstan.neon b/seed/php-sdk/basic-auth-optional/phpstan.neon new file mode 100644 index 000000000000..780706b8f8a2 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/phpstan.neon @@ -0,0 +1,6 @@ +parameters: + level: max + reportUnmatchedIgnoredErrors: false + paths: + - src + - tests \ No newline at end of file diff --git a/seed/php-sdk/basic-auth-optional/phpunit.xml b/seed/php-sdk/basic-auth-optional/phpunit.xml new file mode 100644 index 000000000000..54630a51163c --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/phpunit.xml @@ -0,0 +1,7 @@ + + + + tests + + + \ No newline at end of file diff --git a/seed/php-sdk/basic-auth-optional/reference.md b/seed/php-sdk/basic-auth-optional/reference.md new file mode 100644 index 000000000000..76bf05ae1117 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/reference.md @@ -0,0 +1,99 @@ +# Reference +## BasicAuth +

$client->basicAuth->getWithBasicAuth() -> ?bool +
+
+ +#### 📝 Description + +
+
+ +
+
+ +GET request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```php +$client->basicAuth->getWithBasicAuth(); +``` +
+
+
+
+ + +
+
+
+ +
$client->basicAuth->postWithBasicAuth($request) -> ?bool +
+
+ +#### 📝 Description + +
+
+ +
+
+ +POST request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```php +$client->basicAuth->postWithBasicAuth( + [ + 'key' => "value", + ], +); +``` +
+
+
+
+ +#### ⚙️ Parameters + +
+
+ +
+
+ +**$request:** `mixed` + +
+
+
+
+ + +
+
+
+ diff --git a/seed/php-sdk/basic-auth-optional/snippet.json b/seed/php-sdk/basic-auth-optional/snippet.json new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/seed/php-sdk/basic-auth-optional/src/BasicAuth/BasicAuthClient.php b/seed/php-sdk/basic-auth-optional/src/BasicAuth/BasicAuthClient.php new file mode 100644 index 000000000000..ae6eddc5d2e0 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/BasicAuth/BasicAuthClient.php @@ -0,0 +1,146 @@ +, + * } $options @phpstan-ignore-next-line Property is used in endpoint methods via HttpEndpointGenerator + */ + private array $options; + + /** + * @var RawClient $client + */ + private RawClient $client; + + /** + * @param RawClient $client + * @param ?array{ + * baseUrl?: string, + * client?: ClientInterface, + * maxRetries?: int, + * timeout?: float, + * headers?: array, + * } $options + */ + public function __construct( + RawClient $client, + ?array $options = null, + ) { + $this->client = $client; + $this->options = $options ?? []; + } + + /** + * GET request with basic auth scheme + * + * @param ?array{ + * baseUrl?: string, + * maxRetries?: int, + * timeout?: float, + * headers?: array, + * queryParameters?: array, + * bodyProperties?: array, + * } $options + * @return ?bool + * @throws SeedException + * @throws SeedApiException + */ + public function getWithBasicAuth(?array $options = null): ?bool + { + $options = array_merge($this->options, $options ?? []); + try { + $response = $this->client->sendRequest( + new JsonApiRequest( + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? '', + path: "basic-auth", + method: HttpMethod::GET, + ), + $options, + ); + $statusCode = $response->getStatusCode(); + if ($statusCode >= 200 && $statusCode < 400) { + $json = $response->getBody()->getContents(); + if (empty($json)) { + return null; + } + return JsonDecoder::decodeBool($json); + } + } catch (JsonException $e) { + throw new SeedException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); + } catch (ClientExceptionInterface $e) { + throw new SeedException(message: $e->getMessage(), previous: $e); + } + throw new SeedApiException( + message: 'API request failed', + statusCode: $statusCode, + body: $response->getBody()->getContents(), + ); + } + + /** + * POST request with basic auth scheme + * + * @param mixed $request + * @param ?array{ + * baseUrl?: string, + * maxRetries?: int, + * timeout?: float, + * headers?: array, + * queryParameters?: array, + * bodyProperties?: array, + * } $options + * @return ?bool + * @throws SeedException + * @throws SeedApiException + */ + public function postWithBasicAuth(mixed $request, ?array $options = null): ?bool + { + $options = array_merge($this->options, $options ?? []); + try { + $response = $this->client->sendRequest( + new JsonApiRequest( + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? '', + path: "basic-auth", + method: HttpMethod::POST, + body: $request, + ), + $options, + ); + $statusCode = $response->getStatusCode(); + if ($statusCode >= 200 && $statusCode < 400) { + $json = $response->getBody()->getContents(); + if (empty($json)) { + return null; + } + return JsonDecoder::decodeBool($json); + } + } catch (JsonException $e) { + throw new SeedException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); + } catch (ClientExceptionInterface $e) { + throw new SeedException(message: $e->getMessage(), previous: $e); + } + throw new SeedApiException( + message: 'API request failed', + statusCode: $statusCode, + body: $response->getBody()->getContents(), + ); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Client/BaseApiRequest.php b/seed/php-sdk/basic-auth-optional/src/Core/Client/BaseApiRequest.php new file mode 100644 index 000000000000..5e1283e2b6f6 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Client/BaseApiRequest.php @@ -0,0 +1,22 @@ + $headers Additional headers for the request (optional) + * @param array $query Query parameters for the request (optional) + */ + public function __construct( + public readonly string $baseUrl, + public readonly string $path, + public readonly HttpMethod $method, + public readonly array $headers = [], + public readonly array $query = [], + ) { + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Client/HttpClientBuilder.php b/seed/php-sdk/basic-auth-optional/src/Core/Client/HttpClientBuilder.php new file mode 100644 index 000000000000..8ac806af0325 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Client/HttpClientBuilder.php @@ -0,0 +1,56 @@ + + */ + private array $responses = []; + + /** + * @var array + */ + private array $requests = []; + + /** + * @param ResponseInterface ...$responses + */ + public function append(ResponseInterface ...$responses): void + { + foreach ($responses as $response) { + $this->responses[] = $response; + } + } + + /** + * @param RequestInterface $request + * @return ResponseInterface + */ + public function sendRequest(RequestInterface $request): ResponseInterface + { + $this->requests[] = $request; + + if (empty($this->responses)) { + throw new RuntimeException('No more responses in the queue. Add responses using append().'); + } + + return array_shift($this->responses); + } + + /** + * @return ?RequestInterface + */ + public function getLastRequest(): ?RequestInterface + { + if (empty($this->requests)) { + return null; + } + return $this->requests[count($this->requests) - 1]; + } + + /** + * @return int + */ + public function getRequestCount(): int + { + return count($this->requests); + } + + /** + * Returns the number of remaining responses in the queue. + * + * @return int + */ + public function count(): int + { + return count($this->responses); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Client/RawClient.php b/seed/php-sdk/basic-auth-optional/src/Core/Client/RawClient.php new file mode 100644 index 000000000000..14716c7d678b --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Client/RawClient.php @@ -0,0 +1,310 @@ + $headers + */ + private array $headers; + + /** + * @var ?(callable(): array) $getAuthHeaders + */ + private $getAuthHeaders; + + /** + * @param ?array{ + * baseUrl?: string, + * client?: ClientInterface, + * maxRetries?: int, + * timeout?: float, + * headers?: array, + * getAuthHeaders?: callable(): array, + * } $options + */ + public function __construct( + public readonly ?array $options = null, + ) { + $this->client = HttpClientBuilder::build( + $this->options['client'] ?? null, + $this->options['maxRetries'] ?? 2, + ); + $this->requestFactory = HttpClientBuilder::requestFactory(); + $this->streamFactory = HttpClientBuilder::streamFactory(); + $this->headers = $this->options['headers'] ?? []; + $this->getAuthHeaders = $this->options['getAuthHeaders'] ?? null; + } + + /** + * @param BaseApiRequest $request + * @param ?array{ + * maxRetries?: int, + * timeout?: float, + * headers?: array, + * queryParameters?: array, + * bodyProperties?: array, + * } $options + * @return ResponseInterface + * @throws ClientExceptionInterface + */ + public function sendRequest( + BaseApiRequest $request, + ?array $options = null, + ): ResponseInterface { + $opts = $options ?? []; + $httpRequest = $this->buildRequest($request, $opts); + + $timeout = $opts['timeout'] ?? $this->options['timeout'] ?? null; + $maxRetries = $opts['maxRetries'] ?? null; + + return $this->client->send($httpRequest, $timeout, $maxRetries); + } + + /** + * @param BaseApiRequest $request + * @param array{ + * headers?: array, + * queryParameters?: array, + * bodyProperties?: array, + * } $options + * @return RequestInterface + */ + private function buildRequest( + BaseApiRequest $request, + array $options + ): RequestInterface { + $url = $this->buildUrl($request, $options); + $headers = $this->encodeHeaders($request, $options); + + $httpRequest = $this->requestFactory->createRequest( + $request->method->name, + $url, + ); + + // Encode body and, for multipart, capture the Content-Type with boundary. + if ($request instanceof MultipartApiRequest && $request->body !== null) { + $builder = new MultipartStreamBuilder($this->streamFactory); + $request->body->addToBuilder($builder); + $httpRequest = $httpRequest->withBody($builder->build()); + $headers['Content-Type'] = "multipart/form-data; boundary={$builder->getBoundary()}"; + } else { + $body = $this->encodeRequestBody($request, $options); + if ($body !== null) { + $httpRequest = $httpRequest->withBody($body); + } + } + + foreach ($headers as $name => $value) { + $httpRequest = $httpRequest->withHeader($name, $value); + } + + return $httpRequest; + } + + /** + * @param BaseApiRequest $request + * @param array{ + * headers?: array, + * } $options + * @return array + */ + private function encodeHeaders( + BaseApiRequest $request, + array $options, + ): array { + $authHeaders = $this->getAuthHeaders !== null ? ($this->getAuthHeaders)() : []; + return match (get_class($request)) { + JsonApiRequest::class => array_merge( + [ + "Content-Type" => "application/json", + "Accept" => "*/*", + ], + $this->headers, + $authHeaders, + $request->headers, + $options['headers'] ?? [], + ), + MultipartApiRequest::class => array_merge( + $this->headers, + $authHeaders, + $request->headers, + $options['headers'] ?? [], + ), + default => throw new InvalidArgumentException('Unsupported request type: ' . get_class($request)), + }; + } + + /** + * @param BaseApiRequest $request + * @param array{ + * bodyProperties?: array, + * } $options + * @return ?StreamInterface + */ + private function encodeRequestBody( + BaseApiRequest $request, + array $options, + ): ?StreamInterface { + if ($request instanceof JsonApiRequest) { + return $request->body === null ? null : $this->streamFactory->createStream( + JsonEncoder::encode( + $this->buildJsonBody( + $request->body, + $options, + ), + ) + ); + } + + if ($request instanceof MultipartApiRequest) { + return null; + } + + throw new InvalidArgumentException('Unsupported request type: ' . get_class($request)); + } + + /** + * @param mixed $body + * @param array{ + * bodyProperties?: array, + * } $options + * @return mixed + */ + private function buildJsonBody( + mixed $body, + array $options, + ): mixed { + $overrideProperties = $options['bodyProperties'] ?? []; + if (is_array($body) && (empty($body) || self::isSequential($body))) { + return array_merge($body, $overrideProperties); + } + + if ($body instanceof JsonSerializable) { + $result = $body->jsonSerialize(); + } else { + $result = $body; + } + if (is_array($result)) { + $result = array_merge($result, $overrideProperties); + if (empty($result)) { + // force to be serialized as {} instead of [] + return (object)($result); + } + } + + return $result; + } + + /** + * @param BaseApiRequest $request + * @param array{ + * queryParameters?: array, + * } $options + * @return string + */ + private function buildUrl( + BaseApiRequest $request, + array $options, + ): string { + $baseUrl = $request->baseUrl; + $trimmedBaseUrl = rtrim($baseUrl, '/'); + $trimmedBasePath = ltrim($request->path, '/'); + $url = "{$trimmedBaseUrl}/{$trimmedBasePath}"; + $query = array_merge( + $request->query, + $options['queryParameters'] ?? [], + ); + if (!empty($query)) { + $url .= '?' . $this->encodeQuery($query); + } + return $url; + } + + /** + * @param array $query + * @return string + */ + private function encodeQuery(array $query): string + { + $parts = []; + foreach ($query as $key => $value) { + if (is_array($value)) { + foreach ($value as $item) { + $parts[] = urlencode($key) . '=' . $this->encodeQueryValue($item); + } + } else { + $parts[] = urlencode($key) . '=' . $this->encodeQueryValue($value); + } + } + return implode('&', $parts); + } + + private function encodeQueryValue(mixed $value): string + { + if (is_string($value)) { + return urlencode($value); + } + if (is_bool($value)) { + return $value ? 'true' : 'false'; + } + if (is_scalar($value)) { + return urlencode((string)$value); + } + if (is_null($value)) { + return 'null'; + } + // Unreachable, but included for a best effort. + return urlencode(JsonEncoder::encode($value)); + } + + /** + * Check if an array is sequential, not associative. + * @param mixed[] $arr + * @return bool + */ + private static function isSequential(array $arr): bool + { + if (empty($arr)) { + return false; + } + $length = count($arr); + $keys = array_keys($arr); + for ($i = 0; $i < $length; $i++) { + if ($keys[$i] !== $i) { + return false; + } + } + return true; + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Client/RetryDecoratingClient.php b/seed/php-sdk/basic-auth-optional/src/Core/Client/RetryDecoratingClient.php new file mode 100644 index 000000000000..b16170cf2805 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Client/RetryDecoratingClient.php @@ -0,0 +1,241 @@ +client = $client; + $this->maxRetries = $maxRetries; + $this->baseDelay = $baseDelay; + $this->sleepFunction = $sleepFunction ?? 'usleep'; + } + + /** + * @param RequestInterface $request + * @return ResponseInterface + * @throws ClientExceptionInterface + */ + public function sendRequest(RequestInterface $request): ResponseInterface + { + return $this->send($request); + } + + /** + * Sends a request with optional per-request timeout and retry overrides. + * + * When a Guzzle or Symfony PSR-18 client is detected, the timeout is + * forwarded via the client's native API. For other PSR-18 clients the + * timeout value is silently ignored. + * + * @param RequestInterface $request + * @param ?float $timeout Timeout in seconds, or null to use the client default. + * @param ?int $maxRetries Maximum retry attempts, or null to use the client default. + * @return ResponseInterface + * @throws ClientExceptionInterface + */ + public function send( + RequestInterface $request, + ?float $timeout = null, + ?int $maxRetries = null, + ): ResponseInterface { + $maxRetries = $maxRetries ?? $this->maxRetries; + $retryAttempt = 0; + $lastResponse = null; + + while (true) { + try { + $lastResponse = $this->doSend($request, $timeout); + if (!$this->shouldRetry($retryAttempt, $maxRetries, $lastResponse)) { + return $lastResponse; + } + } catch (ClientExceptionInterface $e) { + if ($retryAttempt >= $maxRetries) { + throw $e; + } + } + + $retryAttempt++; + $delay = $this->getRetryDelay($retryAttempt, $lastResponse); + ($this->sleepFunction)($delay * 1000); // Convert milliseconds to microseconds + + // Rewind the request body so retries don't send an empty body. + $request->getBody()->rewind(); + } + } + + /** + * Dispatches the request to the underlying client, forwarding the timeout + * option to Guzzle or Symfony when available. + * + * @param RequestInterface $request + * @param ?float $timeout + * @return ResponseInterface + * @throws ClientExceptionInterface + */ + private function doSend(RequestInterface $request, ?float $timeout): ResponseInterface + { + static $warned = false; + + if ($timeout === null) { + return $this->client->sendRequest($request); + } + + if (class_exists('GuzzleHttp\ClientInterface') + && $this->client instanceof \GuzzleHttp\ClientInterface + ) { + return $this->client->send($request, ['timeout' => $timeout]); + } + if (class_exists('Symfony\Component\HttpClient\Psr18Client') + && $this->client instanceof \Symfony\Component\HttpClient\Psr18Client + ) { + /** @var ClientInterface $clientWithTimeout */ + $clientWithTimeout = $this->client->withOptions(['timeout' => $timeout]); + return $clientWithTimeout->sendRequest($request); + } + + if ($warned) { + return $this->client->sendRequest($request); + } + $warned = true; + trigger_error( + 'Timeout option is not supported for the current PSR-18 client (' + . get_class($this->client) + . '). Use Guzzle or Symfony HttpClient for timeout support.', + E_USER_WARNING, + ); + return $this->client->sendRequest($request); + } + + /** + * @param int $retryAttempt + * @param int $maxRetries + * @param ?ResponseInterface $response + * @return bool + */ + private function shouldRetry( + int $retryAttempt, + int $maxRetries, + ?ResponseInterface $response = null, + ): bool { + if ($retryAttempt >= $maxRetries) { + return false; + } + + if ($response !== null) { + return $response->getStatusCode() >= 500 || + in_array($response->getStatusCode(), self::RETRY_STATUS_CODES); + } + + return false; + } + + /** + * Calculate the retry delay based on response headers or exponential backoff. + * + * @param int $retryAttempt + * @param ?ResponseInterface $response + * @return int milliseconds + */ + private function getRetryDelay(int $retryAttempt, ?ResponseInterface $response): int + { + if ($response !== null) { + // Check Retry-After header + $retryAfter = $response->getHeaderLine('Retry-After'); + if ($retryAfter !== '') { + // Try parsing as integer (seconds) + if (is_numeric($retryAfter)) { + $retryAfterSeconds = (int)$retryAfter; + if ($retryAfterSeconds > 0) { + return min($retryAfterSeconds * 1000, self::MAX_RETRY_DELAY); + } + } + + // Try parsing as HTTP date + $retryAfterDate = strtotime($retryAfter); + if ($retryAfterDate !== false) { + $delay = ($retryAfterDate - time()) * 1000; + if ($delay > 0) { + return min(max($delay, 0), self::MAX_RETRY_DELAY); + } + } + } + + // Check X-RateLimit-Reset header + $rateLimitReset = $response->getHeaderLine('X-RateLimit-Reset'); + if ($rateLimitReset !== '' && is_numeric($rateLimitReset)) { + $resetTime = (int)$rateLimitReset; + $delay = ($resetTime * 1000) - (int)(microtime(true) * 1000); + if ($delay > 0) { + return $this->addPositiveJitter(min($delay, self::MAX_RETRY_DELAY)); + } + } + } + + // Fall back to exponential backoff with symmetric jitter + return $this->addSymmetricJitter( + min($this->exponentialDelay($retryAttempt), self::MAX_RETRY_DELAY) + ); + } + + /** + * Add positive jitter (0% to +20%) to the delay. + * + * @param int $delay + * @return int + */ + private function addPositiveJitter(int $delay): int + { + $jitterMultiplier = 1 + (mt_rand() / mt_getrandmax()) * self::JITTER_FACTOR; + return (int)($delay * $jitterMultiplier); + } + + /** + * Add symmetric jitter (-10% to +10%) to the delay. + * + * @param int $delay + * @return int + */ + private function addSymmetricJitter(int $delay): int + { + $jitterMultiplier = 1 + ((mt_rand() / mt_getrandmax()) - 0.5) * self::JITTER_FACTOR; + return (int)($delay * $jitterMultiplier); + } + + /** + * Default exponential backoff delay function. + * + * @return int milliseconds. + */ + private function exponentialDelay(int $retryAttempt): int + { + return 2 ** ($retryAttempt - 1) * $this->baseDelay; + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonApiRequest.php b/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonApiRequest.php new file mode 100644 index 000000000000..8fdf493606e6 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonApiRequest.php @@ -0,0 +1,28 @@ + $headers Additional headers for the request (optional) + * @param array $query Query parameters for the request (optional) + * @param mixed|null $body The JSON request body (optional) + */ + public function __construct( + string $baseUrl, + string $path, + HttpMethod $method, + array $headers = [], + array $query = [], + public readonly mixed $body = null + ) { + parent::__construct($baseUrl, $path, $method, $headers, $query); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonDecoder.php b/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonDecoder.php new file mode 100644 index 000000000000..2ddff0273482 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonDecoder.php @@ -0,0 +1,161 @@ + $type The type definition for deserialization. + * @return mixed[]|array The deserialized array. + * @throws JsonException If the decoded value is not an array. + */ + public static function decodeArray(string $json, array $type): array + { + $decoded = self::decode($json); + if (!is_array($decoded)) { + throw new JsonException("Unexpected non-array json value: " . $json); + } + return JsonDeserializer::deserializeArray($decoded, $type); + } + + /** + * Decodes a JSON string and deserializes it based on the provided union type definition. + * + * @param string $json The JSON string to decode. + * @param Union $union The union type definition for deserialization. + * @return mixed The deserialized value. + * @throws JsonException If the deserialization for all types in the union fails. + */ + public static function decodeUnion(string $json, Union $union): mixed + { + $decoded = self::decode($json); + return JsonDeserializer::deserializeUnion($decoded, $union); + } + /** + * Decodes a JSON string and returns a mixed. + * + * @param string $json The JSON string to decode. + * @return mixed The decoded mixed. + * @throws JsonException If the decoded value is not an mixed. + */ + public static function decodeMixed(string $json): mixed + { + return self::decode($json); + } + + /** + * Decodes a JSON string into a PHP value. + * + * @param string $json The JSON string to decode. + * @return mixed The decoded value. + * @throws JsonException If an error occurs during JSON decoding. + */ + public static function decode(string $json): mixed + { + return json_decode($json, associative: true, flags: JSON_THROW_ON_ERROR); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonDeserializer.php b/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonDeserializer.php new file mode 100644 index 000000000000..ca73bb2b970e --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonDeserializer.php @@ -0,0 +1,218 @@ + $data The array to be deserialized. + * @param array $type The type definition from the annotation. + * @return array The deserialized array. + * @throws JsonException If deserialization fails. + */ + public static function deserializeArray(array $data, array $type): array + { + return Utils::isMapType($type) + ? self::deserializeMap($data, $type) + : self::deserializeList($data, $type); + } + + /** + * Deserializes a value based on its type definition. + * + * @param mixed $data The data to deserialize. + * @param mixed $type The type definition. + * @return mixed The deserialized value. + * @throws JsonException If deserialization fails. + */ + private static function deserializeValue(mixed $data, mixed $type): mixed + { + if ($type instanceof Union) { + return self::deserializeUnion($data, $type); + } + + if (is_array($type)) { + return self::deserializeArray((array)$data, $type); + } + + if (gettype($type) !== "string") { + throw new JsonException("Unexpected non-string type."); + } + + return self::deserializeSingleValue($data, $type); + } + + /** + * Deserializes a value based on the possible types in a union type definition. + * + * @param mixed $data The data to deserialize. + * @param Union $type The union type definition. + * @return mixed The deserialized value. + * @throws JsonException If none of the union types can successfully deserialize the value. + */ + public static function deserializeUnion(mixed $data, Union $type): mixed + { + foreach ($type->types as $unionType) { + try { + return self::deserializeValue($data, $unionType); + } catch (\Throwable) { + // Catching Throwable instead of Exception to handle TypeError + // that occurs when assigning null to non-nullable typed properties + continue; + } + } + $readableType = Utils::getReadableType($data); + throw new JsonException( + "Cannot deserialize value of type $readableType with any of the union types: " . $type + ); + } + + /** + * Deserializes a single value based on its expected type. + * + * @param mixed $data The data to deserialize. + * @param string $type The expected type. + * @return mixed The deserialized value. + * @throws JsonException If deserialization fails. + */ + private static function deserializeSingleValue(mixed $data, string $type): mixed + { + if ($type === 'null' && $data === null) { + return null; + } + + if ($type === 'date' && is_string($data)) { + return self::deserializeDate($data); + } + + if ($type === 'datetime' && is_string($data)) { + return self::deserializeDateTime($data); + } + + if ($type === 'mixed') { + return $data; + } + + if (class_exists($type) && is_array($data)) { + /** @var array $data */ + return self::deserializeObject($data, $type); + } + + // Handle floats as a special case since gettype($data) returns "double" for float values in PHP, and because + // floats make come through from json_decoded as integers + if ($type === 'float' && (is_numeric($data))) { + return (float) $data; + } + + // Handle bools as a special case since gettype($data) returns "boolean" for bool values in PHP. + if ($type === 'bool' && is_bool($data)) { + return $data; + } + + if (gettype($data) === $type) { + return $data; + } + + throw new JsonException("Unable to deserialize value of type '" . gettype($data) . "' as '$type'."); + } + + /** + * Deserializes an array into an object of the given type. + * + * @param array $data The data to deserialize. + * @param string $type The class name of the object to deserialize into. + * + * @return object The deserialized object. + * + * @throws JsonException If the type does not implement JsonSerializableType. + */ + public static function deserializeObject(array $data, string $type): object + { + if (!is_subclass_of($type, JsonSerializableType::class)) { + throw new JsonException("$type is not a subclass of JsonSerializableType."); + } + return $type::jsonDeserialize($data); + } + + /** + * Deserializes a map (associative array) with defined key and value types. + * + * @param array $data The associative array to deserialize. + * @param array $type The type definition for the map. + * @return array The deserialized map. + * @throws JsonException If deserialization fails. + */ + private static function deserializeMap(array $data, array $type): array + { + $keyType = array_key_first($type); + if ($keyType === null) { + throw new JsonException("Unexpected no key in ArrayType."); + } + $keyType = (string) $keyType; + $valueType = $type[$keyType]; + /** @var array $result */ + $result = []; + + foreach ($data as $key => $item) { + $key = (string) Utils::castKey($key, $keyType); + $result[$key] = self::deserializeValue($item, $valueType); + } + + return $result; + } + + /** + * Deserializes a list (indexed array) with a defined value type. + * + * @param array $data The list to deserialize. + * @param array $type The type definition for the list. + * @return array The deserialized list. + * @throws JsonException If deserialization fails. + */ + private static function deserializeList(array $data, array $type): array + { + $valueType = $type[0]; + /** @var array */ + return array_map(fn ($item) => self::deserializeValue($item, $valueType), $data); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonEncoder.php b/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonEncoder.php new file mode 100644 index 000000000000..0dbf3fcc9948 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonEncoder.php @@ -0,0 +1,20 @@ + Extra properties from JSON that don't map to class properties */ + private array $__additionalProperties = []; + + /** @var array Properties that have been explicitly set via setter methods */ + private array $__explicitlySetProperties = []; + + /** + * Serializes the object to a JSON string. + * + * @return string JSON-encoded string representation of the object. + * @throws Exception If encoding fails. + */ + public function toJson(): string + { + $serializedObject = $this->jsonSerialize(); + $encoded = JsonEncoder::encode($serializedObject); + if (!$encoded) { + throw new Exception("Could not encode type"); + } + return $encoded; + } + + /** + * Serializes the object to an array. + * + * @return mixed[] Array representation of the object. + * @throws JsonException If serialization fails. + */ + public function jsonSerialize(): array + { + $result = []; + $reflectionClass = new \ReflectionClass($this); + foreach ($reflectionClass->getProperties() as $property) { + $jsonKey = self::getJsonKey($property); + if ($jsonKey === null) { + continue; + } + $value = $property->getValue($this); + + // Handle DateTime properties + $dateTypeAttr = $property->getAttributes(Date::class)[0] ?? null; + if ($dateTypeAttr && $value instanceof DateTime) { + $dateType = $dateTypeAttr->newInstance()->type; + $value = ($dateType === Date::TYPE_DATE) + ? JsonSerializer::serializeDate($value) + : JsonSerializer::serializeDateTime($value); + } + + // Handle Union annotations + $unionTypeAttr = $property->getAttributes(Union::class)[0] ?? null; + if ($unionTypeAttr) { + $unionType = $unionTypeAttr->newInstance(); + $value = JsonSerializer::serializeUnion($value, $unionType); + } + + // Handle arrays with type annotations + $arrayTypeAttr = $property->getAttributes(ArrayType::class)[0] ?? null; + if ($arrayTypeAttr && is_array($value)) { + $arrayType = $arrayTypeAttr->newInstance()->type; + $value = JsonSerializer::serializeArray($value, $arrayType); + } + + // Handle object + if (is_object($value)) { + $value = JsonSerializer::serializeObject($value); + } + + // Include the value if it's not null, OR if it was explicitly set (even to null) + if ($value !== null || array_key_exists($property->getName(), $this->__explicitlySetProperties)) { + $result[$jsonKey] = $value; + } + } + return $result; + } + + /** + * Deserializes a JSON string into an instance of the calling class. + * + * @param string $json JSON string to deserialize. + * @return static Deserialized object. + * @throws JsonException If decoding fails or the result is not an array. + * @throws Exception If deserialization fails. + */ + public static function fromJson(string $json): static + { + $decodedJson = JsonDecoder::decode($json); + if (!is_array($decodedJson)) { + throw new JsonException("Unexpected non-array decoded type: " . gettype($decodedJson)); + } + /** @var array $decodedJson */ + return self::jsonDeserialize($decodedJson); + } + + /** + * Deserializes an array into an instance of the calling class. + * + * @param array $data Array data to deserialize. + * @return static Deserialized object. + * @throws JsonException If deserialization fails. + */ + public static function jsonDeserialize(array $data): static + { + $reflectionClass = new \ReflectionClass(static::class); + $constructor = $reflectionClass->getConstructor(); + if ($constructor === null) { + throw new JsonException("No constructor found."); + } + + $args = []; + $properties = []; + $additionalProperties = []; + foreach ($reflectionClass->getProperties() as $property) { + $jsonKey = self::getJsonKey($property) ?? $property->getName(); + $properties[$jsonKey] = $property; + } + + foreach ($data as $jsonKey => $value) { + if (!isset($properties[$jsonKey])) { + // This JSON key doesn't map to any class property - add it to additionalProperties + $additionalProperties[$jsonKey] = $value; + continue; + } + + $property = $properties[$jsonKey]; + + // Handle Date annotation + $dateTypeAttr = $property->getAttributes(Date::class)[0] ?? null; + if ($dateTypeAttr) { + $dateType = $dateTypeAttr->newInstance()->type; + if (!is_string($value)) { + throw new JsonException("Unexpected non-string type for date."); + } + $value = ($dateType === Date::TYPE_DATE) + ? JsonDeserializer::deserializeDate($value) + : JsonDeserializer::deserializeDateTime($value); + } + + // Handle Array annotation + $arrayTypeAttr = $property->getAttributes(ArrayType::class)[0] ?? null; + if (is_array($value) && $arrayTypeAttr) { + $arrayType = $arrayTypeAttr->newInstance()->type; + $value = JsonDeserializer::deserializeArray($value, $arrayType); + } + + // Handle Union annotations + $unionTypeAttr = $property->getAttributes(Union::class)[0] ?? null; + if ($unionTypeAttr) { + $unionType = $unionTypeAttr->newInstance(); + $value = JsonDeserializer::deserializeUnion($value, $unionType); + } + + // Handle object + $type = $property->getType(); + if (is_array($value) && $type instanceof ReflectionNamedType && !$type->isBuiltin()) { + /** @var array $arrayValue */ + $arrayValue = $value; + $value = JsonDeserializer::deserializeObject($arrayValue, $type->getName()); + } + + $args[$property->getName()] = $value; + } + + // Fill in any missing properties with defaults + foreach ($properties as $property) { + if (!isset($args[$property->getName()])) { + $args[$property->getName()] = $property->hasDefaultValue() ? $property->getDefaultValue() : null; + } + } + + // @phpstan-ignore-next-line + $result = new static($args); + $result->__additionalProperties = $additionalProperties; + return $result; + } + + /** + * Get properties from JSON that weren't mapped to class fields + * @return array + */ + public function getAdditionalProperties(): array + { + return $this->__additionalProperties; + } + + /** + * Mark a property as explicitly set. + * This ensures the property will be included in JSON serialization even if null. + * + * @param string $propertyName The name of the property to mark as explicitly set. + */ + protected function _setField(string $propertyName): void + { + $this->__explicitlySetProperties[$propertyName] = true; + } + + /** + * Retrieves the JSON key associated with a property. + * + * @param ReflectionProperty $property The reflection property. + * @return ?string The JSON key, or null if not available. + */ + private static function getJsonKey(ReflectionProperty $property): ?string + { + $jsonPropertyAttr = $property->getAttributes(JsonProperty::class)[0] ?? null; + return $jsonPropertyAttr?->newInstance()?->name; + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonSerializer.php b/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonSerializer.php new file mode 100644 index 000000000000..216de5aa4554 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Json/JsonSerializer.php @@ -0,0 +1,205 @@ +format(Constant::DateFormat); + } + + /** + * Serializes a DateTime object into a string using the date-time format. + * Normalizes UTC times to use 'Z' suffix instead of '+00:00'. + * + * @param DateTime $date The DateTime object to serialize. + * @return string The serialized date-time string. + */ + public static function serializeDateTime(DateTime $date): string + { + $formatted = $date->format(Constant::DateTimeFormat); + if (str_ends_with($formatted, '+00:00')) { + return substr($formatted, 0, -6) . 'Z'; + } + return $formatted; + } + + /** + * Serializes an array based on type annotations (either a list or map). + * + * @param array $data The array to be serialized. + * @param array $type The type definition from the annotation. + * @return array The serialized array. + * @throws JsonException If serialization fails. + */ + public static function serializeArray(array $data, array $type): array + { + return Utils::isMapType($type) + ? self::serializeMap($data, $type) + : self::serializeList($data, $type); + } + + /** + * Serializes a value based on its type definition. + * + * @param mixed $data The value to serialize. + * @param mixed $type The type definition. + * @return mixed The serialized value. + * @throws JsonException If serialization fails. + */ + private static function serializeValue(mixed $data, mixed $type): mixed + { + if ($type instanceof Union) { + return self::serializeUnion($data, $type); + } + + if (is_array($type)) { + return self::serializeArray((array)$data, $type); + } + + if (gettype($type) !== "string") { + throw new JsonException("Unexpected non-string type."); + } + + return self::serializeSingleValue($data, $type); + } + + /** + * Serializes a value for a union type definition. + * + * @param mixed $data The value to serialize. + * @param Union $unionType The union type definition. + * @return mixed The serialized value. + * @throws JsonException If serialization fails for all union types. + */ + public static function serializeUnion(mixed $data, Union $unionType): mixed + { + foreach ($unionType->types as $type) { + try { + return self::serializeValue($data, $type); + } catch (Exception) { + // Try the next type in the union + continue; + } + } + $readableType = Utils::getReadableType($data); + throw new JsonException( + "Cannot serialize value of type $readableType with any of the union types: " . $unionType + ); + } + + /** + * Serializes a single value based on its type. + * + * @param mixed $data The value to serialize. + * @param string $type The expected type. + * @return mixed The serialized value. + * @throws JsonException If serialization fails. + */ + private static function serializeSingleValue(mixed $data, string $type): mixed + { + if ($type === 'null' && $data === null) { + return null; + } + + if (($type === 'date' || $type === 'datetime') && $data instanceof DateTime) { + return $type === 'date' ? self::serializeDate($data) : self::serializeDateTime($data); + } + + if ($type === 'mixed') { + return $data; + } + + if (class_exists($type) && $data instanceof $type) { + return self::serializeObject($data); + } + + // Handle floats as a special case since gettype($data) returns "double" for float values in PHP. + if ($type === 'float' && is_float($data)) { + return $data; + } + + // Handle bools as a special case since gettype($data) returns "boolean" for bool values in PHP. + if ($type === 'bool' && is_bool($data)) { + return $data; + } + + if (gettype($data) === $type) { + return $data; + } + + throw new JsonException("Unable to serialize value of type '" . gettype($data) . "' as '$type'."); + } + + /** + * Serializes an object to a JSON-serializable format. + * + * @param object $data The object to serialize. + * @return mixed The serialized data. + * @throws JsonException If the object does not implement JsonSerializable. + */ + public static function serializeObject(object $data): mixed + { + if (!is_subclass_of($data, JsonSerializable::class)) { + $type = get_class($data); + throw new JsonException("Class $type must implement JsonSerializable."); + } + return $data->jsonSerialize(); + } + + /** + * Serializes a map (associative array) with defined key and value types. + * + * @param array $data The associative array to serialize. + * @param array $type The type definition for the map. + * @return array The serialized map. + * @throws JsonException If serialization fails. + */ + private static function serializeMap(array $data, array $type): array + { + $keyType = array_key_first($type); + if ($keyType === null) { + throw new JsonException("Unexpected no key in ArrayType."); + } + $keyType = (string) $keyType; + $valueType = $type[$keyType]; + /** @var array $result */ + $result = []; + + foreach ($data as $key => $item) { + $key = (string) Utils::castKey($key, $keyType); + $result[$key] = self::serializeValue($item, $valueType); + } + + return $result; + } + + /** + * Serializes a list (indexed array) where only the value type is defined. + * + * @param array $data The list to serialize. + * @param array $type The type definition for the list. + * @return array The serialized list. + * @throws JsonException If serialization fails. + */ + private static function serializeList(array $data, array $type): array + { + $valueType = $type[0]; + /** @var array */ + return array_map(fn ($item) => self::serializeValue($item, $valueType), $data); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Json/Utils.php b/seed/php-sdk/basic-auth-optional/src/Core/Json/Utils.php new file mode 100644 index 000000000000..4099b8253005 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Json/Utils.php @@ -0,0 +1,62 @@ + $type The type definition from the annotation. + * @return bool True if the type is a map, false if it's a list. + */ + public static function isMapType(array $type): bool + { + return count($type) === 1 && !array_is_list($type); + } + + /** + * Casts the key to the appropriate type based on the key type. + * + * @param mixed $key The key to be cast. + * @param string $keyType The type to cast the key to ('string', 'integer', 'float'). + * @return int|string The casted key. + * @throws JsonException + */ + public static function castKey(mixed $key, string $keyType): int|string + { + if (!is_scalar($key)) { + throw new JsonException("Key must be a scalar type."); + } + return match ($keyType) { + 'integer' => (int)$key, + // PHP arrays don't support float keys; truncate to int + 'float' => (int)$key, + 'string' => (string)$key, + default => is_int($key) ? $key : (string)$key, + }; + } + + /** + * Returns a human-readable representation of the input's type. + * + * @param mixed $input The input value to determine the type of. + * @return string A readable description of the input type. + */ + public static function getReadableType(mixed $input): string + { + if (is_object($input)) { + return get_class($input); + } elseif (is_array($input)) { + return 'array(' . count($input) . ' items)'; + } elseif (is_null($input)) { + return 'null'; + } else { + return gettype($input); + } + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartApiRequest.php b/seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartApiRequest.php new file mode 100644 index 000000000000..7760366456c8 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartApiRequest.php @@ -0,0 +1,28 @@ + $headers Additional headers for the request (optional) + * @param array $query Query parameters for the request (optional) + * @param ?MultipartFormData $body The multipart form data for the request (optional) + */ + public function __construct( + string $baseUrl, + string $path, + HttpMethod $method, + array $headers = [], + array $query = [], + public readonly ?MultipartFormData $body = null + ) { + parent::__construct($baseUrl, $path, $method, $headers, $query); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartFormData.php b/seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartFormData.php new file mode 100644 index 000000000000..911a28b6ad64 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartFormData.php @@ -0,0 +1,58 @@ + + */ + private array $parts = []; + + /** + * Adds a new part to the multipart form data. + * + * @param string $name + * @param string|int|bool|float|StreamInterface $value + * @param ?string $contentType + */ + public function add( + string $name, + string|int|bool|float|StreamInterface $value, + ?string $contentType = null, + ): void { + $headers = $contentType !== null ? ['Content-Type' => $contentType] : null; + $this->addPart( + new MultipartFormDataPart( + name: $name, + value: $value, + headers: $headers, + ) + ); + } + + /** + * Adds a new part to the multipart form data. + * + * @param MultipartFormDataPart $part + */ + public function addPart(MultipartFormDataPart $part): void + { + $this->parts[] = $part; + } + + /** + * Adds all parts to a MultipartStreamBuilder. + * + * @param MultipartStreamBuilder $builder + */ + public function addToBuilder(MultipartStreamBuilder $builder): void + { + foreach ($this->parts as $part) { + $part->addToBuilder($builder); + } + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartFormDataPart.php b/seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartFormDataPart.php new file mode 100644 index 000000000000..4db35e58ae37 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Multipart/MultipartFormDataPart.php @@ -0,0 +1,62 @@ + + */ + private ?array $headers; + + /** + * @param string $name + * @param string|bool|float|int|StreamInterface $value + * @param ?string $filename + * @param ?array $headers + */ + public function __construct( + string $name, + string|bool|float|int|StreamInterface $value, + ?string $filename = null, + ?array $headers = null + ) { + $this->name = $name; + $this->contents = $value instanceof StreamInterface ? $value : (string)$value; + $this->filename = $filename; + $this->headers = $headers; + } + + /** + * Adds this part to a MultipartStreamBuilder. + * + * @param MultipartStreamBuilder $builder + */ + public function addToBuilder(MultipartStreamBuilder $builder): void + { + $options = array_filter([ + 'filename' => $this->filename, + 'headers' => $this->headers, + ], fn ($value) => $value !== null); + + $builder->addResource($this->name, $this->contents, $options); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Types/ArrayType.php b/seed/php-sdk/basic-auth-optional/src/Core/Types/ArrayType.php new file mode 100644 index 000000000000..a26d29008ec3 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Types/ArrayType.php @@ -0,0 +1,16 @@ + 'valueType'] for maps, or ['valueType'] for lists + */ + public function __construct(public array $type) + { + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Core/Types/Constant.php b/seed/php-sdk/basic-auth-optional/src/Core/Types/Constant.php new file mode 100644 index 000000000000..5ac4518cc6d6 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Core/Types/Constant.php @@ -0,0 +1,12 @@ +> The types allowed for this property, which can be strings, arrays, or nested Union types. + */ + public array $types; + + /** + * Constructor for the Union attribute. + * + * @param string|Union|array ...$types The list of types that the property can accept. + * This can include primitive types (e.g., 'string', 'int'), arrays, or other Union instances. + * + * Example: + * ```php + * #[Union('string', 'null', 'date', new Union('boolean', 'int'))] + * ``` + */ + public function __construct(string|Union|array ...$types) + { + $this->types = $types; + } + + /** + * Converts the Union type to a string representation. + * + * @return string A string representation of the union types. + */ + public function __toString(): string + { + return implode(' | ', array_map(function ($type) { + if (is_string($type)) { + return $type; + } elseif ($type instanceof Union) { + return (string) $type; // Recursively handle nested unions + } elseif (is_array($type)) { + return 'array'; // Handle arrays + } + }, $this->types)); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Errors/Types/UnauthorizedRequestErrorBody.php b/seed/php-sdk/basic-auth-optional/src/Errors/Types/UnauthorizedRequestErrorBody.php new file mode 100644 index 000000000000..131d5f01b080 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Errors/Types/UnauthorizedRequestErrorBody.php @@ -0,0 +1,34 @@ +message = $values['message']; + } + + /** + * @return string + */ + public function __toString(): string + { + return $this->toJson(); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Exceptions/SeedApiException.php b/seed/php-sdk/basic-auth-optional/src/Exceptions/SeedApiException.php new file mode 100644 index 000000000000..6d0bba7c39b3 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Exceptions/SeedApiException.php @@ -0,0 +1,53 @@ +body = $body; + parent::__construct($message, $statusCode, $previous); + } + + /** + * Returns the body of the response that triggered the exception. + * + * @return mixed + */ + public function getBody(): mixed + { + return $this->body; + } + + /** + * @return string + */ + public function __toString(): string + { + if (empty($this->body)) { + return $this->message . '; Status Code: ' . $this->getCode() . "\n"; + } + return $this->message . '; Status Code: ' . $this->getCode() . '; Body: ' . print_r($this->body, true) . "\n"; + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Exceptions/SeedException.php b/seed/php-sdk/basic-auth-optional/src/Exceptions/SeedException.php new file mode 100644 index 000000000000..457035276737 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Exceptions/SeedException.php @@ -0,0 +1,12 @@ +, + * } $options @phpstan-ignore-next-line Property is used in endpoint methods via HttpEndpointGenerator + */ + private array $options; + + /** + * @var RawClient $client + */ + private RawClient $client; + + /** + * @param string $username The username to use for authentication. + * @param string $password The username to use for authentication. + * @param ?array{ + * baseUrl?: string, + * client?: ClientInterface, + * maxRetries?: int, + * timeout?: float, + * headers?: array, + * } $options + */ + public function __construct( + string $username, + string $password, + ?array $options = null, + ) { + $defaultHeaders = [ + 'X-Fern-Language' => 'PHP', + 'X-Fern-SDK-Name' => 'Seed', + 'X-Fern-SDK-Version' => '0.0.1', + 'User-Agent' => 'seed/seed/0.0.1', + ]; + $defaultHeaders['Authorization'] = "Basic " . base64_encode($username . ":" . $password); + + $this->options = $options ?? []; + + $this->options['headers'] = array_merge( + $defaultHeaders, + $this->options['headers'] ?? [], + ); + + $this->client = new RawClient( + options: $this->options, + ); + + $this->basicAuth = new BasicAuthClient($this->client, $this->options); + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/Utils/File.php b/seed/php-sdk/basic-auth-optional/src/Utils/File.php new file mode 100644 index 000000000000..ee2af27b8909 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/Utils/File.php @@ -0,0 +1,129 @@ +filename = $filename; + $this->contentType = $contentType; + $this->stream = $stream; + } + + /** + * Creates a File instance from a filepath. + * + * @param string $filepath + * @param ?string $filename + * @param ?string $contentType + * @return File + * @throws Exception + */ + public static function createFromFilepath( + string $filepath, + ?string $filename = null, + ?string $contentType = null, + ): File { + $resource = @fopen($filepath, 'r'); + if (!$resource) { + throw new Exception("Unable to open file $filepath"); + } + $stream = Psr17FactoryDiscovery::findStreamFactory()->createStreamFromResource($resource); + if (!$stream->isReadable()) { + throw new Exception("File $filepath is not readable"); + } + return new self( + stream: $stream, + filename: $filename ?? basename($filepath), + contentType: $contentType, + ); + } + + /** + * Creates a File instance from a string. + * + * @param string $content + * @param ?string $filename + * @param ?string $contentType + * @return File + */ + public static function createFromString( + string $content, + ?string $filename, + ?string $contentType = null, + ): File { + return new self( + stream: Psr17FactoryDiscovery::findStreamFactory()->createStream($content), + filename: $filename, + contentType: $contentType, + ); + } + + /** + * Maps this File into a multipart form data part. + * + * @param string $name The name of the multipart form data part. + * @param ?string $contentType Overrides the Content-Type associated with the file, if any. + * @return MultipartFormDataPart + */ + public function toMultipartFormDataPart(string $name, ?string $contentType = null): MultipartFormDataPart + { + $contentType ??= $this->contentType; + $headers = $contentType !== null + ? ['Content-Type' => $contentType] + : null; + + return new MultipartFormDataPart( + name: $name, + value: $this->stream, + filename: $this->filename, + headers: $headers, + ); + } + + /** + * Closes the file stream. + */ + public function close(): void + { + $this->stream->close(); + } + + /** + * Destructor to ensure stream is closed. + */ + public function __destruct() + { + try { + $this->close(); + } catch (\Throwable) { + // Swallow errors during garbage collection to avoid fatal errors. + } + } +} diff --git a/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example0/snippet.php b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example0/snippet.php new file mode 100644 index 000000000000..f17c29ac0c45 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example0/snippet.php @@ -0,0 +1,14 @@ +', + password: '', + options: [ + 'baseUrl' => 'https://api.fern.com', + ], +); +$client->basicAuth->getWithBasicAuth(); diff --git a/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example1/snippet.php b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example1/snippet.php new file mode 100644 index 000000000000..f17c29ac0c45 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example1/snippet.php @@ -0,0 +1,14 @@ +', + password: '', + options: [ + 'baseUrl' => 'https://api.fern.com', + ], +); +$client->basicAuth->getWithBasicAuth(); diff --git a/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example2/snippet.php b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example2/snippet.php new file mode 100644 index 000000000000..f17c29ac0c45 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example2/snippet.php @@ -0,0 +1,14 @@ +', + password: '', + options: [ + 'baseUrl' => 'https://api.fern.com', + ], +); +$client->basicAuth->getWithBasicAuth(); diff --git a/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example3/snippet.php b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example3/snippet.php new file mode 100644 index 000000000000..379913d20cec --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example3/snippet.php @@ -0,0 +1,18 @@ +', + password: '', + options: [ + 'baseUrl' => 'https://api.fern.com', + ], +); +$client->basicAuth->postWithBasicAuth( + [ + 'key' => "value", + ], +); diff --git a/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example4/snippet.php b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example4/snippet.php new file mode 100644 index 000000000000..379913d20cec --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example4/snippet.php @@ -0,0 +1,18 @@ +', + password: '', + options: [ + 'baseUrl' => 'https://api.fern.com', + ], +); +$client->basicAuth->postWithBasicAuth( + [ + 'key' => "value", + ], +); diff --git a/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example5/snippet.php b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example5/snippet.php new file mode 100644 index 000000000000..379913d20cec --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example5/snippet.php @@ -0,0 +1,18 @@ +', + password: '', + options: [ + 'baseUrl' => 'https://api.fern.com', + ], +); +$client->basicAuth->postWithBasicAuth( + [ + 'key' => "value", + ], +); diff --git a/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example6/snippet.php b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example6/snippet.php new file mode 100644 index 000000000000..379913d20cec --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/src/dynamic-snippets/example6/snippet.php @@ -0,0 +1,18 @@ +', + password: '', + options: [ + 'baseUrl' => 'https://api.fern.com', + ], +); +$client->basicAuth->postWithBasicAuth( + [ + 'key' => "value", + ], +); diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Client/RawClientTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Client/RawClientTest.php new file mode 100644 index 000000000000..df36dc918894 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Client/RawClientTest.php @@ -0,0 +1,1074 @@ +name = $values['name']; + } + + /** + * @return string + */ + public function getName(): ?string + { + return $this->name; + } +} + +class RawClientTest extends TestCase +{ + private string $baseUrl = 'https://api.example.com'; + private MockHttpClient $mockClient; + private RawClient $rawClient; + + protected function setUp(): void + { + $this->mockClient = new MockHttpClient(); + $this->rawClient = new RawClient(['client' => $this->mockClient, 'maxRetries' => 0]); + } + + /** + * @throws ClientExceptionInterface + */ + public function testHeaders(): void + { + $this->mockClient->append(self::createResponse(200)); + + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::GET, + ['X-Custom-Header' => 'TestValue'] + ); + + $this->rawClient->sendRequest($request); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + $this->assertEquals('application/json', $lastRequest->getHeaderLine('Content-Type')); + $this->assertEquals('TestValue', $lastRequest->getHeaderLine('X-Custom-Header')); + } + + /** + * @throws ClientExceptionInterface + */ + public function testQueryParameters(): void + { + $this->mockClient->append(self::createResponse(200)); + + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::GET, + [], + ['param1' => 'value1', 'param2' => ['a', 'b'], 'param3' => 'true'] + ); + + $this->rawClient->sendRequest($request); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + $this->assertEquals( + 'https://api.example.com/test?param1=value1¶m2=a¶m2=b¶m3=true', + (string)$lastRequest->getUri() + ); + } + + /** + * @throws ClientExceptionInterface + */ + public function testJsonBody(): void + { + $this->mockClient->append(self::createResponse(200)); + + $body = ['key' => 'value']; + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::POST, + [], + [], + $body + ); + + $this->rawClient->sendRequest($request); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + $this->assertEquals('application/json', $lastRequest->getHeaderLine('Content-Type')); + $this->assertEquals(JsonEncoder::encode($body), (string)$lastRequest->getBody()); + } + + public function testAdditionalHeaders(): void + { + $this->mockClient->append(self::createResponse(200)); + + $body = new JsonRequest([ + 'name' => 'john.doe' + ]); + $headers = [ + 'X-API-Version' => '1.0.0', + ]; + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::POST, + $headers, + [], + $body + ); + + $this->rawClient->sendRequest( + $request, + options: [ + 'headers' => [ + 'X-Tenancy' => 'test' + ] + ] + ); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + $this->assertEquals('application/json', $lastRequest->getHeaderLine('Content-Type')); + $this->assertEquals('1.0.0', $lastRequest->getHeaderLine('X-API-Version')); + $this->assertEquals('test', $lastRequest->getHeaderLine('X-Tenancy')); + } + + public function testOverrideAdditionalHeaders(): void + { + $this->mockClient->append(self::createResponse(200)); + + $body = new JsonRequest([ + 'name' => 'john.doe' + ]); + $headers = [ + 'X-API-Version' => '1.0.0', + ]; + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::POST, + $headers, + [], + $body + ); + + $this->rawClient->sendRequest( + $request, + options: [ + 'headers' => [ + 'X-API-Version' => '2.0.0' + ] + ] + ); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + $this->assertEquals('application/json', $lastRequest->getHeaderLine('Content-Type')); + $this->assertEquals('2.0.0', $lastRequest->getHeaderLine('X-API-Version')); + } + + public function testAdditionalBodyProperties(): void + { + $this->mockClient->append(self::createResponse(200)); + + $body = new JsonRequest([ + 'name' => 'john.doe' + ]); + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::POST, + [], + [], + $body + ); + + $this->rawClient->sendRequest( + $request, + options: [ + 'bodyProperties' => [ + 'age' => 42 + ] + ] + ); + + $expectedJson = [ + 'name' => 'john.doe', + 'age' => 42 + ]; + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + $this->assertEquals('application/json', $lastRequest->getHeaderLine('Content-Type')); + $this->assertEquals(JsonEncoder::encode($expectedJson), (string)$lastRequest->getBody()); + } + + public function testOverrideAdditionalBodyProperties(): void + { + $this->mockClient->append(self::createResponse(200)); + + $body = [ + 'name' => 'john.doe' + ]; + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::POST, + [], + [], + $body + ); + + $this->rawClient->sendRequest( + $request, + options: [ + 'bodyProperties' => [ + 'name' => 'jane.doe' + ] + ] + ); + + $expectedJson = [ + 'name' => 'jane.doe', + ]; + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + $this->assertEquals('application/json', $lastRequest->getHeaderLine('Content-Type')); + $this->assertEquals(JsonEncoder::encode($expectedJson), (string)$lastRequest->getBody()); + } + + public function testAdditionalQueryParameters(): void + { + $this->mockClient->append(self::createResponse(200)); + + $query = ['key' => 'value']; + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::POST, + [], + $query, + [] + ); + + $this->rawClient->sendRequest( + $request, + options: [ + 'queryParameters' => [ + 'extra' => 42 + ] + ] + ); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + $this->assertEquals('application/json', $lastRequest->getHeaderLine('Content-Type')); + $this->assertEquals('key=value&extra=42', $lastRequest->getUri()->getQuery()); + } + + public function testOverrideQueryParameters(): void + { + $this->mockClient->append(self::createResponse(200)); + + $query = ['key' => 'invalid']; + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::POST, + [], + $query, + [] + ); + + $this->rawClient->sendRequest( + $request, + options: [ + 'queryParameters' => [ + 'key' => 'value' + ] + ] + ); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + $this->assertEquals('application/json', $lastRequest->getHeaderLine('Content-Type')); + $this->assertEquals('key=value', $lastRequest->getUri()->getQuery()); + } + + public function testDefaultRetries(): void + { + $this->mockClient->append(self::createResponse(500)); + + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::GET + ); + + $response = $this->rawClient->sendRequest($request); + $this->assertEquals(500, $response->getStatusCode()); + $this->assertEquals(0, $this->mockClient->count()); + } + + /** + * @throws ClientExceptionInterface + */ + public function testExplicitRetriesSuccess(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append(self::createResponse(500), self::createResponse(500), self::createResponse(200)); + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: 2, + sleepFunction: function (int $_microseconds): void { + }, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $response = $retryClient->sendRequest($request); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals(0, $mockClient->count()); + } + + public function testExplicitRetriesFailure(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append(self::createResponse(500), self::createResponse(500), self::createResponse(500)); + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: 2, + sleepFunction: function (int $_microseconds): void { + }, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $response = $retryClient->sendRequest($request); + + $this->assertEquals(500, $response->getStatusCode()); + $this->assertEquals(0, $mockClient->count()); + } + + /** + * @throws ClientExceptionInterface + */ + public function testShouldRetryOnStatusCodes(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append( + self::createResponse(408), + self::createResponse(429), + self::createResponse(500), + self::createResponse(501), + self::createResponse(502), + self::createResponse(503), + self::createResponse(504), + self::createResponse(505), + self::createResponse(599), + self::createResponse(200), + ); + $countOfErrorRequests = $mockClient->count() - 1; + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: $countOfErrorRequests, + sleepFunction: function (int $_microseconds): void { + }, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $response = $retryClient->sendRequest($request); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals(0, $mockClient->count()); + } + + public function testShouldFailOn400Response(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append(self::createResponse(400), self::createResponse(200)); + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: 2, + sleepFunction: function (int $_microseconds): void { + }, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $response = $retryClient->sendRequest($request); + + $this->assertEquals(400, $response->getStatusCode()); + $this->assertEquals(1, $mockClient->count()); + } + + public function testRetryAfterSecondsHeaderControlsDelay(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append( + self::createResponse(503, ['Retry-After' => '10']), + self::createResponse(200), + ); + + $capturedDelays = []; + $sleepFunction = function (int $microseconds) use (&$capturedDelays): void { + $capturedDelays[] = (int) ($microseconds / 1000); // Convert microseconds to milliseconds + }; + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: 2, + baseDelay: 1000, + sleepFunction: $sleepFunction, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $retryClient->sendRequest($request); + + $this->assertCount(1, $capturedDelays); + $this->assertGreaterThanOrEqual(10000, $capturedDelays[0]); + $this->assertLessThanOrEqual(12000, $capturedDelays[0]); + } + + public function testRetryAfterHttpDateHeaderIsHandled(): void + { + $retryAfterDate = gmdate('D, d M Y H:i:s \G\M\T', time() + 5); + + $mockClient = new MockHttpClient(); + $mockClient->append( + self::createResponse(503, ['Retry-After' => $retryAfterDate]), + self::createResponse(200), + ); + + $capturedDelays = []; + $sleepFunction = function (int $microseconds) use (&$capturedDelays): void { + $capturedDelays[] = (int) ($microseconds / 1000); + }; + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: 2, + baseDelay: 1000, + sleepFunction: $sleepFunction, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $retryClient->sendRequest($request); + + $this->assertCount(1, $capturedDelays); + $this->assertGreaterThan(0, $capturedDelays[0]); + $this->assertLessThanOrEqual(60000, $capturedDelays[0]); + } + + public function testRateLimitResetHeaderControlsDelay(): void + { + $resetTime = (int) floor(microtime(true)) + 5; + + $mockClient = new MockHttpClient(); + $mockClient->append( + self::createResponse(429, ['X-RateLimit-Reset' => (string) $resetTime]), + self::createResponse(200), + ); + + $capturedDelays = []; + $sleepFunction = function (int $microseconds) use (&$capturedDelays): void { + $capturedDelays[] = (int) ($microseconds / 1000); + }; + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: 2, + baseDelay: 1000, + sleepFunction: $sleepFunction, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $retryClient->sendRequest($request); + + $this->assertCount(1, $capturedDelays); + $this->assertGreaterThan(0, $capturedDelays[0]); + $this->assertLessThanOrEqual(60000, $capturedDelays[0]); + } + + public function testRateLimitResetHeaderRespectsMaxDelayAndPositiveJitter(): void + { + $resetTime = (int) floor(microtime(true)) + 1000; + + $mockClient = new MockHttpClient(); + $mockClient->append( + self::createResponse(429, ['X-RateLimit-Reset' => (string) $resetTime]), + self::createResponse(200), + ); + + $capturedDelays = []; + $sleepFunction = function (int $microseconds) use (&$capturedDelays): void { + $capturedDelays[] = (int) ($microseconds / 1000); + }; + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: 1, + baseDelay: 1000, + sleepFunction: $sleepFunction, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $retryClient->sendRequest($request); + + $this->assertCount(1, $capturedDelays); + $this->assertGreaterThanOrEqual(60000, $capturedDelays[0]); + $this->assertLessThanOrEqual(72000, $capturedDelays[0]); + } + + public function testExponentialBackoffWithSymmetricJitterWhenNoHeaders(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append( + self::createResponse(503), + self::createResponse(200), + ); + + $capturedDelays = []; + $sleepFunction = function (int $microseconds) use (&$capturedDelays): void { + $capturedDelays[] = (int) ($microseconds / 1000); + }; + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: 1, + baseDelay: 1000, + sleepFunction: $sleepFunction, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $retryClient->sendRequest($request); + + $this->assertCount(1, $capturedDelays); + $this->assertGreaterThanOrEqual(900, $capturedDelays[0]); + $this->assertLessThanOrEqual(1100, $capturedDelays[0]); + } + + public function testRetryAfterHeaderTakesPrecedenceOverRateLimitReset(): void + { + $resetTime = (int) floor(microtime(true)) + 30; + + $mockClient = new MockHttpClient(); + $mockClient->append( + self::createResponse(503, [ + 'Retry-After' => '5', + 'X-RateLimit-Reset' => (string) $resetTime, + ]), + self::createResponse(200), + ); + + $capturedDelays = []; + $sleepFunction = function (int $microseconds) use (&$capturedDelays): void { + $capturedDelays[] = (int) ($microseconds / 1000); + }; + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: 2, + baseDelay: 1000, + sleepFunction: $sleepFunction, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $retryClient->sendRequest($request); + + $this->assertCount(1, $capturedDelays); + $this->assertGreaterThanOrEqual(5000, $capturedDelays[0]); + $this->assertLessThanOrEqual(6000, $capturedDelays[0]); + } + + public function testMaxDelayCapIsApplied(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append( + self::createResponse(503, ['Retry-After' => '120']), + self::createResponse(200), + ); + + $capturedDelays = []; + $sleepFunction = function (int $microseconds) use (&$capturedDelays): void { + $capturedDelays[] = (int) ($microseconds / 1000); + }; + + $retryClient = new RetryDecoratingClient( + $mockClient, + maxRetries: 2, + baseDelay: 1000, + sleepFunction: $sleepFunction, + ); + + $requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory(); + $request = $requestFactory->createRequest('GET', $this->baseUrl . '/test'); + + $retryClient->sendRequest($request); + + $this->assertCount(1, $capturedDelays); + $this->assertGreaterThanOrEqual(60000, $capturedDelays[0]); + $this->assertLessThanOrEqual(72000, $capturedDelays[0]); + } + + public function testMultipartContentTypeIncludesBoundary(): void + { + $this->mockClient->append(self::createResponse(200)); + + $formData = new MultipartFormData(); + $formData->add('field', 'value'); + + $request = new MultipartApiRequest( + $this->baseUrl, + '/upload', + HttpMethod::POST, + [], + [], + $formData, + ); + + $this->rawClient->sendRequest($request); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + + $contentType = $lastRequest->getHeaderLine('Content-Type'); + $this->assertStringStartsWith('multipart/form-data; boundary=', $contentType); + + $boundary = substr($contentType, strlen('multipart/form-data; boundary=')); + $body = (string) $lastRequest->getBody(); + $this->assertStringContainsString("--{$boundary}\r\n", $body); + $this->assertStringContainsString("Content-Disposition: form-data; name=\"field\"\r\n", $body); + $this->assertStringContainsString("value", $body); + $this->assertStringContainsString("--{$boundary}--\r\n", $body); + } + + public function testMultipartWithFilename(): void + { + $this->mockClient->append(self::createResponse(200)); + + $formData = new MultipartFormData(); + $formData->addPart(new MultipartFormDataPart( + name: 'document', + value: 'file-contents', + filename: 'report.pdf', + headers: ['Content-Type' => 'application/pdf'], + )); + + $request = new MultipartApiRequest( + $this->baseUrl, + '/upload', + HttpMethod::POST, + [], + [], + $formData, + ); + + $this->rawClient->sendRequest($request); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + + $body = (string) $lastRequest->getBody(); + $this->assertStringContainsString( + 'Content-Disposition: form-data; name="document"; filename="report.pdf"', + $body, + ); + $this->assertStringContainsString('Content-Type: application/pdf', $body); + $this->assertStringContainsString('file-contents', $body); + } + + public function testMultipartWithMultipleParts(): void + { + $this->mockClient->append(self::createResponse(200)); + + $formData = new MultipartFormData(); + $formData->add('name', 'John'); + $formData->add('age', 30); + $formData->addPart(new MultipartFormDataPart( + name: 'avatar', + value: 'image-data', + filename: 'avatar.png', + )); + + $request = new MultipartApiRequest( + $this->baseUrl, + '/profile', + HttpMethod::POST, + [], + [], + $formData, + ); + + $this->rawClient->sendRequest($request); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + + $body = (string) $lastRequest->getBody(); + $this->assertStringContainsString('name="name"', $body); + $this->assertStringContainsString('John', $body); + $this->assertStringContainsString('name="age"', $body); + $this->assertStringContainsString('30', $body); + $this->assertStringContainsString('name="avatar"; filename="avatar.png"', $body); + $this->assertStringContainsString('image-data', $body); + } + + public function testMultipartDoesNotIncludeJsonContentType(): void + { + $this->mockClient->append(self::createResponse(200)); + + $formData = new MultipartFormData(); + $formData->add('field', 'value'); + + $request = new MultipartApiRequest( + $this->baseUrl, + '/upload', + HttpMethod::POST, + [], + [], + $formData, + ); + + $this->rawClient->sendRequest($request); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + + $contentType = $lastRequest->getHeaderLine('Content-Type'); + $this->assertStringStartsWith('multipart/form-data; boundary=', $contentType); + $this->assertStringNotContainsString('application/json', $contentType); + } + + public function testMultipartNullBodySendsNoBody(): void + { + $this->mockClient->append(self::createResponse(200)); + + $request = new MultipartApiRequest( + $this->baseUrl, + '/upload', + HttpMethod::POST, + ); + + $this->rawClient->sendRequest($request); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + + $this->assertEquals('', (string) $lastRequest->getBody()); + $this->assertStringNotContainsString('multipart/form-data', $lastRequest->getHeaderLine('Content-Type')); + } + + public function testJsonNullBodySendsNoBody(): void + { + $this->mockClient->append(self::createResponse(200)); + + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::POST, + ); + + $this->rawClient->sendRequest($request); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + + $this->assertEquals('', (string) $lastRequest->getBody()); + } + + public function testEmptyJsonBodySerializesAsObject(): void + { + $this->mockClient->append(self::createResponse(200)); + + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::POST, + [], + [], + ['key' => 'value'], + ); + + $this->rawClient->sendRequest( + $request, + options: [ + 'bodyProperties' => [ + 'key' => 'value', + ], + ], + ); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + + // When bodyProperties override all keys, the merged result should still + // serialize as a JSON object {}, not an array []. + $decoded = json_decode((string) $lastRequest->getBody(), true); + $this->assertIsArray($decoded); + $this->assertEquals('value', $decoded['key']); + } + + public function testAuthHeadersAreIncluded(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append(self::createResponse(200)); + + $rawClient = new RawClient([ + 'client' => $mockClient, + 'maxRetries' => 0, + 'getAuthHeaders' => fn () => ['Authorization' => 'Bearer test-token'], + ]); + + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::GET, + ); + + $rawClient->sendRequest($request); + + $lastRequest = $mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + + $this->assertEquals('Bearer test-token', $lastRequest->getHeaderLine('Authorization')); + } + + public function testAuthHeadersAreIncludedInMultipart(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append(self::createResponse(200)); + + $rawClient = new RawClient([ + 'client' => $mockClient, + 'maxRetries' => 0, + 'getAuthHeaders' => fn () => ['Authorization' => 'Bearer test-token'], + ]); + + $formData = new MultipartFormData(); + $formData->add('field', 'value'); + + $request = new MultipartApiRequest( + $this->baseUrl, + '/upload', + HttpMethod::POST, + [], + [], + $formData, + ); + + $rawClient->sendRequest($request); + + $lastRequest = $mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + + $this->assertEquals('Bearer test-token', $lastRequest->getHeaderLine('Authorization')); + $this->assertStringStartsWith('multipart/form-data; boundary=', $lastRequest->getHeaderLine('Content-Type')); + } + + /** + * Creates a PSR-7 response using discovery, without depending on any specific implementation. + * + * @param int $statusCode + * @param array $headers + * @param string $body + * @return ResponseInterface + */ + private static function createResponse( + int $statusCode = 200, + array $headers = [], + string $body = '', + ): ResponseInterface { + $response = \Http\Discovery\Psr17FactoryDiscovery::findResponseFactory() + ->createResponse($statusCode); + foreach ($headers as $name => $value) { + $response = $response->withHeader($name, $value); + } + if ($body !== '') { + $response = $response->withBody( + \Http\Discovery\Psr17FactoryDiscovery::findStreamFactory() + ->createStream($body), + ); + } + return $response; + } + + + public function testTimeoutOptionIsAccepted(): void + { + $this->mockClient->append(self::createResponse(200)); + + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::GET, + ); + + // MockHttpClient is not Guzzle/Symfony, so a warning is triggered once. + set_error_handler(static function (int $errno, string $errstr): bool { + return $errno === E_USER_WARNING + && str_contains($errstr, 'Timeout option is not supported'); + }); + + try { + $response = $this->rawClient->sendRequest( + $request, + options: [ + 'timeout' => 3.0 + ] + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $lastRequest = $this->mockClient->getLastRequest(); + $this->assertInstanceOf(RequestInterface::class, $lastRequest); + } finally { + restore_error_handler(); + } + } + + public function testClientLevelTimeoutIsAccepted(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append(self::createResponse(200)); + + $rawClient = new RawClient([ + 'client' => $mockClient, + 'maxRetries' => 0, + 'timeout' => 5.0, + ]); + + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::GET, + ); + + set_error_handler(static function (int $errno, string $errstr): bool { + return $errno === E_USER_WARNING + && str_contains($errstr, 'Timeout option is not supported'); + }); + + try { + $response = $rawClient->sendRequest($request); + $this->assertEquals(200, $response->getStatusCode()); + } finally { + restore_error_handler(); + } + } + + public function testPerRequestTimeoutOverridesClientTimeout(): void + { + $mockClient = new MockHttpClient(); + $mockClient->append(self::createResponse(200)); + + $rawClient = new RawClient([ + 'client' => $mockClient, + 'maxRetries' => 0, + 'timeout' => 5.0, + ]); + + $request = new JsonApiRequest( + $this->baseUrl, + '/test', + HttpMethod::GET, + ); + + set_error_handler(static function (int $errno, string $errstr): bool { + return $errno === E_USER_WARNING + && str_contains($errstr, 'Timeout option is not supported'); + }); + + try { + $response = $rawClient->sendRequest( + $request, + options: [ + 'timeout' => 1.0 + ] + ); + + $this->assertEquals(200, $response->getStatusCode()); + } finally { + restore_error_handler(); + } + } + + public function testDiscoveryFindsHttpClient(): void + { + // HttpClientBuilder::build() with no client arg uses Psr18ClientDiscovery. + $client = HttpClientBuilder::build(); + $this->assertInstanceOf(\Psr\Http\Client\ClientInterface::class, $client); + } + + public function testDiscoveryFindsFactories(): void + { + $requestFactory = HttpClientBuilder::requestFactory(); + $this->assertInstanceOf(\Psr\Http\Message\RequestFactoryInterface::class, $requestFactory); + + $streamFactory = HttpClientBuilder::streamFactory(); + $this->assertInstanceOf(\Psr\Http\Message\StreamFactoryInterface::class, $streamFactory); + + // Verify they produce usable objects + $request = $requestFactory->createRequest('GET', 'https://example.com'); + $this->assertEquals('GET', $request->getMethod()); + + $stream = $streamFactory->createStream('hello'); + $this->assertEquals('hello', (string) $stream); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/AdditionalPropertiesTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/AdditionalPropertiesTest.php new file mode 100644 index 000000000000..2c32002340e7 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/AdditionalPropertiesTest.php @@ -0,0 +1,76 @@ +name; + } + + /** + * @return string|null + */ + public function getEmail(): ?string + { + return $this->email; + } + + /** + * @param array{ + * name: string, + * email?: string|null, + * } $values + */ + public function __construct( + array $values, + ) { + $this->name = $values['name']; + $this->email = $values['email'] ?? null; + } +} + +class AdditionalPropertiesTest extends TestCase +{ + public function testExtraProperties(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'name' => 'john.doe', + 'email' => 'john.doe@example.com', + 'age' => 42 + ], + ); + + $person = Person::fromJson($expectedJson); + $this->assertEquals('john.doe', $person->getName()); + $this->assertEquals('john.doe@example.com', $person->getEmail()); + $this->assertEquals( + [ + 'age' => 42 + ], + $person->getAdditionalProperties(), + ); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/DateArrayTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/DateArrayTest.php new file mode 100644 index 000000000000..e7794d652432 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/DateArrayTest.php @@ -0,0 +1,54 @@ +dates = $values['dates']; + } +} + +class DateArrayTest extends TestCase +{ + public function testDateTimeInArrays(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'dates' => ['2023-01-01', '2023-02-01', '2023-03-01'] + ], + ); + + $object = DateArray::fromJson($expectedJson); + $this->assertInstanceOf(DateTime::class, $object->dates[0], 'dates[0] should be a DateTime instance.'); + $this->assertEquals('2023-01-01', $object->dates[0]->format('Y-m-d'), 'dates[0] should have the correct date.'); + $this->assertInstanceOf(DateTime::class, $object->dates[1], 'dates[1] should be a DateTime instance.'); + $this->assertEquals('2023-02-01', $object->dates[1]->format('Y-m-d'), 'dates[1] should have the correct date.'); + $this->assertInstanceOf(DateTime::class, $object->dates[2], 'dates[2] should be a DateTime instance.'); + $this->assertEquals('2023-03-01', $object->dates[2]->format('Y-m-d'), 'dates[2] should have the correct date.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match original JSON for dates array.'); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/EmptyArrayTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/EmptyArrayTest.php new file mode 100644 index 000000000000..b5f217e01f76 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/EmptyArrayTest.php @@ -0,0 +1,71 @@ + $emptyMapArray + */ + #[JsonProperty('empty_map_array')] + #[ArrayType(['integer' => new Union('string', 'null')])] + public array $emptyMapArray; + + /** + * @var array $emptyDatesArray + */ + #[ArrayType([new Union('date', 'null')])] + #[JsonProperty('empty_dates_array')] + public array $emptyDatesArray; + + /** + * @param array{ + * emptyStringArray: string[], + * emptyMapArray: array, + * emptyDatesArray: array, + * } $values + */ + public function __construct( + array $values, + ) { + $this->emptyStringArray = $values['emptyStringArray']; + $this->emptyMapArray = $values['emptyMapArray']; + $this->emptyDatesArray = $values['emptyDatesArray']; + } +} + +class EmptyArrayTest extends TestCase +{ + public function testEmptyArray(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'empty_string_array' => [], + 'empty_map_array' => [], + 'empty_dates_array' => [] + ], + ); + + $object = EmptyArray::fromJson($expectedJson); + $this->assertEmpty($object->emptyStringArray, 'empty_string_array should be empty.'); + $this->assertEmpty($object->emptyMapArray, 'empty_map_array should be empty.'); + $this->assertEmpty($object->emptyDatesArray, 'empty_dates_array should be empty.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match original JSON for EmptyArraysType.'); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/EnumTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/EnumTest.php new file mode 100644 index 000000000000..72dc6f2cfa00 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/EnumTest.php @@ -0,0 +1,77 @@ +value; + } +} + +class ShapeType extends JsonSerializableType +{ + /** + * @var Shape $shape + */ + #[JsonProperty('shape')] + public Shape $shape; + + /** + * @var Shape[] $shapes + */ + #[ArrayType([Shape::class])] + #[JsonProperty('shapes')] + public array $shapes; + + /** + * @param Shape $shape + * @param Shape[] $shapes + */ + public function __construct( + Shape $shape, + array $shapes, + ) { + $this->shape = $shape; + $this->shapes = $shapes; + } +} + +class EnumTest extends TestCase +{ + public function testEnumSerialization(): void + { + $object = new ShapeType( + Shape::Circle, + [Shape::Square, Shape::Circle, Shape::Triangle] + ); + + $expectedJson = JsonEncoder::encode([ + 'shape' => 'CIRCLE', + 'shapes' => ['SQUARE', 'CIRCLE', 'TRIANGLE'] + ]); + + $actualJson = $object->toJson(); + + $this->assertJsonStringEqualsJsonString( + $expectedJson, + $actualJson, + 'Serialized JSON does not match expected JSON for shape and shapes properties.' + ); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/ExhaustiveTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/ExhaustiveTest.php new file mode 100644 index 000000000000..4c288378b48b --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/ExhaustiveTest.php @@ -0,0 +1,197 @@ +nestedProperty = $values['nestedProperty']; + } +} + +class Type extends JsonSerializableType +{ + /** + * @var Nested nestedType + */ + #[JsonProperty('nested_type')] + public Nested $nestedType; /** + + * @var string $simpleProperty + */ + #[JsonProperty('simple_property')] + public string $simpleProperty; + + /** + * @var DateTime $dateProperty + */ + #[Date(Date::TYPE_DATE)] + #[JsonProperty('date_property')] + public DateTime $dateProperty; + + /** + * @var DateTime $datetimeProperty + */ + #[Date(Date::TYPE_DATETIME)] + #[JsonProperty('datetime_property')] + public DateTime $datetimeProperty; + + /** + * @var array $stringArray + */ + #[ArrayType(['string'])] + #[JsonProperty('string_array')] + public array $stringArray; + + /** + * @var array $mapProperty + */ + #[ArrayType(['string' => 'integer'])] + #[JsonProperty('map_property')] + public array $mapProperty; + + /** + * @var array $objectArray + */ + #[ArrayType(['integer' => new Union(Nested::class, 'null')])] + #[JsonProperty('object_array')] + public array $objectArray; + + /** + * @var array> $nestedArray + */ + #[ArrayType(['integer' => ['integer' => new Union('string', 'null')]])] + #[JsonProperty('nested_array')] + public array $nestedArray; + + /** + * @var array $datesArray + */ + #[ArrayType([new Union('date', 'null')])] + #[JsonProperty('dates_array')] + public array $datesArray; + + /** + * @var string|null $nullableProperty + */ + #[JsonProperty('nullable_property')] + public ?string $nullableProperty; + + /** + * @param array{ + * nestedType: Nested, + * simpleProperty: string, + * dateProperty: DateTime, + * datetimeProperty: DateTime, + * stringArray: array, + * mapProperty: array, + * objectArray: array, + * nestedArray: array>, + * datesArray: array, + * nullableProperty?: string|null, + * } $values + */ + public function __construct( + array $values, + ) { + $this->nestedType = $values['nestedType']; + $this->simpleProperty = $values['simpleProperty']; + $this->dateProperty = $values['dateProperty']; + $this->datetimeProperty = $values['datetimeProperty']; + $this->stringArray = $values['stringArray']; + $this->mapProperty = $values['mapProperty']; + $this->objectArray = $values['objectArray']; + $this->nestedArray = $values['nestedArray']; + $this->datesArray = $values['datesArray']; + $this->nullableProperty = $values['nullableProperty'] ?? null; + } +} + +class ExhaustiveTest extends TestCase +{ + /** + * Test serialization and deserialization of all types in Type. + */ + public function testExhaustive(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'nested_type' => ['nested_property' => '1995-07-20'], + 'simple_property' => 'Test String', + // Omit 'nullable_property' to test null serialization + 'date_property' => '2023-01-01', + 'datetime_property' => '2023-01-01T12:34:56Z', + 'string_array' => ['one', 'two', 'three'], + 'map_property' => ['key1' => 1, 'key2' => 2], + 'object_array' => [ + 1 => ['nested_property' => '2021-07-20'], + 2 => null, // Testing nullable objects in array + ], + 'nested_array' => [ + 1 => [1 => 'value1', 2 => null], // Testing nullable strings in nested array + 2 => [3 => 'value3', 4 => 'value4'] + ], + 'dates_array' => ['2023-01-01', null, '2023-03-01'] // Testing nullable dates in array> + ], + ); + + $object = Type::fromJson($expectedJson); + + // Check that nullable property is null and not included in JSON + $this->assertNull($object->nullableProperty, 'Nullable property should be null.'); + + // Check date properties + $this->assertInstanceOf(DateTime::class, $object->dateProperty, 'date_property should be a DateTime instance.'); + $this->assertEquals('2023-01-01', $object->dateProperty->format('Y-m-d'), 'date_property should have the correct date.'); + $this->assertInstanceOf(DateTime::class, $object->datetimeProperty, 'datetime_property should be a DateTime instance.'); + $this->assertEquals('2023-01-01 12:34:56', $object->datetimeProperty->format('Y-m-d H:i:s'), 'datetime_property should have the correct datetime.'); + + // Check scalar arrays + $this->assertEquals(['one', 'two', 'three'], $object->stringArray, 'string_array should match the original data.'); + $this->assertEquals(['key1' => 1, 'key2' => 2], $object->mapProperty, 'map_property should match the original data.'); + + // Check object array with nullable elements + $this->assertInstanceOf(Nested::class, $object->objectArray[1], 'object_array[1] should be an instance of TestNestedType1.'); + $this->assertEquals('2021-07-20', $object->objectArray[1]->nestedProperty->format('Y-m-d'), 'object_array[1]->nestedProperty should match the original data.'); + $this->assertNull($object->objectArray[2], 'object_array[2] should be null.'); + + // Check nested array with nullable strings + $this->assertEquals('value1', $object->nestedArray[1][1], 'nested_array[1][1] should match the original data.'); + $this->assertNull($object->nestedArray[1][2], 'nested_array[1][2] should be null.'); + $this->assertEquals('value3', $object->nestedArray[2][3], 'nested_array[2][3] should match the original data.'); + $this->assertEquals('value4', $object->nestedArray[2][4], 'nested_array[2][4] should match the original data.'); + + // Check dates array with nullable DateTime objects + $this->assertInstanceOf(DateTime::class, $object->datesArray[0], 'dates_array[0] should be a DateTime instance.'); + $this->assertEquals('2023-01-01', $object->datesArray[0]->format('Y-m-d'), 'dates_array[0] should have the correct date.'); + $this->assertNull($object->datesArray[1], 'dates_array[1] should be null.'); + $this->assertInstanceOf(DateTime::class, $object->datesArray[2], 'dates_array[2] should be a DateTime instance.'); + $this->assertEquals('2023-03-01', $object->datesArray[2]->format('Y-m-d'), 'dates_array[2] should have the correct date.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'The serialized JSON does not match the original JSON.'); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/InvalidTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/InvalidTest.php new file mode 100644 index 000000000000..9d845ea113b8 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/InvalidTest.php @@ -0,0 +1,42 @@ +integerProperty = $values['integerProperty']; + } +} + +class InvalidTest extends TestCase +{ + public function testInvalidJsonThrowsException(): void + { + $this->expectException(\TypeError::class); + $json = JsonEncoder::encode( + [ + 'integer_property' => 'not_an_integer' + ], + ); + Invalid::fromJson($json); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/NestedUnionArrayTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/NestedUnionArrayTest.php new file mode 100644 index 000000000000..8fbbeb939f02 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/NestedUnionArrayTest.php @@ -0,0 +1,89 @@ +nestedProperty = $values['nestedProperty']; + } +} + +class NestedUnionArray extends JsonSerializableType +{ + /** + * @var array> $nestedArray + */ + #[ArrayType(['integer' => ['integer' => new Union(UnionObject::class, 'null', 'date')]])] + #[JsonProperty('nested_array')] + public array $nestedArray; + + /** + * @param array{ + * nestedArray: array>, + * } $values + */ + public function __construct( + array $values, + ) { + $this->nestedArray = $values['nestedArray']; + } +} + +class NestedUnionArrayTest extends TestCase +{ + public function testNestedUnionArray(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'nested_array' => [ + 1 => [ + 1 => ['nested_property' => 'Nested One'], + 2 => null, + 4 => '2023-01-02' + ], + 2 => [ + 5 => ['nested_property' => 'Nested Two'], + 7 => '2023-02-02' + ] + ] + ], + ); + + $object = NestedUnionArray::fromJson($expectedJson); + $this->assertInstanceOf(UnionObject::class, $object->nestedArray[1][1], 'nested_array[1][1] should be an instance of Object.'); + $this->assertEquals('Nested One', $object->nestedArray[1][1]->nestedProperty, 'nested_array[1][1]->nestedProperty should match the original data.'); + $this->assertNull($object->nestedArray[1][2], 'nested_array[1][2] should be null.'); + $this->assertInstanceOf(DateTime::class, $object->nestedArray[1][4], 'nested_array[1][4] should be a DateTime instance.'); + $this->assertEquals('2023-01-02T00:00:00+00:00', $object->nestedArray[1][4]->format(Constant::DateTimeFormat), 'nested_array[1][4] should have the correct datetime.'); + $this->assertInstanceOf(UnionObject::class, $object->nestedArray[2][5], 'nested_array[2][5] should be an instance of Object.'); + $this->assertEquals('Nested Two', $object->nestedArray[2][5]->nestedProperty, 'nested_array[2][5]->nestedProperty should match the original data.'); + $this->assertInstanceOf(DateTime::class, $object->nestedArray[2][7], 'nested_array[1][4] should be a DateTime instance.'); + $this->assertEquals('2023-02-02', $object->nestedArray[2][7]->format('Y-m-d'), 'nested_array[1][4] should have the correct date.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match original JSON for nested_array.'); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/NullPropertyTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/NullPropertyTest.php new file mode 100644 index 000000000000..ce20a2442825 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/NullPropertyTest.php @@ -0,0 +1,53 @@ +nonNullProperty = $values['nonNullProperty']; + $this->nullProperty = $values['nullProperty'] ?? null; + } +} + +class NullPropertyTest extends TestCase +{ + public function testNullPropertiesAreOmitted(): void + { + $object = new NullProperty( + [ + "nonNullProperty" => "Test String", + "nullProperty" => null + ] + ); + + $serialized = $object->jsonSerialize(); + $this->assertArrayHasKey('non_null_property', $serialized, 'non_null_property should be present in the serialized JSON.'); + $this->assertArrayNotHasKey('null_property', $serialized, 'null_property should be omitted from the serialized JSON.'); + $this->assertEquals('Test String', $serialized['non_null_property'], 'non_null_property should have the correct value.'); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/NullableArrayTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/NullableArrayTest.php new file mode 100644 index 000000000000..d1749c434a4c --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/NullableArrayTest.php @@ -0,0 +1,49 @@ + $nullableStringArray + */ + #[ArrayType([new Union('string', 'null')])] + #[JsonProperty('nullable_string_array')] + public array $nullableStringArray; + + /** + * @param array{ + * nullableStringArray: array, + * } $values + */ + public function __construct( + array $values, + ) { + $this->nullableStringArray = $values['nullableStringArray']; + } +} + +class NullableArrayTest extends TestCase +{ + public function testNullableArray(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'nullable_string_array' => ['one', null, 'three'] + ], + ); + + $object = NullableArray::fromJson($expectedJson); + $this->assertEquals(['one', null, 'three'], $object->nullableStringArray, 'nullable_string_array should match the original data.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match original JSON for nullable_string_array.'); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/ScalarTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/ScalarTest.php new file mode 100644 index 000000000000..ad4db0251bb5 --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/ScalarTest.php @@ -0,0 +1,116 @@ + $intFloatArray + */ + #[ArrayType([new Union('integer', 'float')])] + #[JsonProperty('int_float_array')] + public array $intFloatArray; + + /** + * @var array $floatArray + */ + #[ArrayType(['float'])] + #[JsonProperty('float_array')] + public array $floatArray; + + /** + * @var bool|null $nullableBooleanProperty + */ + #[JsonProperty('nullable_boolean_property')] + public ?bool $nullableBooleanProperty; + + /** + * @param array{ + * integerProperty: int, + * floatProperty: float, + * otherFloatProperty: float, + * booleanProperty: bool, + * stringProperty: string, + * intFloatArray: array, + * floatArray: array, + * nullableBooleanProperty?: bool|null, + * } $values + */ + public function __construct( + array $values, + ) { + $this->integerProperty = $values['integerProperty']; + $this->floatProperty = $values['floatProperty']; + $this->otherFloatProperty = $values['otherFloatProperty']; + $this->booleanProperty = $values['booleanProperty']; + $this->stringProperty = $values['stringProperty']; + $this->intFloatArray = $values['intFloatArray']; + $this->floatArray = $values['floatArray']; + $this->nullableBooleanProperty = $values['nullableBooleanProperty'] ?? null; + } +} + +class ScalarTest extends TestCase +{ + public function testAllScalarTypesIncludingFloat(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'integer_property' => 42, + 'float_property' => 3.14159, + 'other_float_property' => 3, + 'boolean_property' => true, + 'string_property' => 'Hello, World!', + 'int_float_array' => [1, 2.5, 3, 4.75], + 'float_array' => [1, 2, 3, 4] // Ensure we handle "integer-looking" floats + ], + ); + + $object = Scalar::fromJson($expectedJson); + $this->assertEquals(42, $object->integerProperty, 'integer_property should be 42.'); + $this->assertEquals(3.14159, $object->floatProperty, 'float_property should be 3.14159.'); + $this->assertTrue($object->booleanProperty, 'boolean_property should be true.'); + $this->assertEquals('Hello, World!', $object->stringProperty, 'string_property should be "Hello, World!".'); + $this->assertNull($object->nullableBooleanProperty, 'nullable_boolean_property should be null.'); + $this->assertEquals([1, 2.5, 3, 4.75], $object->intFloatArray, 'int_float_array should match the original data.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match original JSON for ScalarTypesTest.'); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/TraitTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/TraitTest.php new file mode 100644 index 000000000000..e18f06d4191b --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/TraitTest.php @@ -0,0 +1,60 @@ +integerProperty = $values['integerProperty']; + $this->stringProperty = $values['stringProperty']; + } +} + +class TraitTest extends TestCase +{ + public function testTraitPropertyAndString(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'integer_property' => 42, + 'string_property' => 'Hello, World!', + ], + ); + + $object = TypeWithTrait::fromJson($expectedJson); + $this->assertEquals(42, $object->integerProperty, 'integer_property should be 42.'); + $this->assertEquals('Hello, World!', $object->stringProperty, 'string_property should be "Hello, World!".'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match original JSON for ScalarTypesTestWithTrait.'); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/UnionArrayTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/UnionArrayTest.php new file mode 100644 index 000000000000..de20cf9fde1b --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/UnionArrayTest.php @@ -0,0 +1,57 @@ + $mixedDates + */ + #[ArrayType(['integer' => new Union('datetime', 'string', 'null')])] + #[JsonProperty('mixed_dates')] + public array $mixedDates; + + /** + * @param array{ + * mixedDates: array, + * } $values + */ + public function __construct( + array $values, + ) { + $this->mixedDates = $values['mixedDates']; + } +} + +class UnionArrayTest extends TestCase +{ + public function testUnionArray(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'mixed_dates' => [ + 1 => '2023-01-01T12:00:00Z', + 2 => null, + 3 => 'Some String' + ] + ], + ); + + $object = UnionArray::fromJson($expectedJson); + $this->assertInstanceOf(DateTime::class, $object->mixedDates[1], 'mixed_dates[1] should be a DateTime instance.'); + $this->assertEquals('2023-01-01 12:00:00', $object->mixedDates[1]->format('Y-m-d H:i:s'), 'mixed_dates[1] should have the correct datetime.'); + $this->assertNull($object->mixedDates[2], 'mixed_dates[2] should be null.'); + $this->assertEquals('Some String', $object->mixedDates[3], 'mixed_dates[3] should be "Some String".'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match original JSON for mixed_dates.'); + } +} diff --git a/seed/php-sdk/basic-auth-optional/tests/Core/Json/UnionPropertyTest.php b/seed/php-sdk/basic-auth-optional/tests/Core/Json/UnionPropertyTest.php new file mode 100644 index 000000000000..f733062cfabc --- /dev/null +++ b/seed/php-sdk/basic-auth-optional/tests/Core/Json/UnionPropertyTest.php @@ -0,0 +1,111 @@ + 'integer'], UnionProperty::class)] + #[JsonProperty('complexUnion')] + public mixed $complexUnion; + + /** + * @param array{ + * complexUnion: string|int|null|array|UnionProperty + * } $values + */ + public function __construct( + array $values, + ) { + $this->complexUnion = $values['complexUnion']; + } +} + +class UnionPropertyTest extends TestCase +{ + public function testWithMapOfIntToInt(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'complexUnion' => [1 => 100, 2 => 200] + ], + ); + + $object = UnionProperty::fromJson($expectedJson); + $this->assertIsArray($object->complexUnion, 'complexUnion should be an array.'); + $this->assertEquals([1 => 100, 2 => 200], $object->complexUnion, 'complexUnion should match the original map of int => int.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match the original JSON.'); + } + + public function testWithNestedUnionPropertyType(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'complexUnion' => new UnionProperty( + [ + 'complexUnion' => 'Nested String' + ] + ) + ], + ); + + $object = UnionProperty::fromJson($expectedJson); + $this->assertInstanceOf(UnionProperty::class, $object->complexUnion, 'complexUnion should be an instance of UnionPropertyType.'); + $this->assertEquals('Nested String', $object->complexUnion->complexUnion, 'Nested complexUnion should match the original value.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match the original JSON.'); + } + + public function testWithNull(): void + { + $expectedJson = JsonEncoder::encode( + [], + ); + + $object = UnionProperty::fromJson($expectedJson); + $this->assertNull($object->complexUnion, 'complexUnion should be null.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match the original JSON.'); + } + + public function testWithInteger(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'complexUnion' => 42 + ], + ); + + $object = UnionProperty::fromJson($expectedJson); + $this->assertIsInt($object->complexUnion, 'complexUnion should be an integer.'); + $this->assertEquals(42, $object->complexUnion, 'complexUnion should match the original integer.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match the original JSON.'); + } + + public function testWithString(): void + { + $expectedJson = JsonEncoder::encode( + [ + 'complexUnion' => 'Some String' + ], + ); + + $object = UnionProperty::fromJson($expectedJson); + $this->assertIsString($object->complexUnion, 'complexUnion should be a string.'); + $this->assertEquals('Some String', $object->complexUnion, 'complexUnion should match the original string.'); + + $actualJson = $object->toJson(); + $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match the original JSON.'); + } +} diff --git a/seed/python-sdk/basic-auth-optional/.fern/metadata.json b/seed/python-sdk/basic-auth-optional/.fern/metadata.json new file mode 100644 index 000000000000..a3141989173b --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/.fern/metadata.json @@ -0,0 +1,7 @@ +{ + "cliVersion": "DUMMY", + "generatorName": "fernapi/fern-python-sdk", + "generatorVersion": "latest", + "originGitCommit": "DUMMY", + "sdkVersion": "0.0.1" +} \ No newline at end of file diff --git a/seed/python-sdk/basic-auth-optional/.github/workflows/ci.yml b/seed/python-sdk/basic-auth-optional/.github/workflows/ci.yml new file mode 100644 index 000000000000..f48e9eb7577d --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/.github/workflows/ci.yml @@ -0,0 +1,65 @@ +name: ci +on: [push] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + compile: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + - name: Set up python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + - name: Bootstrap poetry + run: | + curl -sSL https://install.python-poetry.org | python - -y --version 1.5.1 + - name: Install dependencies + run: poetry install + - name: Compile + run: poetry run mypy . + test: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + - name: Set up python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + - name: Bootstrap poetry + run: | + curl -sSL https://install.python-poetry.org | python - -y --version 1.5.1 + - name: Install dependencies + run: poetry install + + - name: Test + run: poetry run pytest -rP -n auto . + + publish: + needs: [compile, test] + if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + - name: Set up python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + - name: Bootstrap poetry + run: | + curl -sSL https://install.python-poetry.org | python - -y --version 1.5.1 + - name: Install dependencies + run: poetry install + - name: Publish to pypi + run: | + poetry config repositories.remote + poetry --no-interaction -v publish --build --repository remote --username "$PYPI_USERNAME" --password "$PYPI_PASSWORD" + env: + PYPI_USERNAME: ${{ secrets.PYPI_USERNAME }} + PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }} diff --git a/seed/python-sdk/basic-auth-optional/.gitignore b/seed/python-sdk/basic-auth-optional/.gitignore new file mode 100644 index 000000000000..d2e4ca808d21 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/.gitignore @@ -0,0 +1,5 @@ +.mypy_cache/ +.ruff_cache/ +__pycache__/ +dist/ +poetry.toml diff --git a/seed/python-sdk/basic-auth-optional/README.md b/seed/python-sdk/basic-auth-optional/README.md new file mode 100644 index 000000000000..9bb342b7a6d3 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/README.md @@ -0,0 +1,168 @@ +# Seed Python Library + +[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=Seed%2FPython) +[![pypi](https://img.shields.io/pypi/v/fern_basic-auth-optional)](https://pypi.python.org/pypi/fern_basic-auth-optional) + +The Seed Python library provides convenient access to the Seed APIs from Python. + +## Table of Contents + +- [Installation](#installation) +- [Reference](#reference) +- [Usage](#usage) +- [Async Client](#async-client) +- [Exception Handling](#exception-handling) +- [Advanced](#advanced) + - [Access Raw Response Data](#access-raw-response-data) + - [Retries](#retries) + - [Timeouts](#timeouts) + - [Custom Client](#custom-client) +- [Contributing](#contributing) + +## Installation + +```sh +pip install fern_basic-auth-optional +``` + +## Reference + +A full reference for this library is available [here](./reference.md). + +## Usage + +Instantiate and use the client with the following: + +```python +from seed import SeedBasicAuthOptional + +client = SeedBasicAuthOptional( + username="", + password="", + base_url="https://yourhost.com/path/to/api", +) + +client.basic_auth.post_with_basic_auth( + request={"key": "value"}, +) +``` + +## Async Client + +The SDK also exports an `async` client so that you can make non-blocking calls to our API. Note that if you are constructing an Async httpx client class to pass into this client, use `httpx.AsyncClient()` instead of `httpx.Client()` (e.g. for the `httpx_client` parameter of this client). + +```python +import asyncio + +from seed import AsyncSeedBasicAuthOptional + +client = AsyncSeedBasicAuthOptional( + username="", + password="", + base_url="https://yourhost.com/path/to/api", +) + + +async def main() -> None: + await client.basic_auth.post_with_basic_auth( + request={"key": "value"}, + ) + + +asyncio.run(main()) +``` + +## Exception Handling + +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error +will be thrown. + +```python +from seed.core.api_error import ApiError + +try: + client.basic_auth.post_with_basic_auth(...) +except ApiError as e: + print(e.status_code) + print(e.body) +``` + +## Advanced + +### Access Raw Response Data + +The SDK provides access to raw response data, including headers, through the `.with_raw_response` property. +The `.with_raw_response` property returns a "raw" client that can be used to access the `.headers` and `.data` attributes. + +```python +from seed import SeedBasicAuthOptional + +client = SeedBasicAuthOptional(...) +response = client.basic_auth.with_raw_response.post_with_basic_auth(...) +print(response.headers) # access the response headers +print(response.status_code) # access the response status code +print(response.data) # access the underlying object +``` + +### Retries + +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured +retry limit (default: 2). + +A request is deemed retryable when any of the following HTTP status codes is returned: + +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) + +Use the `max_retries` request option to configure this behavior. + +```python +client.basic_auth.post_with_basic_auth(..., request_options={ + "max_retries": 1 +}) +``` + +### Timeouts + +The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level. + +```python +from seed import SeedBasicAuthOptional + +client = SeedBasicAuthOptional(..., timeout=20.0) + +# Override timeout for a specific method +client.basic_auth.post_with_basic_auth(..., request_options={ + "timeout_in_seconds": 1 +}) +``` + +### Custom Client + +You can override the `httpx` client to customize it for your use-case. Some common use-cases include support for proxies +and transports. + +```python +import httpx +from seed import SeedBasicAuthOptional + +client = SeedBasicAuthOptional( + ..., + httpx_client=httpx.Client( + proxy="http://my.test.proxy.example.com", + transport=httpx.HTTPTransport(local_address="0.0.0.0"), + ), +) +``` + +## Contributing + +While we value open-source contributions to this SDK, this library is generated programmatically. +Additions made directly to this library would have to be moved over to our generation code, +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening +an issue first to discuss with us! + +On the other hand, contributions to the README are always very welcome! diff --git a/seed/python-sdk/basic-auth-optional/poetry.lock b/seed/python-sdk/basic-auth-optional/poetry.lock new file mode 100644 index 000000000000..f3487f4eb233 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/poetry.lock @@ -0,0 +1,646 @@ +# This file is automatically @generated by Poetry 1.8.5 and should not be changed by hand. + +[[package]] +name = "annotated-types" +version = "0.7.0" +description = "Reusable constraint types to use with typing.Annotated" +optional = false +python-versions = ">=3.8" +files = [ + {file = "annotated_types-0.7.0-py3-none-any.whl", hash = "sha256:1f02e8b43a8fbbc3f3e0d4f0f4bfc8131bcb4eebe8849b8e5c773f3a1c582a53"}, + {file = "annotated_types-0.7.0.tar.gz", hash = "sha256:aff07c09a53a08bc8cfccb9c85b05f1aa9a2a6f23728d790723543408344ce89"}, +] + +[[package]] +name = "anyio" +version = "4.13.0" +description = "High-level concurrency and networking framework on top of asyncio or Trio" +optional = false +python-versions = ">=3.10" +files = [ + {file = "anyio-4.13.0-py3-none-any.whl", hash = "sha256:08b310f9e24a9594186fd75b4f73f4a4152069e3853f1ed8bfbf58369f4ad708"}, + {file = "anyio-4.13.0.tar.gz", hash = "sha256:334b70e641fd2221c1505b3890c69882fe4a2df910cba14d97019b90b24439dc"}, +] + +[package.dependencies] +exceptiongroup = {version = ">=1.0.2", markers = "python_version < \"3.11\""} +idna = ">=2.8" +typing_extensions = {version = ">=4.5", markers = "python_version < \"3.13\""} + +[package.extras] +trio = ["trio (>=0.32.0)"] + +[[package]] +name = "backports-asyncio-runner" +version = "1.2.0" +description = "Backport of asyncio.Runner, a context manager that controls event loop life cycle." +optional = false +python-versions = "<3.11,>=3.8" +files = [ + {file = "backports_asyncio_runner-1.2.0-py3-none-any.whl", hash = "sha256:0da0a936a8aeb554eccb426dc55af3ba63bcdc69fa1a600b5bb305413a4477b5"}, + {file = "backports_asyncio_runner-1.2.0.tar.gz", hash = "sha256:a5aa7b2b7d8f8bfcaa2b57313f70792df84e32a2a746f585213373f900b42162"}, +] + +[[package]] +name = "certifi" +version = "2026.2.25" +description = "Python package for providing Mozilla's CA Bundle." +optional = false +python-versions = ">=3.7" +files = [ + {file = "certifi-2026.2.25-py3-none-any.whl", hash = "sha256:027692e4402ad994f1c42e52a4997a9763c646b73e4096e4d5d6db8af1d6f0fa"}, + {file = "certifi-2026.2.25.tar.gz", hash = "sha256:e887ab5cee78ea814d3472169153c2d12cd43b14bd03329a39a9c6e2e80bfba7"}, +] + +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "exceptiongroup" +version = "1.3.1" +description = "Backport of PEP 654 (exception groups)" +optional = false +python-versions = ">=3.7" +files = [ + {file = "exceptiongroup-1.3.1-py3-none-any.whl", hash = "sha256:a7a39a3bd276781e98394987d3a5701d0c4edffb633bb7a5144577f82c773598"}, + {file = "exceptiongroup-1.3.1.tar.gz", hash = "sha256:8b412432c6055b0b7d14c310000ae93352ed6754f70fa8f7c34141f91c4e3219"}, +] + +[package.dependencies] +typing-extensions = {version = ">=4.6.0", markers = "python_version < \"3.13\""} + +[package.extras] +test = ["pytest (>=6)"] + +[[package]] +name = "execnet" +version = "2.1.2" +description = "execnet: rapid multi-Python deployment" +optional = false +python-versions = ">=3.8" +files = [ + {file = "execnet-2.1.2-py3-none-any.whl", hash = "sha256:67fba928dd5a544b783f6056f449e5e3931a5c378b128bc18501f7ea79e296ec"}, + {file = "execnet-2.1.2.tar.gz", hash = "sha256:63d83bfdd9a23e35b9c6a3261412324f964c2ec8dcd8d3c6916ee9373e0befcd"}, +] + +[package.extras] +testing = ["hatch", "pre-commit", "pytest", "tox"] + +[[package]] +name = "h11" +version = "0.16.0" +description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1" +optional = false +python-versions = ">=3.8" +files = [ + {file = "h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86"}, + {file = "h11-0.16.0.tar.gz", hash = "sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1"}, +] + +[[package]] +name = "httpcore" +version = "1.0.9" +description = "A minimal low-level HTTP client." +optional = false +python-versions = ">=3.8" +files = [ + {file = "httpcore-1.0.9-py3-none-any.whl", hash = "sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55"}, + {file = "httpcore-1.0.9.tar.gz", hash = "sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8"}, +] + +[package.dependencies] +certifi = "*" +h11 = ">=0.16" + +[package.extras] +asyncio = ["anyio (>=4.0,<5.0)"] +http2 = ["h2 (>=3,<5)"] +socks = ["socksio (==1.*)"] +trio = ["trio (>=0.22.0,<1.0)"] + +[[package]] +name = "httpx" +version = "0.28.1" +description = "The next generation HTTP client." +optional = false +python-versions = ">=3.8" +files = [ + {file = "httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad"}, + {file = "httpx-0.28.1.tar.gz", hash = "sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc"}, +] + +[package.dependencies] +anyio = "*" +certifi = "*" +httpcore = "==1.*" +idna = "*" + +[package.extras] +brotli = ["brotli", "brotlicffi"] +cli = ["click (==8.*)", "pygments (==2.*)", "rich (>=10,<14)"] +http2 = ["h2 (>=3,<5)"] +socks = ["socksio (==1.*)"] +zstd = ["zstandard (>=0.18.0)"] + +[[package]] +name = "idna" +version = "3.11" +description = "Internationalized Domain Names in Applications (IDNA)" +optional = false +python-versions = ">=3.8" +files = [ + {file = "idna-3.11-py3-none-any.whl", hash = "sha256:771a87f49d9defaf64091e6e6fe9c18d4833f140bd19464795bc32d966ca37ea"}, + {file = "idna-3.11.tar.gz", hash = "sha256:795dafcc9c04ed0c1fb032c2aa73654d8e8c5023a7df64a53f39190ada629902"}, +] + +[package.extras] +all = ["flake8 (>=7.1.1)", "mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.6.2)"] + +[[package]] +name = "iniconfig" +version = "2.3.0" +description = "brain-dead simple config-ini parsing" +optional = false +python-versions = ">=3.10" +files = [ + {file = "iniconfig-2.3.0-py3-none-any.whl", hash = "sha256:f631c04d2c48c52b84d0d0549c99ff3859c98df65b3101406327ecc7d53fbf12"}, + {file = "iniconfig-2.3.0.tar.gz", hash = "sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730"}, +] + +[[package]] +name = "mypy" +version = "1.13.0" +description = "Optional static typing for Python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "mypy-1.13.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:6607e0f1dd1fb7f0aca14d936d13fd19eba5e17e1cd2a14f808fa5f8f6d8f60a"}, + {file = "mypy-1.13.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:8a21be69bd26fa81b1f80a61ee7ab05b076c674d9b18fb56239d72e21d9f4c80"}, + {file = "mypy-1.13.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:7b2353a44d2179846a096e25691d54d59904559f4232519d420d64da6828a3a7"}, + {file = "mypy-1.13.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:0730d1c6a2739d4511dc4253f8274cdd140c55c32dfb0a4cf8b7a43f40abfa6f"}, + {file = "mypy-1.13.0-cp310-cp310-win_amd64.whl", hash = "sha256:c5fc54dbb712ff5e5a0fca797e6e0aa25726c7e72c6a5850cfd2adbc1eb0a372"}, + {file = "mypy-1.13.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:581665e6f3a8a9078f28d5502f4c334c0c8d802ef55ea0e7276a6e409bc0d82d"}, + {file = "mypy-1.13.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:3ddb5b9bf82e05cc9a627e84707b528e5c7caaa1c55c69e175abb15a761cec2d"}, + {file = "mypy-1.13.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:20c7ee0bc0d5a9595c46f38beb04201f2620065a93755704e141fcac9f59db2b"}, + {file = "mypy-1.13.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:3790ded76f0b34bc9c8ba4def8f919dd6a46db0f5a6610fb994fe8efdd447f73"}, + {file = "mypy-1.13.0-cp311-cp311-win_amd64.whl", hash = "sha256:51f869f4b6b538229c1d1bcc1dd7d119817206e2bc54e8e374b3dfa202defcca"}, + {file = "mypy-1.13.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:5c7051a3461ae84dfb5dd15eff5094640c61c5f22257c8b766794e6dd85e72d5"}, + {file = "mypy-1.13.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:39bb21c69a5d6342f4ce526e4584bc5c197fd20a60d14a8624d8743fffb9472e"}, + {file = "mypy-1.13.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:164f28cb9d6367439031f4c81e84d3ccaa1e19232d9d05d37cb0bd880d3f93c2"}, + {file = "mypy-1.13.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:a4c1bfcdbce96ff5d96fc9b08e3831acb30dc44ab02671eca5953eadad07d6d0"}, + {file = "mypy-1.13.0-cp312-cp312-win_amd64.whl", hash = "sha256:a0affb3a79a256b4183ba09811e3577c5163ed06685e4d4b46429a271ba174d2"}, + {file = "mypy-1.13.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:a7b44178c9760ce1a43f544e595d35ed61ac2c3de306599fa59b38a6048e1aa7"}, + {file = "mypy-1.13.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:5d5092efb8516d08440e36626f0153b5006d4088c1d663d88bf79625af3d1d62"}, + {file = "mypy-1.13.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:de2904956dac40ced10931ac967ae63c5089bd498542194b436eb097a9f77bc8"}, + {file = "mypy-1.13.0-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:7bfd8836970d33c2105562650656b6846149374dc8ed77d98424b40b09340ba7"}, + {file = "mypy-1.13.0-cp313-cp313-win_amd64.whl", hash = "sha256:9f73dba9ec77acb86457a8fc04b5239822df0c14a082564737833d2963677dbc"}, + {file = "mypy-1.13.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:100fac22ce82925f676a734af0db922ecfea991e1d7ec0ceb1e115ebe501301a"}, + {file = "mypy-1.13.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:7bcb0bb7f42a978bb323a7c88f1081d1b5dee77ca86f4100735a6f541299d8fb"}, + {file = "mypy-1.13.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:bde31fc887c213e223bbfc34328070996061b0833b0a4cfec53745ed61f3519b"}, + {file = "mypy-1.13.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:07de989f89786f62b937851295ed62e51774722e5444a27cecca993fc3f9cd74"}, + {file = "mypy-1.13.0-cp38-cp38-win_amd64.whl", hash = "sha256:4bde84334fbe19bad704b3f5b78c4abd35ff1026f8ba72b29de70dda0916beb6"}, + {file = "mypy-1.13.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:0246bcb1b5de7f08f2826451abd947bf656945209b140d16ed317f65a17dc7dc"}, + {file = "mypy-1.13.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:7f5b7deae912cf8b77e990b9280f170381fdfbddf61b4ef80927edd813163732"}, + {file = "mypy-1.13.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:7029881ec6ffb8bc233a4fa364736789582c738217b133f1b55967115288a2bc"}, + {file = "mypy-1.13.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:3e38b980e5681f28f033f3be86b099a247b13c491f14bb8b1e1e134d23bb599d"}, + {file = "mypy-1.13.0-cp39-cp39-win_amd64.whl", hash = "sha256:a6789be98a2017c912ae6ccb77ea553bbaf13d27605d2ca20a76dfbced631b24"}, + {file = "mypy-1.13.0-py3-none-any.whl", hash = "sha256:9c250883f9fd81d212e0952c92dbfcc96fc237f4b7c92f56ac81fd48460b3e5a"}, + {file = "mypy-1.13.0.tar.gz", hash = "sha256:0291a61b6fbf3e6673e3405cfcc0e7650bebc7939659fdca2702958038bd835e"}, +] + +[package.dependencies] +mypy-extensions = ">=1.0.0" +tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} +typing-extensions = ">=4.6.0" + +[package.extras] +dmypy = ["psutil (>=4.0)"] +faster-cache = ["orjson"] +install-types = ["pip"] +mypyc = ["setuptools (>=50)"] +reports = ["lxml"] + +[[package]] +name = "mypy-extensions" +version = "1.1.0" +description = "Type system extensions for programs checked with the mypy type checker." +optional = false +python-versions = ">=3.8" +files = [ + {file = "mypy_extensions-1.1.0-py3-none-any.whl", hash = "sha256:1be4cccdb0f2482337c4743e60421de3a356cd97508abadd57d47403e94f5505"}, + {file = "mypy_extensions-1.1.0.tar.gz", hash = "sha256:52e68efc3284861e772bbcd66823fde5ae21fd2fdb51c62a211403730b916558"}, +] + +[[package]] +name = "packaging" +version = "26.0" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.8" +files = [ + {file = "packaging-26.0-py3-none-any.whl", hash = "sha256:b36f1fef9334a5588b4166f8bcd26a14e521f2b55e6b9de3aaa80d3ff7a37529"}, + {file = "packaging-26.0.tar.gz", hash = "sha256:00243ae351a257117b6a241061796684b084ed1c516a08c48a3f7e147a9d80b4"}, +] + +[[package]] +name = "pluggy" +version = "1.6.0" +description = "plugin and hook calling mechanisms for python" +optional = false +python-versions = ">=3.9" +files = [ + {file = "pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746"}, + {file = "pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3"}, +] + +[package.extras] +dev = ["pre-commit", "tox"] +testing = ["coverage", "pytest", "pytest-benchmark"] + +[[package]] +name = "pydantic" +version = "2.12.5" +description = "Data validation using Python type hints" +optional = false +python-versions = ">=3.9" +files = [ + {file = "pydantic-2.12.5-py3-none-any.whl", hash = "sha256:e561593fccf61e8a20fc46dfc2dfe075b8be7d0188df33f221ad1f0139180f9d"}, + {file = "pydantic-2.12.5.tar.gz", hash = "sha256:4d351024c75c0f085a9febbb665ce8c0c6ec5d30e903bdb6394b7ede26aebb49"}, +] + +[package.dependencies] +annotated-types = ">=0.6.0" +pydantic-core = "2.41.5" +typing-extensions = ">=4.14.1" +typing-inspection = ">=0.4.2" + +[package.extras] +email = ["email-validator (>=2.0.0)"] +timezone = ["tzdata"] + +[[package]] +name = "pydantic-core" +version = "2.41.5" +description = "Core functionality for Pydantic validation and serialization" +optional = false +python-versions = ">=3.9" +files = [ + {file = "pydantic_core-2.41.5-cp310-cp310-macosx_10_12_x86_64.whl", hash = "sha256:77b63866ca88d804225eaa4af3e664c5faf3568cea95360d21f4725ab6e07146"}, + {file = "pydantic_core-2.41.5-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:dfa8a0c812ac681395907e71e1274819dec685fec28273a28905df579ef137e2"}, + {file = "pydantic_core-2.41.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5921a4d3ca3aee735d9fd163808f5e8dd6c6972101e4adbda9a4667908849b97"}, + {file = "pydantic_core-2.41.5-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e25c479382d26a2a41b7ebea1043564a937db462816ea07afa8a44c0866d52f9"}, + {file = "pydantic_core-2.41.5-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f547144f2966e1e16ae626d8ce72b4cfa0caedc7fa28052001c94fb2fcaa1c52"}, + {file = "pydantic_core-2.41.5-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:6f52298fbd394f9ed112d56f3d11aabd0d5bd27beb3084cc3d8ad069483b8941"}, + {file = "pydantic_core-2.41.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:100baa204bb412b74fe285fb0f3a385256dad1d1879f0a5cb1499ed2e83d132a"}, + {file = "pydantic_core-2.41.5-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:05a2c8852530ad2812cb7914dc61a1125dc4e06252ee98e5638a12da6cc6fb6c"}, + {file = "pydantic_core-2.41.5-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:29452c56df2ed968d18d7e21f4ab0ac55e71dc59524872f6fc57dcf4a3249ed2"}, + {file = "pydantic_core-2.41.5-cp310-cp310-musllinux_1_1_armv7l.whl", hash = "sha256:d5160812ea7a8a2ffbe233d8da666880cad0cbaf5d4de74ae15c313213d62556"}, + {file = "pydantic_core-2.41.5-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:df3959765b553b9440adfd3c795617c352154e497a4eaf3752555cfb5da8fc49"}, + {file = "pydantic_core-2.41.5-cp310-cp310-win32.whl", hash = "sha256:1f8d33a7f4d5a7889e60dc39856d76d09333d8a6ed0f5f1190635cbec70ec4ba"}, + {file = "pydantic_core-2.41.5-cp310-cp310-win_amd64.whl", hash = "sha256:62de39db01b8d593e45871af2af9e497295db8d73b085f6bfd0b18c83c70a8f9"}, + {file = "pydantic_core-2.41.5-cp311-cp311-macosx_10_12_x86_64.whl", hash = "sha256:a3a52f6156e73e7ccb0f8cced536adccb7042be67cb45f9562e12b319c119da6"}, + {file = "pydantic_core-2.41.5-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:7f3bf998340c6d4b0c9a2f02d6a400e51f123b59565d74dc60d252ce888c260b"}, + {file = "pydantic_core-2.41.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:378bec5c66998815d224c9ca994f1e14c0c21cb95d2f52b6021cc0b2a58f2a5a"}, + {file = "pydantic_core-2.41.5-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e7b576130c69225432866fe2f4a469a85a54ade141d96fd396dffcf607b558f8"}, + {file = "pydantic_core-2.41.5-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:6cb58b9c66f7e4179a2d5e0f849c48eff5c1fca560994d6eb6543abf955a149e"}, + {file = "pydantic_core-2.41.5-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:88942d3a3dff3afc8288c21e565e476fc278902ae4d6d134f1eeda118cc830b1"}, + {file = "pydantic_core-2.41.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f31d95a179f8d64d90f6831d71fa93290893a33148d890ba15de25642c5d075b"}, + {file = "pydantic_core-2.41.5-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:c1df3d34aced70add6f867a8cf413e299177e0c22660cc767218373d0779487b"}, + {file = "pydantic_core-2.41.5-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:4009935984bd36bd2c774e13f9a09563ce8de4abaa7226f5108262fa3e637284"}, + {file = "pydantic_core-2.41.5-cp311-cp311-musllinux_1_1_armv7l.whl", hash = "sha256:34a64bc3441dc1213096a20fe27e8e128bd3ff89921706e83c0b1ac971276594"}, + {file = "pydantic_core-2.41.5-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:c9e19dd6e28fdcaa5a1de679aec4141f691023916427ef9bae8584f9c2fb3b0e"}, + {file = "pydantic_core-2.41.5-cp311-cp311-win32.whl", hash = "sha256:2c010c6ded393148374c0f6f0bf89d206bf3217f201faa0635dcd56bd1520f6b"}, + {file = "pydantic_core-2.41.5-cp311-cp311-win_amd64.whl", hash = "sha256:76ee27c6e9c7f16f47db7a94157112a2f3a00e958bc626e2f4ee8bec5c328fbe"}, + {file = "pydantic_core-2.41.5-cp311-cp311-win_arm64.whl", hash = "sha256:4bc36bbc0b7584de96561184ad7f012478987882ebf9f9c389b23f432ea3d90f"}, + {file = "pydantic_core-2.41.5-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:f41a7489d32336dbf2199c8c0a215390a751c5b014c2c1c5366e817202e9cdf7"}, + {file = "pydantic_core-2.41.5-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:070259a8818988b9a84a449a2a7337c7f430a22acc0859c6b110aa7212a6d9c0"}, + {file = "pydantic_core-2.41.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e96cea19e34778f8d59fe40775a7a574d95816eb150850a85a7a4c8f4b94ac69"}, + {file = "pydantic_core-2.41.5-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:ed2e99c456e3fadd05c991f8f437ef902e00eedf34320ba2b0842bd1c3ca3a75"}, + {file = "pydantic_core-2.41.5-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:65840751b72fbfd82c3c640cff9284545342a4f1eb1586ad0636955b261b0b05"}, + {file = "pydantic_core-2.41.5-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e536c98a7626a98feb2d3eaf75944ef6f3dbee447e1f841eae16f2f0a72d8ddc"}, + {file = "pydantic_core-2.41.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:eceb81a8d74f9267ef4081e246ffd6d129da5d87e37a77c9bde550cb04870c1c"}, + {file = "pydantic_core-2.41.5-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:d38548150c39b74aeeb0ce8ee1d8e82696f4a4e16ddc6de7b1d8823f7de4b9b5"}, + {file = "pydantic_core-2.41.5-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:c23e27686783f60290e36827f9c626e63154b82b116d7fe9adba1fda36da706c"}, + {file = "pydantic_core-2.41.5-cp312-cp312-musllinux_1_1_armv7l.whl", hash = "sha256:482c982f814460eabe1d3bb0adfdc583387bd4691ef00b90575ca0d2b6fe2294"}, + {file = "pydantic_core-2.41.5-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:bfea2a5f0b4d8d43adf9d7b8bf019fb46fdd10a2e5cde477fbcb9d1fa08c68e1"}, + {file = "pydantic_core-2.41.5-cp312-cp312-win32.whl", hash = "sha256:b74557b16e390ec12dca509bce9264c3bbd128f8a2c376eaa68003d7f327276d"}, + {file = "pydantic_core-2.41.5-cp312-cp312-win_amd64.whl", hash = "sha256:1962293292865bca8e54702b08a4f26da73adc83dd1fcf26fbc875b35d81c815"}, + {file = "pydantic_core-2.41.5-cp312-cp312-win_arm64.whl", hash = "sha256:1746d4a3d9a794cacae06a5eaaccb4b8643a131d45fbc9af23e353dc0a5ba5c3"}, + {file = "pydantic_core-2.41.5-cp313-cp313-macosx_10_12_x86_64.whl", hash = "sha256:941103c9be18ac8daf7b7adca8228f8ed6bb7a1849020f643b3a14d15b1924d9"}, + {file = "pydantic_core-2.41.5-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:112e305c3314f40c93998e567879e887a3160bb8689ef3d2c04b6cc62c33ac34"}, + {file = "pydantic_core-2.41.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0cbaad15cb0c90aa221d43c00e77bb33c93e8d36e0bf74760cd00e732d10a6a0"}, + {file = "pydantic_core-2.41.5-cp313-cp313-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:03ca43e12fab6023fc79d28ca6b39b05f794ad08ec2feccc59a339b02f2b3d33"}, + {file = "pydantic_core-2.41.5-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:dc799088c08fa04e43144b164feb0c13f9a0bc40503f8df3e9fde58a3c0c101e"}, + {file = "pydantic_core-2.41.5-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:97aeba56665b4c3235a0e52b2c2f5ae9cd071b8a8310ad27bddb3f7fb30e9aa2"}, + {file = "pydantic_core-2.41.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:406bf18d345822d6c21366031003612b9c77b3e29ffdb0f612367352aab7d586"}, + {file = "pydantic_core-2.41.5-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:b93590ae81f7010dbe380cdeab6f515902ebcbefe0b9327cc4804d74e93ae69d"}, + {file = "pydantic_core-2.41.5-cp313-cp313-musllinux_1_1_aarch64.whl", hash = "sha256:01a3d0ab748ee531f4ea6c3e48ad9dac84ddba4b0d82291f87248f2f9de8d740"}, + {file = "pydantic_core-2.41.5-cp313-cp313-musllinux_1_1_armv7l.whl", hash = "sha256:6561e94ba9dacc9c61bce40e2d6bdc3bfaa0259d3ff36ace3b1e6901936d2e3e"}, + {file = "pydantic_core-2.41.5-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:915c3d10f81bec3a74fbd4faebe8391013ba61e5a1a8d48c4455b923bdda7858"}, + {file = "pydantic_core-2.41.5-cp313-cp313-win32.whl", hash = "sha256:650ae77860b45cfa6e2cdafc42618ceafab3a2d9a3811fcfbd3bbf8ac3c40d36"}, + {file = "pydantic_core-2.41.5-cp313-cp313-win_amd64.whl", hash = "sha256:79ec52ec461e99e13791ec6508c722742ad745571f234ea6255bed38c6480f11"}, + {file = "pydantic_core-2.41.5-cp313-cp313-win_arm64.whl", hash = "sha256:3f84d5c1b4ab906093bdc1ff10484838aca54ef08de4afa9de0f5f14d69639cd"}, + {file = "pydantic_core-2.41.5-cp314-cp314-macosx_10_12_x86_64.whl", hash = "sha256:3f37a19d7ebcdd20b96485056ba9e8b304e27d9904d233d7b1015db320e51f0a"}, + {file = "pydantic_core-2.41.5-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:1d1d9764366c73f996edd17abb6d9d7649a7eb690006ab6adbda117717099b14"}, + {file = "pydantic_core-2.41.5-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:25e1c2af0fce638d5f1988b686f3b3ea8cd7de5f244ca147c777769e798a9cd1"}, + {file = "pydantic_core-2.41.5-cp314-cp314-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:506d766a8727beef16b7adaeb8ee6217c64fc813646b424d0804d67c16eddb66"}, + {file = "pydantic_core-2.41.5-cp314-cp314-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:4819fa52133c9aa3c387b3328f25c1facc356491e6135b459f1de698ff64d869"}, + {file = "pydantic_core-2.41.5-cp314-cp314-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:2b761d210c9ea91feda40d25b4efe82a1707da2ef62901466a42492c028553a2"}, + {file = "pydantic_core-2.41.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:22f0fb8c1c583a3b6f24df2470833b40207e907b90c928cc8d3594b76f874375"}, + {file = "pydantic_core-2.41.5-cp314-cp314-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:2782c870e99878c634505236d81e5443092fba820f0373997ff75f90f68cd553"}, + {file = "pydantic_core-2.41.5-cp314-cp314-musllinux_1_1_aarch64.whl", hash = "sha256:0177272f88ab8312479336e1d777f6b124537d47f2123f89cb37e0accea97f90"}, + {file = "pydantic_core-2.41.5-cp314-cp314-musllinux_1_1_armv7l.whl", hash = "sha256:63510af5e38f8955b8ee5687740d6ebf7c2a0886d15a6d65c32814613681bc07"}, + {file = "pydantic_core-2.41.5-cp314-cp314-musllinux_1_1_x86_64.whl", hash = "sha256:e56ba91f47764cc14f1daacd723e3e82d1a89d783f0f5afe9c364b8bb491ccdb"}, + {file = "pydantic_core-2.41.5-cp314-cp314-win32.whl", hash = "sha256:aec5cf2fd867b4ff45b9959f8b20ea3993fc93e63c7363fe6851424c8a7e7c23"}, + {file = "pydantic_core-2.41.5-cp314-cp314-win_amd64.whl", hash = "sha256:8e7c86f27c585ef37c35e56a96363ab8de4e549a95512445b85c96d3e2f7c1bf"}, + {file = "pydantic_core-2.41.5-cp314-cp314-win_arm64.whl", hash = "sha256:e672ba74fbc2dc8eea59fb6d4aed6845e6905fc2a8afe93175d94a83ba2a01a0"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:8566def80554c3faa0e65ac30ab0932b9e3a5cd7f8323764303d468e5c37595a"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:b80aa5095cd3109962a298ce14110ae16b8c1aece8b72f9dafe81cf597ad80b3"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3006c3dd9ba34b0c094c544c6006cc79e87d8612999f1a5d43b769b89181f23c"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:72f6c8b11857a856bcfa48c86f5368439f74453563f951e473514579d44aa612"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:5cb1b2f9742240e4bb26b652a5aeb840aa4b417c7748b6f8387927bc6e45e40d"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:bd3d54f38609ff308209bd43acea66061494157703364ae40c951f83ba99a1a9"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2ff4321e56e879ee8d2a879501c8e469414d948f4aba74a2d4593184eb326660"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:d0d2568a8c11bf8225044aa94409e21da0cb09dcdafe9ecd10250b2baad531a9"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-musllinux_1_1_aarch64.whl", hash = "sha256:a39455728aabd58ceabb03c90e12f71fd30fa69615760a075b9fec596456ccc3"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-musllinux_1_1_armv7l.whl", hash = "sha256:239edca560d05757817c13dc17c50766136d21f7cd0fac50295499ae24f90fdf"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-musllinux_1_1_x86_64.whl", hash = "sha256:2a5e06546e19f24c6a96a129142a75cee553cc018ffee48a460059b1185f4470"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-win32.whl", hash = "sha256:b4ececa40ac28afa90871c2cc2b9ffd2ff0bf749380fbdf57d165fd23da353aa"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-win_amd64.whl", hash = "sha256:80aa89cad80b32a912a65332f64a4450ed00966111b6615ca6816153d3585a8c"}, + {file = "pydantic_core-2.41.5-cp314-cp314t-win_arm64.whl", hash = "sha256:35b44f37a3199f771c3eaa53051bc8a70cd7b54f333531c59e29fd4db5d15008"}, + {file = "pydantic_core-2.41.5-cp39-cp39-macosx_10_12_x86_64.whl", hash = "sha256:8bfeaf8735be79f225f3fefab7f941c712aaca36f1128c9d7e2352ee1aa87bdf"}, + {file = "pydantic_core-2.41.5-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:346285d28e4c8017da95144c7f3acd42740d637ff41946af5ce6e5e420502dd5"}, + {file = "pydantic_core-2.41.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a75dafbf87d6276ddc5b2bf6fae5254e3d0876b626eb24969a574fff9149ee5d"}, + {file = "pydantic_core-2.41.5-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:7b93a4d08587e2b7e7882de461e82b6ed76d9026ce91ca7915e740ecc7855f60"}, + {file = "pydantic_core-2.41.5-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e8465ab91a4bd96d36dde3263f06caa6a8a6019e4113f24dc753d79a8b3a3f82"}, + {file = "pydantic_core-2.41.5-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:299e0a22e7ae2b85c1a57f104538b2656e8ab1873511fd718a1c1c6f149b77b5"}, + {file = "pydantic_core-2.41.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:707625ef0983fcfb461acfaf14de2067c5942c6bb0f3b4c99158bed6fedd3cf3"}, + {file = "pydantic_core-2.41.5-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:f41eb9797986d6ebac5e8edff36d5cef9de40def462311b3eb3eeded1431e425"}, + {file = "pydantic_core-2.41.5-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:0384e2e1021894b1ff5a786dbf94771e2986ebe2869533874d7e43bc79c6f504"}, + {file = "pydantic_core-2.41.5-cp39-cp39-musllinux_1_1_armv7l.whl", hash = "sha256:f0cd744688278965817fd0839c4a4116add48d23890d468bc436f78beb28abf5"}, + {file = "pydantic_core-2.41.5-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:753e230374206729bf0a807954bcc6c150d3743928a73faffee51ac6557a03c3"}, + {file = "pydantic_core-2.41.5-cp39-cp39-win32.whl", hash = "sha256:873e0d5b4fb9b89ef7c2d2a963ea7d02879d9da0da8d9d4933dee8ee86a8b460"}, + {file = "pydantic_core-2.41.5-cp39-cp39-win_amd64.whl", hash = "sha256:e4f4a984405e91527a0d62649ee21138f8e3d0ef103be488c1dc11a80d7f184b"}, + {file = "pydantic_core-2.41.5-graalpy311-graalpy242_311_native-macosx_10_12_x86_64.whl", hash = "sha256:b96d5f26b05d03cc60f11a7761a5ded1741da411e7fe0909e27a5e6a0cb7b034"}, + {file = "pydantic_core-2.41.5-graalpy311-graalpy242_311_native-macosx_11_0_arm64.whl", hash = "sha256:634e8609e89ceecea15e2d61bc9ac3718caaaa71963717bf3c8f38bfde64242c"}, + {file = "pydantic_core-2.41.5-graalpy311-graalpy242_311_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:93e8740d7503eb008aa2df04d3b9735f845d43ae845e6dcd2be0b55a2da43cd2"}, + {file = "pydantic_core-2.41.5-graalpy311-graalpy242_311_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f15489ba13d61f670dcc96772e733aad1a6f9c429cc27574c6cdaed82d0146ad"}, + {file = "pydantic_core-2.41.5-graalpy312-graalpy250_312_native-macosx_10_12_x86_64.whl", hash = "sha256:7da7087d756b19037bc2c06edc6c170eeef3c3bafcb8f532ff17d64dc427adfd"}, + {file = "pydantic_core-2.41.5-graalpy312-graalpy250_312_native-macosx_11_0_arm64.whl", hash = "sha256:aabf5777b5c8ca26f7824cb4a120a740c9588ed58df9b2d196ce92fba42ff8dc"}, + {file = "pydantic_core-2.41.5-graalpy312-graalpy250_312_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c007fe8a43d43b3969e8469004e9845944f1a80e6acd47c150856bb87f230c56"}, + {file = "pydantic_core-2.41.5-graalpy312-graalpy250_312_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:76d0819de158cd855d1cbb8fcafdf6f5cf1eb8e470abe056d5d161106e38062b"}, + {file = "pydantic_core-2.41.5-pp310-pypy310_pp73-macosx_10_12_x86_64.whl", hash = "sha256:b5819cd790dbf0c5eb9f82c73c16b39a65dd6dd4d1439dcdea7816ec9adddab8"}, + {file = "pydantic_core-2.41.5-pp310-pypy310_pp73-macosx_11_0_arm64.whl", hash = "sha256:5a4e67afbc95fa5c34cf27d9089bca7fcab4e51e57278d710320a70b956d1b9a"}, + {file = "pydantic_core-2.41.5-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ece5c59f0ce7d001e017643d8d24da587ea1f74f6993467d85ae8a5ef9d4f42b"}, + {file = "pydantic_core-2.41.5-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:16f80f7abe3351f8ea6858914ddc8c77e02578544a0ebc15b4c2e1a0e813b0b2"}, + {file = "pydantic_core-2.41.5-pp310-pypy310_pp73-musllinux_1_1_aarch64.whl", hash = "sha256:33cb885e759a705b426baada1fe68cbb0a2e68e34c5d0d0289a364cf01709093"}, + {file = "pydantic_core-2.41.5-pp310-pypy310_pp73-musllinux_1_1_armv7l.whl", hash = "sha256:c8d8b4eb992936023be7dee581270af5c6e0697a8559895f527f5b7105ecd36a"}, + {file = "pydantic_core-2.41.5-pp310-pypy310_pp73-musllinux_1_1_x86_64.whl", hash = "sha256:242a206cd0318f95cd21bdacff3fcc3aab23e79bba5cac3db5a841c9ef9c6963"}, + {file = "pydantic_core-2.41.5-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:d3a978c4f57a597908b7e697229d996d77a6d3c94901e9edee593adada95ce1a"}, + {file = "pydantic_core-2.41.5-pp311-pypy311_pp73-macosx_10_12_x86_64.whl", hash = "sha256:b2379fa7ed44ddecb5bfe4e48577d752db9fc10be00a6b7446e9663ba143de26"}, + {file = "pydantic_core-2.41.5-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:266fb4cbf5e3cbd0b53669a6d1b039c45e3ce651fd5442eff4d07c2cc8d66808"}, + {file = "pydantic_core-2.41.5-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:58133647260ea01e4d0500089a8c4f07bd7aa6ce109682b1426394988d8aaacc"}, + {file = "pydantic_core-2.41.5-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:287dad91cfb551c363dc62899a80e9e14da1f0e2b6ebde82c806612ca2a13ef1"}, + {file = "pydantic_core-2.41.5-pp311-pypy311_pp73-musllinux_1_1_aarch64.whl", hash = "sha256:03b77d184b9eb40240ae9fd676ca364ce1085f203e1b1256f8ab9984dca80a84"}, + {file = "pydantic_core-2.41.5-pp311-pypy311_pp73-musllinux_1_1_armv7l.whl", hash = "sha256:a668ce24de96165bb239160b3d854943128f4334822900534f2fe947930e5770"}, + {file = "pydantic_core-2.41.5-pp311-pypy311_pp73-musllinux_1_1_x86_64.whl", hash = "sha256:f14f8f046c14563f8eb3f45f499cc658ab8d10072961e07225e507adb700e93f"}, + {file = "pydantic_core-2.41.5-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:56121965f7a4dc965bff783d70b907ddf3d57f6eba29b6d2e5dabfaf07799c51"}, + {file = "pydantic_core-2.41.5.tar.gz", hash = "sha256:08daa51ea16ad373ffd5e7606252cc32f07bc72b28284b6bc9c6df804816476e"}, +] + +[package.dependencies] +typing-extensions = ">=4.14.1" + +[[package]] +name = "pygments" +version = "2.20.0" +description = "Pygments is a syntax highlighting package written in Python." +optional = false +python-versions = ">=3.9" +files = [ + {file = "pygments-2.20.0-py3-none-any.whl", hash = "sha256:81a9e26dd42fd28a23a2d169d86d7ac03b46e2f8b59ed4698fb4785f946d0176"}, + {file = "pygments-2.20.0.tar.gz", hash = "sha256:6757cd03768053ff99f3039c1a36d6c0aa0b263438fcab17520b30a303a82b5f"}, +] + +[package.extras] +windows-terminal = ["colorama (>=0.4.6)"] + +[[package]] +name = "pytest" +version = "8.4.2" +description = "pytest: simple powerful testing with Python" +optional = false +python-versions = ">=3.9" +files = [ + {file = "pytest-8.4.2-py3-none-any.whl", hash = "sha256:872f880de3fc3a5bdc88a11b39c9710c3497a547cfa9320bc3c5e62fbf272e79"}, + {file = "pytest-8.4.2.tar.gz", hash = "sha256:86c0d0b93306b961d58d62a4db4879f27fe25513d4b969df351abdddb3c30e01"}, +] + +[package.dependencies] +colorama = {version = ">=0.4", markers = "sys_platform == \"win32\""} +exceptiongroup = {version = ">=1", markers = "python_version < \"3.11\""} +iniconfig = ">=1" +packaging = ">=20" +pluggy = ">=1.5,<2" +pygments = ">=2.7.2" +tomli = {version = ">=1", markers = "python_version < \"3.11\""} + +[package.extras] +dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "requests", "setuptools", "xmlschema"] + +[[package]] +name = "pytest-asyncio" +version = "1.3.0" +description = "Pytest support for asyncio" +optional = false +python-versions = ">=3.10" +files = [ + {file = "pytest_asyncio-1.3.0-py3-none-any.whl", hash = "sha256:611e26147c7f77640e6d0a92a38ed17c3e9848063698d5c93d5aa7aa11cebff5"}, + {file = "pytest_asyncio-1.3.0.tar.gz", hash = "sha256:d7f52f36d231b80ee124cd216ffb19369aa168fc10095013c6b014a34d3ee9e5"}, +] + +[package.dependencies] +backports-asyncio-runner = {version = ">=1.1,<2", markers = "python_version < \"3.11\""} +pytest = ">=8.2,<10" +typing-extensions = {version = ">=4.12", markers = "python_version < \"3.13\""} + +[package.extras] +docs = ["sphinx (>=5.3)", "sphinx-rtd-theme (>=1)"] +testing = ["coverage (>=6.2)", "hypothesis (>=5.7.1)"] + +[[package]] +name = "pytest-xdist" +version = "3.8.0" +description = "pytest xdist plugin for distributed testing, most importantly across multiple CPUs" +optional = false +python-versions = ">=3.9" +files = [ + {file = "pytest_xdist-3.8.0-py3-none-any.whl", hash = "sha256:202ca578cfeb7370784a8c33d6d05bc6e13b4f25b5053c30a152269fd10f0b88"}, + {file = "pytest_xdist-3.8.0.tar.gz", hash = "sha256:7e578125ec9bc6050861aa93f2d59f1d8d085595d6551c2c90b6f4fad8d3a9f1"}, +] + +[package.dependencies] +execnet = ">=2.1" +pytest = ">=7.0.0" + +[package.extras] +psutil = ["psutil (>=3.0)"] +setproctitle = ["setproctitle"] +testing = ["filelock"] + +[[package]] +name = "python-dateutil" +version = "2.9.0.post0" +description = "Extensions to the standard Python datetime module" +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" +files = [ + {file = "python-dateutil-2.9.0.post0.tar.gz", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3"}, + {file = "python_dateutil-2.9.0.post0-py2.py3-none-any.whl", hash = "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427"}, +] + +[package.dependencies] +six = ">=1.5" + +[[package]] +name = "ruff" +version = "0.11.5" +description = "An extremely fast Python linter and code formatter, written in Rust." +optional = false +python-versions = ">=3.7" +files = [ + {file = "ruff-0.11.5-py3-none-linux_armv6l.whl", hash = "sha256:2561294e108eb648e50f210671cc56aee590fb6167b594144401532138c66c7b"}, + {file = "ruff-0.11.5-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:ac12884b9e005c12d0bd121f56ccf8033e1614f736f766c118ad60780882a077"}, + {file = "ruff-0.11.5-py3-none-macosx_11_0_arm64.whl", hash = "sha256:4bfd80a6ec559a5eeb96c33f832418bf0fb96752de0539905cf7b0cc1d31d779"}, + {file = "ruff-0.11.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0947c0a1afa75dcb5db4b34b070ec2bccee869d40e6cc8ab25aca11a7d527794"}, + {file = "ruff-0.11.5-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:ad871ff74b5ec9caa66cb725b85d4ef89b53f8170f47c3406e32ef040400b038"}, + {file = "ruff-0.11.5-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e6cf918390cfe46d240732d4d72fa6e18e528ca1f60e318a10835cf2fa3dc19f"}, + {file = "ruff-0.11.5-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:56145ee1478582f61c08f21076dc59153310d606ad663acc00ea3ab5b2125f82"}, + {file = "ruff-0.11.5-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e5f66f8f1e8c9fc594cbd66fbc5f246a8d91f916cb9667e80208663ec3728304"}, + {file = "ruff-0.11.5-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:80b4df4d335a80315ab9afc81ed1cff62be112bd165e162b5eed8ac55bfc8470"}, + {file = "ruff-0.11.5-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3068befab73620b8a0cc2431bd46b3cd619bc17d6f7695a3e1bb166b652c382a"}, + {file = "ruff-0.11.5-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:f5da2e710a9641828e09aa98b92c9ebbc60518fdf3921241326ca3e8f8e55b8b"}, + {file = "ruff-0.11.5-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:ef39f19cb8ec98cbc762344921e216f3857a06c47412030374fffd413fb8fd3a"}, + {file = "ruff-0.11.5-py3-none-musllinux_1_2_i686.whl", hash = "sha256:b2a7cedf47244f431fd11aa5a7e2806dda2e0c365873bda7834e8f7d785ae159"}, + {file = "ruff-0.11.5-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:81be52e7519f3d1a0beadcf8e974715b2dfc808ae8ec729ecfc79bddf8dbb783"}, + {file = "ruff-0.11.5-py3-none-win32.whl", hash = "sha256:e268da7b40f56e3eca571508a7e567e794f9bfcc0f412c4b607931d3af9c4afe"}, + {file = "ruff-0.11.5-py3-none-win_amd64.whl", hash = "sha256:6c6dc38af3cfe2863213ea25b6dc616d679205732dc0fb673356c2d69608f800"}, + {file = "ruff-0.11.5-py3-none-win_arm64.whl", hash = "sha256:67e241b4314f4eacf14a601d586026a962f4002a475aa702c69980a38087aa4e"}, + {file = "ruff-0.11.5.tar.gz", hash = "sha256:cae2e2439cb88853e421901ec040a758960b576126dab520fa08e9de431d1bef"}, +] + +[[package]] +name = "six" +version = "1.17.0" +description = "Python 2 and 3 compatibility utilities" +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" +files = [ + {file = "six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274"}, + {file = "six-1.17.0.tar.gz", hash = "sha256:ff70335d468e7eb6ec65b95b99d3a2836546063f63acc5171de367e834932a81"}, +] + +[[package]] +name = "tomli" +version = "2.4.1" +description = "A lil' TOML parser" +optional = false +python-versions = ">=3.8" +files = [ + {file = "tomli-2.4.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:f8f0fc26ec2cc2b965b7a3b87cd19c5c6b8c5e5f436b984e85f486d652285c30"}, + {file = "tomli-2.4.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:4ab97e64ccda8756376892c53a72bd1f964e519c77236368527f758fbc36a53a"}, + {file = "tomli-2.4.1-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:96481a5786729fd470164b47cdb3e0e58062a496f455ee41b4403be77cb5a076"}, + {file = "tomli-2.4.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:5a881ab208c0baf688221f8cecc5401bd291d67e38a1ac884d6736cbcd8247e9"}, + {file = "tomli-2.4.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:47149d5bd38761ac8be13a84864bf0b7b70bc051806bc3669ab1cbc56216b23c"}, + {file = "tomli-2.4.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:ec9bfaf3ad2df51ace80688143a6a4ebc09a248f6ff781a9945e51937008fcbc"}, + {file = "tomli-2.4.1-cp311-cp311-win32.whl", hash = "sha256:ff2983983d34813c1aeb0fa89091e76c3a22889ee83ab27c5eeb45100560c049"}, + {file = "tomli-2.4.1-cp311-cp311-win_amd64.whl", hash = "sha256:5ee18d9ebdb417e384b58fe414e8d6af9f4e7a0ae761519fb50f721de398dd4e"}, + {file = "tomli-2.4.1-cp311-cp311-win_arm64.whl", hash = "sha256:c2541745709bad0264b7d4705ad453b76ccd191e64aa6f0fc66b69a293a45ece"}, + {file = "tomli-2.4.1-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:c742f741d58a28940ce01d58f0ab2ea3ced8b12402f162f4d534dfe18ba1cd6a"}, + {file = "tomli-2.4.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:7f86fd587c4ed9dd76f318225e7d9b29cfc5a9d43de44e5754db8d1128487085"}, + {file = "tomli-2.4.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ff18e6a727ee0ab0388507b89d1bc6a22b138d1e2fa56d1ad494586d61d2eae9"}, + {file = "tomli-2.4.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:136443dbd7e1dee43c68ac2694fde36b2849865fa258d39bf822c10e8068eac5"}, + {file = "tomli-2.4.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:5e262d41726bc187e69af7825504c933b6794dc3fbd5945e41a79bb14c31f585"}, + {file = "tomli-2.4.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:5cb41aa38891e073ee49d55fbc7839cfdb2bc0e600add13874d048c94aadddd1"}, + {file = "tomli-2.4.1-cp312-cp312-win32.whl", hash = "sha256:da25dc3563bff5965356133435b757a795a17b17d01dbc0f42fb32447ddfd917"}, + {file = "tomli-2.4.1-cp312-cp312-win_amd64.whl", hash = "sha256:52c8ef851d9a240f11a88c003eacb03c31fc1c9c4ec64a99a0f922b93874fda9"}, + {file = "tomli-2.4.1-cp312-cp312-win_arm64.whl", hash = "sha256:f758f1b9299d059cc3f6546ae2af89670cb1c4d48ea29c3cacc4fe7de3058257"}, + {file = "tomli-2.4.1-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:36d2bd2ad5fb9eaddba5226aa02c8ec3fa4f192631e347b3ed28186d43be6b54"}, + {file = "tomli-2.4.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:eb0dc4e38e6a1fd579e5d50369aa2e10acfc9cace504579b2faabb478e76941a"}, + {file = "tomli-2.4.1-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c7f2c7f2b9ca6bdeef8f0fa897f8e05085923eb091721675170254cbc5b02897"}, + {file = "tomli-2.4.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:f3c6818a1a86dd6dca7ddcaaf76947d5ba31aecc28cb1b67009a5877c9a64f3f"}, + {file = "tomli-2.4.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:d312ef37c91508b0ab2cee7da26ec0b3ed2f03ce12bd87a588d771ae15dcf82d"}, + {file = "tomli-2.4.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:51529d40e3ca50046d7606fa99ce3956a617f9b36380da3b7f0dd3dd28e68cb5"}, + {file = "tomli-2.4.1-cp313-cp313-win32.whl", hash = "sha256:2190f2e9dd7508d2a90ded5ed369255980a1bcdd58e52f7fe24b8162bf9fedbd"}, + {file = "tomli-2.4.1-cp313-cp313-win_amd64.whl", hash = "sha256:8d65a2fbf9d2f8352685bc1364177ee3923d6baf5e7f43ea4959d7d8bc326a36"}, + {file = "tomli-2.4.1-cp313-cp313-win_arm64.whl", hash = "sha256:4b605484e43cdc43f0954ddae319fb75f04cc10dd80d830540060ee7cd0243cd"}, + {file = "tomli-2.4.1-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:fd0409a3653af6c147209d267a0e4243f0ae46b011aa978b1080359fddc9b6cf"}, + {file = "tomli-2.4.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:a120733b01c45e9a0c34aeef92bf0cf1d56cfe81ed9d47d562f9ed591a9828ac"}, + {file = "tomli-2.4.1-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:559db847dc486944896521f68d8190be1c9e719fced785720d2216fe7022b662"}, + {file = "tomli-2.4.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:01f520d4f53ef97964a240a035ec2a869fe1a37dde002b57ebc4417a27ccd853"}, + {file = "tomli-2.4.1-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:7f94b27a62cfad8496c8d2513e1a222dd446f095fca8987fceef261225538a15"}, + {file = "tomli-2.4.1-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:ede3e6487c5ef5d28634ba3f31f989030ad6af71edfb0055cbbd14189ff240ba"}, + {file = "tomli-2.4.1-cp314-cp314-win32.whl", hash = "sha256:3d48a93ee1c9b79c04bb38772ee1b64dcf18ff43085896ea460ca8dec96f35f6"}, + {file = "tomli-2.4.1-cp314-cp314-win_amd64.whl", hash = "sha256:88dceee75c2c63af144e456745e10101eb67361050196b0b6af5d717254dddf7"}, + {file = "tomli-2.4.1-cp314-cp314-win_arm64.whl", hash = "sha256:b8c198f8c1805dc42708689ed6864951fd2494f924149d3e4bce7710f8eb5232"}, + {file = "tomli-2.4.1-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:d4d8fe59808a54658fcc0160ecfb1b30f9089906c50b23bcb4c69eddc19ec2b4"}, + {file = "tomli-2.4.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:7008df2e7655c495dd12d2a4ad038ff878d4ca4b81fccaf82b714e07eae4402c"}, + {file = "tomli-2.4.1-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:1d8591993e228b0c930c4bb0db464bdad97b3289fb981255d6c9a41aedc84b2d"}, + {file = "tomli-2.4.1-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:734e20b57ba95624ecf1841e72b53f6e186355e216e5412de414e3c51e5e3c41"}, + {file = "tomli-2.4.1-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:8a650c2dbafa08d42e51ba0b62740dae4ecb9338eefa093aa5c78ceb546fcd5c"}, + {file = "tomli-2.4.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:504aa796fe0569bb43171066009ead363de03675276d2d121ac1a4572397870f"}, + {file = "tomli-2.4.1-cp314-cp314t-win32.whl", hash = "sha256:b1d22e6e9387bf4739fbe23bfa80e93f6b0373a7f1b96c6227c32bef95a4d7a8"}, + {file = "tomli-2.4.1-cp314-cp314t-win_amd64.whl", hash = "sha256:2c1c351919aca02858f740c6d33adea0c5deea37f9ecca1cc1ef9e884a619d26"}, + {file = "tomli-2.4.1-cp314-cp314t-win_arm64.whl", hash = "sha256:eab21f45c7f66c13f2a9e0e1535309cee140182a9cdae1e041d02e47291e8396"}, + {file = "tomli-2.4.1-py3-none-any.whl", hash = "sha256:0d85819802132122da43cb86656f8d1f8c6587d54ae7dcaf30e90533028b49fe"}, + {file = "tomli-2.4.1.tar.gz", hash = "sha256:7c7e1a961a0b2f2472c1ac5b69affa0ae1132c39adcb67aba98568702b9cc23f"}, +] + +[[package]] +name = "types-python-dateutil" +version = "2.9.0.20260323" +description = "Typing stubs for python-dateutil" +optional = false +python-versions = ">=3.10" +files = [ + {file = "types_python_dateutil-2.9.0.20260323-py3-none-any.whl", hash = "sha256:a23a50a07f6eb87e729d4cb0c2eb511c81761eeb3f505db2c1413be94aae8335"}, + {file = "types_python_dateutil-2.9.0.20260323.tar.gz", hash = "sha256:a107aef5841db41ace381dbbbd7e4945220fc940f7a72172a0be5a92d9ab7164"}, +] + +[[package]] +name = "typing-extensions" +version = "4.15.0" +description = "Backported and Experimental Type Hints for Python 3.9+" +optional = false +python-versions = ">=3.9" +files = [ + {file = "typing_extensions-4.15.0-py3-none-any.whl", hash = "sha256:f0fa19c6845758ab08074a0cfa8b7aecb71c999ca73d62883bc25cc018c4e548"}, + {file = "typing_extensions-4.15.0.tar.gz", hash = "sha256:0cea48d173cc12fa28ecabc3b837ea3cf6f38c6d1136f85cbaaf598984861466"}, +] + +[[package]] +name = "typing-inspection" +version = "0.4.2" +description = "Runtime typing introspection tools" +optional = false +python-versions = ">=3.9" +files = [ + {file = "typing_inspection-0.4.2-py3-none-any.whl", hash = "sha256:4ed1cacbdc298c220f1bd249ed5287caa16f34d44ef4e9c3d0cbad5b521545e7"}, + {file = "typing_inspection-0.4.2.tar.gz", hash = "sha256:ba561c48a67c5958007083d386c3295464928b01faa735ab8547c5692e87f464"}, +] + +[package.dependencies] +typing-extensions = ">=4.12.0" + +[metadata] +lock-version = "2.0" +python-versions = "^3.10" +content-hash = "38d3ecf12c1dec83f3d75b7fb34e0360e556c17672ddce7d0b373e7f8afde1ba" diff --git a/seed/python-sdk/basic-auth-optional/pyproject.toml b/seed/python-sdk/basic-auth-optional/pyproject.toml new file mode 100644 index 000000000000..18ff2a085e19 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/pyproject.toml @@ -0,0 +1,92 @@ +[project] +name = "fern_basic-auth-optional" +dynamic = ["version"] + +[tool.poetry] +name = "fern_basic-auth-optional" +version = "0.0.1" +description = "" +readme = "README.md" +authors = [] +keywords = [ + "fern", + "test" +] + +classifiers = [ + "Intended Audience :: Developers", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", + "Programming Language :: Python :: 3.15", + "Operating System :: OS Independent", + "Operating System :: POSIX", + "Operating System :: MacOS", + "Operating System :: POSIX :: Linux", + "Operating System :: Microsoft :: Windows", + "Topic :: Software Development :: Libraries :: Python Modules", + "Typing :: Typed" +] +packages = [ + { include = "seed", from = "src"} +] + +[tool.poetry.urls] +Documentation = 'https://buildwithfern.com/learn' +Homepage = 'https://buildwithfern.com/' +Repository = 'https://github.com/basic-auth-optional/fern' + +[tool.poetry.dependencies] +python = "^3.10" +httpx = ">=0.21.2" +pydantic = ">= 1.9.2" +pydantic-core = ">=2.18.2,<2.44.0" +typing_extensions = ">= 4.0.0" + +[tool.poetry.group.dev.dependencies] +mypy = "==1.13.0" +pytest = "^8.2.0" +pytest-asyncio = "^1.0.0" +pytest-xdist = "^3.6.1" +python-dateutil = "^2.9.0" +types-python-dateutil = "^2.9.0.20240316" +ruff = "==0.11.5" + +[tool.pytest.ini_options] +testpaths = [ "tests" ] +asyncio_mode = "auto" + +[tool.mypy] +plugins = ["pydantic.mypy"] + +[tool.ruff] +line-length = 120 + +[tool.ruff.lint] +select = [ + "E", # pycodestyle errors + "F", # pyflakes + "I", # isort +] +ignore = [ + "E402", # Module level import not at top of file + "E501", # Line too long + "E711", # Comparison to `None` should be `cond is not None` + "E712", # Avoid equality comparisons to `True`; use `if ...:` checks + "E721", # Use `is` and `is not` for type comparisons, or `isinstance()` for insinstance checks + "E722", # Do not use bare `except` + "E731", # Do not assign a `lambda` expression, use a `def` + "F821", # Undefined name + "F841" # Local variable ... is assigned to but never used +] + +[tool.ruff.lint.isort] +section-order = ["future", "standard-library", "third-party", "first-party"] + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/seed/python-sdk/basic-auth-optional/reference.md b/seed/python-sdk/basic-auth-optional/reference.md new file mode 100644 index 000000000000..6bda1fddd2f8 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/reference.md @@ -0,0 +1,138 @@ +# Reference +## BasicAuth +
client.basic_auth.get_with_basic_auth() -> bool +
+
+ +#### 📝 Description + +
+
+ +
+
+ +GET request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```python +from seed import SeedBasicAuthOptional + +client = SeedBasicAuthOptional( + username="", + password="", + base_url="https://yourhost.com/path/to/api", +) + +client.basic_auth.get_with_basic_auth() + +``` +
+
+
+
+ +#### ⚙️ Parameters + +
+
+ +
+
+ +**request_options:** `typing.Optional[RequestOptions]` — Request-specific configuration. + +
+
+
+
+ + +
+
+
+ +
client.basic_auth.post_with_basic_auth(...) -> bool +
+
+ +#### 📝 Description + +
+
+ +
+
+ +POST request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```python +from seed import SeedBasicAuthOptional + +client = SeedBasicAuthOptional( + username="", + password="", + base_url="https://yourhost.com/path/to/api", +) + +client.basic_auth.post_with_basic_auth( + request={"key": "value"}, +) + +``` +
+
+
+
+ +#### ⚙️ Parameters + +
+
+ +
+
+ +**request:** `typing.Any` + +
+
+ +
+
+ +**request_options:** `typing.Optional[RequestOptions]` — Request-specific configuration. + +
+
+
+
+ + +
+
+
+ diff --git a/seed/python-sdk/basic-auth-optional/requirements.txt b/seed/python-sdk/basic-auth-optional/requirements.txt new file mode 100644 index 000000000000..0141a1a5014b --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/requirements.txt @@ -0,0 +1,4 @@ +httpx>=0.21.2 +pydantic>= 1.9.2 +pydantic-core>=2.18.2,<2.44.0 +typing_extensions>= 4.0.0 diff --git a/seed/python-sdk/basic-auth-optional/snippet.json b/seed/python-sdk/basic-auth-optional/snippet.json new file mode 100644 index 000000000000..2bbf238b62a5 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/snippet.json @@ -0,0 +1,31 @@ +{ + "types": {}, + "endpoints": [ + { + "example_identifier": "default", + "id": { + "path": "/basic-auth", + "method": "GET", + "identifier_override": "endpoint_basic-auth.getWithBasicAuth" + }, + "snippet": { + "sync_client": "from seed import SeedBasicAuthOptional\n\nclient = SeedBasicAuthOptional(\n username=\"YOUR_USERNAME\",\n password=\"YOUR_PASSWORD\",\n base_url=\"https://yourhost.com/path/to/api\",\n)\nclient.basic_auth.get_with_basic_auth()\n", + "async_client": "import asyncio\n\nfrom seed import AsyncSeedBasicAuthOptional\n\nclient = AsyncSeedBasicAuthOptional(\n username=\"YOUR_USERNAME\",\n password=\"YOUR_PASSWORD\",\n base_url=\"https://yourhost.com/path/to/api\",\n)\n\n\nasync def main() -> None:\n await client.basic_auth.get_with_basic_auth()\n\n\nasyncio.run(main())\n", + "type": "python" + } + }, + { + "example_identifier": "default", + "id": { + "path": "/basic-auth", + "method": "POST", + "identifier_override": "endpoint_basic-auth.postWithBasicAuth" + }, + "snippet": { + "sync_client": "from seed import SeedBasicAuthOptional\n\nclient = SeedBasicAuthOptional(\n username=\"YOUR_USERNAME\",\n password=\"YOUR_PASSWORD\",\n base_url=\"https://yourhost.com/path/to/api\",\n)\nclient.basic_auth.post_with_basic_auth(\n request={\"key\": \"value\"},\n)\n", + "async_client": "import asyncio\n\nfrom seed import AsyncSeedBasicAuthOptional\n\nclient = AsyncSeedBasicAuthOptional(\n username=\"YOUR_USERNAME\",\n password=\"YOUR_PASSWORD\",\n base_url=\"https://yourhost.com/path/to/api\",\n)\n\n\nasync def main() -> None:\n await client.basic_auth.post_with_basic_auth(\n request={\"key\": \"value\"},\n )\n\n\nasyncio.run(main())\n", + "type": "python" + } + } + ] +} \ No newline at end of file diff --git a/seed/python-sdk/basic-auth-optional/src/seed/__init__.py b/seed/python-sdk/basic-auth-optional/src/seed/__init__.py new file mode 100644 index 000000000000..a9c6abbc275e --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/__init__.py @@ -0,0 +1,55 @@ +# This file was auto-generated by Fern from our API Definition. + +# isort: skip_file + +import typing +from importlib import import_module + +if typing.TYPE_CHECKING: + from .errors import BadRequest, UnauthorizedRequest, UnauthorizedRequestErrorBody + from . import basic_auth, errors + from .client import AsyncSeedBasicAuthOptional, SeedBasicAuthOptional + from .version import __version__ +_dynamic_imports: typing.Dict[str, str] = { + "AsyncSeedBasicAuthOptional": ".client", + "BadRequest": ".errors", + "SeedBasicAuthOptional": ".client", + "UnauthorizedRequest": ".errors", + "UnauthorizedRequestErrorBody": ".errors", + "__version__": ".version", + "basic_auth": ".basic_auth", + "errors": ".errors", +} + + +def __getattr__(attr_name: str) -> typing.Any: + module_name = _dynamic_imports.get(attr_name) + if module_name is None: + raise AttributeError(f"No {attr_name} found in _dynamic_imports for module name -> {__name__}") + try: + module = import_module(module_name, __package__) + if module_name == f".{attr_name}": + return module + else: + return getattr(module, attr_name) + except ImportError as e: + raise ImportError(f"Failed to import {attr_name} from {module_name}: {e}") from e + except AttributeError as e: + raise AttributeError(f"Failed to get {attr_name} from {module_name}: {e}") from e + + +def __dir__(): + lazy_attrs = list(_dynamic_imports.keys()) + return sorted(lazy_attrs) + + +__all__ = [ + "AsyncSeedBasicAuthOptional", + "BadRequest", + "SeedBasicAuthOptional", + "UnauthorizedRequest", + "UnauthorizedRequestErrorBody", + "__version__", + "basic_auth", + "errors", +] diff --git a/seed/python-sdk/basic-auth-optional/src/seed/basic_auth/__init__.py b/seed/python-sdk/basic-auth-optional/src/seed/basic_auth/__init__.py new file mode 100644 index 000000000000..5cde0202dcf3 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/basic_auth/__init__.py @@ -0,0 +1,4 @@ +# This file was auto-generated by Fern from our API Definition. + +# isort: skip_file + diff --git a/seed/python-sdk/basic-auth-optional/src/seed/basic_auth/client.py b/seed/python-sdk/basic-auth-optional/src/seed/basic_auth/client.py new file mode 100644 index 000000000000..2126381f8570 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/basic_auth/client.py @@ -0,0 +1,178 @@ +# This file was auto-generated by Fern from our API Definition. + +import typing + +from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper +from ..core.request_options import RequestOptions +from .raw_client import AsyncRawBasicAuthClient, RawBasicAuthClient + +# this is used as the default value for optional parameters +OMIT = typing.cast(typing.Any, ...) + + +class BasicAuthClient: + def __init__(self, *, client_wrapper: SyncClientWrapper): + self._raw_client = RawBasicAuthClient(client_wrapper=client_wrapper) + + @property + def with_raw_response(self) -> RawBasicAuthClient: + """ + Retrieves a raw implementation of this client that returns raw responses. + + Returns + ------- + RawBasicAuthClient + """ + return self._raw_client + + def get_with_basic_auth(self, *, request_options: typing.Optional[RequestOptions] = None) -> bool: + """ + GET request with basic auth scheme + + Parameters + ---------- + request_options : typing.Optional[RequestOptions] + Request-specific configuration. + + Returns + ------- + bool + + Examples + -------- + from seed import SeedBasicAuthOptional + + client = SeedBasicAuthOptional( + username="YOUR_USERNAME", + password="YOUR_PASSWORD", + base_url="https://yourhost.com/path/to/api", + ) + client.basic_auth.get_with_basic_auth() + """ + _response = self._raw_client.get_with_basic_auth(request_options=request_options) + return _response.data + + def post_with_basic_auth( + self, *, request: typing.Any, request_options: typing.Optional[RequestOptions] = None + ) -> bool: + """ + POST request with basic auth scheme + + Parameters + ---------- + request : typing.Any + + request_options : typing.Optional[RequestOptions] + Request-specific configuration. + + Returns + ------- + bool + + Examples + -------- + from seed import SeedBasicAuthOptional + + client = SeedBasicAuthOptional( + username="YOUR_USERNAME", + password="YOUR_PASSWORD", + base_url="https://yourhost.com/path/to/api", + ) + client.basic_auth.post_with_basic_auth( + request={"key": "value"}, + ) + """ + _response = self._raw_client.post_with_basic_auth(request=request, request_options=request_options) + return _response.data + + +class AsyncBasicAuthClient: + def __init__(self, *, client_wrapper: AsyncClientWrapper): + self._raw_client = AsyncRawBasicAuthClient(client_wrapper=client_wrapper) + + @property + def with_raw_response(self) -> AsyncRawBasicAuthClient: + """ + Retrieves a raw implementation of this client that returns raw responses. + + Returns + ------- + AsyncRawBasicAuthClient + """ + return self._raw_client + + async def get_with_basic_auth(self, *, request_options: typing.Optional[RequestOptions] = None) -> bool: + """ + GET request with basic auth scheme + + Parameters + ---------- + request_options : typing.Optional[RequestOptions] + Request-specific configuration. + + Returns + ------- + bool + + Examples + -------- + import asyncio + + from seed import AsyncSeedBasicAuthOptional + + client = AsyncSeedBasicAuthOptional( + username="YOUR_USERNAME", + password="YOUR_PASSWORD", + base_url="https://yourhost.com/path/to/api", + ) + + + async def main() -> None: + await client.basic_auth.get_with_basic_auth() + + + asyncio.run(main()) + """ + _response = await self._raw_client.get_with_basic_auth(request_options=request_options) + return _response.data + + async def post_with_basic_auth( + self, *, request: typing.Any, request_options: typing.Optional[RequestOptions] = None + ) -> bool: + """ + POST request with basic auth scheme + + Parameters + ---------- + request : typing.Any + + request_options : typing.Optional[RequestOptions] + Request-specific configuration. + + Returns + ------- + bool + + Examples + -------- + import asyncio + + from seed import AsyncSeedBasicAuthOptional + + client = AsyncSeedBasicAuthOptional( + username="YOUR_USERNAME", + password="YOUR_PASSWORD", + base_url="https://yourhost.com/path/to/api", + ) + + + async def main() -> None: + await client.basic_auth.post_with_basic_auth( + request={"key": "value"}, + ) + + + asyncio.run(main()) + """ + _response = await self._raw_client.post_with_basic_auth(request=request, request_options=request_options) + return _response.data diff --git a/seed/python-sdk/basic-auth-optional/src/seed/basic_auth/raw_client.py b/seed/python-sdk/basic-auth-optional/src/seed/basic_auth/raw_client.py new file mode 100644 index 000000000000..b5d50e093f6d --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/basic_auth/raw_client.py @@ -0,0 +1,238 @@ +# This file was auto-generated by Fern from our API Definition. + +import typing +from json.decoder import JSONDecodeError + +from ..core.api_error import ApiError +from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper +from ..core.http_response import AsyncHttpResponse, HttpResponse +from ..core.parse_error import ParsingError +from ..core.pydantic_utilities import parse_obj_as +from ..core.request_options import RequestOptions +from ..errors.errors.bad_request import BadRequest +from ..errors.errors.unauthorized_request import UnauthorizedRequest +from ..errors.types.unauthorized_request_error_body import UnauthorizedRequestErrorBody +from pydantic import ValidationError + +# this is used as the default value for optional parameters +OMIT = typing.cast(typing.Any, ...) + + +class RawBasicAuthClient: + def __init__(self, *, client_wrapper: SyncClientWrapper): + self._client_wrapper = client_wrapper + + def get_with_basic_auth(self, *, request_options: typing.Optional[RequestOptions] = None) -> HttpResponse[bool]: + """ + GET request with basic auth scheme + + Parameters + ---------- + request_options : typing.Optional[RequestOptions] + Request-specific configuration. + + Returns + ------- + HttpResponse[bool] + """ + _response = self._client_wrapper.httpx_client.request( + "basic-auth", + method="GET", + request_options=request_options, + ) + try: + if 200 <= _response.status_code < 300: + _data = typing.cast( + bool, + parse_obj_as( + type_=bool, # type: ignore + object_=_response.json(), + ), + ) + return HttpResponse(response=_response, data=_data) + if _response.status_code == 401: + raise UnauthorizedRequest( + headers=dict(_response.headers), + body=typing.cast( + UnauthorizedRequestErrorBody, + parse_obj_as( + type_=UnauthorizedRequestErrorBody, # type: ignore + object_=_response.json(), + ), + ), + ) + _response_json = _response.json() + except JSONDecodeError: + raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text) + except ValidationError as e: + raise ParsingError( + status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e + ) + raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json) + + def post_with_basic_auth( + self, *, request: typing.Any, request_options: typing.Optional[RequestOptions] = None + ) -> HttpResponse[bool]: + """ + POST request with basic auth scheme + + Parameters + ---------- + request : typing.Any + + request_options : typing.Optional[RequestOptions] + Request-specific configuration. + + Returns + ------- + HttpResponse[bool] + """ + _response = self._client_wrapper.httpx_client.request( + "basic-auth", + method="POST", + json=request, + request_options=request_options, + omit=OMIT, + ) + try: + if 200 <= _response.status_code < 300: + _data = typing.cast( + bool, + parse_obj_as( + type_=bool, # type: ignore + object_=_response.json(), + ), + ) + return HttpResponse(response=_response, data=_data) + if _response.status_code == 401: + raise UnauthorizedRequest( + headers=dict(_response.headers), + body=typing.cast( + UnauthorizedRequestErrorBody, + parse_obj_as( + type_=UnauthorizedRequestErrorBody, # type: ignore + object_=_response.json(), + ), + ), + ) + if _response.status_code == 400: + raise BadRequest(headers=dict(_response.headers)) + _response_json = _response.json() + except JSONDecodeError: + raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text) + except ValidationError as e: + raise ParsingError( + status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e + ) + raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json) + + +class AsyncRawBasicAuthClient: + def __init__(self, *, client_wrapper: AsyncClientWrapper): + self._client_wrapper = client_wrapper + + async def get_with_basic_auth( + self, *, request_options: typing.Optional[RequestOptions] = None + ) -> AsyncHttpResponse[bool]: + """ + GET request with basic auth scheme + + Parameters + ---------- + request_options : typing.Optional[RequestOptions] + Request-specific configuration. + + Returns + ------- + AsyncHttpResponse[bool] + """ + _response = await self._client_wrapper.httpx_client.request( + "basic-auth", + method="GET", + request_options=request_options, + ) + try: + if 200 <= _response.status_code < 300: + _data = typing.cast( + bool, + parse_obj_as( + type_=bool, # type: ignore + object_=_response.json(), + ), + ) + return AsyncHttpResponse(response=_response, data=_data) + if _response.status_code == 401: + raise UnauthorizedRequest( + headers=dict(_response.headers), + body=typing.cast( + UnauthorizedRequestErrorBody, + parse_obj_as( + type_=UnauthorizedRequestErrorBody, # type: ignore + object_=_response.json(), + ), + ), + ) + _response_json = _response.json() + except JSONDecodeError: + raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text) + except ValidationError as e: + raise ParsingError( + status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e + ) + raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json) + + async def post_with_basic_auth( + self, *, request: typing.Any, request_options: typing.Optional[RequestOptions] = None + ) -> AsyncHttpResponse[bool]: + """ + POST request with basic auth scheme + + Parameters + ---------- + request : typing.Any + + request_options : typing.Optional[RequestOptions] + Request-specific configuration. + + Returns + ------- + AsyncHttpResponse[bool] + """ + _response = await self._client_wrapper.httpx_client.request( + "basic-auth", + method="POST", + json=request, + request_options=request_options, + omit=OMIT, + ) + try: + if 200 <= _response.status_code < 300: + _data = typing.cast( + bool, + parse_obj_as( + type_=bool, # type: ignore + object_=_response.json(), + ), + ) + return AsyncHttpResponse(response=_response, data=_data) + if _response.status_code == 401: + raise UnauthorizedRequest( + headers=dict(_response.headers), + body=typing.cast( + UnauthorizedRequestErrorBody, + parse_obj_as( + type_=UnauthorizedRequestErrorBody, # type: ignore + object_=_response.json(), + ), + ), + ) + if _response.status_code == 400: + raise BadRequest(headers=dict(_response.headers)) + _response_json = _response.json() + except JSONDecodeError: + raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text) + except ValidationError as e: + raise ParsingError( + status_code=_response.status_code, headers=dict(_response.headers), body=_response.json(), cause=e + ) + raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json) diff --git a/seed/python-sdk/basic-auth-optional/src/seed/client.py b/seed/python-sdk/basic-auth-optional/src/seed/client.py new file mode 100644 index 000000000000..a2c698bd1d06 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/client.py @@ -0,0 +1,164 @@ +# This file was auto-generated by Fern from our API Definition. + +from __future__ import annotations + +import typing + +import httpx +from .core.client_wrapper import AsyncClientWrapper, SyncClientWrapper +from .core.logging import LogConfig, Logger + +if typing.TYPE_CHECKING: + from .basic_auth.client import AsyncBasicAuthClient, BasicAuthClient + + +class SeedBasicAuthOptional: + """ + Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions. + + Parameters + ---------- + base_url : str + The base url to use for requests from the client. + + username : typing.Union[str, typing.Callable[[], str]] + password : typing.Union[str, typing.Callable[[], str]] + headers : typing.Optional[typing.Dict[str, str]] + Additional headers to send with every request. + + timeout : typing.Optional[float] + The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced. + + follow_redirects : typing.Optional[bool] + Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in. + + httpx_client : typing.Optional[httpx.Client] + The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration. + + logging : typing.Optional[typing.Union[LogConfig, Logger]] + Configure logging for the SDK. Accepts a LogConfig dict with 'level' (debug/info/warn/error), 'logger' (custom logger implementation), and 'silent' (boolean, defaults to True) fields. You can also pass a pre-configured Logger instance. + + Examples + -------- + from seed import SeedBasicAuthOptional + + client = SeedBasicAuthOptional( + username="YOUR_USERNAME", + password="YOUR_PASSWORD", + base_url="https://yourhost.com/path/to/api", + ) + """ + + def __init__( + self, + *, + base_url: str, + username: typing.Union[str, typing.Callable[[], str]], + password: typing.Union[str, typing.Callable[[], str]], + headers: typing.Optional[typing.Dict[str, str]] = None, + timeout: typing.Optional[float] = None, + follow_redirects: typing.Optional[bool] = True, + httpx_client: typing.Optional[httpx.Client] = None, + logging: typing.Optional[typing.Union[LogConfig, Logger]] = None, + ): + _defaulted_timeout = ( + timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read + ) + self._client_wrapper = SyncClientWrapper( + base_url=base_url, + username=username, + password=password, + headers=headers, + httpx_client=httpx_client + if httpx_client is not None + else httpx.Client(timeout=_defaulted_timeout, follow_redirects=follow_redirects) + if follow_redirects is not None + else httpx.Client(timeout=_defaulted_timeout), + timeout=_defaulted_timeout, + logging=logging, + ) + self._basic_auth: typing.Optional[BasicAuthClient] = None + + @property + def basic_auth(self): + if self._basic_auth is None: + from .basic_auth.client import BasicAuthClient # noqa: E402 + + self._basic_auth = BasicAuthClient(client_wrapper=self._client_wrapper) + return self._basic_auth + + +class AsyncSeedBasicAuthOptional: + """ + Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions. + + Parameters + ---------- + base_url : str + The base url to use for requests from the client. + + username : typing.Union[str, typing.Callable[[], str]] + password : typing.Union[str, typing.Callable[[], str]] + headers : typing.Optional[typing.Dict[str, str]] + Additional headers to send with every request. + + timeout : typing.Optional[float] + The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced. + + follow_redirects : typing.Optional[bool] + Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in. + + httpx_client : typing.Optional[httpx.AsyncClient] + The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration. + + logging : typing.Optional[typing.Union[LogConfig, Logger]] + Configure logging for the SDK. Accepts a LogConfig dict with 'level' (debug/info/warn/error), 'logger' (custom logger implementation), and 'silent' (boolean, defaults to True) fields. You can also pass a pre-configured Logger instance. + + Examples + -------- + from seed import AsyncSeedBasicAuthOptional + + client = AsyncSeedBasicAuthOptional( + username="YOUR_USERNAME", + password="YOUR_PASSWORD", + base_url="https://yourhost.com/path/to/api", + ) + """ + + def __init__( + self, + *, + base_url: str, + username: typing.Union[str, typing.Callable[[], str]], + password: typing.Union[str, typing.Callable[[], str]], + headers: typing.Optional[typing.Dict[str, str]] = None, + timeout: typing.Optional[float] = None, + follow_redirects: typing.Optional[bool] = True, + httpx_client: typing.Optional[httpx.AsyncClient] = None, + logging: typing.Optional[typing.Union[LogConfig, Logger]] = None, + ): + _defaulted_timeout = ( + timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read + ) + self._client_wrapper = AsyncClientWrapper( + base_url=base_url, + username=username, + password=password, + headers=headers, + httpx_client=httpx_client + if httpx_client is not None + else httpx.AsyncClient(timeout=_defaulted_timeout, follow_redirects=follow_redirects) + if follow_redirects is not None + else httpx.AsyncClient(timeout=_defaulted_timeout), + timeout=_defaulted_timeout, + logging=logging, + ) + self._basic_auth: typing.Optional[AsyncBasicAuthClient] = None + + @property + def basic_auth(self): + if self._basic_auth is None: + from .basic_auth.client import AsyncBasicAuthClient # noqa: E402 + + self._basic_auth = AsyncBasicAuthClient(client_wrapper=self._client_wrapper) + return self._basic_auth diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/__init__.py b/seed/python-sdk/basic-auth-optional/src/seed/core/__init__.py new file mode 100644 index 000000000000..4fb6e12e0dff --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/__init__.py @@ -0,0 +1,125 @@ +# This file was auto-generated by Fern from our API Definition. + +# isort: skip_file + +import typing +from importlib import import_module + +if typing.TYPE_CHECKING: + from .api_error import ApiError + from .client_wrapper import AsyncClientWrapper, BaseClientWrapper, SyncClientWrapper + from .datetime_utils import Rfc2822DateTime, parse_rfc2822_datetime, serialize_datetime + from .file import File, convert_file_dict_to_httpx_tuples, with_content_type + from .http_client import AsyncHttpClient, HttpClient + from .http_response import AsyncHttpResponse, HttpResponse + from .jsonable_encoder import jsonable_encoder + from .logging import ConsoleLogger, ILogger, LogConfig, LogLevel, Logger, create_logger + from .parse_error import ParsingError + from .pydantic_utilities import ( + IS_PYDANTIC_V2, + UniversalBaseModel, + UniversalRootModel, + parse_obj_as, + universal_field_validator, + universal_root_validator, + update_forward_refs, + ) + from .query_encoder import encode_query + from .remove_none_from_dict import remove_none_from_dict + from .request_options import RequestOptions + from .serialization import FieldMetadata, convert_and_respect_annotation_metadata +_dynamic_imports: typing.Dict[str, str] = { + "ApiError": ".api_error", + "AsyncClientWrapper": ".client_wrapper", + "AsyncHttpClient": ".http_client", + "AsyncHttpResponse": ".http_response", + "BaseClientWrapper": ".client_wrapper", + "ConsoleLogger": ".logging", + "FieldMetadata": ".serialization", + "File": ".file", + "HttpClient": ".http_client", + "HttpResponse": ".http_response", + "ILogger": ".logging", + "IS_PYDANTIC_V2": ".pydantic_utilities", + "LogConfig": ".logging", + "LogLevel": ".logging", + "Logger": ".logging", + "ParsingError": ".parse_error", + "RequestOptions": ".request_options", + "Rfc2822DateTime": ".datetime_utils", + "SyncClientWrapper": ".client_wrapper", + "UniversalBaseModel": ".pydantic_utilities", + "UniversalRootModel": ".pydantic_utilities", + "convert_and_respect_annotation_metadata": ".serialization", + "convert_file_dict_to_httpx_tuples": ".file", + "create_logger": ".logging", + "encode_query": ".query_encoder", + "jsonable_encoder": ".jsonable_encoder", + "parse_obj_as": ".pydantic_utilities", + "parse_rfc2822_datetime": ".datetime_utils", + "remove_none_from_dict": ".remove_none_from_dict", + "serialize_datetime": ".datetime_utils", + "universal_field_validator": ".pydantic_utilities", + "universal_root_validator": ".pydantic_utilities", + "update_forward_refs": ".pydantic_utilities", + "with_content_type": ".file", +} + + +def __getattr__(attr_name: str) -> typing.Any: + module_name = _dynamic_imports.get(attr_name) + if module_name is None: + raise AttributeError(f"No {attr_name} found in _dynamic_imports for module name -> {__name__}") + try: + module = import_module(module_name, __package__) + if module_name == f".{attr_name}": + return module + else: + return getattr(module, attr_name) + except ImportError as e: + raise ImportError(f"Failed to import {attr_name} from {module_name}: {e}") from e + except AttributeError as e: + raise AttributeError(f"Failed to get {attr_name} from {module_name}: {e}") from e + + +def __dir__(): + lazy_attrs = list(_dynamic_imports.keys()) + return sorted(lazy_attrs) + + +__all__ = [ + "ApiError", + "AsyncClientWrapper", + "AsyncHttpClient", + "AsyncHttpResponse", + "BaseClientWrapper", + "ConsoleLogger", + "FieldMetadata", + "File", + "HttpClient", + "HttpResponse", + "ILogger", + "IS_PYDANTIC_V2", + "LogConfig", + "LogLevel", + "Logger", + "ParsingError", + "RequestOptions", + "Rfc2822DateTime", + "SyncClientWrapper", + "UniversalBaseModel", + "UniversalRootModel", + "convert_and_respect_annotation_metadata", + "convert_file_dict_to_httpx_tuples", + "create_logger", + "encode_query", + "jsonable_encoder", + "parse_obj_as", + "parse_rfc2822_datetime", + "remove_none_from_dict", + "serialize_datetime", + "universal_field_validator", + "universal_root_validator", + "update_forward_refs", + "with_content_type", +] diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/api_error.py b/seed/python-sdk/basic-auth-optional/src/seed/core/api_error.py new file mode 100644 index 000000000000..6f850a60cba3 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/api_error.py @@ -0,0 +1,23 @@ +# This file was auto-generated by Fern from our API Definition. + +from typing import Any, Dict, Optional + + +class ApiError(Exception): + headers: Optional[Dict[str, str]] + status_code: Optional[int] + body: Any + + def __init__( + self, + *, + headers: Optional[Dict[str, str]] = None, + status_code: Optional[int] = None, + body: Any = None, + ) -> None: + self.headers = headers + self.status_code = status_code + self.body = body + + def __str__(self) -> str: + return f"headers: {self.headers}, status_code: {self.status_code}, body: {self.body}" diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/client_wrapper.py b/seed/python-sdk/basic-auth-optional/src/seed/core/client_wrapper.py new file mode 100644 index 000000000000..16988c81f98c --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/client_wrapper.py @@ -0,0 +1,120 @@ +# This file was auto-generated by Fern from our API Definition. + +import typing + +import httpx +from .http_client import AsyncHttpClient, HttpClient +from .logging import LogConfig, Logger + + +class BaseClientWrapper: + def __init__( + self, + *, + username: typing.Union[str, typing.Callable[[], str]], + password: typing.Union[str, typing.Callable[[], str]], + headers: typing.Optional[typing.Dict[str, str]] = None, + base_url: str, + timeout: typing.Optional[float] = None, + logging: typing.Optional[typing.Union[LogConfig, Logger]] = None, + ): + self._username = username + self._password = password + self._headers = headers + self._base_url = base_url + self._timeout = timeout + self._logging = logging + + def get_headers(self) -> typing.Dict[str, str]: + import platform + + headers: typing.Dict[str, str] = { + "User-Agent": "fern_basic-auth-optional/0.0.1", + "X-Fern-Language": "Python", + "X-Fern-Runtime": f"python/{platform.python_version()}", + "X-Fern-Platform": f"{platform.system().lower()}/{platform.release()}", + "X-Fern-SDK-Name": "fern_basic-auth-optional", + "X-Fern-SDK-Version": "0.0.1", + **(self.get_custom_headers() or {}), + } + headers["Authorization"] = httpx.BasicAuth(self._get_username() or "", self._get_password() or "")._auth_header + return headers + + def _get_username(self) -> str: + if isinstance(self._username, str): + return self._username + else: + return self._username() + + def _get_password(self) -> str: + if isinstance(self._password, str): + return self._password + else: + return self._password() + + def get_custom_headers(self) -> typing.Optional[typing.Dict[str, str]]: + return self._headers + + def get_base_url(self) -> str: + return self._base_url + + def get_timeout(self) -> typing.Optional[float]: + return self._timeout + + +class SyncClientWrapper(BaseClientWrapper): + def __init__( + self, + *, + username: typing.Union[str, typing.Callable[[], str]], + password: typing.Union[str, typing.Callable[[], str]], + headers: typing.Optional[typing.Dict[str, str]] = None, + base_url: str, + timeout: typing.Optional[float] = None, + logging: typing.Optional[typing.Union[LogConfig, Logger]] = None, + httpx_client: httpx.Client, + ): + super().__init__( + username=username, password=password, headers=headers, base_url=base_url, timeout=timeout, logging=logging + ) + self.httpx_client = HttpClient( + httpx_client=httpx_client, + base_headers=self.get_headers, + base_timeout=self.get_timeout, + base_url=self.get_base_url, + logging_config=self._logging, + ) + + +class AsyncClientWrapper(BaseClientWrapper): + def __init__( + self, + *, + username: typing.Union[str, typing.Callable[[], str]], + password: typing.Union[str, typing.Callable[[], str]], + headers: typing.Optional[typing.Dict[str, str]] = None, + base_url: str, + timeout: typing.Optional[float] = None, + logging: typing.Optional[typing.Union[LogConfig, Logger]] = None, + async_token: typing.Optional[typing.Callable[[], typing.Awaitable[str]]] = None, + httpx_client: httpx.AsyncClient, + ): + super().__init__( + username=username, password=password, headers=headers, base_url=base_url, timeout=timeout, logging=logging + ) + self._async_token = async_token + self.httpx_client = AsyncHttpClient( + httpx_client=httpx_client, + base_headers=self.get_headers, + base_timeout=self.get_timeout, + base_url=self.get_base_url, + async_base_headers=self.async_get_headers, + logging_config=self._logging, + ) + + async def async_get_headers(self) -> typing.Dict[str, str]: + headers = self.get_headers() + if self._async_token is not None: + token = await self._async_token() + headers["Authorization"] = f"Bearer {token}" + return headers diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/datetime_utils.py b/seed/python-sdk/basic-auth-optional/src/seed/core/datetime_utils.py new file mode 100644 index 000000000000..a12b2ad03c53 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/datetime_utils.py @@ -0,0 +1,70 @@ +# This file was auto-generated by Fern from our API Definition. + +import datetime as dt +from email.utils import parsedate_to_datetime +from typing import Any + +import pydantic + +IS_PYDANTIC_V2 = pydantic.VERSION.startswith("2.") + + +def parse_rfc2822_datetime(v: Any) -> dt.datetime: + """ + Parse an RFC 2822 datetime string (e.g., "Wed, 02 Oct 2002 13:00:00 GMT") + into a datetime object. If the value is already a datetime, return it as-is. + Falls back to ISO 8601 parsing if RFC 2822 parsing fails. + """ + if isinstance(v, dt.datetime): + return v + if isinstance(v, str): + try: + return parsedate_to_datetime(v) + except Exception: + pass + # Fallback to ISO 8601 parsing + return dt.datetime.fromisoformat(v.replace("Z", "+00:00")) + raise ValueError(f"Expected str or datetime, got {type(v)}") + + +class Rfc2822DateTime(dt.datetime): + """A datetime subclass that parses RFC 2822 date strings. + + On Pydantic V1, uses __get_validators__ for pre-validation. + On Pydantic V2, uses __get_pydantic_core_schema__ for BeforeValidator-style parsing. + """ + + @classmethod + def __get_validators__(cls): # type: ignore[no-untyped-def] + yield parse_rfc2822_datetime + + @classmethod + def __get_pydantic_core_schema__(cls, _source_type: Any, _handler: Any) -> Any: # type: ignore[override] + from pydantic_core import core_schema + + return core_schema.no_info_before_validator_function(parse_rfc2822_datetime, core_schema.datetime_schema()) + + +def serialize_datetime(v: dt.datetime) -> str: + """ + Serialize a datetime including timezone info. + + Uses the timezone info provided if present, otherwise uses the current runtime's timezone info. + + UTC datetimes end in "Z" while all other timezones are represented as offset from UTC, e.g. +05:00. + """ + + def _serialize_zoned_datetime(v: dt.datetime) -> str: + if v.tzinfo is not None and v.tzinfo.tzname(None) == dt.timezone.utc.tzname(None): + # UTC is a special case where we use "Z" at the end instead of "+00:00" + return v.isoformat().replace("+00:00", "Z") + else: + # Delegate to the typical +/- offset format + return v.isoformat() + + if v.tzinfo is not None: + return _serialize_zoned_datetime(v) + else: + local_tz = dt.datetime.now().astimezone().tzinfo + localized_dt = v.replace(tzinfo=local_tz) + return _serialize_zoned_datetime(localized_dt) diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/file.py b/seed/python-sdk/basic-auth-optional/src/seed/core/file.py new file mode 100644 index 000000000000..44b0d27c0895 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/file.py @@ -0,0 +1,67 @@ +# This file was auto-generated by Fern from our API Definition. + +from typing import IO, Dict, List, Mapping, Optional, Tuple, Union, cast + +# File typing inspired by the flexibility of types within the httpx library +# https://github.com/encode/httpx/blob/master/httpx/_types.py +FileContent = Union[IO[bytes], bytes, str] +File = Union[ + # file (or bytes) + FileContent, + # (filename, file (or bytes)) + Tuple[Optional[str], FileContent], + # (filename, file (or bytes), content_type) + Tuple[Optional[str], FileContent, Optional[str]], + # (filename, file (or bytes), content_type, headers) + Tuple[ + Optional[str], + FileContent, + Optional[str], + Mapping[str, str], + ], +] + + +def convert_file_dict_to_httpx_tuples( + d: Dict[str, Union[File, List[File]]], +) -> List[Tuple[str, File]]: + """ + The format we use is a list of tuples, where the first element is the + name of the file and the second is the file object. Typically HTTPX wants + a dict, but to be able to send lists of files, you have to use the list + approach (which also works for non-lists) + https://github.com/encode/httpx/pull/1032 + """ + + httpx_tuples = [] + for key, file_like in d.items(): + if isinstance(file_like, list): + for file_like_item in file_like: + httpx_tuples.append((key, file_like_item)) + else: + httpx_tuples.append((key, file_like)) + return httpx_tuples + + +def with_content_type(*, file: File, default_content_type: str) -> File: + """ + This function resolves to the file's content type, if provided, and defaults + to the default_content_type value if not. + """ + if isinstance(file, tuple): + if len(file) == 2: + filename, content = cast(Tuple[Optional[str], FileContent], file) # type: ignore + return (filename, content, default_content_type) + elif len(file) == 3: + filename, content, file_content_type = cast(Tuple[Optional[str], FileContent, Optional[str]], file) # type: ignore + out_content_type = file_content_type or default_content_type + return (filename, content, out_content_type) + elif len(file) == 4: + filename, content, file_content_type, headers = cast( # type: ignore + Tuple[Optional[str], FileContent, Optional[str], Mapping[str, str]], file + ) + out_content_type = file_content_type or default_content_type + return (filename, content, out_content_type, headers) + else: + raise ValueError(f"Unexpected tuple length: {len(file)}") + return (None, file, default_content_type) diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/force_multipart.py b/seed/python-sdk/basic-auth-optional/src/seed/core/force_multipart.py new file mode 100644 index 000000000000..5440913fd4bc --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/force_multipart.py @@ -0,0 +1,18 @@ +# This file was auto-generated by Fern from our API Definition. + +from typing import Any, Dict + + +class ForceMultipartDict(Dict[str, Any]): + """ + A dictionary subclass that always evaluates to True in boolean contexts. + + This is used to force multipart/form-data encoding in HTTP requests even when + the dictionary is empty, which would normally evaluate to False. + """ + + def __bool__(self) -> bool: + return True + + +FORCE_MULTIPART = ForceMultipartDict() diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/http_client.py b/seed/python-sdk/basic-auth-optional/src/seed/core/http_client.py new file mode 100644 index 000000000000..f0a39ca8243a --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/http_client.py @@ -0,0 +1,840 @@ +# This file was auto-generated by Fern from our API Definition. + +import asyncio +import email.utils +import re +import time +import typing +from contextlib import asynccontextmanager, contextmanager +from random import random + +import httpx +from .file import File, convert_file_dict_to_httpx_tuples +from .force_multipart import FORCE_MULTIPART +from .jsonable_encoder import jsonable_encoder +from .logging import LogConfig, Logger, create_logger +from .query_encoder import encode_query +from .remove_none_from_dict import remove_none_from_dict as remove_none_from_dict +from .request_options import RequestOptions +from httpx._types import RequestFiles + +INITIAL_RETRY_DELAY_SECONDS = 1.0 +MAX_RETRY_DELAY_SECONDS = 60.0 +JITTER_FACTOR = 0.2 # 20% random jitter + + +def _parse_retry_after(response_headers: httpx.Headers) -> typing.Optional[float]: + """ + This function parses the `Retry-After` header in a HTTP response and returns the number of seconds to wait. + + Inspired by the urllib3 retry implementation. + """ + retry_after_ms = response_headers.get("retry-after-ms") + if retry_after_ms is not None: + try: + return int(retry_after_ms) / 1000 if retry_after_ms > 0 else 0 + except Exception: + pass + + retry_after = response_headers.get("retry-after") + if retry_after is None: + return None + + # Attempt to parse the header as an int. + if re.match(r"^\s*[0-9]+\s*$", retry_after): + seconds = float(retry_after) + # Fallback to parsing it as a date. + else: + retry_date_tuple = email.utils.parsedate_tz(retry_after) + if retry_date_tuple is None: + return None + if retry_date_tuple[9] is None: # Python 2 + # Assume UTC if no timezone was specified + # On Python2.7, parsedate_tz returns None for a timezone offset + # instead of 0 if no timezone is given, where mktime_tz treats + # a None timezone offset as local time. + retry_date_tuple = retry_date_tuple[:9] + (0,) + retry_date_tuple[10:] + + retry_date = email.utils.mktime_tz(retry_date_tuple) + seconds = retry_date - time.time() + + if seconds < 0: + seconds = 0 + + return seconds + + +def _add_positive_jitter(delay: float) -> float: + """Add positive jitter (0-20%) to prevent thundering herd.""" + jitter_multiplier = 1 + random() * JITTER_FACTOR + return delay * jitter_multiplier + + +def _add_symmetric_jitter(delay: float) -> float: + """Add symmetric jitter (±10%) for exponential backoff.""" + jitter_multiplier = 1 + (random() - 0.5) * JITTER_FACTOR + return delay * jitter_multiplier + + +def _parse_x_ratelimit_reset(response_headers: httpx.Headers) -> typing.Optional[float]: + """ + Parse the X-RateLimit-Reset header (Unix timestamp in seconds). + Returns seconds to wait, or None if header is missing/invalid. + """ + reset_time_str = response_headers.get("x-ratelimit-reset") + if reset_time_str is None: + return None + + try: + reset_time = int(reset_time_str) + delay = reset_time - time.time() + if delay > 0: + return delay + except (ValueError, TypeError): + pass + + return None + + +def _retry_timeout(response: httpx.Response, retries: int) -> float: + """ + Determine the amount of time to wait before retrying a request. + This function begins by trying to parse a retry-after header from the response, and then proceeds to use exponential backoff + with a jitter to determine the number of seconds to wait. + """ + + # 1. Check Retry-After header first + retry_after = _parse_retry_after(response.headers) + if retry_after is not None and retry_after > 0: + return min(retry_after, MAX_RETRY_DELAY_SECONDS) + + # 2. Check X-RateLimit-Reset header (with positive jitter) + ratelimit_reset = _parse_x_ratelimit_reset(response.headers) + if ratelimit_reset is not None: + return _add_positive_jitter(min(ratelimit_reset, MAX_RETRY_DELAY_SECONDS)) + + # 3. Fall back to exponential backoff (with symmetric jitter) + backoff = min(INITIAL_RETRY_DELAY_SECONDS * pow(2.0, retries), MAX_RETRY_DELAY_SECONDS) + return _add_symmetric_jitter(backoff) + + +def _retry_timeout_from_retries(retries: int) -> float: + """Determine retry timeout using exponential backoff when no response is available.""" + backoff = min(INITIAL_RETRY_DELAY_SECONDS * pow(2.0, retries), MAX_RETRY_DELAY_SECONDS) + return _add_symmetric_jitter(backoff) + + +def _should_retry(response: httpx.Response) -> bool: + retryable_400s = [429, 408, 409] + return response.status_code >= 500 or response.status_code in retryable_400s + + +_SENSITIVE_HEADERS = frozenset( + { + "authorization", + "www-authenticate", + "x-api-key", + "api-key", + "apikey", + "x-api-token", + "x-auth-token", + "auth-token", + "cookie", + "set-cookie", + "proxy-authorization", + "proxy-authenticate", + "x-csrf-token", + "x-xsrf-token", + "x-session-token", + "x-access-token", + } +) + + +def _redact_headers(headers: typing.Dict[str, str]) -> typing.Dict[str, str]: + return {k: ("[REDACTED]" if k.lower() in _SENSITIVE_HEADERS else v) for k, v in headers.items()} + + +def _build_url(base_url: str, path: typing.Optional[str]) -> str: + """ + Build a full URL by joining a base URL with a path. + + This function correctly handles base URLs that contain path prefixes (e.g., tenant-based URLs) + by using string concatenation instead of urllib.parse.urljoin(), which would incorrectly + strip path components when the path starts with '/'. + + Example: + >>> _build_url("https://cloud.example.com/org/tenant/api", "/users") + 'https://cloud.example.com/org/tenant/api/users' + + Args: + base_url: The base URL, which may contain path prefixes. + path: The path to append. Can be None or empty string. + + Returns: + The full URL with base_url and path properly joined. + """ + if not path: + return base_url + return f"{base_url.rstrip('/')}/{path.lstrip('/')}" + + +def _maybe_filter_none_from_multipart_data( + data: typing.Optional[typing.Any], + request_files: typing.Optional[RequestFiles], + force_multipart: typing.Optional[bool], +) -> typing.Optional[typing.Any]: + """ + Filter None values from data body for multipart/form requests. + This prevents httpx from converting None to empty strings in multipart encoding. + Only applies when files are present or force_multipart is True. + """ + if data is not None and isinstance(data, typing.Mapping) and (request_files or force_multipart): + return remove_none_from_dict(data) + return data + + +def remove_omit_from_dict( + original: typing.Dict[str, typing.Optional[typing.Any]], + omit: typing.Optional[typing.Any], +) -> typing.Dict[str, typing.Any]: + if omit is None: + return original + new: typing.Dict[str, typing.Any] = {} + for key, value in original.items(): + if value is not omit: + new[key] = value + return new + + +def maybe_filter_request_body( + data: typing.Optional[typing.Any], + request_options: typing.Optional[RequestOptions], + omit: typing.Optional[typing.Any], +) -> typing.Optional[typing.Any]: + if data is None: + return ( + jsonable_encoder(request_options.get("additional_body_parameters", {})) or {} + if request_options is not None + else None + ) + elif not isinstance(data, typing.Mapping): + data_content = jsonable_encoder(data) + else: + data_content = { + **(jsonable_encoder(remove_omit_from_dict(data, omit))), # type: ignore + **( + jsonable_encoder(request_options.get("additional_body_parameters", {})) or {} + if request_options is not None + else {} + ), + } + return data_content + + +# Abstracted out for testing purposes +def get_request_body( + *, + json: typing.Optional[typing.Any], + data: typing.Optional[typing.Any], + request_options: typing.Optional[RequestOptions], + omit: typing.Optional[typing.Any], +) -> typing.Tuple[typing.Optional[typing.Any], typing.Optional[typing.Any]]: + json_body = None + data_body = None + if data is not None: + data_body = maybe_filter_request_body(data, request_options, omit) + else: + # If both data and json are None, we send json data in the event extra properties are specified + json_body = maybe_filter_request_body(json, request_options, omit) + + has_additional_body_parameters = bool( + request_options is not None and request_options.get("additional_body_parameters") + ) + + # Only collapse empty dict to None when the body was not explicitly provided + # and there are no additional body parameters. This preserves explicit empty + # bodies (e.g., when an endpoint has a request body type but all fields are optional). + if json_body == {} and json is None and not has_additional_body_parameters: + json_body = None + if data_body == {} and data is None and not has_additional_body_parameters: + data_body = None + + return json_body, data_body + + +class HttpClient: + def __init__( + self, + *, + httpx_client: httpx.Client, + base_timeout: typing.Callable[[], typing.Optional[float]], + base_headers: typing.Callable[[], typing.Dict[str, str]], + base_url: typing.Optional[typing.Callable[[], str]] = None, + base_max_retries: int = 2, + logging_config: typing.Optional[typing.Union[LogConfig, Logger]] = None, + ): + self.base_url = base_url + self.base_timeout = base_timeout + self.base_headers = base_headers + self.base_max_retries = base_max_retries + self.httpx_client = httpx_client + self.logger = create_logger(logging_config) + + def get_base_url(self, maybe_base_url: typing.Optional[str]) -> str: + base_url = maybe_base_url + if self.base_url is not None and base_url is None: + base_url = self.base_url() + + if base_url is None: + raise ValueError("A base_url is required to make this request, please provide one and try again.") + return base_url + + def request( + self, + path: typing.Optional[str] = None, + *, + method: str, + base_url: typing.Optional[str] = None, + params: typing.Optional[typing.Dict[str, typing.Any]] = None, + json: typing.Optional[typing.Any] = None, + data: typing.Optional[typing.Any] = None, + content: typing.Optional[typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]]] = None, + files: typing.Optional[ + typing.Union[ + typing.Dict[str, typing.Optional[typing.Union[File, typing.List[File]]]], + typing.List[typing.Tuple[str, File]], + ] + ] = None, + headers: typing.Optional[typing.Dict[str, typing.Any]] = None, + request_options: typing.Optional[RequestOptions] = None, + retries: int = 0, + omit: typing.Optional[typing.Any] = None, + force_multipart: typing.Optional[bool] = None, + ) -> httpx.Response: + base_url = self.get_base_url(base_url) + timeout = ( + request_options.get("timeout_in_seconds") + if request_options is not None and request_options.get("timeout_in_seconds") is not None + else self.base_timeout() + ) + + json_body, data_body = get_request_body(json=json, data=data, request_options=request_options, omit=omit) + + request_files: typing.Optional[RequestFiles] = ( + convert_file_dict_to_httpx_tuples(remove_omit_from_dict(remove_none_from_dict(files), omit)) + if (files is not None and files is not omit and isinstance(files, dict)) + else None + ) + + if (request_files is None or len(request_files) == 0) and force_multipart: + request_files = FORCE_MULTIPART + + data_body = _maybe_filter_none_from_multipart_data(data_body, request_files, force_multipart) + + # Compute encoded params separately to avoid passing empty list to httpx + # (httpx strips existing query params from URL when params=[] is passed) + _encoded_params = encode_query( + jsonable_encoder( + remove_none_from_dict( + remove_omit_from_dict( + { + **(params if params is not None else {}), + **( + request_options.get("additional_query_parameters", {}) or {} + if request_options is not None + else {} + ), + }, + omit, + ) + ) + ) + ) + + _request_url = _build_url(base_url, path) + _request_headers = jsonable_encoder( + remove_none_from_dict( + { + **self.base_headers(), + **(headers if headers is not None else {}), + **(request_options.get("additional_headers", {}) or {} if request_options is not None else {}), + } + ) + ) + + if self.logger.is_debug(): + self.logger.debug( + "Making HTTP request", + method=method, + url=_request_url, + headers=_redact_headers(_request_headers), + has_body=json_body is not None or data_body is not None, + ) + + max_retries: int = ( + request_options.get("max_retries", self.base_max_retries) + if request_options is not None + else self.base_max_retries + ) + + try: + response = self.httpx_client.request( + method=method, + url=_request_url, + headers=_request_headers, + params=_encoded_params if _encoded_params else None, + json=json_body, + data=data_body, + content=content, + files=request_files, + timeout=timeout, + ) + except (httpx.ConnectError, httpx.RemoteProtocolError): + if retries < max_retries: + time.sleep(_retry_timeout_from_retries(retries=retries)) + return self.request( + path=path, + method=method, + base_url=base_url, + params=params, + json=json, + data=data, + content=content, + files=files, + headers=headers, + request_options=request_options, + retries=retries + 1, + omit=omit, + force_multipart=force_multipart, + ) + raise + + if _should_retry(response=response): + if retries < max_retries: + time.sleep(_retry_timeout(response=response, retries=retries)) + return self.request( + path=path, + method=method, + base_url=base_url, + params=params, + json=json, + data=data, + content=content, + files=files, + headers=headers, + request_options=request_options, + retries=retries + 1, + omit=omit, + force_multipart=force_multipart, + ) + + if self.logger.is_debug(): + if 200 <= response.status_code < 400: + self.logger.debug( + "HTTP request succeeded", + method=method, + url=_request_url, + status_code=response.status_code, + ) + + if self.logger.is_error(): + if response.status_code >= 400: + self.logger.error( + "HTTP request failed with error status", + method=method, + url=_request_url, + status_code=response.status_code, + ) + + return response + + @contextmanager + def stream( + self, + path: typing.Optional[str] = None, + *, + method: str, + base_url: typing.Optional[str] = None, + params: typing.Optional[typing.Dict[str, typing.Any]] = None, + json: typing.Optional[typing.Any] = None, + data: typing.Optional[typing.Any] = None, + content: typing.Optional[typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]]] = None, + files: typing.Optional[ + typing.Union[ + typing.Dict[str, typing.Optional[typing.Union[File, typing.List[File]]]], + typing.List[typing.Tuple[str, File]], + ] + ] = None, + headers: typing.Optional[typing.Dict[str, typing.Any]] = None, + request_options: typing.Optional[RequestOptions] = None, + retries: int = 0, + omit: typing.Optional[typing.Any] = None, + force_multipart: typing.Optional[bool] = None, + ) -> typing.Iterator[httpx.Response]: + base_url = self.get_base_url(base_url) + timeout = ( + request_options.get("timeout_in_seconds") + if request_options is not None and request_options.get("timeout_in_seconds") is not None + else self.base_timeout() + ) + + request_files: typing.Optional[RequestFiles] = ( + convert_file_dict_to_httpx_tuples(remove_omit_from_dict(remove_none_from_dict(files), omit)) + if (files is not None and files is not omit and isinstance(files, dict)) + else None + ) + + if (request_files is None or len(request_files) == 0) and force_multipart: + request_files = FORCE_MULTIPART + + json_body, data_body = get_request_body(json=json, data=data, request_options=request_options, omit=omit) + + data_body = _maybe_filter_none_from_multipart_data(data_body, request_files, force_multipart) + + # Compute encoded params separately to avoid passing empty list to httpx + # (httpx strips existing query params from URL when params=[] is passed) + _encoded_params = encode_query( + jsonable_encoder( + remove_none_from_dict( + remove_omit_from_dict( + { + **(params if params is not None else {}), + **( + request_options.get("additional_query_parameters", {}) + if request_options is not None + else {} + ), + }, + omit, + ) + ) + ) + ) + + _request_url = _build_url(base_url, path) + _request_headers = jsonable_encoder( + remove_none_from_dict( + { + **self.base_headers(), + **(headers if headers is not None else {}), + **(request_options.get("additional_headers", {}) if request_options is not None else {}), + } + ) + ) + + if self.logger.is_debug(): + self.logger.debug( + "Making streaming HTTP request", + method=method, + url=_request_url, + headers=_redact_headers(_request_headers), + ) + + with self.httpx_client.stream( + method=method, + url=_request_url, + headers=_request_headers, + params=_encoded_params if _encoded_params else None, + json=json_body, + data=data_body, + content=content, + files=request_files, + timeout=timeout, + ) as stream: + yield stream + + +class AsyncHttpClient: + def __init__( + self, + *, + httpx_client: httpx.AsyncClient, + base_timeout: typing.Callable[[], typing.Optional[float]], + base_headers: typing.Callable[[], typing.Dict[str, str]], + base_url: typing.Optional[typing.Callable[[], str]] = None, + base_max_retries: int = 2, + async_base_headers: typing.Optional[typing.Callable[[], typing.Awaitable[typing.Dict[str, str]]]] = None, + logging_config: typing.Optional[typing.Union[LogConfig, Logger]] = None, + ): + self.base_url = base_url + self.base_timeout = base_timeout + self.base_headers = base_headers + self.base_max_retries = base_max_retries + self.async_base_headers = async_base_headers + self.httpx_client = httpx_client + self.logger = create_logger(logging_config) + + async def _get_headers(self) -> typing.Dict[str, str]: + if self.async_base_headers is not None: + return await self.async_base_headers() + return self.base_headers() + + def get_base_url(self, maybe_base_url: typing.Optional[str]) -> str: + base_url = maybe_base_url + if self.base_url is not None and base_url is None: + base_url = self.base_url() + + if base_url is None: + raise ValueError("A base_url is required to make this request, please provide one and try again.") + return base_url + + async def request( + self, + path: typing.Optional[str] = None, + *, + method: str, + base_url: typing.Optional[str] = None, + params: typing.Optional[typing.Dict[str, typing.Any]] = None, + json: typing.Optional[typing.Any] = None, + data: typing.Optional[typing.Any] = None, + content: typing.Optional[typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]]] = None, + files: typing.Optional[ + typing.Union[ + typing.Dict[str, typing.Optional[typing.Union[File, typing.List[File]]]], + typing.List[typing.Tuple[str, File]], + ] + ] = None, + headers: typing.Optional[typing.Dict[str, typing.Any]] = None, + request_options: typing.Optional[RequestOptions] = None, + retries: int = 0, + omit: typing.Optional[typing.Any] = None, + force_multipart: typing.Optional[bool] = None, + ) -> httpx.Response: + base_url = self.get_base_url(base_url) + timeout = ( + request_options.get("timeout_in_seconds") + if request_options is not None and request_options.get("timeout_in_seconds") is not None + else self.base_timeout() + ) + + request_files: typing.Optional[RequestFiles] = ( + convert_file_dict_to_httpx_tuples(remove_omit_from_dict(remove_none_from_dict(files), omit)) + if (files is not None and files is not omit and isinstance(files, dict)) + else None + ) + + if (request_files is None or len(request_files) == 0) and force_multipart: + request_files = FORCE_MULTIPART + + json_body, data_body = get_request_body(json=json, data=data, request_options=request_options, omit=omit) + + data_body = _maybe_filter_none_from_multipart_data(data_body, request_files, force_multipart) + + # Get headers (supports async token providers) + _headers = await self._get_headers() + + # Compute encoded params separately to avoid passing empty list to httpx + # (httpx strips existing query params from URL when params=[] is passed) + _encoded_params = encode_query( + jsonable_encoder( + remove_none_from_dict( + remove_omit_from_dict( + { + **(params if params is not None else {}), + **( + request_options.get("additional_query_parameters", {}) or {} + if request_options is not None + else {} + ), + }, + omit, + ) + ) + ) + ) + + _request_url = _build_url(base_url, path) + _request_headers = jsonable_encoder( + remove_none_from_dict( + { + **_headers, + **(headers if headers is not None else {}), + **(request_options.get("additional_headers", {}) or {} if request_options is not None else {}), + } + ) + ) + + if self.logger.is_debug(): + self.logger.debug( + "Making HTTP request", + method=method, + url=_request_url, + headers=_redact_headers(_request_headers), + has_body=json_body is not None or data_body is not None, + ) + + max_retries: int = ( + request_options.get("max_retries", self.base_max_retries) + if request_options is not None + else self.base_max_retries + ) + + try: + response = await self.httpx_client.request( + method=method, + url=_request_url, + headers=_request_headers, + params=_encoded_params if _encoded_params else None, + json=json_body, + data=data_body, + content=content, + files=request_files, + timeout=timeout, + ) + except (httpx.ConnectError, httpx.RemoteProtocolError): + if retries < max_retries: + await asyncio.sleep(_retry_timeout_from_retries(retries=retries)) + return await self.request( + path=path, + method=method, + base_url=base_url, + params=params, + json=json, + data=data, + content=content, + files=files, + headers=headers, + request_options=request_options, + retries=retries + 1, + omit=omit, + force_multipart=force_multipart, + ) + raise + + if _should_retry(response=response): + if retries < max_retries: + await asyncio.sleep(_retry_timeout(response=response, retries=retries)) + return await self.request( + path=path, + method=method, + base_url=base_url, + params=params, + json=json, + data=data, + content=content, + files=files, + headers=headers, + request_options=request_options, + retries=retries + 1, + omit=omit, + force_multipart=force_multipart, + ) + + if self.logger.is_debug(): + if 200 <= response.status_code < 400: + self.logger.debug( + "HTTP request succeeded", + method=method, + url=_request_url, + status_code=response.status_code, + ) + + if self.logger.is_error(): + if response.status_code >= 400: + self.logger.error( + "HTTP request failed with error status", + method=method, + url=_request_url, + status_code=response.status_code, + ) + + return response + + @asynccontextmanager + async def stream( + self, + path: typing.Optional[str] = None, + *, + method: str, + base_url: typing.Optional[str] = None, + params: typing.Optional[typing.Dict[str, typing.Any]] = None, + json: typing.Optional[typing.Any] = None, + data: typing.Optional[typing.Any] = None, + content: typing.Optional[typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]]] = None, + files: typing.Optional[ + typing.Union[ + typing.Dict[str, typing.Optional[typing.Union[File, typing.List[File]]]], + typing.List[typing.Tuple[str, File]], + ] + ] = None, + headers: typing.Optional[typing.Dict[str, typing.Any]] = None, + request_options: typing.Optional[RequestOptions] = None, + retries: int = 0, + omit: typing.Optional[typing.Any] = None, + force_multipart: typing.Optional[bool] = None, + ) -> typing.AsyncIterator[httpx.Response]: + base_url = self.get_base_url(base_url) + timeout = ( + request_options.get("timeout_in_seconds") + if request_options is not None and request_options.get("timeout_in_seconds") is not None + else self.base_timeout() + ) + + request_files: typing.Optional[RequestFiles] = ( + convert_file_dict_to_httpx_tuples(remove_omit_from_dict(remove_none_from_dict(files), omit)) + if (files is not None and files is not omit and isinstance(files, dict)) + else None + ) + + if (request_files is None or len(request_files) == 0) and force_multipart: + request_files = FORCE_MULTIPART + + json_body, data_body = get_request_body(json=json, data=data, request_options=request_options, omit=omit) + + data_body = _maybe_filter_none_from_multipart_data(data_body, request_files, force_multipart) + + # Get headers (supports async token providers) + _headers = await self._get_headers() + + # Compute encoded params separately to avoid passing empty list to httpx + # (httpx strips existing query params from URL when params=[] is passed) + _encoded_params = encode_query( + jsonable_encoder( + remove_none_from_dict( + remove_omit_from_dict( + { + **(params if params is not None else {}), + **( + request_options.get("additional_query_parameters", {}) + if request_options is not None + else {} + ), + }, + omit=omit, + ) + ) + ) + ) + + _request_url = _build_url(base_url, path) + _request_headers = jsonable_encoder( + remove_none_from_dict( + { + **_headers, + **(headers if headers is not None else {}), + **(request_options.get("additional_headers", {}) if request_options is not None else {}), + } + ) + ) + + if self.logger.is_debug(): + self.logger.debug( + "Making streaming HTTP request", + method=method, + url=_request_url, + headers=_redact_headers(_request_headers), + ) + + async with self.httpx_client.stream( + method=method, + url=_request_url, + headers=_request_headers, + params=_encoded_params if _encoded_params else None, + json=json_body, + data=data_body, + content=content, + files=request_files, + timeout=timeout, + ) as stream: + yield stream diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/http_response.py b/seed/python-sdk/basic-auth-optional/src/seed/core/http_response.py new file mode 100644 index 000000000000..00bb1096d2d0 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/http_response.py @@ -0,0 +1,59 @@ +# This file was auto-generated by Fern from our API Definition. + +from typing import Dict, Generic, TypeVar + +import httpx + +# Generic to represent the underlying type of the data wrapped by the HTTP response. +T = TypeVar("T") + + +class BaseHttpResponse: + """Minimalist HTTP response wrapper that exposes response headers and status code.""" + + _response: httpx.Response + + def __init__(self, response: httpx.Response): + self._response = response + + @property + def headers(self) -> Dict[str, str]: + return dict(self._response.headers) + + @property + def status_code(self) -> int: + return self._response.status_code + + +class HttpResponse(Generic[T], BaseHttpResponse): + """HTTP response wrapper that exposes response headers and data.""" + + _data: T + + def __init__(self, response: httpx.Response, data: T): + super().__init__(response) + self._data = data + + @property + def data(self) -> T: + return self._data + + def close(self) -> None: + self._response.close() + + +class AsyncHttpResponse(Generic[T], BaseHttpResponse): + """HTTP response wrapper that exposes response headers and data.""" + + _data: T + + def __init__(self, response: httpx.Response, data: T): + super().__init__(response) + self._data = data + + @property + def data(self) -> T: + return self._data + + async def close(self) -> None: + await self._response.aclose() diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/__init__.py b/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/__init__.py new file mode 100644 index 000000000000..730e5a3382eb --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/__init__.py @@ -0,0 +1,42 @@ +# This file was auto-generated by Fern from our API Definition. + +# isort: skip_file + +import typing +from importlib import import_module + +if typing.TYPE_CHECKING: + from ._api import EventSource, aconnect_sse, connect_sse + from ._exceptions import SSEError + from ._models import ServerSentEvent +_dynamic_imports: typing.Dict[str, str] = { + "EventSource": "._api", + "SSEError": "._exceptions", + "ServerSentEvent": "._models", + "aconnect_sse": "._api", + "connect_sse": "._api", +} + + +def __getattr__(attr_name: str) -> typing.Any: + module_name = _dynamic_imports.get(attr_name) + if module_name is None: + raise AttributeError(f"No {attr_name} found in _dynamic_imports for module name -> {__name__}") + try: + module = import_module(module_name, __package__) + if module_name == f".{attr_name}": + return module + else: + return getattr(module, attr_name) + except ImportError as e: + raise ImportError(f"Failed to import {attr_name} from {module_name}: {e}") from e + except AttributeError as e: + raise AttributeError(f"Failed to get {attr_name} from {module_name}: {e}") from e + + +def __dir__(): + lazy_attrs = list(_dynamic_imports.keys()) + return sorted(lazy_attrs) + + +__all__ = ["EventSource", "SSEError", "ServerSentEvent", "aconnect_sse", "connect_sse"] diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_api.py b/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_api.py new file mode 100644 index 000000000000..f900b3b686de --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_api.py @@ -0,0 +1,112 @@ +# This file was auto-generated by Fern from our API Definition. + +import re +from contextlib import asynccontextmanager, contextmanager +from typing import Any, AsyncGenerator, AsyncIterator, Iterator, cast + +import httpx +from ._decoders import SSEDecoder +from ._exceptions import SSEError +from ._models import ServerSentEvent + + +class EventSource: + def __init__(self, response: httpx.Response) -> None: + self._response = response + + def _check_content_type(self) -> None: + content_type = self._response.headers.get("content-type", "").partition(";")[0] + if "text/event-stream" not in content_type: + raise SSEError( + f"Expected response header Content-Type to contain 'text/event-stream', got {content_type!r}" + ) + + def _get_charset(self) -> str: + """Extract charset from Content-Type header, fallback to UTF-8.""" + content_type = self._response.headers.get("content-type", "") + + # Parse charset parameter using regex + charset_match = re.search(r"charset=([^;\s]+)", content_type, re.IGNORECASE) + if charset_match: + charset = charset_match.group(1).strip("\"'") + # Validate that it's a known encoding + try: + # Test if the charset is valid by trying to encode/decode + "test".encode(charset).decode(charset) + return charset + except (LookupError, UnicodeError): + # If charset is invalid, fall back to UTF-8 + pass + + # Default to UTF-8 if no charset specified or invalid charset + return "utf-8" + + @property + def response(self) -> httpx.Response: + return self._response + + def iter_sse(self) -> Iterator[ServerSentEvent]: + self._check_content_type() + decoder = SSEDecoder() + charset = self._get_charset() + + buffer = "" + for chunk in self._response.iter_bytes(): + # Decode chunk using detected charset + text_chunk = chunk.decode(charset, errors="replace") + buffer += text_chunk + + # Process complete lines + while "\n" in buffer: + line, buffer = buffer.split("\n", 1) + line = line.rstrip("\r") + sse = decoder.decode(line) + # when we reach a "\n\n" => line = '' + # => decoder will attempt to return an SSE Event + if sse is not None: + yield sse + + # Process any remaining data in buffer + if buffer.strip(): + line = buffer.rstrip("\r") + sse = decoder.decode(line) + if sse is not None: + yield sse + + async def aiter_sse(self) -> AsyncGenerator[ServerSentEvent, None]: + self._check_content_type() + decoder = SSEDecoder() + lines = cast(AsyncGenerator[str, None], self._response.aiter_lines()) + try: + async for line in lines: + line = line.rstrip("\n") + sse = decoder.decode(line) + if sse is not None: + yield sse + finally: + await lines.aclose() + + +@contextmanager +def connect_sse(client: httpx.Client, method: str, url: str, **kwargs: Any) -> Iterator[EventSource]: + headers = kwargs.pop("headers", {}) + headers["Accept"] = "text/event-stream" + headers["Cache-Control"] = "no-store" + + with client.stream(method, url, headers=headers, **kwargs) as response: + yield EventSource(response) + + +@asynccontextmanager +async def aconnect_sse( + client: httpx.AsyncClient, + method: str, + url: str, + **kwargs: Any, +) -> AsyncIterator[EventSource]: + headers = kwargs.pop("headers", {}) + headers["Accept"] = "text/event-stream" + headers["Cache-Control"] = "no-store" + + async with client.stream(method, url, headers=headers, **kwargs) as response: + yield EventSource(response) diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_decoders.py b/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_decoders.py new file mode 100644 index 000000000000..339b08901381 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_decoders.py @@ -0,0 +1,61 @@ +# This file was auto-generated by Fern from our API Definition. + +from typing import List, Optional + +from ._models import ServerSentEvent + + +class SSEDecoder: + def __init__(self) -> None: + self._event = "" + self._data: List[str] = [] + self._last_event_id = "" + self._retry: Optional[int] = None + + def decode(self, line: str) -> Optional[ServerSentEvent]: + # See: https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation # noqa: E501 + + if not line: + if not self._event and not self._data and not self._last_event_id and self._retry is None: + return None + + sse = ServerSentEvent( + event=self._event, + data="\n".join(self._data), + id=self._last_event_id, + retry=self._retry, + ) + + # NOTE: as per the SSE spec, do not reset last_event_id. + self._event = "" + self._data = [] + self._retry = None + + return sse + + if line.startswith(":"): + return None + + fieldname, _, value = line.partition(":") + + if value.startswith(" "): + value = value[1:] + + if fieldname == "event": + self._event = value + elif fieldname == "data": + self._data.append(value) + elif fieldname == "id": + if "\0" in value: + pass + else: + self._last_event_id = value + elif fieldname == "retry": + try: + self._retry = int(value) + except (TypeError, ValueError): + pass + else: + pass # Field is ignored. + + return None diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_exceptions.py b/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_exceptions.py new file mode 100644 index 000000000000..81605a8a65ed --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_exceptions.py @@ -0,0 +1,7 @@ +# This file was auto-generated by Fern from our API Definition. + +import httpx + + +class SSEError(httpx.TransportError): + pass diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_models.py b/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_models.py new file mode 100644 index 000000000000..1af57f8fd0d2 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/http_sse/_models.py @@ -0,0 +1,17 @@ +# This file was auto-generated by Fern from our API Definition. + +import json +from dataclasses import dataclass +from typing import Any, Optional + + +@dataclass(frozen=True) +class ServerSentEvent: + event: str = "message" + data: str = "" + id: str = "" + retry: Optional[int] = None + + def json(self) -> Any: + """Parse the data field as JSON.""" + return json.loads(self.data) diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/jsonable_encoder.py b/seed/python-sdk/basic-auth-optional/src/seed/core/jsonable_encoder.py new file mode 100644 index 000000000000..f8beaeafb17f --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/jsonable_encoder.py @@ -0,0 +1,108 @@ +# This file was auto-generated by Fern from our API Definition. + +""" +jsonable_encoder converts a Python object to a JSON-friendly dict +(e.g. datetimes to strings, Pydantic models to dicts). + +Taken from FastAPI, and made a bit simpler +https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py +""" + +import base64 +import dataclasses +import datetime as dt +from enum import Enum +from pathlib import PurePath +from types import GeneratorType +from typing import Any, Callable, Dict, List, Optional, Set, Union + +import pydantic +from .datetime_utils import serialize_datetime +from .pydantic_utilities import ( + IS_PYDANTIC_V2, + encode_by_type, + to_jsonable_with_fallback, +) + +SetIntStr = Set[Union[int, str]] +DictIntStrAny = Dict[Union[int, str], Any] + + +def jsonable_encoder(obj: Any, custom_encoder: Optional[Dict[Any, Callable[[Any], Any]]] = None) -> Any: + custom_encoder = custom_encoder or {} + # Generated SDKs use Ellipsis (`...`) as the sentinel value for "OMIT". + # OMIT values should be excluded from serialized payloads. + if obj is Ellipsis: + return None + if custom_encoder: + if type(obj) in custom_encoder: + return custom_encoder[type(obj)](obj) + else: + for encoder_type, encoder_instance in custom_encoder.items(): + if isinstance(obj, encoder_type): + return encoder_instance(obj) + if isinstance(obj, pydantic.BaseModel): + if IS_PYDANTIC_V2: + encoder = getattr(obj.model_config, "json_encoders", {}) # type: ignore # Pydantic v2 + else: + encoder = getattr(obj.__config__, "json_encoders", {}) # type: ignore # Pydantic v1 + if custom_encoder: + encoder.update(custom_encoder) + obj_dict = obj.dict(by_alias=True) + if "__root__" in obj_dict: + obj_dict = obj_dict["__root__"] + if "root" in obj_dict: + obj_dict = obj_dict["root"] + return jsonable_encoder(obj_dict, custom_encoder=encoder) + if dataclasses.is_dataclass(obj): + obj_dict = dataclasses.asdict(obj) # type: ignore + return jsonable_encoder(obj_dict, custom_encoder=custom_encoder) + if isinstance(obj, bytes): + return base64.b64encode(obj).decode("utf-8") + if isinstance(obj, Enum): + return obj.value + if isinstance(obj, PurePath): + return str(obj) + if isinstance(obj, (str, int, float, type(None))): + return obj + if isinstance(obj, dt.datetime): + return serialize_datetime(obj) + if isinstance(obj, dt.date): + return str(obj) + if isinstance(obj, dict): + encoded_dict = {} + allowed_keys = set(obj.keys()) + for key, value in obj.items(): + if key in allowed_keys: + if value is Ellipsis: + continue + encoded_key = jsonable_encoder(key, custom_encoder=custom_encoder) + encoded_value = jsonable_encoder(value, custom_encoder=custom_encoder) + encoded_dict[encoded_key] = encoded_value + return encoded_dict + if isinstance(obj, (list, set, frozenset, GeneratorType, tuple)): + encoded_list = [] + for item in obj: + if item is Ellipsis: + continue + encoded_list.append(jsonable_encoder(item, custom_encoder=custom_encoder)) + return encoded_list + + def fallback_serializer(o: Any) -> Any: + attempt_encode = encode_by_type(o) + if attempt_encode is not None: + return attempt_encode + + try: + data = dict(o) + except Exception as e: + errors: List[Exception] = [] + errors.append(e) + try: + data = vars(o) + except Exception as e: + errors.append(e) + raise ValueError(errors) from e + return jsonable_encoder(data, custom_encoder=custom_encoder) + + return to_jsonable_with_fallback(obj, fallback_serializer) diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/logging.py b/seed/python-sdk/basic-auth-optional/src/seed/core/logging.py new file mode 100644 index 000000000000..e5e572458bc8 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/logging.py @@ -0,0 +1,107 @@ +# This file was auto-generated by Fern from our API Definition. + +import logging +import typing + +LogLevel = typing.Literal["debug", "info", "warn", "error"] + +_LOG_LEVEL_MAP: typing.Dict[LogLevel, int] = { + "debug": 1, + "info": 2, + "warn": 3, + "error": 4, +} + + +class ILogger(typing.Protocol): + def debug(self, message: str, **kwargs: typing.Any) -> None: ... + def info(self, message: str, **kwargs: typing.Any) -> None: ... + def warn(self, message: str, **kwargs: typing.Any) -> None: ... + def error(self, message: str, **kwargs: typing.Any) -> None: ... + + +class ConsoleLogger: + _logger: logging.Logger + + def __init__(self) -> None: + self._logger = logging.getLogger("fern") + if not self._logger.handlers: + handler = logging.StreamHandler() + handler.setFormatter(logging.Formatter("%(levelname)s - %(message)s")) + self._logger.addHandler(handler) + self._logger.setLevel(logging.DEBUG) + + def debug(self, message: str, **kwargs: typing.Any) -> None: + self._logger.debug(message, extra=kwargs) + + def info(self, message: str, **kwargs: typing.Any) -> None: + self._logger.info(message, extra=kwargs) + + def warn(self, message: str, **kwargs: typing.Any) -> None: + self._logger.warning(message, extra=kwargs) + + def error(self, message: str, **kwargs: typing.Any) -> None: + self._logger.error(message, extra=kwargs) + + +class LogConfig(typing.TypedDict, total=False): + level: LogLevel + logger: ILogger + silent: bool + + +class Logger: + _level: int + _logger: ILogger + _silent: bool + + def __init__(self, *, level: LogLevel, logger: ILogger, silent: bool) -> None: + self._level = _LOG_LEVEL_MAP[level] + self._logger = logger + self._silent = silent + + def _should_log(self, level: LogLevel) -> bool: + return not self._silent and self._level <= _LOG_LEVEL_MAP[level] + + def is_debug(self) -> bool: + return self._should_log("debug") + + def is_info(self) -> bool: + return self._should_log("info") + + def is_warn(self) -> bool: + return self._should_log("warn") + + def is_error(self) -> bool: + return self._should_log("error") + + def debug(self, message: str, **kwargs: typing.Any) -> None: + if self.is_debug(): + self._logger.debug(message, **kwargs) + + def info(self, message: str, **kwargs: typing.Any) -> None: + if self.is_info(): + self._logger.info(message, **kwargs) + + def warn(self, message: str, **kwargs: typing.Any) -> None: + if self.is_warn(): + self._logger.warn(message, **kwargs) + + def error(self, message: str, **kwargs: typing.Any) -> None: + if self.is_error(): + self._logger.error(message, **kwargs) + + +_default_logger: Logger = Logger(level="info", logger=ConsoleLogger(), silent=True) + + +def create_logger(config: typing.Optional[typing.Union[LogConfig, Logger]] = None) -> Logger: + if config is None: + return _default_logger + if isinstance(config, Logger): + return config + return Logger( + level=config.get("level", "info"), + logger=config.get("logger", ConsoleLogger()), + silent=config.get("silent", True), + ) diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/parse_error.py b/seed/python-sdk/basic-auth-optional/src/seed/core/parse_error.py new file mode 100644 index 000000000000..4527c6a8adec --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/parse_error.py @@ -0,0 +1,36 @@ +# This file was auto-generated by Fern from our API Definition. + +from typing import Any, Dict, Optional + + +class ParsingError(Exception): + """ + Raised when the SDK fails to parse/validate a response from the server. + This typically indicates that the server returned a response whose shape + does not match the expected schema. + """ + + headers: Optional[Dict[str, str]] + status_code: Optional[int] + body: Any + cause: Optional[Exception] + + def __init__( + self, + *, + headers: Optional[Dict[str, str]] = None, + status_code: Optional[int] = None, + body: Any = None, + cause: Optional[Exception] = None, + ) -> None: + self.headers = headers + self.status_code = status_code + self.body = body + self.cause = cause + super().__init__() + if cause is not None: + self.__cause__ = cause + + def __str__(self) -> str: + cause_str = f", cause: {self.cause}" if self.cause is not None else "" + return f"headers: {self.headers}, status_code: {self.status_code}, body: {self.body}{cause_str}" diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/pydantic_utilities.py b/seed/python-sdk/basic-auth-optional/src/seed/core/pydantic_utilities.py new file mode 100644 index 000000000000..fea3a08d3268 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/pydantic_utilities.py @@ -0,0 +1,634 @@ +# This file was auto-generated by Fern from our API Definition. + +# nopycln: file +import datetime as dt +import inspect +import json +import logging +from collections import defaultdict +from dataclasses import asdict +from typing import ( + TYPE_CHECKING, + Any, + Callable, + ClassVar, + Dict, + List, + Mapping, + Optional, + Set, + Tuple, + Type, + TypeVar, + Union, + cast, +) + +import pydantic +import typing_extensions +from pydantic.fields import FieldInfo as _FieldInfo + +_logger = logging.getLogger(__name__) + +if TYPE_CHECKING: + from .http_sse._models import ServerSentEvent + +IS_PYDANTIC_V2 = pydantic.VERSION.startswith("2.") + +if IS_PYDANTIC_V2: + _datetime_adapter = pydantic.TypeAdapter(dt.datetime) # type: ignore[attr-defined] + _date_adapter = pydantic.TypeAdapter(dt.date) # type: ignore[attr-defined] + + def parse_datetime(value: Any) -> dt.datetime: # type: ignore[misc] + if isinstance(value, dt.datetime): + return value + return _datetime_adapter.validate_python(value) + + def parse_date(value: Any) -> dt.date: # type: ignore[misc] + if isinstance(value, dt.datetime): + return value.date() + if isinstance(value, dt.date): + return value + return _date_adapter.validate_python(value) + + # Avoid importing from pydantic.v1 to maintain Python 3.14 compatibility. + from typing import get_args as get_args # type: ignore[assignment] + from typing import get_origin as get_origin # type: ignore[assignment] + + def is_literal_type(tp: Optional[Type[Any]]) -> bool: # type: ignore[misc] + return typing_extensions.get_origin(tp) is typing_extensions.Literal + + def is_union(tp: Optional[Type[Any]]) -> bool: # type: ignore[misc] + return tp is Union or typing_extensions.get_origin(tp) is Union # type: ignore[comparison-overlap] + + # Inline encoders_by_type to avoid importing from pydantic.v1.json + import re as _re + from collections import deque as _deque + from decimal import Decimal as _Decimal + from enum import Enum as _Enum + from ipaddress import ( + IPv4Address as _IPv4Address, + ) + from ipaddress import ( + IPv4Interface as _IPv4Interface, + ) + from ipaddress import ( + IPv4Network as _IPv4Network, + ) + from ipaddress import ( + IPv6Address as _IPv6Address, + ) + from ipaddress import ( + IPv6Interface as _IPv6Interface, + ) + from ipaddress import ( + IPv6Network as _IPv6Network, + ) + from pathlib import Path as _Path + from types import GeneratorType as _GeneratorType + from uuid import UUID as _UUID + + from pydantic.fields import FieldInfo as ModelField # type: ignore[no-redef, assignment] + + def _decimal_encoder(dec_value: Any) -> Any: + if dec_value.as_tuple().exponent >= 0: + return int(dec_value) + return float(dec_value) + + encoders_by_type: Dict[Type[Any], Callable[[Any], Any]] = { # type: ignore[no-redef] + bytes: lambda o: o.decode(), + dt.date: lambda o: o.isoformat(), + dt.datetime: lambda o: o.isoformat(), + dt.time: lambda o: o.isoformat(), + dt.timedelta: lambda td: td.total_seconds(), + _Decimal: _decimal_encoder, + _Enum: lambda o: o.value, + frozenset: list, + _deque: list, + _GeneratorType: list, + _IPv4Address: str, + _IPv4Interface: str, + _IPv4Network: str, + _IPv6Address: str, + _IPv6Interface: str, + _IPv6Network: str, + _Path: str, + _re.Pattern: lambda o: o.pattern, + set: list, + _UUID: str, + } +else: + from pydantic.datetime_parse import parse_date as parse_date # type: ignore[no-redef] + from pydantic.datetime_parse import parse_datetime as parse_datetime # type: ignore[no-redef] + from pydantic.fields import ModelField as ModelField # type: ignore[attr-defined, no-redef, assignment] + from pydantic.json import ENCODERS_BY_TYPE as encoders_by_type # type: ignore[no-redef] + from pydantic.typing import get_args as get_args # type: ignore[no-redef] + from pydantic.typing import get_origin as get_origin # type: ignore[no-redef] + from pydantic.typing import is_literal_type as is_literal_type # type: ignore[no-redef, assignment] + from pydantic.typing import is_union as is_union # type: ignore[no-redef] + +from .datetime_utils import serialize_datetime +from .serialization import convert_and_respect_annotation_metadata +from typing_extensions import TypeAlias + +T = TypeVar("T") +Model = TypeVar("Model", bound=pydantic.BaseModel) + + +def _get_discriminator_and_variants(type_: Type[Any]) -> Tuple[Optional[str], Optional[List[Type[Any]]]]: + """ + Extract the discriminator field name and union variants from a discriminated union type. + Supports Annotated[Union[...], Field(discriminator=...)] patterns. + Returns (discriminator, variants) or (None, None) if not a discriminated union. + """ + origin = typing_extensions.get_origin(type_) + + if origin is typing_extensions.Annotated: + args = typing_extensions.get_args(type_) + if len(args) >= 2: + inner_type = args[0] + # Check annotations for discriminator + discriminator = None + for annotation in args[1:]: + if hasattr(annotation, "discriminator"): + discriminator = getattr(annotation, "discriminator", None) + break + + if discriminator: + inner_origin = typing_extensions.get_origin(inner_type) + if inner_origin is Union: + variants = list(typing_extensions.get_args(inner_type)) + return discriminator, variants + return None, None + + +def _get_field_annotation(model: Type[Any], field_name: str) -> Optional[Type[Any]]: + """Get the type annotation of a field from a Pydantic model.""" + if IS_PYDANTIC_V2: + fields = getattr(model, "model_fields", {}) + field_info = fields.get(field_name) + if field_info: + return cast(Optional[Type[Any]], field_info.annotation) + else: + fields = getattr(model, "__fields__", {}) + field_info = fields.get(field_name) + if field_info: + return cast(Optional[Type[Any]], field_info.outer_type_) + return None + + +def _find_variant_by_discriminator( + variants: List[Type[Any]], + discriminator: str, + discriminator_value: Any, +) -> Optional[Type[Any]]: + """Find the union variant that matches the discriminator value.""" + for variant in variants: + if not (inspect.isclass(variant) and issubclass(variant, pydantic.BaseModel)): + continue + + disc_annotation = _get_field_annotation(variant, discriminator) + if disc_annotation and is_literal_type(disc_annotation): + literal_args = get_args(disc_annotation) + if literal_args and literal_args[0] == discriminator_value: + return variant + return None + + +def _is_string_type(type_: Type[Any]) -> bool: + """Check if a type is str or Optional[str].""" + if type_ is str: + return True + + origin = typing_extensions.get_origin(type_) + if origin is Union: + args = typing_extensions.get_args(type_) + # Optional[str] = Union[str, None] + non_none_args = [a for a in args if a is not type(None)] + if len(non_none_args) == 1 and non_none_args[0] is str: + return True + + return False + + +def parse_sse_obj(sse: "ServerSentEvent", type_: Type[T]) -> T: + """ + Parse a ServerSentEvent into the appropriate type. + + Handles two scenarios based on where the discriminator field is located: + + 1. Data-level discrimination: The discriminator (e.g., 'type') is inside the 'data' payload. + The union describes the data content, not the SSE envelope. + -> Returns: json.loads(data) parsed into the type + + Example: ChatStreamResponse with discriminator='type' + Input: ServerSentEvent(event="message", data='{"type": "content-delta", ...}', id="") + Output: ContentDeltaEvent (parsed from data, SSE envelope stripped) + + 2. Event-level discrimination: The discriminator (e.g., 'event') is at the SSE event level. + The union describes the full SSE event structure. + -> Returns: SSE envelope with 'data' field JSON-parsed only if the variant expects non-string + + Example: JobStreamResponse with discriminator='event' + Input: ServerSentEvent(event="ERROR", data='{"code": "FAILED", ...}', id="123") + Output: JobStreamResponse_Error with data as ErrorData object + + But for variants where data is str (like STATUS_UPDATE): + Input: ServerSentEvent(event="STATUS_UPDATE", data='{"status": "processing"}', id="1") + Output: JobStreamResponse_StatusUpdate with data as string (not parsed) + + Args: + sse: The ServerSentEvent object to parse + type_: The target discriminated union type + + Returns: + The parsed object of type T + + Note: + This function is only available in SDK contexts where http_sse module exists. + """ + sse_event = asdict(sse) + discriminator, variants = _get_discriminator_and_variants(type_) + + if discriminator is None or variants is None: + # Not a discriminated union - parse the data field as JSON + data_value = sse_event.get("data") + if isinstance(data_value, str) and data_value: + try: + parsed_data = json.loads(data_value) + return parse_obj_as(type_, parsed_data) + except json.JSONDecodeError as e: + _logger.warning( + "Failed to parse SSE data field as JSON: %s, data: %s", + e, + data_value[:100] if len(data_value) > 100 else data_value, + ) + return parse_obj_as(type_, sse_event) + + data_value = sse_event.get("data") + + # Check if discriminator is at the top level (event-level discrimination) + if discriminator in sse_event: + # Case 2: Event-level discrimination + # Find the matching variant to check if 'data' field needs JSON parsing + disc_value = sse_event.get(discriminator) + matching_variant = _find_variant_by_discriminator(variants, discriminator, disc_value) + + if matching_variant is not None: + # Check what type the variant expects for 'data' + data_type = _get_field_annotation(matching_variant, "data") + if data_type is not None and not _is_string_type(data_type): + # Variant expects non-string data - parse JSON + if isinstance(data_value, str) and data_value: + try: + parsed_data = json.loads(data_value) + new_object = dict(sse_event) + new_object["data"] = parsed_data + return parse_obj_as(type_, new_object) + except json.JSONDecodeError as e: + _logger.warning( + "Failed to parse SSE data field as JSON for event-level discrimination: %s, data: %s", + e, + data_value[:100] if len(data_value) > 100 else data_value, + ) + # Either no matching variant, data is string type, or JSON parse failed + return parse_obj_as(type_, sse_event) + + else: + # Case 1: Data-level discrimination + # The discriminator is inside the data payload - extract and parse data only + if isinstance(data_value, str) and data_value: + try: + parsed_data = json.loads(data_value) + return parse_obj_as(type_, parsed_data) + except json.JSONDecodeError as e: + _logger.warning( + "Failed to parse SSE data field as JSON for data-level discrimination: %s, data: %s", + e, + data_value[:100] if len(data_value) > 100 else data_value, + ) + return parse_obj_as(type_, sse_event) + + +def parse_obj_as(type_: Type[T], object_: Any) -> T: + # convert_and_respect_annotation_metadata is required for TypedDict aliasing. + # + # For Pydantic models, whether we should pre-dealias depends on how the model encodes aliasing: + # - If the model uses real Pydantic aliases (pydantic.Field(alias=...)), then we must pass wire keys through + # unchanged so Pydantic can validate them. + # - If the model encodes aliasing only via FieldMetadata annotations, then we MUST pre-dealias because Pydantic + # will not recognize those aliases during validation. + if inspect.isclass(type_) and issubclass(type_, pydantic.BaseModel): + has_pydantic_aliases = False + if IS_PYDANTIC_V2: + for field_name, field_info in getattr(type_, "model_fields", {}).items(): # type: ignore[attr-defined] + alias = getattr(field_info, "alias", None) + if alias is not None and alias != field_name: + has_pydantic_aliases = True + break + else: + for field in getattr(type_, "__fields__", {}).values(): + alias = getattr(field, "alias", None) + name = getattr(field, "name", None) + if alias is not None and name is not None and alias != name: + has_pydantic_aliases = True + break + + dealiased_object = ( + object_ + if has_pydantic_aliases + else convert_and_respect_annotation_metadata(object_=object_, annotation=type_, direction="read") + ) + else: + dealiased_object = convert_and_respect_annotation_metadata(object_=object_, annotation=type_, direction="read") + if IS_PYDANTIC_V2: + adapter = pydantic.TypeAdapter(type_) # type: ignore[attr-defined] + return adapter.validate_python(dealiased_object) + return pydantic.parse_obj_as(type_, dealiased_object) + + +def to_jsonable_with_fallback(obj: Any, fallback_serializer: Callable[[Any], Any]) -> Any: + if IS_PYDANTIC_V2: + from pydantic_core import to_jsonable_python + + return to_jsonable_python(obj, fallback=fallback_serializer) + return fallback_serializer(obj) + + +class UniversalBaseModel(pydantic.BaseModel): + if IS_PYDANTIC_V2: + model_config: ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict( # type: ignore[typeddict-unknown-key] + # Allow fields beginning with `model_` to be used in the model + protected_namespaces=(), + ) + + @pydantic.model_validator(mode="before") # type: ignore[attr-defined] + @classmethod + def _coerce_field_names_to_aliases(cls, data: Any) -> Any: + """ + Accept Python field names in input by rewriting them to their Pydantic aliases, + while avoiding silent collisions when a key could refer to multiple fields. + """ + if not isinstance(data, Mapping): + return data + + fields = getattr(cls, "model_fields", {}) # type: ignore[attr-defined] + name_to_alias: Dict[str, str] = {} + alias_to_name: Dict[str, str] = {} + + for name, field_info in fields.items(): + alias = getattr(field_info, "alias", None) or name + name_to_alias[name] = alias + if alias != name: + alias_to_name[alias] = name + + # Detect ambiguous keys: a key that is an alias for one field and a name for another. + ambiguous_keys = set(alias_to_name.keys()).intersection(set(name_to_alias.keys())) + for key in ambiguous_keys: + if key in data and name_to_alias[key] not in data: + raise ValueError( + f"Ambiguous input key '{key}': it is both a field name and an alias. " + "Provide the explicit alias key to disambiguate." + ) + + original_keys = set(data.keys()) + rewritten: Dict[str, Any] = dict(data) + for name, alias in name_to_alias.items(): + if alias != name and name in original_keys and alias not in rewritten: + rewritten[alias] = rewritten.pop(name) + + return rewritten + + @pydantic.model_serializer(mode="plain", when_used="json") # type: ignore[attr-defined] + def serialize_model(self) -> Any: # type: ignore[name-defined] + serialized = self.dict() # type: ignore[attr-defined] + data = {k: serialize_datetime(v) if isinstance(v, dt.datetime) else v for k, v in serialized.items()} + return data + + else: + + class Config: + smart_union = True + json_encoders = {dt.datetime: serialize_datetime} + + @pydantic.root_validator(pre=True) + def _coerce_field_names_to_aliases(cls, values: Any) -> Any: + """ + Pydantic v1 equivalent of _coerce_field_names_to_aliases. + """ + if not isinstance(values, Mapping): + return values + + fields = getattr(cls, "__fields__", {}) + name_to_alias: Dict[str, str] = {} + alias_to_name: Dict[str, str] = {} + + for name, field in fields.items(): + alias = getattr(field, "alias", None) or name + name_to_alias[name] = alias + if alias != name: + alias_to_name[alias] = name + + ambiguous_keys = set(alias_to_name.keys()).intersection(set(name_to_alias.keys())) + for key in ambiguous_keys: + if key in values and name_to_alias[key] not in values: + raise ValueError( + f"Ambiguous input key '{key}': it is both a field name and an alias. " + "Provide the explicit alias key to disambiguate." + ) + + original_keys = set(values.keys()) + rewritten: Dict[str, Any] = dict(values) + for name, alias in name_to_alias.items(): + if alias != name and name in original_keys and alias not in rewritten: + rewritten[alias] = rewritten.pop(name) + + return rewritten + + @classmethod + def model_construct(cls: Type["Model"], _fields_set: Optional[Set[str]] = None, **values: Any) -> "Model": + dealiased_object = convert_and_respect_annotation_metadata(object_=values, annotation=cls, direction="read") + return cls.construct(_fields_set, **dealiased_object) + + @classmethod + def construct(cls: Type["Model"], _fields_set: Optional[Set[str]] = None, **values: Any) -> "Model": + dealiased_object = convert_and_respect_annotation_metadata(object_=values, annotation=cls, direction="read") + if IS_PYDANTIC_V2: + return super().model_construct(_fields_set, **dealiased_object) # type: ignore[misc] + return super().construct(_fields_set, **dealiased_object) + + def json(self, **kwargs: Any) -> str: + kwargs_with_defaults = { + "by_alias": True, + "exclude_unset": True, + **kwargs, + } + if IS_PYDANTIC_V2: + return super().model_dump_json(**kwargs_with_defaults) # type: ignore[misc] + return super().json(**kwargs_with_defaults) + + def dict(self, **kwargs: Any) -> Dict[str, Any]: + """ + Override the default dict method to `exclude_unset` by default. This function patches + `exclude_unset` to work include fields within non-None default values. + """ + # Note: the logic here is multiplexed given the levers exposed in Pydantic V1 vs V2 + # Pydantic V1's .dict can be extremely slow, so we do not want to call it twice. + # + # We'd ideally do the same for Pydantic V2, but it shells out to a library to serialize models + # that we have less control over, and this is less intrusive than custom serializers for now. + if IS_PYDANTIC_V2: + kwargs_with_defaults_exclude_unset = { + **kwargs, + "by_alias": True, + "exclude_unset": True, + "exclude_none": False, + } + kwargs_with_defaults_exclude_none = { + **kwargs, + "by_alias": True, + "exclude_none": True, + "exclude_unset": False, + } + dict_dump = deep_union_pydantic_dicts( + super().model_dump(**kwargs_with_defaults_exclude_unset), # type: ignore[misc] + super().model_dump(**kwargs_with_defaults_exclude_none), # type: ignore[misc] + ) + + else: + _fields_set = self.__fields_set__.copy() + + fields = _get_model_fields(self.__class__) + for name, field in fields.items(): + if name not in _fields_set: + default = _get_field_default(field) + + # If the default values are non-null act like they've been set + # This effectively allows exclude_unset to work like exclude_none where + # the latter passes through intentionally set none values. + if default is not None or ("exclude_unset" in kwargs and not kwargs["exclude_unset"]): + _fields_set.add(name) + + if default is not None: + self.__fields_set__.add(name) + + kwargs_with_defaults_exclude_unset_include_fields = { + "by_alias": True, + "exclude_unset": True, + "include": _fields_set, + **kwargs, + } + + dict_dump = super().dict(**kwargs_with_defaults_exclude_unset_include_fields) + + return cast( + Dict[str, Any], + convert_and_respect_annotation_metadata(object_=dict_dump, annotation=self.__class__, direction="write"), + ) + + +def _union_list_of_pydantic_dicts(source: List[Any], destination: List[Any]) -> List[Any]: + converted_list: List[Any] = [] + for i, item in enumerate(source): + destination_value = destination[i] + if isinstance(item, dict): + converted_list.append(deep_union_pydantic_dicts(item, destination_value)) + elif isinstance(item, list): + converted_list.append(_union_list_of_pydantic_dicts(item, destination_value)) + else: + converted_list.append(item) + return converted_list + + +def deep_union_pydantic_dicts(source: Dict[str, Any], destination: Dict[str, Any]) -> Dict[str, Any]: + for key, value in source.items(): + node = destination.setdefault(key, {}) + if isinstance(value, dict): + deep_union_pydantic_dicts(value, node) + # Note: we do not do this same processing for sets given we do not have sets of models + # and given the sets are unordered, the processing of the set and matching objects would + # be non-trivial. + elif isinstance(value, list): + destination[key] = _union_list_of_pydantic_dicts(value, node) + else: + destination[key] = value + + return destination + + +if IS_PYDANTIC_V2: + + class V2RootModel(UniversalBaseModel, pydantic.RootModel): # type: ignore[misc, name-defined, type-arg] + pass + + UniversalRootModel: TypeAlias = V2RootModel # type: ignore[misc] +else: + UniversalRootModel: TypeAlias = UniversalBaseModel # type: ignore[misc, no-redef] + + +def encode_by_type(o: Any) -> Any: + encoders_by_class_tuples: Dict[Callable[[Any], Any], Tuple[Any, ...]] = defaultdict(tuple) + for type_, encoder in encoders_by_type.items(): + encoders_by_class_tuples[encoder] += (type_,) + + if type(o) in encoders_by_type: + return encoders_by_type[type(o)](o) + for encoder, classes_tuple in encoders_by_class_tuples.items(): + if isinstance(o, classes_tuple): + return encoder(o) + + +def update_forward_refs(model: Type["Model"], **localns: Any) -> None: + if IS_PYDANTIC_V2: + model.model_rebuild(raise_errors=False) # type: ignore[attr-defined] + else: + model.update_forward_refs(**localns) + + +# Mirrors Pydantic's internal typing +AnyCallable = Callable[..., Any] + + +def universal_root_validator( + pre: bool = False, +) -> Callable[[AnyCallable], AnyCallable]: + def decorator(func: AnyCallable) -> AnyCallable: + if IS_PYDANTIC_V2: + # In Pydantic v2, for RootModel we always use "before" mode + # The custom validators transform the input value before the model is created + return cast(AnyCallable, pydantic.model_validator(mode="before")(func)) # type: ignore[attr-defined] + return cast(AnyCallable, pydantic.root_validator(pre=pre)(func)) # type: ignore[call-overload] + + return decorator + + +def universal_field_validator(field_name: str, pre: bool = False) -> Callable[[AnyCallable], AnyCallable]: + def decorator(func: AnyCallable) -> AnyCallable: + if IS_PYDANTIC_V2: + return cast(AnyCallable, pydantic.field_validator(field_name, mode="before" if pre else "after")(func)) # type: ignore[attr-defined] + return cast(AnyCallable, pydantic.validator(field_name, pre=pre)(func)) + + return decorator + + +PydanticField = Union[ModelField, _FieldInfo] + + +def _get_model_fields(model: Type["Model"]) -> Mapping[str, PydanticField]: + if IS_PYDANTIC_V2: + return cast(Mapping[str, PydanticField], model.model_fields) # type: ignore[attr-defined] + return cast(Mapping[str, PydanticField], model.__fields__) + + +def _get_field_default(field: PydanticField) -> Any: + try: + value = field.get_default() # type: ignore[union-attr] + except: + value = field.default + if IS_PYDANTIC_V2: + from pydantic_core import PydanticUndefined + + if value == PydanticUndefined: + return None + return value + return value diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/query_encoder.py b/seed/python-sdk/basic-auth-optional/src/seed/core/query_encoder.py new file mode 100644 index 000000000000..3183001d4046 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/query_encoder.py @@ -0,0 +1,58 @@ +# This file was auto-generated by Fern from our API Definition. + +from typing import Any, Dict, List, Optional, Tuple + +import pydantic + + +# Flattens dicts to be of the form {"key[subkey][subkey2]": value} where value is not a dict +def traverse_query_dict(dict_flat: Dict[str, Any], key_prefix: Optional[str] = None) -> List[Tuple[str, Any]]: + result = [] + for k, v in dict_flat.items(): + key = f"{key_prefix}[{k}]" if key_prefix is not None else k + if isinstance(v, dict): + result.extend(traverse_query_dict(v, key)) + elif isinstance(v, list): + for arr_v in v: + if isinstance(arr_v, dict): + result.extend(traverse_query_dict(arr_v, key)) + else: + result.append((key, arr_v)) + else: + result.append((key, v)) + return result + + +def single_query_encoder(query_key: str, query_value: Any) -> List[Tuple[str, Any]]: + if isinstance(query_value, pydantic.BaseModel) or isinstance(query_value, dict): + if isinstance(query_value, pydantic.BaseModel): + obj_dict = query_value.dict(by_alias=True) + else: + obj_dict = query_value + return traverse_query_dict(obj_dict, query_key) + elif isinstance(query_value, list): + encoded_values: List[Tuple[str, Any]] = [] + for value in query_value: + if isinstance(value, pydantic.BaseModel) or isinstance(value, dict): + if isinstance(value, pydantic.BaseModel): + obj_dict = value.dict(by_alias=True) + elif isinstance(value, dict): + obj_dict = value + + encoded_values.extend(single_query_encoder(query_key, obj_dict)) + else: + encoded_values.append((query_key, value)) + + return encoded_values + + return [(query_key, query_value)] + + +def encode_query(query: Optional[Dict[str, Any]]) -> Optional[List[Tuple[str, Any]]]: + if query is None: + return None + + encoded_query = [] + for k, v in query.items(): + encoded_query.extend(single_query_encoder(k, v)) + return encoded_query diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/remove_none_from_dict.py b/seed/python-sdk/basic-auth-optional/src/seed/core/remove_none_from_dict.py new file mode 100644 index 000000000000..c2298143f14a --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/remove_none_from_dict.py @@ -0,0 +1,11 @@ +# This file was auto-generated by Fern from our API Definition. + +from typing import Any, Dict, Mapping, Optional + + +def remove_none_from_dict(original: Mapping[str, Optional[Any]]) -> Dict[str, Any]: + new: Dict[str, Any] = {} + for key, value in original.items(): + if value is not None: + new[key] = value + return new diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/request_options.py b/seed/python-sdk/basic-auth-optional/src/seed/core/request_options.py new file mode 100644 index 000000000000..1b38804432ba --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/request_options.py @@ -0,0 +1,35 @@ +# This file was auto-generated by Fern from our API Definition. + +import typing + +try: + from typing import NotRequired # type: ignore +except ImportError: + from typing_extensions import NotRequired + + +class RequestOptions(typing.TypedDict, total=False): + """ + Additional options for request-specific configuration when calling APIs via the SDK. + This is used primarily as an optional final parameter for service functions. + + Attributes: + - timeout_in_seconds: int. The number of seconds to await an API call before timing out. + + - max_retries: int. The max number of retries to attempt if the API call fails. + + - additional_headers: typing.Dict[str, typing.Any]. A dictionary containing additional parameters to spread into the request's header dict + + - additional_query_parameters: typing.Dict[str, typing.Any]. A dictionary containing additional parameters to spread into the request's query parameters dict + + - additional_body_parameters: typing.Dict[str, typing.Any]. A dictionary containing additional parameters to spread into the request's body parameters dict + + - chunk_size: int. The size, in bytes, to process each chunk of data being streamed back within the response. This equates to leveraging `chunk_size` within `requests` or `httpx`, and is only leveraged for file downloads. + """ + + timeout_in_seconds: NotRequired[int] + max_retries: NotRequired[int] + additional_headers: NotRequired[typing.Dict[str, typing.Any]] + additional_query_parameters: NotRequired[typing.Dict[str, typing.Any]] + additional_body_parameters: NotRequired[typing.Dict[str, typing.Any]] + chunk_size: NotRequired[int] diff --git a/seed/python-sdk/basic-auth-optional/src/seed/core/serialization.py b/seed/python-sdk/basic-auth-optional/src/seed/core/serialization.py new file mode 100644 index 000000000000..c36e865cc729 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/core/serialization.py @@ -0,0 +1,276 @@ +# This file was auto-generated by Fern from our API Definition. + +import collections +import inspect +import typing + +import pydantic +import typing_extensions + + +class FieldMetadata: + """ + Metadata class used to annotate fields to provide additional information. + + Example: + class MyDict(TypedDict): + field: typing.Annotated[str, FieldMetadata(alias="field_name")] + + Will serialize: `{"field": "value"}` + To: `{"field_name": "value"}` + """ + + alias: str + + def __init__(self, *, alias: str) -> None: + self.alias = alias + + +def convert_and_respect_annotation_metadata( + *, + object_: typing.Any, + annotation: typing.Any, + inner_type: typing.Optional[typing.Any] = None, + direction: typing.Literal["read", "write"], +) -> typing.Any: + """ + Respect the metadata annotations on a field, such as aliasing. This function effectively + manipulates the dict-form of an object to respect the metadata annotations. This is primarily used for + TypedDicts, which cannot support aliasing out of the box, and can be extended for additional + utilities, such as defaults. + + Parameters + ---------- + object_ : typing.Any + + annotation : type + The type we're looking to apply typing annotations from + + inner_type : typing.Optional[type] + + Returns + ------- + typing.Any + """ + + if object_ is None: + return None + if inner_type is None: + inner_type = annotation + + clean_type = _remove_annotations(inner_type) + # Pydantic models + if ( + inspect.isclass(clean_type) + and issubclass(clean_type, pydantic.BaseModel) + and isinstance(object_, typing.Mapping) + ): + return _convert_mapping(object_, clean_type, direction) + # TypedDicts + if typing_extensions.is_typeddict(clean_type) and isinstance(object_, typing.Mapping): + return _convert_mapping(object_, clean_type, direction) + + if ( + typing_extensions.get_origin(clean_type) == typing.Dict + or typing_extensions.get_origin(clean_type) == dict + or clean_type == typing.Dict + ) and isinstance(object_, typing.Dict): + key_type = typing_extensions.get_args(clean_type)[0] + value_type = typing_extensions.get_args(clean_type)[1] + + return { + key: convert_and_respect_annotation_metadata( + object_=value, + annotation=annotation, + inner_type=value_type, + direction=direction, + ) + for key, value in object_.items() + } + + # If you're iterating on a string, do not bother to coerce it to a sequence. + if not isinstance(object_, str): + if ( + typing_extensions.get_origin(clean_type) == typing.Set + or typing_extensions.get_origin(clean_type) == set + or clean_type == typing.Set + ) and isinstance(object_, typing.Set): + inner_type = typing_extensions.get_args(clean_type)[0] + return { + convert_and_respect_annotation_metadata( + object_=item, + annotation=annotation, + inner_type=inner_type, + direction=direction, + ) + for item in object_ + } + elif ( + ( + typing_extensions.get_origin(clean_type) == typing.List + or typing_extensions.get_origin(clean_type) == list + or clean_type == typing.List + ) + and isinstance(object_, typing.List) + ) or ( + ( + typing_extensions.get_origin(clean_type) == typing.Sequence + or typing_extensions.get_origin(clean_type) == collections.abc.Sequence + or clean_type == typing.Sequence + ) + and isinstance(object_, typing.Sequence) + ): + inner_type = typing_extensions.get_args(clean_type)[0] + return [ + convert_and_respect_annotation_metadata( + object_=item, + annotation=annotation, + inner_type=inner_type, + direction=direction, + ) + for item in object_ + ] + + if typing_extensions.get_origin(clean_type) == typing.Union: + # We should be able to ~relatively~ safely try to convert keys against all + # member types in the union, the edge case here is if one member aliases a field + # of the same name to a different name from another member + # Or if another member aliases a field of the same name that another member does not. + for member in typing_extensions.get_args(clean_type): + object_ = convert_and_respect_annotation_metadata( + object_=object_, + annotation=annotation, + inner_type=member, + direction=direction, + ) + return object_ + + annotated_type = _get_annotation(annotation) + if annotated_type is None: + return object_ + + # If the object is not a TypedDict, a Union, or other container (list, set, sequence, etc.) + # Then we can safely call it on the recursive conversion. + return object_ + + +def _convert_mapping( + object_: typing.Mapping[str, object], + expected_type: typing.Any, + direction: typing.Literal["read", "write"], +) -> typing.Mapping[str, object]: + converted_object: typing.Dict[str, object] = {} + try: + annotations = typing_extensions.get_type_hints(expected_type, include_extras=True) + except NameError: + # The TypedDict contains a circular reference, so + # we use the __annotations__ attribute directly. + annotations = getattr(expected_type, "__annotations__", {}) + aliases_to_field_names = _get_alias_to_field_name(annotations) + for key, value in object_.items(): + if direction == "read" and key in aliases_to_field_names: + dealiased_key = aliases_to_field_names.get(key) + if dealiased_key is not None: + type_ = annotations.get(dealiased_key) + else: + type_ = annotations.get(key) + # Note you can't get the annotation by the field name if you're in read mode, so you must check the aliases map + # + # So this is effectively saying if we're in write mode, and we don't have a type, or if we're in read mode and we don't have an alias + # then we can just pass the value through as is + if type_ is None: + converted_object[key] = value + elif direction == "read" and key not in aliases_to_field_names: + converted_object[key] = convert_and_respect_annotation_metadata( + object_=value, annotation=type_, direction=direction + ) + else: + converted_object[_alias_key(key, type_, direction, aliases_to_field_names)] = ( + convert_and_respect_annotation_metadata(object_=value, annotation=type_, direction=direction) + ) + return converted_object + + +def _get_annotation(type_: typing.Any) -> typing.Optional[typing.Any]: + maybe_annotated_type = typing_extensions.get_origin(type_) + if maybe_annotated_type is None: + return None + + if maybe_annotated_type == typing_extensions.NotRequired: + type_ = typing_extensions.get_args(type_)[0] + maybe_annotated_type = typing_extensions.get_origin(type_) + + if maybe_annotated_type == typing_extensions.Annotated: + return type_ + + return None + + +def _remove_annotations(type_: typing.Any) -> typing.Any: + maybe_annotated_type = typing_extensions.get_origin(type_) + if maybe_annotated_type is None: + return type_ + + if maybe_annotated_type == typing_extensions.NotRequired: + return _remove_annotations(typing_extensions.get_args(type_)[0]) + + if maybe_annotated_type == typing_extensions.Annotated: + return _remove_annotations(typing_extensions.get_args(type_)[0]) + + return type_ + + +def get_alias_to_field_mapping(type_: typing.Any) -> typing.Dict[str, str]: + annotations = typing_extensions.get_type_hints(type_, include_extras=True) + return _get_alias_to_field_name(annotations) + + +def get_field_to_alias_mapping(type_: typing.Any) -> typing.Dict[str, str]: + annotations = typing_extensions.get_type_hints(type_, include_extras=True) + return _get_field_to_alias_name(annotations) + + +def _get_alias_to_field_name( + field_to_hint: typing.Dict[str, typing.Any], +) -> typing.Dict[str, str]: + aliases = {} + for field, hint in field_to_hint.items(): + maybe_alias = _get_alias_from_type(hint) + if maybe_alias is not None: + aliases[maybe_alias] = field + return aliases + + +def _get_field_to_alias_name( + field_to_hint: typing.Dict[str, typing.Any], +) -> typing.Dict[str, str]: + aliases = {} + for field, hint in field_to_hint.items(): + maybe_alias = _get_alias_from_type(hint) + if maybe_alias is not None: + aliases[field] = maybe_alias + return aliases + + +def _get_alias_from_type(type_: typing.Any) -> typing.Optional[str]: + maybe_annotated_type = _get_annotation(type_) + + if maybe_annotated_type is not None: + # The actual annotations are 1 onward, the first is the annotated type + annotations = typing_extensions.get_args(maybe_annotated_type)[1:] + + for annotation in annotations: + if isinstance(annotation, FieldMetadata) and annotation.alias is not None: + return annotation.alias + return None + + +def _alias_key( + key: str, + type_: typing.Any, + direction: typing.Literal["read", "write"], + aliases_to_field_names: typing.Dict[str, str], +) -> str: + if direction == "read": + return aliases_to_field_names.get(key, key) + return _get_alias_from_type(type_=type_) or key diff --git a/seed/python-sdk/basic-auth-optional/src/seed/errors/__init__.py b/seed/python-sdk/basic-auth-optional/src/seed/errors/__init__.py new file mode 100644 index 000000000000..cd9c162d3e99 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/errors/__init__.py @@ -0,0 +1,39 @@ +# This file was auto-generated by Fern from our API Definition. + +# isort: skip_file + +import typing +from importlib import import_module + +if typing.TYPE_CHECKING: + from .types import UnauthorizedRequestErrorBody + from .errors import BadRequest, UnauthorizedRequest +_dynamic_imports: typing.Dict[str, str] = { + "BadRequest": ".errors", + "UnauthorizedRequest": ".errors", + "UnauthorizedRequestErrorBody": ".types", +} + + +def __getattr__(attr_name: str) -> typing.Any: + module_name = _dynamic_imports.get(attr_name) + if module_name is None: + raise AttributeError(f"No {attr_name} found in _dynamic_imports for module name -> {__name__}") + try: + module = import_module(module_name, __package__) + if module_name == f".{attr_name}": + return module + else: + return getattr(module, attr_name) + except ImportError as e: + raise ImportError(f"Failed to import {attr_name} from {module_name}: {e}") from e + except AttributeError as e: + raise AttributeError(f"Failed to get {attr_name} from {module_name}: {e}") from e + + +def __dir__(): + lazy_attrs = list(_dynamic_imports.keys()) + return sorted(lazy_attrs) + + +__all__ = ["BadRequest", "UnauthorizedRequest", "UnauthorizedRequestErrorBody"] diff --git a/seed/python-sdk/basic-auth-optional/src/seed/errors/errors/__init__.py b/seed/python-sdk/basic-auth-optional/src/seed/errors/errors/__init__.py new file mode 100644 index 000000000000..786cc9a53f3b --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/errors/errors/__init__.py @@ -0,0 +1,35 @@ +# This file was auto-generated by Fern from our API Definition. + +# isort: skip_file + +import typing +from importlib import import_module + +if typing.TYPE_CHECKING: + from .bad_request import BadRequest + from .unauthorized_request import UnauthorizedRequest +_dynamic_imports: typing.Dict[str, str] = {"BadRequest": ".bad_request", "UnauthorizedRequest": ".unauthorized_request"} + + +def __getattr__(attr_name: str) -> typing.Any: + module_name = _dynamic_imports.get(attr_name) + if module_name is None: + raise AttributeError(f"No {attr_name} found in _dynamic_imports for module name -> {__name__}") + try: + module = import_module(module_name, __package__) + if module_name == f".{attr_name}": + return module + else: + return getattr(module, attr_name) + except ImportError as e: + raise ImportError(f"Failed to import {attr_name} from {module_name}: {e}") from e + except AttributeError as e: + raise AttributeError(f"Failed to get {attr_name} from {module_name}: {e}") from e + + +def __dir__(): + lazy_attrs = list(_dynamic_imports.keys()) + return sorted(lazy_attrs) + + +__all__ = ["BadRequest", "UnauthorizedRequest"] diff --git a/seed/python-sdk/basic-auth-optional/src/seed/errors/errors/bad_request.py b/seed/python-sdk/basic-auth-optional/src/seed/errors/errors/bad_request.py new file mode 100644 index 000000000000..634bebc60527 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/errors/errors/bad_request.py @@ -0,0 +1,13 @@ +# This file was auto-generated by Fern from our API Definition. + +import typing + +from ...core.api_error import ApiError + + +class BadRequest(ApiError): + def __init__(self, headers: typing.Optional[typing.Dict[str, str]] = None): + super().__init__( + status_code=400, + headers=headers, + ) diff --git a/seed/python-sdk/basic-auth-optional/src/seed/errors/errors/unauthorized_request.py b/seed/python-sdk/basic-auth-optional/src/seed/errors/errors/unauthorized_request.py new file mode 100644 index 000000000000..1ac2826faad6 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/errors/errors/unauthorized_request.py @@ -0,0 +1,11 @@ +# This file was auto-generated by Fern from our API Definition. + +import typing + +from ...core.api_error import ApiError +from ..types.unauthorized_request_error_body import UnauthorizedRequestErrorBody + + +class UnauthorizedRequest(ApiError): + def __init__(self, body: UnauthorizedRequestErrorBody, headers: typing.Optional[typing.Dict[str, str]] = None): + super().__init__(status_code=401, headers=headers, body=body) diff --git a/seed/python-sdk/basic-auth-optional/src/seed/errors/types/__init__.py b/seed/python-sdk/basic-auth-optional/src/seed/errors/types/__init__.py new file mode 100644 index 000000000000..dad37fbdca08 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/errors/types/__init__.py @@ -0,0 +1,34 @@ +# This file was auto-generated by Fern from our API Definition. + +# isort: skip_file + +import typing +from importlib import import_module + +if typing.TYPE_CHECKING: + from .unauthorized_request_error_body import UnauthorizedRequestErrorBody +_dynamic_imports: typing.Dict[str, str] = {"UnauthorizedRequestErrorBody": ".unauthorized_request_error_body"} + + +def __getattr__(attr_name: str) -> typing.Any: + module_name = _dynamic_imports.get(attr_name) + if module_name is None: + raise AttributeError(f"No {attr_name} found in _dynamic_imports for module name -> {__name__}") + try: + module = import_module(module_name, __package__) + if module_name == f".{attr_name}": + return module + else: + return getattr(module, attr_name) + except ImportError as e: + raise ImportError(f"Failed to import {attr_name} from {module_name}: {e}") from e + except AttributeError as e: + raise AttributeError(f"Failed to get {attr_name} from {module_name}: {e}") from e + + +def __dir__(): + lazy_attrs = list(_dynamic_imports.keys()) + return sorted(lazy_attrs) + + +__all__ = ["UnauthorizedRequestErrorBody"] diff --git a/seed/python-sdk/basic-auth-optional/src/seed/errors/types/unauthorized_request_error_body.py b/seed/python-sdk/basic-auth-optional/src/seed/errors/types/unauthorized_request_error_body.py new file mode 100644 index 000000000000..dea32e5165e8 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/errors/types/unauthorized_request_error_body.py @@ -0,0 +1,19 @@ +# This file was auto-generated by Fern from our API Definition. + +import typing + +import pydantic +from ...core.pydantic_utilities import IS_PYDANTIC_V2, UniversalBaseModel + + +class UnauthorizedRequestErrorBody(UniversalBaseModel): + message: str + + if IS_PYDANTIC_V2: + model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow", frozen=True) # type: ignore # Pydantic v2 + else: + + class Config: + frozen = True + smart_union = True + extra = pydantic.Extra.allow diff --git a/seed/python-sdk/basic-auth-optional/src/seed/py.typed b/seed/python-sdk/basic-auth-optional/src/seed/py.typed new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/seed/python-sdk/basic-auth-optional/src/seed/version.py b/seed/python-sdk/basic-auth-optional/src/seed/version.py new file mode 100644 index 000000000000..b8bd9635d24c --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/src/seed/version.py @@ -0,0 +1,3 @@ +from importlib import metadata + +__version__ = metadata.version("fern_basic-auth-optional") diff --git a/seed/python-sdk/basic-auth-optional/tests/custom/test_client.py b/seed/python-sdk/basic-auth-optional/tests/custom/test_client.py new file mode 100644 index 000000000000..ab04ce6393ef --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/custom/test_client.py @@ -0,0 +1,7 @@ +import pytest + + +# Get started with writing tests with pytest at https://docs.pytest.org +@pytest.mark.skip(reason="Unimplemented") +def test_client() -> None: + assert True diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/__init__.py b/seed/python-sdk/basic-auth-optional/tests/utils/__init__.py new file mode 100644 index 000000000000..f3ea2659bb1c --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/__init__.py @@ -0,0 +1,2 @@ +# This file was auto-generated by Fern from our API Definition. + diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/__init__.py b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/__init__.py new file mode 100644 index 000000000000..2cf01263529d --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/__init__.py @@ -0,0 +1,21 @@ +# This file was auto-generated by Fern from our API Definition. + +# This file was auto-generated by Fern from our API Definition. + +from .circle import CircleParams +from .object_with_defaults import ObjectWithDefaultsParams +from .object_with_optional_field import ObjectWithOptionalFieldParams +from .shape import Shape_CircleParams, Shape_SquareParams, ShapeParams +from .square import SquareParams +from .undiscriminated_shape import UndiscriminatedShapeParams + +__all__ = [ + "CircleParams", + "ObjectWithDefaultsParams", + "ObjectWithOptionalFieldParams", + "ShapeParams", + "Shape_CircleParams", + "Shape_SquareParams", + "SquareParams", + "UndiscriminatedShapeParams", +] diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/circle.py b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/circle.py new file mode 100644 index 000000000000..74ecf38c308b --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/circle.py @@ -0,0 +1,11 @@ +# This file was auto-generated by Fern from our API Definition. + +# This file was auto-generated by Fern from our API Definition. + +import typing_extensions + +from seed.core.serialization import FieldMetadata + + +class CircleParams(typing_extensions.TypedDict): + radius_measurement: typing_extensions.Annotated[float, FieldMetadata(alias="radiusMeasurement")] diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/color.py b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/color.py new file mode 100644 index 000000000000..2aa2c4c52f0c --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/color.py @@ -0,0 +1,7 @@ +# This file was auto-generated by Fern from our API Definition. + +# This file was auto-generated by Fern from our API Definition. + +import typing + +Color = typing.Union[typing.Literal["red", "blue"], typing.Any] diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/object_with_defaults.py b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/object_with_defaults.py new file mode 100644 index 000000000000..a977b1d2aa1c --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/object_with_defaults.py @@ -0,0 +1,15 @@ +# This file was auto-generated by Fern from our API Definition. + +# This file was auto-generated by Fern from our API Definition. + +import typing_extensions + + +class ObjectWithDefaultsParams(typing_extensions.TypedDict): + """ + Defines properties with default values and validation rules. + """ + + decimal: typing_extensions.NotRequired[float] + string: typing_extensions.NotRequired[str] + required_string: str diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/object_with_optional_field.py b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/object_with_optional_field.py new file mode 100644 index 000000000000..6b5608bc05b6 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/object_with_optional_field.py @@ -0,0 +1,35 @@ +# This file was auto-generated by Fern from our API Definition. + +# This file was auto-generated by Fern from our API Definition. + +import datetime as dt +import typing +import uuid + +import typing_extensions +from .color import Color +from .shape import ShapeParams +from .undiscriminated_shape import UndiscriminatedShapeParams + +from seed.core.serialization import FieldMetadata + + +class ObjectWithOptionalFieldParams(typing_extensions.TypedDict): + literal: typing.Literal["lit_one"] + string: typing_extensions.NotRequired[str] + integer: typing_extensions.NotRequired[int] + long_: typing_extensions.NotRequired[typing_extensions.Annotated[int, FieldMetadata(alias="long")]] + double: typing_extensions.NotRequired[float] + bool_: typing_extensions.NotRequired[typing_extensions.Annotated[bool, FieldMetadata(alias="bool")]] + datetime: typing_extensions.NotRequired[dt.datetime] + date: typing_extensions.NotRequired[dt.date] + uuid_: typing_extensions.NotRequired[typing_extensions.Annotated[uuid.UUID, FieldMetadata(alias="uuid")]] + base_64: typing_extensions.NotRequired[typing_extensions.Annotated[str, FieldMetadata(alias="base64")]] + list_: typing_extensions.NotRequired[typing_extensions.Annotated[typing.Sequence[str], FieldMetadata(alias="list")]] + set_: typing_extensions.NotRequired[typing_extensions.Annotated[typing.Set[str], FieldMetadata(alias="set")]] + map_: typing_extensions.NotRequired[typing_extensions.Annotated[typing.Dict[int, str], FieldMetadata(alias="map")]] + enum: typing_extensions.NotRequired[Color] + union: typing_extensions.NotRequired[ShapeParams] + second_union: typing_extensions.NotRequired[ShapeParams] + undiscriminated_union: typing_extensions.NotRequired[UndiscriminatedShapeParams] + any: typing.Optional[typing.Any] diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/shape.py b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/shape.py new file mode 100644 index 000000000000..7e70010a251f --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/shape.py @@ -0,0 +1,28 @@ +# This file was auto-generated by Fern from our API Definition. + +# This file was auto-generated by Fern from our API Definition. + +from __future__ import annotations + +import typing + +import typing_extensions + +from seed.core.serialization import FieldMetadata + + +class Base(typing_extensions.TypedDict): + id: str + + +class Shape_CircleParams(Base): + shape_type: typing_extensions.Annotated[typing.Literal["circle"], FieldMetadata(alias="shapeType")] + radius_measurement: typing_extensions.Annotated[float, FieldMetadata(alias="radiusMeasurement")] + + +class Shape_SquareParams(Base): + shape_type: typing_extensions.Annotated[typing.Literal["square"], FieldMetadata(alias="shapeType")] + length_measurement: typing_extensions.Annotated[float, FieldMetadata(alias="lengthMeasurement")] + + +ShapeParams = typing.Union[Shape_CircleParams, Shape_SquareParams] diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/square.py b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/square.py new file mode 100644 index 000000000000..71c7d25fd4ad --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/square.py @@ -0,0 +1,11 @@ +# This file was auto-generated by Fern from our API Definition. + +# This file was auto-generated by Fern from our API Definition. + +import typing_extensions + +from seed.core.serialization import FieldMetadata + + +class SquareParams(typing_extensions.TypedDict): + length_measurement: typing_extensions.Annotated[float, FieldMetadata(alias="lengthMeasurement")] diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/undiscriminated_shape.py b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/undiscriminated_shape.py new file mode 100644 index 000000000000..99f12b300d1d --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/assets/models/undiscriminated_shape.py @@ -0,0 +1,10 @@ +# This file was auto-generated by Fern from our API Definition. + +# This file was auto-generated by Fern from our API Definition. + +import typing + +from .circle import CircleParams +from .square import SquareParams + +UndiscriminatedShapeParams = typing.Union[CircleParams, SquareParams] diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/test_http_client.py b/seed/python-sdk/basic-auth-optional/tests/utils/test_http_client.py new file mode 100644 index 000000000000..aa2a8b4e4700 --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/test_http_client.py @@ -0,0 +1,662 @@ +# This file was auto-generated by Fern from our API Definition. + +from typing import Any, Dict +from unittest.mock import AsyncMock, MagicMock, patch + +import httpx +import pytest + +from seed.core.http_client import ( + AsyncHttpClient, + HttpClient, + _build_url, + get_request_body, + remove_none_from_dict, +) +from seed.core.request_options import RequestOptions + + +# Stub clients for testing HttpClient and AsyncHttpClient +class _DummySyncClient: + """A minimal stub for httpx.Client that records request arguments.""" + + def __init__(self) -> None: + self.last_request_kwargs: Dict[str, Any] = {} + + def request(self, **kwargs: Any) -> "_DummyResponse": + self.last_request_kwargs = kwargs + return _DummyResponse() + + +class _DummyAsyncClient: + """A minimal stub for httpx.AsyncClient that records request arguments.""" + + def __init__(self) -> None: + self.last_request_kwargs: Dict[str, Any] = {} + + async def request(self, **kwargs: Any) -> "_DummyResponse": + self.last_request_kwargs = kwargs + return _DummyResponse() + + +class _DummyResponse: + """A minimal stub for httpx.Response.""" + + status_code = 200 + headers: Dict[str, str] = {} + + +def get_request_options() -> RequestOptions: + return {"additional_body_parameters": {"see you": "later"}} + + +def get_request_options_with_none() -> RequestOptions: + return {"additional_body_parameters": {"see you": "later", "optional": None}} + + +def test_get_json_request_body() -> None: + json_body, data_body = get_request_body(json={"hello": "world"}, data=None, request_options=None, omit=None) + assert json_body == {"hello": "world"} + assert data_body is None + + json_body_extras, data_body_extras = get_request_body( + json={"goodbye": "world"}, data=None, request_options=get_request_options(), omit=None + ) + + assert json_body_extras == {"goodbye": "world", "see you": "later"} + assert data_body_extras is None + + +def test_get_files_request_body() -> None: + json_body, data_body = get_request_body(json=None, data={"hello": "world"}, request_options=None, omit=None) + assert data_body == {"hello": "world"} + assert json_body is None + + json_body_extras, data_body_extras = get_request_body( + json=None, data={"goodbye": "world"}, request_options=get_request_options(), omit=None + ) + + assert data_body_extras == {"goodbye": "world", "see you": "later"} + assert json_body_extras is None + + +def test_get_none_request_body() -> None: + json_body, data_body = get_request_body(json=None, data=None, request_options=None, omit=None) + assert data_body is None + assert json_body is None + + json_body_extras, data_body_extras = get_request_body( + json=None, data=None, request_options=get_request_options(), omit=None + ) + + assert json_body_extras == {"see you": "later"} + assert data_body_extras is None + + +def test_get_empty_json_request_body() -> None: + """Test that implicit empty bodies (json=None) are collapsed to None.""" + unrelated_request_options: RequestOptions = {"max_retries": 3} + json_body, data_body = get_request_body(json=None, data=None, request_options=unrelated_request_options, omit=None) + assert json_body is None + assert data_body is None + + +def test_explicit_empty_json_body_is_preserved() -> None: + """Test that explicit empty bodies (json={}) are preserved and sent as {}. + + This is important for endpoints where the request body is required but all + fields are optional. The server expects valid JSON ({}) not an empty body. + """ + unrelated_request_options: RequestOptions = {"max_retries": 3} + + # Explicit json={} should be preserved + json_body, data_body = get_request_body(json={}, data=None, request_options=unrelated_request_options, omit=None) + assert json_body == {} + assert data_body is None + + # Explicit data={} should also be preserved + json_body2, data_body2 = get_request_body(json=None, data={}, request_options=unrelated_request_options, omit=None) + assert json_body2 is None + assert data_body2 == {} + + +def test_json_body_preserves_none_values() -> None: + """Test that JSON bodies preserve None values (they become JSON null).""" + json_body, data_body = get_request_body( + json={"hello": "world", "optional": None}, data=None, request_options=None, omit=None + ) + # JSON bodies should preserve None values + assert json_body == {"hello": "world", "optional": None} + assert data_body is None + + +def test_data_body_preserves_none_values_without_multipart() -> None: + """Test that data bodies preserve None values when not using multipart. + + The filtering of None values happens in HttpClient.request/stream methods, + not in get_request_body. This test verifies get_request_body doesn't filter None. + """ + json_body, data_body = get_request_body( + json=None, data={"hello": "world", "optional": None}, request_options=None, omit=None + ) + # get_request_body should preserve None values in data body + # The filtering happens later in HttpClient.request when multipart is detected + assert data_body == {"hello": "world", "optional": None} + assert json_body is None + + +def test_remove_none_from_dict_filters_none_values() -> None: + """Test that remove_none_from_dict correctly filters out None values.""" + original = {"hello": "world", "optional": None, "another": "value", "also_none": None} + filtered = remove_none_from_dict(original) + assert filtered == {"hello": "world", "another": "value"} + # Original should not be modified + assert original == {"hello": "world", "optional": None, "another": "value", "also_none": None} + + +def test_remove_none_from_dict_empty_dict() -> None: + """Test that remove_none_from_dict handles empty dict.""" + assert remove_none_from_dict({}) == {} + + +def test_remove_none_from_dict_all_none() -> None: + """Test that remove_none_from_dict handles dict with all None values.""" + assert remove_none_from_dict({"a": None, "b": None}) == {} + + +def test_http_client_does_not_pass_empty_params_list() -> None: + """Test that HttpClient passes params=None when params are empty. + + This prevents httpx from stripping existing query parameters from the URL, + which happens when params=[] or params={} is passed. + """ + dummy_client = _DummySyncClient() + http_client = HttpClient( + httpx_client=dummy_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com", + ) + + # Use a path with query params (e.g., pagination cursor URL) + http_client.request( + path="resource?after=123", + method="GET", + params=None, + request_options=None, + ) + + # We care that httpx receives params=None, not [] or {} + assert "params" in dummy_client.last_request_kwargs + assert dummy_client.last_request_kwargs["params"] is None + + # Verify the query string in the URL is preserved + url = str(dummy_client.last_request_kwargs["url"]) + assert "after=123" in url, f"Expected query param 'after=123' in URL, got: {url}" + + +def test_http_client_passes_encoded_params_when_present() -> None: + """Test that HttpClient passes encoded params when params are provided.""" + dummy_client = _DummySyncClient() + http_client = HttpClient( + httpx_client=dummy_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com/resource", + ) + + http_client.request( + path="", + method="GET", + params={"after": "456"}, + request_options=None, + ) + + params = dummy_client.last_request_kwargs["params"] + # For a simple dict, encode_query should give a single (key, value) tuple + assert params == [("after", "456")] + + +@pytest.mark.asyncio +async def test_async_http_client_does_not_pass_empty_params_list() -> None: + """Test that AsyncHttpClient passes params=None when params are empty. + + This prevents httpx from stripping existing query parameters from the URL, + which happens when params=[] or params={} is passed. + """ + dummy_client = _DummyAsyncClient() + http_client = AsyncHttpClient( + httpx_client=dummy_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com", + async_base_headers=None, + ) + + # Use a path with query params (e.g., pagination cursor URL) + await http_client.request( + path="resource?after=123", + method="GET", + params=None, + request_options=None, + ) + + # We care that httpx receives params=None, not [] or {} + assert "params" in dummy_client.last_request_kwargs + assert dummy_client.last_request_kwargs["params"] is None + + # Verify the query string in the URL is preserved + url = str(dummy_client.last_request_kwargs["url"]) + assert "after=123" in url, f"Expected query param 'after=123' in URL, got: {url}" + + +@pytest.mark.asyncio +async def test_async_http_client_passes_encoded_params_when_present() -> None: + """Test that AsyncHttpClient passes encoded params when params are provided.""" + dummy_client = _DummyAsyncClient() + http_client = AsyncHttpClient( + httpx_client=dummy_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com/resource", + async_base_headers=None, + ) + + await http_client.request( + path="", + method="GET", + params={"after": "456"}, + request_options=None, + ) + + params = dummy_client.last_request_kwargs["params"] + # For a simple dict, encode_query should give a single (key, value) tuple + assert params == [("after", "456")] + + +def test_basic_url_joining() -> None: + """Test basic URL joining with a simple base URL and path.""" + result = _build_url("https://api.example.com", "/users") + assert result == "https://api.example.com/users" + + +def test_basic_url_joining_trailing_slash() -> None: + """Test basic URL joining with a simple base URL and path.""" + result = _build_url("https://api.example.com/", "/users") + assert result == "https://api.example.com/users" + + +def test_preserves_base_url_path_prefix() -> None: + """Test that path prefixes in base URL are preserved. + + This is the critical bug fix - urllib.parse.urljoin() would strip + the path prefix when the path starts with '/'. + """ + result = _build_url("https://cloud.example.com/org/tenant/api", "/users") + assert result == "https://cloud.example.com/org/tenant/api/users" + + +def test_preserves_base_url_path_prefix_trailing_slash() -> None: + """Test that path prefixes in base URL are preserved.""" + result = _build_url("https://cloud.example.com/org/tenant/api/", "/users") + assert result == "https://cloud.example.com/org/tenant/api/users" + + +# --------------------------------------------------------------------------- +# Connection error retry tests +# --------------------------------------------------------------------------- + + +def _make_sync_http_client(mock_client: Any) -> HttpClient: + return HttpClient( + httpx_client=mock_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com", + ) + + +def _make_async_http_client(mock_client: Any) -> AsyncHttpClient: + return AsyncHttpClient( + httpx_client=mock_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com", + async_base_headers=None, + ) + + +@patch("seed.core.http_client.time.sleep", return_value=None) +def test_sync_retries_on_connect_error(mock_sleep: MagicMock) -> None: + """Sync: connection error retries on httpx.ConnectError.""" + mock_client = MagicMock() + mock_client.request.side_effect = [ + httpx.ConnectError("connection failed"), + _DummyResponse(), + ] + http_client = _make_sync_http_client(mock_client) + + response = http_client.request(path="/test", method="GET") + + assert response.status_code == 200 + assert mock_client.request.call_count == 2 + mock_sleep.assert_called_once() + + +@patch("seed.core.http_client.time.sleep", return_value=None) +def test_sync_retries_on_remote_protocol_error(mock_sleep: MagicMock) -> None: + """Sync: connection error retries on httpx.RemoteProtocolError.""" + mock_client = MagicMock() + mock_client.request.side_effect = [ + httpx.RemoteProtocolError("Remote end closed connection without response"), + _DummyResponse(), + ] + http_client = _make_sync_http_client(mock_client) + + response = http_client.request(path="/test", method="GET") + + assert response.status_code == 200 + assert mock_client.request.call_count == 2 + mock_sleep.assert_called_once() + + +@patch("seed.core.http_client.time.sleep", return_value=None) +def test_sync_connection_error_exhausts_retries(mock_sleep: MagicMock) -> None: + """Sync: connection error exhausts retries then raises.""" + mock_client = MagicMock() + mock_client.request.side_effect = httpx.ConnectError("connection failed") + http_client = _make_sync_http_client(mock_client) + + with pytest.raises(httpx.ConnectError): + http_client.request( + path="/test", + method="GET", + request_options={"max_retries": 2}, + ) + + # 1 initial + 2 retries = 3 total attempts + assert mock_client.request.call_count == 3 + assert mock_sleep.call_count == 2 + + +@patch("seed.core.http_client.time.sleep", return_value=None) +def test_sync_connection_error_respects_max_retries_zero(mock_sleep: MagicMock) -> None: + """Sync: connection error respects max_retries=0.""" + mock_client = MagicMock() + mock_client.request.side_effect = httpx.ConnectError("connection failed") + http_client = _make_sync_http_client(mock_client) + + with pytest.raises(httpx.ConnectError): + http_client.request( + path="/test", + method="GET", + request_options={"max_retries": 0}, + ) + + # No retries, just the initial attempt + assert mock_client.request.call_count == 1 + mock_sleep.assert_not_called() + + +@pytest.mark.asyncio +@patch("seed.core.http_client.asyncio.sleep", new_callable=AsyncMock) +async def test_async_retries_on_connect_error(mock_sleep: AsyncMock) -> None: + """Async: connection error retries on httpx.ConnectError.""" + mock_client = MagicMock() + mock_client.request = AsyncMock( + side_effect=[ + httpx.ConnectError("connection failed"), + _DummyResponse(), + ] + ) + http_client = _make_async_http_client(mock_client) + + response = await http_client.request(path="/test", method="GET") + + assert response.status_code == 200 + assert mock_client.request.call_count == 2 + mock_sleep.assert_called_once() + + +@pytest.mark.asyncio +@patch("seed.core.http_client.asyncio.sleep", new_callable=AsyncMock) +async def test_async_retries_on_remote_protocol_error(mock_sleep: AsyncMock) -> None: + """Async: connection error retries on httpx.RemoteProtocolError.""" + mock_client = MagicMock() + mock_client.request = AsyncMock( + side_effect=[ + httpx.RemoteProtocolError("Remote end closed connection without response"), + _DummyResponse(), + ] + ) + http_client = _make_async_http_client(mock_client) + + response = await http_client.request(path="/test", method="GET") + + assert response.status_code == 200 + assert mock_client.request.call_count == 2 + mock_sleep.assert_called_once() + + +@pytest.mark.asyncio +@patch("seed.core.http_client.asyncio.sleep", new_callable=AsyncMock) +async def test_async_connection_error_exhausts_retries(mock_sleep: AsyncMock) -> None: + """Async: connection error exhausts retries then raises.""" + mock_client = MagicMock() + mock_client.request = AsyncMock(side_effect=httpx.ConnectError("connection failed")) + http_client = _make_async_http_client(mock_client) + + with pytest.raises(httpx.ConnectError): + await http_client.request( + path="/test", + method="GET", + request_options={"max_retries": 2}, + ) + + # 1 initial + 2 retries = 3 total attempts + assert mock_client.request.call_count == 3 + assert mock_sleep.call_count == 2 + + +# --------------------------------------------------------------------------- +# base_max_retries constructor parameter tests +# --------------------------------------------------------------------------- + + +def test_sync_http_client_default_base_max_retries() -> None: + """HttpClient defaults to base_max_retries=2.""" + http_client = HttpClient( + httpx_client=MagicMock(), # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + ) + assert http_client.base_max_retries == 2 + + +def test_async_http_client_default_base_max_retries() -> None: + """AsyncHttpClient defaults to base_max_retries=2.""" + http_client = AsyncHttpClient( + httpx_client=MagicMock(), # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + ) + assert http_client.base_max_retries == 2 + + +def test_sync_http_client_custom_base_max_retries() -> None: + """HttpClient accepts a custom base_max_retries value.""" + http_client = HttpClient( + httpx_client=MagicMock(), # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_max_retries=5, + ) + assert http_client.base_max_retries == 5 + + +def test_async_http_client_custom_base_max_retries() -> None: + """AsyncHttpClient accepts a custom base_max_retries value.""" + http_client = AsyncHttpClient( + httpx_client=MagicMock(), # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_max_retries=5, + ) + assert http_client.base_max_retries == 5 + + +@patch("seed.core.http_client.time.sleep", return_value=None) +def test_sync_base_max_retries_zero_disables_retries(mock_sleep: MagicMock) -> None: + """Sync: base_max_retries=0 disables retries when no request_options override.""" + mock_client = MagicMock() + mock_client.request.side_effect = httpx.ConnectError("connection failed") + http_client = HttpClient( + httpx_client=mock_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com", + base_max_retries=0, + ) + + with pytest.raises(httpx.ConnectError): + http_client.request(path="/test", method="GET") + + # No retries, just the initial attempt + assert mock_client.request.call_count == 1 + mock_sleep.assert_not_called() + + +@pytest.mark.asyncio +@patch("seed.core.http_client.asyncio.sleep", new_callable=AsyncMock) +async def test_async_base_max_retries_zero_disables_retries(mock_sleep: AsyncMock) -> None: + """Async: base_max_retries=0 disables retries when no request_options override.""" + mock_client = MagicMock() + mock_client.request = AsyncMock(side_effect=httpx.ConnectError("connection failed")) + http_client = AsyncHttpClient( + httpx_client=mock_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com", + base_max_retries=0, + ) + + with pytest.raises(httpx.ConnectError): + await http_client.request(path="/test", method="GET") + + # No retries, just the initial attempt + assert mock_client.request.call_count == 1 + mock_sleep.assert_not_called() + + +@patch("seed.core.http_client.time.sleep", return_value=None) +def test_sync_request_options_override_base_max_retries(mock_sleep: MagicMock) -> None: + """Sync: request_options max_retries overrides base_max_retries.""" + mock_client = MagicMock() + mock_client.request.side_effect = [ + httpx.ConnectError("connection failed"), + httpx.ConnectError("connection failed"), + _DummyResponse(), + ] + http_client = HttpClient( + httpx_client=mock_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com", + base_max_retries=0, # base says no retries + ) + + # But request_options overrides to allow 2 retries + response = http_client.request( + path="/test", + method="GET", + request_options={"max_retries": 2}, + ) + + assert response.status_code == 200 + # 1 initial + 2 retries = 3 total attempts + assert mock_client.request.call_count == 3 + + +@pytest.mark.asyncio +@patch("seed.core.http_client.asyncio.sleep", new_callable=AsyncMock) +async def test_async_request_options_override_base_max_retries(mock_sleep: AsyncMock) -> None: + """Async: request_options max_retries overrides base_max_retries.""" + mock_client = MagicMock() + mock_client.request = AsyncMock( + side_effect=[ + httpx.ConnectError("connection failed"), + httpx.ConnectError("connection failed"), + _DummyResponse(), + ] + ) + http_client = AsyncHttpClient( + httpx_client=mock_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com", + base_max_retries=0, # base says no retries + ) + + # But request_options overrides to allow 2 retries + response = await http_client.request( + path="/test", + method="GET", + request_options={"max_retries": 2}, + ) + + assert response.status_code == 200 + # 1 initial + 2 retries = 3 total attempts + assert mock_client.request.call_count == 3 + + +@patch("seed.core.http_client.time.sleep", return_value=None) +def test_sync_base_max_retries_used_as_default(mock_sleep: MagicMock) -> None: + """Sync: base_max_retries is used when request_options has no max_retries.""" + mock_client = MagicMock() + mock_client.request.side_effect = [ + httpx.ConnectError("fail"), + httpx.ConnectError("fail"), + httpx.ConnectError("fail"), + _DummyResponse(), + ] + http_client = HttpClient( + httpx_client=mock_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com", + base_max_retries=3, + ) + + response = http_client.request(path="/test", method="GET") + + assert response.status_code == 200 + # 1 initial + 3 retries = 4 total attempts + assert mock_client.request.call_count == 4 + + +@pytest.mark.asyncio +@patch("seed.core.http_client.asyncio.sleep", new_callable=AsyncMock) +async def test_async_base_max_retries_used_as_default(mock_sleep: AsyncMock) -> None: + """Async: base_max_retries is used when request_options has no max_retries.""" + mock_client = MagicMock() + mock_client.request = AsyncMock( + side_effect=[ + httpx.ConnectError("fail"), + httpx.ConnectError("fail"), + httpx.ConnectError("fail"), + _DummyResponse(), + ] + ) + http_client = AsyncHttpClient( + httpx_client=mock_client, # type: ignore[arg-type] + base_timeout=lambda: None, + base_headers=lambda: {}, + base_url=lambda: "https://example.com", + base_max_retries=3, + ) + + response = await http_client.request(path="/test", method="GET") + + assert response.status_code == 200 + # 1 initial + 3 retries = 4 total attempts + assert mock_client.request.call_count == 4 diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/test_query_encoding.py b/seed/python-sdk/basic-auth-optional/tests/utils/test_query_encoding.py new file mode 100644 index 000000000000..ef5fd7094f9b --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/test_query_encoding.py @@ -0,0 +1,36 @@ +# This file was auto-generated by Fern from our API Definition. + +from seed.core.query_encoder import encode_query + + +def test_query_encoding_deep_objects() -> None: + assert encode_query({"hello world": "hello world"}) == [("hello world", "hello world")] + assert encode_query({"hello_world": {"hello": "world"}}) == [("hello_world[hello]", "world")] + assert encode_query({"hello_world": {"hello": {"world": "today"}, "test": "this"}, "hi": "there"}) == [ + ("hello_world[hello][world]", "today"), + ("hello_world[test]", "this"), + ("hi", "there"), + ] + + +def test_query_encoding_deep_object_arrays() -> None: + assert encode_query({"objects": [{"key": "hello", "value": "world"}, {"key": "foo", "value": "bar"}]}) == [ + ("objects[key]", "hello"), + ("objects[value]", "world"), + ("objects[key]", "foo"), + ("objects[value]", "bar"), + ] + assert encode_query( + {"users": [{"name": "string", "tags": ["string"]}, {"name": "string2", "tags": ["string2", "string3"]}]} + ) == [ + ("users[name]", "string"), + ("users[tags]", "string"), + ("users[name]", "string2"), + ("users[tags]", "string2"), + ("users[tags]", "string3"), + ] + + +def test_encode_query_with_none() -> None: + encoded = encode_query(None) + assert encoded is None diff --git a/seed/python-sdk/basic-auth-optional/tests/utils/test_serialization.py b/seed/python-sdk/basic-auth-optional/tests/utils/test_serialization.py new file mode 100644 index 000000000000..b298db89c4bd --- /dev/null +++ b/seed/python-sdk/basic-auth-optional/tests/utils/test_serialization.py @@ -0,0 +1,72 @@ +# This file was auto-generated by Fern from our API Definition. + +from typing import Any, List + +from .assets.models import ObjectWithOptionalFieldParams, ShapeParams + +from seed.core.serialization import convert_and_respect_annotation_metadata + +UNION_TEST: ShapeParams = {"radius_measurement": 1.0, "shape_type": "circle", "id": "1"} +UNION_TEST_CONVERTED = {"shapeType": "circle", "radiusMeasurement": 1.0, "id": "1"} + + +def test_convert_and_respect_annotation_metadata() -> None: + data: ObjectWithOptionalFieldParams = { + "string": "string", + "long_": 12345, + "bool_": True, + "literal": "lit_one", + "any": "any", + } + converted = convert_and_respect_annotation_metadata( + object_=data, annotation=ObjectWithOptionalFieldParams, direction="write" + ) + assert converted == {"string": "string", "long": 12345, "bool": True, "literal": "lit_one", "any": "any"} + + +def test_convert_and_respect_annotation_metadata_in_list() -> None: + data: List[ObjectWithOptionalFieldParams] = [ + {"string": "string", "long_": 12345, "bool_": True, "literal": "lit_one", "any": "any"}, + {"string": "another string", "long_": 67890, "list_": [], "literal": "lit_one", "any": "any"}, + ] + converted = convert_and_respect_annotation_metadata( + object_=data, annotation=List[ObjectWithOptionalFieldParams], direction="write" + ) + + assert converted == [ + {"string": "string", "long": 12345, "bool": True, "literal": "lit_one", "any": "any"}, + {"string": "another string", "long": 67890, "list": [], "literal": "lit_one", "any": "any"}, + ] + + +def test_convert_and_respect_annotation_metadata_in_nested_object() -> None: + data: ObjectWithOptionalFieldParams = { + "string": "string", + "long_": 12345, + "union": UNION_TEST, + "literal": "lit_one", + "any": "any", + } + converted = convert_and_respect_annotation_metadata( + object_=data, annotation=ObjectWithOptionalFieldParams, direction="write" + ) + + assert converted == { + "string": "string", + "long": 12345, + "union": UNION_TEST_CONVERTED, + "literal": "lit_one", + "any": "any", + } + + +def test_convert_and_respect_annotation_metadata_in_union() -> None: + converted = convert_and_respect_annotation_metadata(object_=UNION_TEST, annotation=ShapeParams, direction="write") + + assert converted == UNION_TEST_CONVERTED + + +def test_convert_and_respect_annotation_metadata_with_empty_object() -> None: + data: Any = {} + converted = convert_and_respect_annotation_metadata(object_=data, annotation=ShapeParams, direction="write") + assert converted == data diff --git a/seed/ruby-sdk-v2/basic-auth-optional/.fern/metadata.json b/seed/ruby-sdk-v2/basic-auth-optional/.fern/metadata.json new file mode 100644 index 000000000000..885f09fc37f2 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/.fern/metadata.json @@ -0,0 +1,7 @@ +{ + "cliVersion": "DUMMY", + "generatorName": "fernapi/fern-ruby-sdk-v2", + "generatorVersion": "latest", + "originGitCommit": "DUMMY", + "sdkVersion": "0.0.1" +} \ No newline at end of file diff --git a/seed/ruby-sdk-v2/basic-auth-optional/.github/workflows/ci.yml b/seed/ruby-sdk-v2/basic-auth-optional/.github/workflows/ci.yml new file mode 100644 index 000000000000..72178ea4c8f1 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/.github/workflows/ci.yml @@ -0,0 +1,75 @@ +name: ci + +on: [push, pull_request] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: "3.3" + + - name: Install dependencies + run: bundle install + + - name: Run Rubocop + run: bundle exec rubocop + + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: "3.3" + + - name: Install dependencies + run: bundle install + + - name: Run Tests + run: bundle exec rake test + + publish: + name: Publish to RubyGems.org + runs-on: ubuntu-latest + needs: [lint, test] + if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') + + permissions: + id-token: write + contents: write + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: "3.3" + + - name: Install dependencies + run: bundle install + + - name: Configure RubyGems credentials + uses: rubygems/configure-rubygems-credentials@v1.0.0 + + - name: Build gem + run: bundle exec rake build + + - name: Push gem to RubyGems + run: gem push pkg/*.gem diff --git a/seed/ruby-sdk-v2/basic-auth-optional/.gitignore b/seed/ruby-sdk-v2/basic-auth-optional/.gitignore new file mode 100644 index 000000000000..c111b331371a --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/.gitignore @@ -0,0 +1 @@ +*.gem diff --git a/seed/ruby-sdk-v2/basic-auth-optional/.rubocop.yml b/seed/ruby-sdk-v2/basic-auth-optional/.rubocop.yml new file mode 100644 index 000000000000..75d8f836f2f0 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/.rubocop.yml @@ -0,0 +1,69 @@ +plugins: + - rubocop-minitest + +AllCops: + TargetRubyVersion: 3.3 + NewCops: enable + +Style/StringLiterals: + EnforcedStyle: double_quotes + +Style/StringLiteralsInInterpolation: + EnforcedStyle: double_quotes + +Style/AccessModifierDeclarations: + Enabled: false + +Lint/ConstantDefinitionInBlock: + Enabled: false + +Metrics/AbcSize: + Enabled: false + +Metrics/BlockLength: + Enabled: false + +Metrics/ClassLength: + Enabled: false + +Metrics/MethodLength: + Enabled: false + +Metrics/ParameterLists: + Enabled: false + +Metrics/PerceivedComplexity: + Enabled: false + +Metrics/CyclomaticComplexity: + Enabled: false + +Metrics/ModuleLength: + Enabled: false + +Layout/LineLength: + Enabled: false + +Naming/VariableNumber: + EnforcedStyle: normalcase + +Style/Documentation: + Enabled: false + +Style/Lambda: + EnforcedStyle: literal + +Minitest/MultipleAssertions: + Enabled: false + +Minitest/UselessAssertion: + Enabled: false + +# Dynamic snippets are code samples for documentation, not standalone Ruby files. +Style/FrozenStringLiteralComment: + Exclude: + - "dynamic-snippets/**/*" + +Layout/FirstHashElementIndentation: + Exclude: + - "dynamic-snippets/**/*" diff --git a/seed/ruby-sdk-v2/basic-auth-optional/Gemfile b/seed/ruby-sdk-v2/basic-auth-optional/Gemfile new file mode 100644 index 000000000000..29b144d77f48 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/Gemfile @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gemspec + +group :test, :development do + gem "rake", "~> 13.0" + + gem "minitest", "~> 5.16" + gem "minitest-rg" + + gem "rubocop", "~> 1.21" + gem "rubocop-minitest" + + gem "pry" + + gem "webmock" +end + +# Load custom Gemfile configuration if it exists +custom_gemfile = File.join(__dir__, "Gemfile.custom") +eval_gemfile(custom_gemfile) if File.exist?(custom_gemfile) diff --git a/seed/ruby-sdk-v2/basic-auth-optional/Gemfile.custom b/seed/ruby-sdk-v2/basic-auth-optional/Gemfile.custom new file mode 100644 index 000000000000..11bdfaf13f2d --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/Gemfile.custom @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Custom Gemfile configuration file +# This file is automatically loaded by the main Gemfile. You can add custom gems, +# groups, or other Gemfile configurations here. If you do make changes to this file, +# you will need to add it to the .fernignore file to prevent your changes from being +# overwritten by the generator. + +# Example usage: +# group :test, :development do +# gem 'custom-gem', '~> 2.0' +# end + +# Add your custom gem dependencies here \ No newline at end of file diff --git a/seed/ruby-sdk-v2/basic-auth-optional/README.md b/seed/ruby-sdk-v2/basic-auth-optional/README.md new file mode 100644 index 000000000000..39dcc2ae12d7 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/README.md @@ -0,0 +1,157 @@ +# Seed Ruby Library + +[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=Seed%2FRuby) + +The Seed Ruby library provides convenient access to the Seed APIs from Ruby. + +## Table of Contents + +- [Reference](#reference) +- [Usage](#usage) +- [Environments](#environments) +- [Errors](#errors) +- [Advanced](#advanced) + - [Retries](#retries) + - [Timeouts](#timeouts) + - [Additional Headers](#additional-headers) + - [Additional Query Parameters](#additional-query-parameters) +- [Contributing](#contributing) + +## Reference + +A full reference for this library is available [here](./reference.md). + +## Usage + +Instantiate and use the client with the following: + +```ruby +require "seed" + +client = Seed::Client.new( + username: "", + password: "" +) + +client.basic_auth.post_with_basic_auth +``` + +## Environments + +This SDK allows you to configure different custom URLs for API requests. You can specify your own custom URL. + +### Custom URL +```ruby +require "seed" + +client = Seed::Client.new( + base_url: "https://example.com" +) +``` + +## Errors + +Failed API calls will raise errors that can be rescued from granularly. + +```ruby +require "seed" + +client = Seed::Client.new( + base_url: "https://example.com" +) + +begin + result = client.basic_auth.post_with_basic_auth +rescue Seed::Errors::TimeoutError + puts "API didn't respond before our timeout elapsed" +rescue Seed::Errors::ServiceUnavailableError + puts "API returned status 503, is probably overloaded, try again later" +rescue Seed::Errors::ServerError + puts "API returned some other 5xx status, this is probably a bug" +rescue Seed::Errors::ResponseError => e + puts "API returned an unexpected status other than 5xx: #{e.code} #{e.message}" +rescue Seed::Errors::ApiError => e + puts "Some other error occurred when calling the API: #{e.message}" +end +``` + +## Advanced + +### Retries + +The SDK is instrumented with automatic retries. A request will be retried as long as the request is deemed +retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2). + +A request is deemed retryable when any of the following HTTP status codes is returned: + +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) + +Use the `max_retries` option to configure this behavior. + +```ruby +require "seed" + +client = Seed::Client.new( + base_url: "https://example.com", + max_retries: 3 # Configure max retries (default is 2) +) +``` + +### Timeouts + +The SDK defaults to a 60 second timeout. Use the `timeout` option to configure this behavior. + +```ruby +require "seed" + +response = client.basic_auth.post_with_basic_auth( + ..., + timeout: 30 # 30 second timeout +) +``` + +### Additional Headers + +If you would like to send additional headers as part of the request, use the `additional_headers` request option. + +```ruby +require "seed" + +response = client.basic_auth.post_with_basic_auth( + ..., + request_options: { + additional_headers: { + "X-Custom-Header" => "custom-value" + } + } +) +``` + +### Additional Query Parameters + +If you would like to send additional query parameters as part of the request, use the `additional_query_parameters` request option. + +```ruby +require "seed" + +response = client.basic_auth.post_with_basic_auth( + ..., + request_options: { + additional_query_parameters: { + "custom_param" => "custom-value" + } + } +) +``` + +## Contributing + +While we value open-source contributions to this SDK, this library is generated programmatically. +Additions made directly to this library would have to be moved over to our generation code, +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening +an issue first to discuss with us! + +On the other hand, contributions to the README are always very welcome! diff --git a/seed/ruby-sdk-v2/basic-auth-optional/Rakefile b/seed/ruby-sdk-v2/basic-auth-optional/Rakefile new file mode 100644 index 000000000000..9bdd4a6ce80b --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/Rakefile @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "bundler/gem_tasks" +require "minitest/test_task" + +Minitest::TestTask.create + +require "rubocop/rake_task" + +RuboCop::RakeTask.new + +task default: %i[test] + +task lint: %i[rubocop] + +# Run only the custom test file +Minitest::TestTask.create(:customtest) do |t| + t.libs << "test" + t.test_globs = ["test/custom.test.rb"] +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/custom.gemspec.rb b/seed/ruby-sdk-v2/basic-auth-optional/custom.gemspec.rb new file mode 100644 index 000000000000..86d8efd3cd3c --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/custom.gemspec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# Custom gemspec configuration file +# This file is automatically loaded by the main gemspec file. The 'spec' variable is available +# in this context from the main gemspec file. You can modify this file to add custom metadata, +# dependencies, or other gemspec configurations. If you do make changes to this file, you will +# need to add it to the .fernignore file to prevent your changes from being overwritten. + +def add_custom_gemspec_data(spec) + # Example custom configurations (uncomment and modify as needed) + + # spec.authors = ["Your name"] + # spec.email = ["your.email@example.com"] + # spec.homepage = "https://github.com/your-org/seed-ruby" + # spec.license = "Your license" +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example0/snippet.rb b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example0/snippet.rb new file mode 100644 index 000000000000..2d4035b00456 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example0/snippet.rb @@ -0,0 +1,9 @@ +require "seed" + +client = Seed::Client.new( + username: "", + password: "", + base_url: "https://api.fern.com" +) + +client.basic_auth.get_with_basic_auth diff --git a/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example1/snippet.rb b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example1/snippet.rb new file mode 100644 index 000000000000..2d4035b00456 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example1/snippet.rb @@ -0,0 +1,9 @@ +require "seed" + +client = Seed::Client.new( + username: "", + password: "", + base_url: "https://api.fern.com" +) + +client.basic_auth.get_with_basic_auth diff --git a/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example2/snippet.rb b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example2/snippet.rb new file mode 100644 index 000000000000..2d4035b00456 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example2/snippet.rb @@ -0,0 +1,9 @@ +require "seed" + +client = Seed::Client.new( + username: "", + password: "", + base_url: "https://api.fern.com" +) + +client.basic_auth.get_with_basic_auth diff --git a/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example3/snippet.rb b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example3/snippet.rb new file mode 100644 index 000000000000..37fb27d10d1a --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example3/snippet.rb @@ -0,0 +1,9 @@ +require "seed" + +client = Seed::Client.new( + username: "", + password: "", + base_url: "https://api.fern.com" +) + +client.basic_auth.post_with_basic_auth diff --git a/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example4/snippet.rb b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example4/snippet.rb new file mode 100644 index 000000000000..37fb27d10d1a --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example4/snippet.rb @@ -0,0 +1,9 @@ +require "seed" + +client = Seed::Client.new( + username: "", + password: "", + base_url: "https://api.fern.com" +) + +client.basic_auth.post_with_basic_auth diff --git a/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example5/snippet.rb b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example5/snippet.rb new file mode 100644 index 000000000000..37fb27d10d1a --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example5/snippet.rb @@ -0,0 +1,9 @@ +require "seed" + +client = Seed::Client.new( + username: "", + password: "", + base_url: "https://api.fern.com" +) + +client.basic_auth.post_with_basic_auth diff --git a/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example6/snippet.rb b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example6/snippet.rb new file mode 100644 index 000000000000..37fb27d10d1a --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/dynamic-snippets/example6/snippet.rb @@ -0,0 +1,9 @@ +require "seed" + +client = Seed::Client.new( + username: "", + password: "", + base_url: "https://api.fern.com" +) + +client.basic_auth.post_with_basic_auth diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed.rb new file mode 100644 index 000000000000..3b8182547ec3 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require "json" +require "net/http" +require "securerandom" +require "base64" + +require_relative "seed/internal/json/serializable" +require_relative "seed/internal/types/type" +require_relative "seed/internal/types/utils" +require_relative "seed/internal/types/union" +require_relative "seed/internal/errors/constraint_error" +require_relative "seed/internal/errors/type_error" +require_relative "seed/internal/http/base_request" +require_relative "seed/internal/json/request" +require_relative "seed/internal/http/raw_client" +require_relative "seed/internal/multipart/multipart_encoder" +require_relative "seed/internal/multipart/multipart_form_data_part" +require_relative "seed/internal/multipart/multipart_form_data" +require_relative "seed/internal/multipart/multipart_request" +require_relative "seed/internal/types/model/field" +require_relative "seed/internal/types/model" +require_relative "seed/internal/types/array" +require_relative "seed/internal/types/boolean" +require_relative "seed/internal/types/enum" +require_relative "seed/internal/types/hash" +require_relative "seed/internal/types/unknown" +require_relative "seed/errors/api_error" +require_relative "seed/errors/response_error" +require_relative "seed/errors/client_error" +require_relative "seed/errors/redirect_error" +require_relative "seed/errors/server_error" +require_relative "seed/errors/timeout_error" +require_relative "seed/internal/iterators/item_iterator" +require_relative "seed/internal/iterators/cursor_item_iterator" +require_relative "seed/internal/iterators/offset_item_iterator" +require_relative "seed/internal/iterators/cursor_page_iterator" +require_relative "seed/internal/iterators/offset_page_iterator" +require_relative "seed/errors/types/unauthorized_request_error_body" +require_relative "seed/client" +require_relative "seed/basic_auth/client" diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/basic_auth/client.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/basic_auth/client.rb new file mode 100644 index 000000000000..e90716fb53a6 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/basic_auth/client.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +module Seed + module BasicAuth + class Client + # @param client [Seed::Internal::Http::RawClient] + # + # @return [void] + def initialize(client:) + @client = client + end + + # GET request with basic auth scheme + # + # @param request_options [Hash] + # @param params [Hash] + # @option request_options [String] :base_url + # @option request_options [Hash{String => Object}] :additional_headers + # @option request_options [Hash{String => Object}] :additional_query_parameters + # @option request_options [Hash{String => Object}] :additional_body_parameters + # @option request_options [Integer] :timeout_in_seconds + # + # @return [Boolean] + def get_with_basic_auth(request_options: {}, **params) + Seed::Internal::Types::Utils.normalize_keys(params) + request = Seed::Internal::JSON::Request.new( + base_url: request_options[:base_url], + method: "GET", + path: "basic-auth", + request_options: request_options + ) + begin + response = @client.send(request) + rescue Net::HTTPRequestTimeout + raise Seed::Errors::TimeoutError + end + code = response.code.to_i + return if code.between?(200, 299) + + error_class = Seed::Errors::ResponseError.subclass_for_code(code) + raise error_class.new(response.body, code: code) + end + + # POST request with basic auth scheme + # + # @param request_options [Hash] + # @param params [Hash] + # @option request_options [String] :base_url + # @option request_options [Hash{String => Object}] :additional_headers + # @option request_options [Hash{String => Object}] :additional_query_parameters + # @option request_options [Hash{String => Object}] :additional_body_parameters + # @option request_options [Integer] :timeout_in_seconds + # + # @return [Boolean] + def post_with_basic_auth(request_options: {}, **params) + params = Seed::Internal::Types::Utils.normalize_keys(params) + request = Seed::Internal::JSON::Request.new( + base_url: request_options[:base_url], + method: "POST", + path: "basic-auth", + body: params, + request_options: request_options + ) + begin + response = @client.send(request) + rescue Net::HTTPRequestTimeout + raise Seed::Errors::TimeoutError + end + code = response.code.to_i + return if code.between?(200, 299) + + error_class = Seed::Errors::ResponseError.subclass_for_code(code) + raise error_class.new(response.body, code: code) + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/client.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/client.rb new file mode 100644 index 000000000000..aa7884f2eb40 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/client.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Seed + class Client + # @param base_url [String, nil] + # @param username [String] + # @param password [String] + # + # @return [void] + def initialize(username:, password:, base_url: nil) + headers = { + "User-Agent" => "fern_basic-auth-optional/0.0.1", + "X-Fern-Language" => "Ruby" + } + headers["Authorization"] = "Basic #{Base64.strict_encode64("#{username}:#{password}")}" + @raw_client = Seed::Internal::Http::RawClient.new( + base_url: base_url, + headers: headers + ) + end + + # @return [Seed::BasicAuth::Client] + def basic_auth + @basic_auth ||= Seed::BasicAuth::Client.new(client: @raw_client) + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/api_error.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/api_error.rb new file mode 100644 index 000000000000..b8ba53889b36 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/api_error.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Seed + module Errors + class ApiError < StandardError + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/client_error.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/client_error.rb new file mode 100644 index 000000000000..c3c6033641e2 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/client_error.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Seed + module Errors + class ClientError < ResponseError + end + + class UnauthorizedError < ClientError + end + + class ForbiddenError < ClientError + end + + class NotFoundError < ClientError + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/redirect_error.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/redirect_error.rb new file mode 100644 index 000000000000..f663c01e7615 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/redirect_error.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Seed + module Errors + class RedirectError < ResponseError + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/response_error.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/response_error.rb new file mode 100644 index 000000000000..beb4a1baf959 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/response_error.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Seed + module Errors + class ResponseError < ApiError + attr_reader :code + + def initialize(msg, code:) + @code = code + super(msg) + end + + def inspect + "#<#{self.class.name} @code=#{code} @body=#{message}>" + end + + # Returns the most appropriate error class for the given code. + # + # @return [Class] + def self.subclass_for_code(code) + case code + when 300..399 + RedirectError + when 401 + UnauthorizedError + when 403 + ForbiddenError + when 404 + NotFoundError + when 400..499 + ClientError + when 503 + ServiceUnavailableError + when 500..599 + ServerError + else + ResponseError + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/server_error.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/server_error.rb new file mode 100644 index 000000000000..1838027cdeab --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/server_error.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Seed + module Errors + class ServerError < ResponseError + end + + class ServiceUnavailableError < ApiError + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/timeout_error.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/timeout_error.rb new file mode 100644 index 000000000000..ec3a24bb7e96 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/timeout_error.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Seed + module Errors + class TimeoutError < ApiError + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/types/unauthorized_request_error_body.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/types/unauthorized_request_error_body.rb new file mode 100644 index 000000000000..a3caea8d9bea --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/errors/types/unauthorized_request_error_body.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Seed + module Errors + module Types + class UnauthorizedRequestErrorBody < Internal::Types::Model + field :message, -> { String }, optional: false, nullable: false + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/errors/constraint_error.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/errors/constraint_error.rb new file mode 100644 index 000000000000..e2f0bd66ac37 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/errors/constraint_error.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Errors + class ConstraintError < StandardError + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/errors/type_error.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/errors/type_error.rb new file mode 100644 index 000000000000..6aec80f59f05 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/errors/type_error.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Errors + class TypeError < StandardError + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/http/base_request.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/http/base_request.rb new file mode 100644 index 000000000000..d35df463e5b0 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/http/base_request.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Http + # @api private + class BaseRequest + attr_reader :base_url, :path, :method, :headers, :query, :request_options + + # @param base_url [String] The base URL for the request + # @param path [String] The path for the request + # @param method [String] The HTTP method for the request (:get, :post, etc.) + # @param headers [Hash] Additional headers for the request (optional) + # @param query [Hash] Query parameters for the request (optional) + # @param request_options [Seed::RequestOptions, Hash{Symbol=>Object}, nil] + def initialize(base_url:, path:, method:, headers: {}, query: {}, request_options: {}) + @base_url = base_url + @path = path + @method = method + @headers = headers + @query = query + @request_options = request_options + end + + # @return [Hash] The query parameters merged with additional query parameters from request options. + def encode_query + additional_query = @request_options&.dig(:additional_query_parameters) || @request_options&.dig("additional_query_parameters") || {} + @query.merge(additional_query) + end + + # Child classes should implement: + # - encode_headers: Returns the encoded HTTP request headers. + # - encode_body: Returns the encoded HTTP request body. + + private + + # Merges additional_headers from request_options into sdk_headers, filtering out + # any keys that collide with SDK-set or client-protected headers (case-insensitive). + # @param sdk_headers [Hash] Headers set by the SDK for this request type. + # @param protected_keys [Array] Additional header keys that must not be overridden. + # @return [Hash] The merged headers. + def merge_additional_headers(sdk_headers, protected_keys: []) + additional_headers = @request_options&.dig(:additional_headers) || @request_options&.dig("additional_headers") || {} + all_protected = (sdk_headers.keys + protected_keys).to_set { |k| k.to_s.downcase } + filtered = additional_headers.reject { |key, _| all_protected.include?(key.to_s.downcase) } + sdk_headers.merge(filtered) + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/http/raw_client.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/http/raw_client.rb new file mode 100644 index 000000000000..482ab9517714 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/http/raw_client.rb @@ -0,0 +1,214 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Http + # @api private + class RawClient + # Default HTTP status codes that trigger a retry + RETRYABLE_STATUSES = [408, 429, 500, 502, 503, 504, 521, 522, 524].freeze + # Initial delay between retries in seconds + INITIAL_RETRY_DELAY = 0.5 + # Maximum delay between retries in seconds + MAX_RETRY_DELAY = 60.0 + # Jitter factor for randomizing retry delays (20%) + JITTER_FACTOR = 0.2 + + # @return [String] The base URL for requests + attr_reader :base_url + + # @param base_url [String] The base url for the request. + # @param max_retries [Integer] The number of times to retry a failed request, defaults to 2. + # @param timeout [Float] The timeout for the request, defaults to 60.0 seconds. + # @param headers [Hash] The headers for the request. + def initialize(base_url:, max_retries: 2, timeout: 60.0, headers: {}) + @base_url = base_url + @max_retries = max_retries + @timeout = timeout + @default_headers = { + "X-Fern-Language": "Ruby", + "X-Fern-SDK-Name": "seed", + "X-Fern-SDK-Version": "0.0.1" + }.merge(headers) + end + + # @param request [Seed::Internal::Http::BaseRequest] The HTTP request. + # @return [HTTP::Response] The HTTP response. + def send(request) + url = build_url(request) + attempt = 0 + response = nil + + loop do + http_request = build_http_request( + url:, + method: request.method, + headers: request.encode_headers(protected_keys: @default_headers.keys), + body: request.encode_body + ) + + conn = connect(url) + conn.open_timeout = @timeout + conn.read_timeout = @timeout + conn.write_timeout = @timeout + conn.continue_timeout = @timeout + + response = conn.request(http_request) + + break unless should_retry?(response, attempt) + + delay = retry_delay(response, attempt) + sleep(delay) + attempt += 1 + end + + response + end + + # Determines if a request should be retried based on the response status code. + # @param response [Net::HTTPResponse] The HTTP response. + # @param attempt [Integer] The current retry attempt (0-indexed). + # @return [Boolean] Whether the request should be retried. + def should_retry?(response, attempt) + return false if attempt >= @max_retries + + status = response.code.to_i + RETRYABLE_STATUSES.include?(status) + end + + # Calculates the delay before the next retry attempt using exponential backoff with jitter. + # Respects Retry-After header if present. + # @param response [Net::HTTPResponse] The HTTP response. + # @param attempt [Integer] The current retry attempt (0-indexed). + # @return [Float] The delay in seconds before the next retry. + def retry_delay(response, attempt) + # Check for Retry-After header (can be seconds or HTTP date) + retry_after = response["Retry-After"] + if retry_after + delay = parse_retry_after(retry_after) + return [delay, MAX_RETRY_DELAY].min if delay&.positive? + end + + # Exponential backoff with jitter: base_delay * 2^attempt + base_delay = INITIAL_RETRY_DELAY * (2**attempt) + add_jitter([base_delay, MAX_RETRY_DELAY].min) + end + + # Parses the Retry-After header value. + # @param value [String] The Retry-After header value (seconds or HTTP date). + # @return [Float, nil] The delay in seconds, or nil if parsing fails. + def parse_retry_after(value) + # Try parsing as integer (seconds) + seconds = Integer(value, exception: false) + return seconds.to_f if seconds + + # Try parsing as HTTP date + begin + retry_time = Time.httpdate(value) + delay = retry_time - Time.now + delay.positive? ? delay : nil + rescue ArgumentError + nil + end + end + + # Adds random jitter to a delay value. + # @param delay [Float] The base delay in seconds. + # @return [Float] The delay with jitter applied. + def add_jitter(delay) + jitter = delay * JITTER_FACTOR * (rand - 0.5) * 2 + [delay + jitter, 0].max + end + + LOCALHOST_HOSTS = %w[localhost 127.0.0.1 [::1]].freeze + + # @param request [Seed::Internal::Http::BaseRequest] The HTTP request. + # @return [URI::Generic] The URL. + def build_url(request) + encoded_query = request.encode_query + + # If the path is already an absolute URL, use it directly + if request.path.start_with?("http://", "https://") + url = request.path + url = "#{url}?#{encode_query(encoded_query)}" if encoded_query&.any? + parsed = URI.parse(url) + validate_https!(parsed) + return parsed + end + + path = request.path.start_with?("/") ? request.path[1..] : request.path + base = request.base_url || @base_url + url = "#{base.chomp("/")}/#{path}" + url = "#{url}?#{encode_query(encoded_query)}" if encoded_query&.any? + parsed = URI.parse(url) + validate_https!(parsed) + parsed + end + + # Raises if the URL uses http:// for a non-localhost host, which would + # send authentication credentials in plaintext. + # @param url [URI::Generic] The parsed URL. + def validate_https!(url) + return if url.scheme != "http" + return if LOCALHOST_HOSTS.include?(url.host) + + raise ArgumentError, "Refusing to send request to non-HTTPS URL: #{url}. " \ + "HTTP is only allowed for localhost. Use HTTPS or pass a localhost URL." + end + + # @param url [URI::Generic] The url to the resource. + # @param method [String] The HTTP method to use. + # @param headers [Hash] The headers for the request. + # @param body [String, nil] The body for the request. + # @return [HTTP::Request] The HTTP request. + def build_http_request(url:, method:, headers: {}, body: nil) + request = Net::HTTPGenericRequest.new( + method, + !body.nil?, + method != "HEAD", + url + ) + + request_headers = @default_headers.merge(headers) + request_headers.each { |name, value| request[name] = value } + request.body = body if body + + request + end + + # @param query [Hash] The query for the request. + # @return [String, nil] The encoded query. + def encode_query(query) + query.to_h.empty? ? nil : URI.encode_www_form(query) + end + + # @param url [URI::Generic] The url to connect to. + # @return [Net::HTTP] The HTTP connection. + def connect(url) + is_https = (url.scheme == "https") + + port = if url.port + url.port + elsif is_https + Net::HTTP.https_default_port + else + Net::HTTP.http_default_port + end + + http = Net::HTTP.new(url.host, port) + http.use_ssl = is_https + http.verify_mode = OpenSSL::SSL::VERIFY_PEER if is_https + # NOTE: We handle retries at the application level with HTTP status code awareness, + # so we set max_retries to 0 to disable Net::HTTP's built-in network-level retries. + http.max_retries = 0 + http + end + + # @return [String] + def inspect + "#<#{self.class.name}:0x#{object_id.to_s(16)} @base_url=#{@base_url.inspect}>" + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/cursor_item_iterator.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/cursor_item_iterator.rb new file mode 100644 index 000000000000..ab627ffc7025 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/cursor_item_iterator.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module Seed + module Internal + class CursorItemIterator < ItemIterator + # Instantiates a CursorItemIterator, an Enumerable class which wraps calls to a cursor-based paginated API and yields individual items from it. + # + # @param initial_cursor [String] The initial cursor to use when iterating, if any. + # @param cursor_field [Symbol] The field in API responses to extract the next cursor from. + # @param item_field [Symbol] The field in API responses to extract the items to iterate over. + # @param block [Proc] A block which is responsible for receiving a cursor to use and returning the given page from the API. + # @return [Seed::Internal::CursorItemIterator] + def initialize(initial_cursor:, cursor_field:, item_field:, &) + super() + @item_field = item_field + @page_iterator = CursorPageIterator.new(initial_cursor:, cursor_field:, &) + @page = nil + end + + # Returns the CursorPageIterator mediating access to the underlying API. + # + # @return [Seed::Internal::CursorPageIterator] + def pages + @page_iterator + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/cursor_page_iterator.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/cursor_page_iterator.rb new file mode 100644 index 000000000000..f479a749fef9 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/cursor_page_iterator.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +module Seed + module Internal + class CursorPageIterator + include Enumerable + + # Instantiates a CursorPageIterator, an Enumerable class which wraps calls to a cursor-based paginated API and yields pages of items. + # + # @param initial_cursor [String] The initial cursor to use when iterating, if any. + # @param cursor_field [Symbol] The name of the field in API responses to extract the next cursor from. + # @param block [Proc] A block which is responsible for receiving a cursor to use and returning the given page from the API. + # @return [Seed::Internal::CursorPageIterator] + def initialize(initial_cursor:, cursor_field:, &block) + @need_initial_load = initial_cursor.nil? + @cursor = initial_cursor + @cursor_field = cursor_field + @get_next_page = block + end + + # Iterates over each page returned by the API. + # + # @param block [Proc] The block which each retrieved page is yielded to. + # @return [NilClass] + def each(&block) + while (page = next_page) + block.call(page) + end + end + + # Whether another page will be available from the API. + # + # @return [Boolean] + def next? + @need_initial_load || !@cursor.nil? + end + + # Retrieves the next page from the API. + # + # @return [Boolean] + def next_page + return if !@need_initial_load && @cursor.nil? + + @need_initial_load = false + fetched_page = @get_next_page.call(@cursor) + @cursor = fetched_page.send(@cursor_field) + fetched_page + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/item_iterator.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/item_iterator.rb new file mode 100644 index 000000000000..1284fb0fd367 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/item_iterator.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +module Seed + module Internal + class ItemIterator + include Enumerable + + # Iterates over each item returned by the API. + # + # @param block [Proc] The block which each retrieved item is yielded to. + # @return [NilClass] + def each(&block) + while (item = next_element) + block.call(item) + end + end + + # Whether another item will be available from the API. + # + # @return [Boolean] + def next? + load_next_page if @page.nil? + return false if @page.nil? + + return true if any_items_in_cached_page? + + load_next_page + any_items_in_cached_page? + end + + # Retrieves the next item from the API. + def next_element + item = next_item_from_cached_page + return item if item + + load_next_page + next_item_from_cached_page + end + + private + + def next_item_from_cached_page + return unless @page + + @page.send(@item_field).shift + end + + def any_items_in_cached_page? + return false unless @page + + !@page.send(@item_field).empty? + end + + def load_next_page + @page = @page_iterator.next_page + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/offset_item_iterator.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/offset_item_iterator.rb new file mode 100644 index 000000000000..f8840246686d --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/offset_item_iterator.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Seed + module Internal + class OffsetItemIterator < ItemIterator + # Instantiates an OffsetItemIterator, an Enumerable class which wraps calls to an offset-based paginated API and yields the individual items from it. + # + # @param initial_page [Integer] The initial page or offset to start from when iterating. + # @param item_field [Symbol] The name of the field in API responses to extract the items to iterate over. + # @param has_next_field [Symbol] The name of the field in API responses containing a boolean of whether another page exists. + # @param step [Boolean] If true, treats the page number as a true offset (i.e. increments the page number by the number of items returned from each call rather than just 1) + # @param block [Proc] A block which is responsible for receiving a page number to use and returning the given page from the API. + # + # @return [Seed::Internal::OffsetItemIterator] + def initialize(initial_page:, item_field:, has_next_field:, step:, &) + super() + @item_field = item_field + @page_iterator = OffsetPageIterator.new(initial_page:, item_field:, has_next_field:, step:, &) + @page = nil + end + + # Returns the OffsetPageIterator that is mediating access to the underlying API. + # + # @return [Seed::Internal::OffsetPageIterator] + def pages + @page_iterator + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/offset_page_iterator.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/offset_page_iterator.rb new file mode 100644 index 000000000000..051b65c5774c --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/iterators/offset_page_iterator.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +module Seed + module Internal + class OffsetPageIterator + include Enumerable + + # Instantiates an OffsetPageIterator, an Enumerable class which wraps calls to an offset-based paginated API and yields pages of items from it. + # + # @param initial_page [Integer] The initial page to use when iterating, if any. + # @param item_field [Symbol] The field to pull the list of items to iterate over. + # @param has_next_field [Symbol] The field to pull the boolean of whether a next page exists from, if any. + # @param step [Boolean] If true, treats the page number as a true offset (i.e. increments the page number by the number of items returned from each call rather than just 1) + # @param block [Proc] A block which is responsible for receiving a page number to use and returning the given page from the API. + # @return [Seed::Internal::OffsetPageIterator] + def initialize(initial_page:, item_field:, has_next_field:, step:, &block) + @page_number = initial_page || (step ? 0 : 1) + @item_field = item_field + @has_next_field = has_next_field + @step = step + @get_next_page = block + + # A cache of whether the API has another page, if it gives us that information... + @next_page = nil + # ...or the actual next page, preloaded, if it doesn't. + @has_next_page = nil + end + + # Iterates over each page returned by the API. + # + # @param block [Proc] The block which each retrieved page is yielded to. + # @return [NilClass] + def each(&block) + while (page = next_page) + block.call(page) + end + end + + # Whether another page will be available from the API. + # + # @return [Boolean] + def next? + return @has_next_page unless @has_next_page.nil? + return true if @next_page + + fetched_page = @get_next_page.call(@page_number) + fetched_page_items = fetched_page&.send(@item_field) + if fetched_page_items.nil? || fetched_page_items.empty? + @has_next_page = false + else + @next_page = fetched_page + true + end + end + + # Returns the next page from the API. + def next_page + return nil if @page_number.nil? + + if @next_page + this_page = @next_page + @next_page = nil + else + this_page = @get_next_page.call(@page_number) + end + + @has_next_page = this_page&.send(@has_next_field) if @has_next_field + + items = this_page.send(@item_field) + if items.nil? || items.empty? + @page_number = nil + return nil + elsif @step + @page_number += items.length + else + @page_number += 1 + end + + this_page + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/json/request.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/json/request.rb new file mode 100644 index 000000000000..667ceae8ac59 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/json/request.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +module Seed + module Internal + module JSON + # @api private + class Request < Seed::Internal::Http::BaseRequest + attr_reader :body + + # @param base_url [String] The base URL for the request + # @param path [String] The path for the request + # @param method [Symbol] The HTTP method for the request (:get, :post, etc.) + # @param headers [Hash] Additional headers for the request (optional) + # @param query [Hash] Query parameters for the request (optional) + # @param body [Object, nil] The JSON request body (optional) + # @param request_options [Seed::RequestOptions, Hash{Symbol=>Object}, nil] + def initialize(base_url:, path:, method:, headers: {}, query: {}, body: nil, request_options: {}) + super(base_url:, path:, method:, headers:, query:, request_options:) + + @body = body + end + + # @return [Hash] The encoded HTTP request headers. + # @param protected_keys [Array] Header keys set by the SDK client (e.g. auth, metadata) + # that must not be overridden by additional_headers from request_options. + def encode_headers(protected_keys: []) + sdk_headers = { + "Content-Type" => "application/json", + "Accept" => "application/json" + }.merge(@headers) + merge_additional_headers(sdk_headers, protected_keys:) + end + + # @return [String, nil] The encoded HTTP request body. + def encode_body + @body.nil? ? nil : ::JSON.generate(@body) + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/json/serializable.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/json/serializable.rb new file mode 100644 index 000000000000..f80a15fb962c --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/json/serializable.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Seed + module Internal + module JSON + module Serializable + # Loads data from JSON into its deserialized form + # + # @param str [String] Raw JSON to load into an object + # @return [Object] + def load(str) + raise NotImplementedError + end + + # Dumps data from its deserialized form into JSON + # + # @param value [Object] The deserialized value + # @return [String] + def dump(value) + raise NotImplementedError + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_encoder.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_encoder.rb new file mode 100644 index 000000000000..307ad7436a57 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_encoder.rb @@ -0,0 +1,141 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Multipart + # Encodes parameters into a `multipart/form-data` payload as described by RFC + # 2388: + # + # https://tools.ietf.org/html/rfc2388 + # + # This is most useful for transferring file-like objects. + # + # Parameters should be added with `#encode`. When ready, use `#body` to get + # the encoded result and `#content_type` to get the value that should be + # placed in the `Content-Type` header of a subsequent request (which includes + # a boundary value). + # + # This abstraction is heavily inspired by Stripe's multipart/form-data implementation, + # which can be found here: + # + # https://github.com/stripe/stripe-ruby/blob/ca00b676f04ac421cf5cb5ff0325f243651677b6/lib/stripe/multipart_encoder.rb#L18 + # + # @api private + class Encoder + CONTENT_TYPE = "multipart/form-data" + CRLF = "\r\n" + + attr_reader :boundary, :body + + def initialize + # Chose the same number of random bytes that Go uses in its standard + # library implementation. Easily enough entropy to ensure that it won't + # be present in a file we're sending. + @boundary = SecureRandom.hex(30) + + @body = String.new + @closed = false + @first_field = true + end + + # Gets the content type string including the boundary. + # + # @return [String] The content type with boundary + def content_type + "#{CONTENT_TYPE}; boundary=#{@boundary}" + end + + # Encode the given FormData object into a multipart/form-data payload. + # + # @param form_data [FormData] The form data to encode + # @return [String] The encoded body. + def encode(form_data) + return "" if form_data.parts.empty? + + form_data.parts.each do |part| + write_part(part) + end + close + + @body + end + + # Writes a FormDataPart to the encoder. + # + # @param part [FormDataPart] The part to write + # @return [nil] + def write_part(part) + raise "Cannot write to closed encoder" if @closed + + write_field( + name: part.name, + data: part.contents, + filename: part.filename, + headers: part.headers + ) + + nil + end + + # Writes a field to the encoder. + # + # @param name [String] The field name + # @param data [String] The field data + # @param filename [String, nil] Optional filename + # @param headers [Hash, nil] Optional additional headers + # @return [nil] + def write_field(name:, data:, filename: nil, headers: nil) + raise "Cannot write to closed encoder" if @closed + + if @first_field + @first_field = false + else + @body << CRLF + end + + @body << "--#{@boundary}#{CRLF}" + @body << %(Content-Disposition: form-data; name="#{escape(name.to_s)}") + @body << %(; filename="#{escape(filename)}") if filename + @body << CRLF + + if headers + headers.each do |key, value| + @body << "#{key}: #{value}#{CRLF}" + end + elsif filename + # Default content type for files. + @body << "Content-Type: application/octet-stream#{CRLF}" + end + + @body << CRLF + @body << data.to_s + + nil + end + + # Finalizes the encoder by writing the final boundary. + # + # @return [nil] + def close + raise "Encoder already closed" if @closed + + @body << CRLF + @body << "--#{@boundary}--" + @closed = true + + nil + end + + private + + # Escapes quotes for use in header values and replaces line breaks with spaces. + # + # @param str [String] The string to escape + # @return [String] The escaped string + def escape(str) + str.to_s.gsub('"', "%22").tr("\n", " ").tr("\r", " ") + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_form_data.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_form_data.rb new file mode 100644 index 000000000000..5be1bb25341f --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_form_data.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Multipart + # @api private + class FormData + # @return [Array] The parts in this multipart form data. + attr_reader :parts + + # @return [Encoder] The encoder for this multipart form data. + private attr_reader :encoder + + def initialize + @encoder = Encoder.new + @parts = [] + end + + # Adds a new part to the multipart form data. + # + # @param name [String] The name of the form field + # @param value [String, Integer, Float, Boolean, #read] The value of the field + # @param content_type [String, nil] Optional content type + # @return [self] Returns self for chaining + def add(name:, value:, content_type: nil) + headers = content_type ? { "Content-Type" => content_type } : nil + add_part(FormDataPart.new(name:, value:, headers:)) + end + + # Adds a file to the multipart form data. + # + # @param name [String] The name of the form field + # @param file [#read] The file or readable object + # @param filename [String, nil] Optional filename (defaults to basename of path for File objects) + # @param content_type [String, nil] Optional content type (e.g. "image/png") + # @return [self] Returns self for chaining + def add_file(name:, file:, filename: nil, content_type: nil) + headers = content_type ? { "Content-Type" => content_type } : nil + filename ||= filename_for(file) + add_part(FormDataPart.new(name:, value: file, filename:, headers:)) + end + + # Adds a pre-created part to the multipart form data. + # + # @param part [FormDataPart] The part to add + # @return [self] Returns self for chaining + def add_part(part) + @parts << part + self + end + + # Gets the content type string including the boundary. + # + # @return [String] The content type with boundary. + def content_type + @encoder.content_type + end + + # Encode the multipart form data into a multipart/form-data payload. + # + # @return [String] The encoded body. + def encode + @encoder.encode(self) + end + + private + + def filename_for(file) + if file.is_a?(::File) || file.respond_to?(:path) + ::File.basename(file.path) + elsif file.respond_to?(:name) + file.name + end + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_form_data_part.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_form_data_part.rb new file mode 100644 index 000000000000..de45416ee087 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_form_data_part.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require "securerandom" + +module Seed + module Internal + module Multipart + # @api private + class FormDataPart + attr_reader :name, :contents, :filename, :headers + + # @param name [String] The name of the form field + # @param value [String, Integer, Float, Boolean, File, #read] The value of the field + # @param filename [String, nil] Optional filename for file uploads + # @param headers [Hash, nil] Optional additional headers + def initialize(name:, value:, filename: nil, headers: nil) + @name = name + @contents = convert_to_content(value) + @filename = filename + @headers = headers + end + + # Converts the part to a hash suitable for serialization. + # + # @return [Hash] A hash representation of the part + def to_hash + result = { + name: @name, + contents: @contents + } + result[:filename] = @filename if @filename + result[:headers] = @headers if @headers + result + end + + private + + # Converts various types of values to a content representation + # @param value [String, Integer, Float, Boolean, #read] The value to convert + # @return [String] The string representation of the value + def convert_to_content(value) + if value.respond_to?(:read) + value.read + else + value.to_s + end + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_request.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_request.rb new file mode 100644 index 000000000000..9fa80cee01ab --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/multipart/multipart_request.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Multipart + # @api private + class Request < Seed::Internal::Http::BaseRequest + attr_reader :body + + # @param base_url [String] The base URL for the request + # @param path [String] The path for the request + # @param method [Symbol] The HTTP method for the request (:get, :post, etc.) + # @param headers [Hash] Additional headers for the request (optional) + # @param query [Hash] Query parameters for the request (optional) + # @param body [MultipartFormData, nil] The multipart form data for the request (optional) + # @param request_options [Seed::RequestOptions, Hash{Symbol=>Object}, nil] + def initialize(base_url:, path:, method:, headers: {}, query: {}, body: nil, request_options: {}) + super(base_url:, path:, method:, headers:, query:, request_options:) + + @body = body + end + + # @return [Hash] The encoded HTTP request headers. + # @param protected_keys [Array] Header keys set by the SDK client (e.g. auth, metadata) + # that must not be overridden by additional_headers from request_options. + def encode_headers(protected_keys: []) + sdk_headers = { + "Content-Type" => @body.content_type + }.merge(@headers) + merge_additional_headers(sdk_headers, protected_keys:) + end + + # @return [String, nil] The encoded HTTP request body. + def encode_body + @body&.encode + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/array.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/array.rb new file mode 100644 index 000000000000..f3c7c1bd9549 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/array.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Types + # An array of a specific type + class Array + include Seed::Internal::Types::Type + + attr_reader :type + + class << self + # Instantiates a new `Array` of a given type + # + # @param type [Object] The member type of this array + # + # @return [Seed::Internal::Types::Array] + def [](type) + new(type) + end + end + + # @api private + def initialize(type) + @type = type + end + + # Coerces a value into this array + # + # @param value [Object] + # @option strict [Boolean] + # @return [::Array] + def coerce(value, strict: strict?) + unless value.is_a?(::Array) + raise Errors::TypeError, "cannot coerce `#{value.class}` to Array<#{type}>" if strict + + return value + end + + value.map do |element| + Utils.coerce(type, element, strict: strict) + end + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/boolean.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/boolean.rb new file mode 100644 index 000000000000..d4e3277e566f --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/boolean.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Types + module Boolean + extend Seed::Internal::Types::Union + + member TrueClass + member FalseClass + + # Overrides the base coercion method for enums to allow integer and string values to become booleans + # + # @param value [Object] + # @option strict [Boolean] + # @return [Object] + def self.coerce(value, strict: strict?) + case value + when TrueClass, FalseClass + return value + when Integer + return value == 1 + when String + return %w[1 true].include?(value) + end + + raise Errors::TypeError, "cannot coerce `#{value.class}` to Boolean" if strict + + value + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/enum.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/enum.rb new file mode 100644 index 000000000000..72e45e4c1f27 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/enum.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Types + # Module for defining enums + module Enum + include Type + + # @api private + # + # @return [Array] + def values + @values ||= constants.map { |c| const_get(c) } + end + + # @api private + def finalize! + values + end + + # @api private + def strict? + @strict ||= false + end + + # @api private + def strict! + @strict = true + end + + def coerce(value, strict: strict?) + coerced_value = Utils.coerce(Symbol, value) + + return coerced_value if values.include?(coerced_value) + + raise Errors::TypeError, "`#{value}` not in enum #{self}" if strict + + value + end + + # Parse JSON string and coerce to the enum value + # + # @param str [String] JSON string to parse + # @return [String] The enum value + def load(str) + coerce(::JSON.parse(str)) + end + + def inspect + "#{name}[#{values.join(", ")}]" + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/hash.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/hash.rb new file mode 100644 index 000000000000..d8bffa63ac11 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/hash.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Types + class Hash + include Type + + attr_reader :key_type, :value_type + + class << self + def [](key_type, value_type) + new(key_type, value_type) + end + end + + def initialize(key_type, value_type) + @key_type = key_type + @value_type = value_type + end + + def coerce(value, strict: strict?) + unless value.is_a?(::Hash) + raise Errors::TypeError, "not hash" if strict + + return value + end + + value.to_h do |k, v| + [Utils.coerce(key_type, k, strict: strict), Utils.coerce(value_type, v, strict: strict)] + end + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/model.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/model.rb new file mode 100644 index 000000000000..8caca14ff7ea --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/model.rb @@ -0,0 +1,208 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Types + # @abstract + # + # An abstract model that all data objects will inherit from + class Model + include Type + + class << self + # The defined fields for this model + # + # @api private + # + # @return [Hash] + def fields + @fields ||= if self < Seed::Internal::Types::Model + superclass.fields.dup + else + {} + end + end + + # Any extra fields that have been created from instantiation + # + # @api private + # + # @return [Hash] + def extra_fields + @extra_fields ||= {} + end + + # Define a new field on this model + # + # @param name [Symbol] The name of the field + # @param type [Class] Type of the field + # @option optional [Boolean] If it is an optional field + # @option nullable [Boolean] If it is a nullable field + # @option api_name [Symbol, String] Name in the API of this field. When serializing/deserializing, will use + # this field name + # @return [void] + def field(name, type, optional: false, nullable: false, api_name: nil, default: nil) + add_field_definition(name: name, type: type, optional: optional, nullable: nullable, api_name: api_name, + default: default) + + define_accessor(name) + define_setter(name) + end + + # Define a new literal for this model + # + # @param name [Symbol] + # @param value [Object] + # @option api_name [Symbol, String] + # @return [void] + def literal(name, value, api_name: nil) + add_field_definition(name: name, type: value.class, optional: false, nullable: false, api_name: api_name, + value: value) + + define_accessor(name) + end + + # Adds a new field definition into the class's fields registry + # + # @api private + # + # @param name [Symbol] + # @param type [Class] + # @option optional [Boolean] + # @return [void] + private def add_field_definition(name:, type:, optional:, nullable:, api_name:, default: nil, value: nil) + fields[name.to_sym] = + Field.new(name: name, type: type, optional: optional, nullable: nullable, api_name: api_name, + value: value, default: default) + end + + # Adds a new field definition into the class's extra fields registry + # + # @api private + # + # @param name [Symbol] + # @param type [Class] + # @option required [Boolean] + # @option optional [Boolean] + # @return [void] + def add_extra_field_definition(name:, type:) + return if extra_fields.key?(name.to_sym) + + extra_fields[name.to_sym] = Field.new(name: name, type: type, optional: true, nullable: false) + + define_accessor(name) + define_setter(name) + end + + # @api private + private def define_accessor(name) + method_name = name.to_sym + + define_method(method_name) do + @data[name] + end + end + + # @api private + private def define_setter(name) + method_name = :"#{name}=" + + define_method(method_name) do |val| + @data[name] = val + end + end + + def coerce(value, strict: (respond_to?(:strict?) ? strict? : false)) # rubocop:disable Lint/UnusedMethodArgument + return value if value.is_a?(self) + + return value unless value.is_a?(::Hash) + + new(value) + end + + def load(str) + coerce(::JSON.parse(str, symbolize_names: true)) + end + + def ===(instance) + instance.class.ancestors.include?(self) + end + end + + # Creates a new instance of this model + # TODO: Should all this logic be in `#coerce` instead? + # + # @param values [Hash] + # @option strict [Boolean] + # @return [self] + def initialize(values = {}) + @data = {} + + values = Utils.symbolize_keys(values.dup) + + self.class.fields.each do |field_name, field| + value = values.delete(field.api_name.to_sym) || values.delete(field.api_name) || values.delete(field_name) + + field_value = value || (if field.literal? + field.value + elsif field.default + field.default + end) + + @data[field_name] = Utils.coerce(field.type, field_value) + end + + # Any remaining values in the input become extra fields + values.each do |name, value| + self.class.add_extra_field_definition(name: name, type: value.class) + + @data[name.to_sym] = value + end + end + + def to_h + result = self.class.fields.merge(self.class.extra_fields).each_with_object({}) do |(name, field), acc| + # If there is a value present in the data, use that value + # If there is a `nil` value present in the data, and it is optional but NOT nullable, exclude key altogether + # If there is a `nil` value present in the data, and it is optional and nullable, use the nil value + + value = @data[name] + + next if value.nil? && field.optional && !field.nullable + + if value.is_a?(::Array) + value = value.map { |item| item.respond_to?(:to_h) ? item.to_h : item } + elsif value.respond_to?(:to_h) + value = value.to_h + end + + acc[field.api_name] = value + end + + # Inject union discriminant if this instance was coerced from a discriminated union + # and the discriminant key is not already present in the result + discriminant_key = instance_variable_get(:@_fern_union_discriminant_key) + discriminant_value = instance_variable_get(:@_fern_union_discriminant_value) + result[discriminant_key] = discriminant_value if discriminant_key && discriminant_value && !result.key?(discriminant_key) + + result + end + + def ==(other) + self.class == other.class && to_h == other.to_h + end + + # @return [String] + def inspect + attrs = @data.map do |name, value| + field = self.class.fields[name] || self.class.extra_fields[name] + display_value = field&.sensitive? ? "[REDACTED]" : value.inspect + "#{name}=#{display_value}" + end + + "#<#{self.class.name}:0x#{object_id&.to_s(16)} #{attrs.join(" ")}>" + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/model/field.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/model/field.rb new file mode 100644 index 000000000000..6ce0186f6a5d --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/model/field.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Types + class Model + # Definition of a field on a model + class Field + SENSITIVE_FIELD_NAMES = %i[ + password secret token api_key apikey access_token refresh_token + client_secret client_id credential bearer authorization + ].freeze + + attr_reader :name, :type, :optional, :nullable, :api_name, :value, :default + + def initialize(name:, type:, optional: false, nullable: false, api_name: nil, value: nil, default: nil) + @name = name.to_sym + @type = type + @optional = optional + @nullable = nullable + @api_name = api_name || name.to_s + @value = value + @default = default + end + + def literal? + !value.nil? + end + + def sensitive? + SENSITIVE_FIELD_NAMES.include?(@name) || + SENSITIVE_FIELD_NAMES.any? { |sensitive| @name.to_s.include?(sensitive.to_s) } + end + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/type.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/type.rb new file mode 100644 index 000000000000..5866caf1dbda --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/type.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Types + # @abstract + module Type + include Seed::Internal::JSON::Serializable + + # Coerces a value to this type + # + # @param value [unknown] + # @option strict [Boolean] If we should strictly coerce this value + def coerce(value, strict: strict?) + raise NotImplementedError + end + + # Returns if strictness is on for this type, defaults to `false` + # + # @return [Boolean] + def strict? + @strict ||= false + end + + # Enable strictness by default for this type + # + # @return [void] + def strict! + @strict = true + self + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/union.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/union.rb new file mode 100644 index 000000000000..f3e118a2fa78 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/union.rb @@ -0,0 +1,161 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Types + # Define a union between two types + module Union + include Seed::Internal::Types::Type + + def members + @members ||= [] + end + + # Add a member to this union + # + # @param type [Object] + # @option key [Symbol, String] + # @return [void] + def member(type, key: nil) + members.push([key, Utils.wrap_type(type)]) + self + end + + def type_member?(type) + members.any? { |_key, type_fn| type == type_fn.call } + end + + # Set the discriminant for this union + # + # @param key [Symbol, String] + # @return [void] + def discriminant(key) + @discriminant = key + end + + # @api private + private def discriminated? + !@discriminant.nil? + end + + # Check if value matches a type, handling type wrapper instances + # (Internal::Types::Hash and Internal::Types::Array instances) + # + # @param value [Object] + # @param member_type [Object] + # @return [Boolean] + private def type_matches?(value, member_type) + case member_type + when Seed::Internal::Types::Hash + value.is_a?(::Hash) + when Seed::Internal::Types::Array + value.is_a?(::Array) + when Class, Module + value.is_a?(member_type) + else + false + end + end + + # Resolves the type of a value to be one of the members + # + # @param value [Object] + # @return [Class] + private def resolve_member(value) + if discriminated? && value.is_a?(::Hash) + # Try both symbol and string keys for the discriminant + discriminant_value = value.fetch(@discriminant, nil) || value.fetch(@discriminant.to_s, nil) + + return if discriminant_value.nil? + + # Convert to string for consistent comparison + discriminant_str = discriminant_value.to_s + + # First try exact match + members_hash = members.to_h + result = members_hash[discriminant_str]&.call + return result if result + + # Try case-insensitive match as fallback + discriminant_lower = discriminant_str.downcase + matching_keys = members_hash.keys.select { |k| k.to_s.downcase == discriminant_lower } + + # Only use case-insensitive match if exactly one key matches (avoid ambiguity) + return members_hash[matching_keys.first]&.call if matching_keys.length == 1 + + nil + else + # First try exact type matching + result = members.find do |_key, mem| + member_type = Utils.unwrap_type(mem) + type_matches?(value, member_type) + end&.last&.call + + return result if result + + # For Hash values, try to coerce into Model member types + if value.is_a?(::Hash) + members.find do |_key, mem| + member_type = Utils.unwrap_type(mem) + # Check if member_type is a Model class + next unless member_type.is_a?(Class) && member_type <= Model + + # Try to coerce the hash into this model type with strict mode + begin + candidate = Utils.coerce(member_type, value, strict: true) + + # Validate that all required (non-optional) fields are present + # This ensures undiscriminated unions properly distinguish between member types + member_type.fields.each do |field_name, field| + raise Errors::TypeError, "Required field `#{field_name}` missing for union member #{member_type.name}" if candidate.instance_variable_get(:@data)[field_name].nil? && !field.optional + end + + true + rescue Errors::TypeError + false + end + end&.last&.call + end + end + end + + def coerce(value, strict: strict?) + type = resolve_member(value) + + unless type + return value unless strict + + if discriminated? + raise Errors::TypeError, + "value of type `#{value.class}` not member of union #{self}" + end + + raise Errors::TypeError, "could not resolve to member of union #{self}" + end + + coerced = Utils.coerce(type, value, strict: strict) + + # For discriminated unions, store the discriminant info on the coerced instance + # so it can be injected back during serialization (to_h) + if discriminated? && value.is_a?(::Hash) && coerced.is_a?(Model) + discriminant_value = value.fetch(@discriminant, nil) || value.fetch(@discriminant.to_s, nil) + if discriminant_value + coerced.instance_variable_set(:@_fern_union_discriminant_key, @discriminant.to_s) + coerced.instance_variable_set(:@_fern_union_discriminant_value, discriminant_value) + end + end + + coerced + end + + # Parse JSON string and coerce to the correct union member type + # + # @param str [String] JSON string to parse + # @return [Object] Coerced value matching a union member + def load(str) + coerce(::JSON.parse(str, symbolize_names: true)) + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/unknown.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/unknown.rb new file mode 100644 index 000000000000..7b58de956da9 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/unknown.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Types + module Unknown + include Seed::Internal::Types::Type + + def coerce(value) + value + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/utils.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/utils.rb new file mode 100644 index 000000000000..5a6eeb23b1b0 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/internal/types/utils.rb @@ -0,0 +1,116 @@ +# frozen_string_literal: true + +module Seed + module Internal + module Types + # Utilities for dealing with and checking types + module Utils + # Wraps a type into a type function + # + # @param type [Proc, Object] + # @return [Proc] + def self.wrap_type(type) + case type + when Proc + type + else + -> { type } + end + end + + # Resolves a type or type function into a type + # + # @param type [Proc, Object] + # @return [Object] + def self.unwrap_type(type) + type.is_a?(Proc) ? type.call : type + end + + def self.coerce(target, value, strict: false) + type = unwrap_type(target) + + case type + in Array + case value + when ::Array + return type.coerce(value, strict: strict) + when Set, ::Hash + return coerce(type, value.to_a) + end + in Hash + case value + when ::Hash + return type.coerce(value, strict: strict) + when ::Array + return coerce(type, value.to_h) + end + in ->(t) { t <= NilClass } + return nil + in ->(t) { t <= String } + case value + when String, Symbol, Numeric, TrueClass, FalseClass + return value.to_s + end + in ->(t) { t <= Symbol } + case value + when Symbol, String + return value.to_sym + end + in ->(t) { t <= Integer } + case value + when Numeric, String, Time + return value.to_i + end + in ->(t) { t <= Float } + case value + when Numeric, Time, String + return value.to_f + end + in ->(t) { t <= Model } + case value + when type + return value + when ::Hash + return type.coerce(value, strict: strict) + end + in Module + case type + in ->(t) { + t.singleton_class.include?(Enum) || + t.singleton_class.include?(Union) + } + return type.coerce(value, strict: strict) + else + value # rubocop:disable Lint/Void + end + else + value # rubocop:disable Lint/Void + end + + raise Errors::TypeError, "cannot coerce value of type `#{value.class}` to `#{target}`" if strict + + value + end + + def self.symbolize_keys(hash) + hash.transform_keys(&:to_sym) + end + + # Converts camelCase keys to snake_case symbols + # This allows SDK methods to accept both snake_case and camelCase keys + # e.g., { refundMethod: ... } becomes { refund_method: ... } + # + # @param hash [Hash] + # @return [Hash] + def self.normalize_keys(hash) + hash.transform_keys do |key| + key_str = key.to_s + # Convert camelCase to snake_case + snake_case = key_str.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase + snake_case.to_sym + end + end + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/version.rb b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/version.rb new file mode 100644 index 000000000000..00dd45cdd958 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/lib/seed/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module Seed + VERSION = "0.0.1" +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/reference.md b/seed/ruby-sdk-v2/basic-auth-optional/reference.md new file mode 100644 index 000000000000..7bfdfbd7ac04 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/reference.md @@ -0,0 +1,118 @@ +# Reference +## BasicAuth +
client.basic_auth.get_with_basic_auth() -> Internal::Types::Boolean +
+
+ +#### 📝 Description + +
+
+ +
+
+ +GET request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```ruby +client.basic_auth.get_with_basic_auth +``` +
+
+
+
+ +#### ⚙️ Parameters + +
+
+ +
+
+ +**request_options:** `Seed::BasicAuth::RequestOptions` + +
+
+
+
+ + +
+
+
+ +
client.basic_auth.post_with_basic_auth(request) -> Internal::Types::Boolean +
+
+ +#### 📝 Description + +
+
+ +
+
+ +POST request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```ruby +client.basic_auth.post_with_basic_auth +``` +
+
+
+
+ +#### ⚙️ Parameters + +
+
+ +
+
+ +**request:** `Object` + +
+
+ +
+
+ +**request_options:** `Seed::BasicAuth::RequestOptions` + +
+
+
+
+ + +
+
+
+ diff --git a/seed/ruby-sdk-v2/basic-auth-optional/seed.gemspec b/seed/ruby-sdk-v2/basic-auth-optional/seed.gemspec new file mode 100644 index 000000000000..aff5ff0c3c1c --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/seed.gemspec @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require_relative "lib/seed/version" +require_relative "custom.gemspec" + +# NOTE: A handful of these fields are required as part of the Ruby specification. +# You can change them here or overwrite them in the custom gemspec file. +Gem::Specification.new do |spec| + spec.name = "fern_basic-auth-optional" + spec.authors = ["Seed"] + spec.version = Seed::VERSION + spec.summary = "Ruby client library for the Seed API" + spec.description = "The Seed Ruby library provides convenient access to the Seed API from Ruby." + spec.required_ruby_version = ">= 3.3.0" + spec.metadata["rubygems_mfa_required"] = "true" + + # Specify which files should be added to the gem when it is released. + # The `git ls-files -z` loads the files in the RubyGem that have been added into git. + gemspec = File.basename(__FILE__) + spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls| + ls.readlines("\x0", chomp: true).reject do |f| + (f == gemspec) || + f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile]) + end + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + spec.add_dependency "base64" + # For more information and examples about making a new gem, check out our + # guide at: https://bundler.io/guides/creating_gem.html + + # Load custom gemspec configuration if it exists + custom_gemspec_file = File.join(__dir__, "custom.gemspec.rb") + add_custom_gemspec_data(spec) if File.exist?(custom_gemspec_file) +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/snippet.json b/seed/ruby-sdk-v2/basic-auth-optional/snippet.json new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/custom.test.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/custom.test.rb new file mode 100644 index 000000000000..4bd57989d43d --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/custom.test.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +# This is a custom test file, if you wish to add more tests +# to your SDK. +# Be sure to mark this file in `.fernignore`. +# +# If you include example requests/responses in your fern definition, +# you will have tests automatically generated for you. + +# This test is run via command line: rake customtest +describe "Custom Test" do + it "Default" do + refute false + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/test_helper.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/test_helper.rb new file mode 100644 index 000000000000..b086fe6d76ec --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/test_helper.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +require_relative "../lib/seed" diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/iterators/test_cursor_item_iterator.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/iterators/test_cursor_item_iterator.rb new file mode 100644 index 000000000000..44f85cb20b35 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/iterators/test_cursor_item_iterator.rb @@ -0,0 +1,189 @@ +# frozen_string_literal: true + +require "minitest/autorun" +require "stringio" +require "json" +require "test_helper" + +NUMBERS = (1..65).to_a +PageResponse = Struct.new(:cards, :next_cursor) + +class CursorItemIteratorTest < Minitest::Test + def make_iterator(initial_cursor:) + @times_called = 0 + + Seed::Internal::CursorItemIterator.new(initial_cursor:, cursor_field: :next_cursor, item_field: :cards) do |cursor| + @times_called += 1 + cursor ||= 0 + next_cursor = cursor + 10 + PageResponse.new( + cards: NUMBERS[cursor...next_cursor], + next_cursor: next_cursor < NUMBERS.length ? next_cursor : nil + ) + end + end + + def test_item_iterator_can_iterate_to_exhaustion + iterator = make_iterator(initial_cursor: 0) + + assert_equal NUMBERS, iterator.to_a + assert_equal 7, @times_called + + iterator = make_iterator(initial_cursor: 10) + + assert_equal (11..65).to_a, iterator.to_a + + iterator = make_iterator(initial_cursor: 5) + + assert_equal (6..65).to_a, iterator.to_a + end + + def test_item_iterator_can_work_without_an_initial_cursor + iterator = make_iterator(initial_cursor: nil) + + assert_equal NUMBERS, iterator.to_a + assert_equal 7, @times_called + end + + def test_items_iterator_iterates_lazily + iterator = make_iterator(initial_cursor: 0) + + assert_equal 0, @times_called + assert_equal 1, iterator.first + assert_equal 1, @times_called + + iterator = make_iterator(initial_cursor: 0) + + assert_equal 0, @times_called + assert_equal (1..15).to_a, iterator.first(15) + assert_equal 2, @times_called + + iterator = make_iterator(initial_cursor: 0) + + assert_equal 0, @times_called + iterator.each do |card| + break if card >= 15 + end + + assert_equal 2, @times_called + end + + def test_items_iterator_implements_enumerable + iterator = make_iterator(initial_cursor: 0) + + assert_equal 0, @times_called + doubled = iterator.map { |card| card * 2 } + + assert_equal 7, @times_called + assert_equal NUMBERS.length, doubled.length + end + + def test_items_iterator_can_be_advanced_manually + iterator = make_iterator(initial_cursor: 0) + + assert_equal 0, @times_called + + items = [] + expected_times_called = 0 + while (item = iterator.next_element) + expected_times_called += 1 if (item % 10) == 1 + + assert_equal expected_times_called, @times_called + assert_equal item != NUMBERS.last, iterator.next?, "#{item} #{iterator}" + items.push(item) + end + + assert_equal 7, @times_called + assert_equal NUMBERS, items + end + + def test_pages_iterator + iterator = make_iterator(initial_cursor: 0).pages + + assert_equal( + [ + (1..10).to_a, + (11..20).to_a, + (21..30).to_a, + (31..40).to_a, + (41..50).to_a, + (51..60).to_a, + (61..65).to_a + ], + iterator.to_a.map(&:cards) + ) + + iterator = make_iterator(initial_cursor: 10).pages + + assert_equal( + [ + (11..20).to_a, + (21..30).to_a, + (31..40).to_a, + (41..50).to_a, + (51..60).to_a, + (61..65).to_a + ], + iterator.to_a.map(&:cards) + ) + end + + def test_pages_iterator_can_work_without_an_initial_cursor + iterator = make_iterator(initial_cursor: nil).pages + + assert_equal 7, iterator.to_a.length + assert_equal 7, @times_called + end + + def test_pages_iterator_iterates_lazily + iterator = make_iterator(initial_cursor: 0).pages + + assert_equal 0, @times_called + iterator.first + + assert_equal 1, @times_called + + iterator = make_iterator(initial_cursor: 0).pages + + assert_equal 0, @times_called + assert_equal 2, iterator.first(2).length + assert_equal 2, @times_called + end + + def test_pages_iterator_knows_whether_another_page_is_upcoming + iterator = make_iterator(initial_cursor: 0).pages + + iterator.each_with_index do |_page, index| + assert_equal index + 1, @times_called + assert_equal index < 6, iterator.next? + end + end + + def test_pages_iterator_can_be_advanced_manually + iterator = make_iterator(initial_cursor: 0).pages + + assert_equal 0, @times_called + + lengths = [] + expected_times_called = 0 + while (page = iterator.next_page) + expected_times_called += 1 + + assert_equal expected_times_called, @times_called + lengths.push(page.cards.length) + end + + assert_equal 7, @times_called + assert_equal [10, 10, 10, 10, 10, 10, 5], lengths + end + + def test_pages_iterator_implements_enumerable + iterator = make_iterator(initial_cursor: 0).pages + + assert_equal 0, @times_called + lengths = iterator.map { |page| page.cards.length } + + assert_equal 7, @times_called + assert_equal [10, 10, 10, 10, 10, 10, 5], lengths + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/iterators/test_offset_item_iterator.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/iterators/test_offset_item_iterator.rb new file mode 100644 index 000000000000..004f394f0a41 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/iterators/test_offset_item_iterator.rb @@ -0,0 +1,151 @@ +# frozen_string_literal: true + +require "minitest/autorun" +require "stringio" +require "json" +require "test_helper" + +OffsetPageResponse = Struct.new(:items, :has_next) +TestIteratorConfig = Struct.new( + :step, + :has_next_field, + :total_item_count, + :per_page, + :initial_page +) do + def first_item_returned + if step + (initial_page || 0) + 1 + else + (((initial_page || 1) - 1) * per_page) + 1 + end + end +end + +LAZY_TEST_ITERATOR_CONFIG = TestIteratorConfig.new(initial_page: 1, step: false, has_next_field: :has_next, total_item_count: 65, per_page: 10) +ALL_TEST_ITERATOR_CONFIGS = [true, false].map do |step| + [:has_next, nil].map do |has_next_field| + [0, 5, 10, 60, 63].map do |total_item_count| + [5, 10].map do |per_page| + initial_pages = [nil, 3, 100] + initial_pages << (step ? 0 : 1) + + initial_pages.map do |initial_page| + TestIteratorConfig.new( + step: step, + has_next_field: has_next_field, + total_item_count: total_item_count, + per_page: per_page, + initial_page: initial_page + ) + end + end + end + end +end.flatten + +class OffsetItemIteratorTest < Minitest::Test + def make_iterator(config) + @times_called = 0 + + items = (1..config.total_item_count).to_a + + Seed::Internal::OffsetItemIterator.new( + initial_page: config.initial_page, + item_field: :items, + has_next_field: config.has_next_field, + step: config.step + ) do |page| + @times_called += 1 + + slice_start = config.step ? page : (page - 1) * config.per_page + slice_end = slice_start + config.per_page + + output = { + items: items[slice_start...slice_end] + } + output[config.has_next_field] = slice_end < items.length if config.has_next_field + + OffsetPageResponse.new(**output) + end + end + + def test_item_iterator_can_iterate_to_exhaustion + ALL_TEST_ITERATOR_CONFIGS.each do |config| + iterator = make_iterator(config) + + assert_equal (config.first_item_returned..config.total_item_count).to_a, iterator.to_a + end + end + + def test_items_iterator_can_be_advanced_manually_and_has_accurate_has_next + ALL_TEST_ITERATOR_CONFIGS.each do |config| + iterator = make_iterator(config) + items = [] + + while (item = iterator.next_element) + assert_equal(item != config.total_item_count, iterator.next?, "#{item} #{iterator}") + items.push(item) + end + + assert_equal (config.first_item_returned..config.total_item_count).to_a, items + end + end + + def test_pages_iterator_can_be_advanced_manually_and_has_accurate_has_next + ALL_TEST_ITERATOR_CONFIGS.each do |config| + iterator = make_iterator(config).pages + pages = [] + + loop do + has_next_output = iterator.next? + page = iterator.next_page + + assert_equal(has_next_output, !page.nil?, "next? was inaccurate: #{config} #{iterator.inspect}") + break if page.nil? + + pages.push(page) + end + + assert_equal pages, make_iterator(config).pages.to_a + end + end + + def test_items_iterator_iterates_lazily + iterator = make_iterator(LAZY_TEST_ITERATOR_CONFIG) + + assert_equal 0, @times_called + assert_equal 1, iterator.first + assert_equal 1, @times_called + + iterator = make_iterator(LAZY_TEST_ITERATOR_CONFIG) + + assert_equal 0, @times_called + assert_equal (1..15).to_a, iterator.first(15) + assert_equal 2, @times_called + + iterator = make_iterator(LAZY_TEST_ITERATOR_CONFIG) + + assert_equal 0, @times_called + iterator.each do |card| + break if card >= 15 + end + + assert_equal 2, @times_called + end + + def test_pages_iterator_iterates_lazily + iterator = make_iterator(LAZY_TEST_ITERATOR_CONFIG).pages + + assert_equal 0, @times_called + iterator.first + + assert_equal 1, @times_called + + iterator = make_iterator(LAZY_TEST_ITERATOR_CONFIG).pages + + assert_equal 0, @times_called + assert_equal 3, iterator.first(3).length + assert_equal 3, @times_called + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_array.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_array.rb new file mode 100644 index 000000000000..e7e6571f03ee --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_array.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require "test_helper" + +describe Seed::Internal::Types::Array do + module TestArray + StringArray = Seed::Internal::Types::Array[String] + end + + describe "#initialize" do + it "sets the type" do + assert_equal String, TestArray::StringArray.type + end + end + + describe "#coerce" do + it "does not perform coercion if not an array" do + assert_equal 1, TestArray::StringArray.coerce(1) + end + + it "raises an error if not an array and strictness is on" do + assert_raises Seed::Internal::Errors::TypeError do + TestArray::StringArray.coerce(1, strict: true) + end + end + + it "coerces the elements" do + assert_equal %w[foobar 1 true], TestArray::StringArray.coerce(["foobar", 1, true]) + end + + it "raises an error if element of array is not coercable and strictness is on" do + assert_raises Seed::Internal::Errors::TypeError do + TestArray::StringArray.coerce([Object.new], strict: true) + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_boolean.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_boolean.rb new file mode 100644 index 000000000000..cba18e48765b --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_boolean.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require "test_helper" + +describe Seed::Internal::Types::Boolean do + describe ".coerce" do + it "coerces true/false" do + assert Seed::Internal::Types::Boolean.coerce(true) + refute Seed::Internal::Types::Boolean.coerce(false) + end + + it "coerces an Integer" do + assert Seed::Internal::Types::Boolean.coerce(1) + refute Seed::Internal::Types::Boolean.coerce(0) + end + + it "coerces a String" do + assert Seed::Internal::Types::Boolean.coerce("1") + assert Seed::Internal::Types::Boolean.coerce("true") + refute Seed::Internal::Types::Boolean.coerce("0") + end + + it "passes through other values with strictness off" do + obj = Object.new + + assert_equal obj, Seed::Internal::Types::Boolean.coerce(obj) + end + + it "raises an error with other values with strictness on" do + assert_raises Seed::Internal::Errors::TypeError do + Seed::Internal::Types::Boolean.coerce(Object.new, strict: true) + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_enum.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_enum.rb new file mode 100644 index 000000000000..e8d89bce467f --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_enum.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require "test_helper" + +describe Seed::Internal::Types::Enum do + module EnumTest + module ExampleEnum + extend Seed::Internal::Types::Enum + + FOO = :foo + BAR = :bar + + finalize! + end + end + + describe "#values" do + it "defines values" do + assert_equal %i[foo bar].sort, EnumTest::ExampleEnum.values.sort + end + end + + describe "#coerce" do + it "coerces an existing member" do + assert_equal :foo, EnumTest::ExampleEnum.coerce(:foo) + end + + it "coerces a string version of a member" do + assert_equal :foo, EnumTest::ExampleEnum.coerce("foo") + end + + it "returns the value if not a member with strictness off" do + assert_equal 1, EnumTest::ExampleEnum.coerce(1) + end + + it "raises an error if value is not a member with strictness on" do + assert_raises Seed::Internal::Errors::TypeError do + EnumTest::ExampleEnum.coerce(1, strict: true) + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_hash.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_hash.rb new file mode 100644 index 000000000000..6c5e58a6a946 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_hash.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require "test_helper" + +describe Seed::Internal::Types::Hash do + module TestHash + SymbolStringHash = Seed::Internal::Types::Hash[Symbol, String] + end + + describe ".[]" do + it "defines the key and value type" do + assert_equal Symbol, TestHash::SymbolStringHash.key_type + assert_equal String, TestHash::SymbolStringHash.value_type + end + end + + describe "#coerce" do + it "coerces the keys" do + assert_equal %i[foo bar], TestHash::SymbolStringHash.coerce({ "foo" => "1", :bar => "2" }).keys + end + + it "coerces the values" do + assert_equal %w[foo 1], TestHash::SymbolStringHash.coerce({ foo: :foo, bar: 1 }).values + end + + it "passes through other values with strictness off" do + obj = Object.new + + assert_equal obj, TestHash::SymbolStringHash.coerce(obj) + end + + it "raises an error with other values with strictness on" do + assert_raises Seed::Internal::Errors::TypeError do + TestHash::SymbolStringHash.coerce(Object.new, strict: true) + end + end + + it "raises an error with non-coercable key types with strictness on" do + assert_raises Seed::Internal::Errors::TypeError do + TestHash::SymbolStringHash.coerce({ Object.new => 1 }, strict: true) + end + end + + it "raises an error with non-coercable value types with strictness on" do + assert_raises Seed::Internal::Errors::TypeError do + TestHash::SymbolStringHash.coerce({ "foobar" => Object.new }, strict: true) + end + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_model.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_model.rb new file mode 100644 index 000000000000..3d87b9f5a8c7 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_model.rb @@ -0,0 +1,154 @@ +# frozen_string_literal: true + +require "test_helper" + +describe Seed::Internal::Types::Model do + module StringInteger + extend Seed::Internal::Types::Union + + member String + member Integer + end + + class ExampleModel < Seed::Internal::Types::Model + field :name, String + field :rating, StringInteger, optional: true + field :year, Integer, optional: true, nullable: true, api_name: "yearOfRelease" + end + + class ExampleModelInheritance < ExampleModel + field :director, String + end + + class ExampleWithDefaults < ExampleModel + field :type, String, default: "example" + end + + class ExampleChild < Seed::Internal::Types::Model + field :value, String + end + + class ExampleParent < Seed::Internal::Types::Model + field :child, ExampleChild + end + + describe ".field" do + before do + @example = ExampleModel.new(name: "Inception", rating: 4) + end + + it "defines fields on model" do + assert_equal %i[name rating year], ExampleModel.fields.keys + end + + it "defines fields from parent models" do + assert_equal %i[name rating year director], ExampleModelInheritance.fields.keys + end + + it "sets the field's type" do + assert_equal String, ExampleModel.fields[:name].type + assert_equal StringInteger, ExampleModel.fields[:rating].type + end + + it "sets the `default` option" do + assert_equal "example", ExampleWithDefaults.fields[:type].default + end + + it "defines getters" do + assert_respond_to @example, :name + assert_respond_to @example, :rating + + assert_equal "Inception", @example.name + assert_equal 4, @example.rating + end + + it "defines setters" do + assert_respond_to @example, :name= + assert_respond_to @example, :rating= + + @example.name = "Inception 2" + @example.rating = 5 + + assert_equal "Inception 2", @example.name + assert_equal 5, @example.rating + end + end + + describe "#initialize" do + it "sets the data" do + example = ExampleModel.new(name: "Inception", rating: 4) + + assert_equal "Inception", example.name + assert_equal 4, example.rating + end + + it "allows extra fields to be set" do + example = ExampleModel.new(name: "Inception", rating: 4, director: "Christopher Nolan") + + assert_equal "Christopher Nolan", example.director + end + + it "sets the defaults where applicable" do + example_using_defaults = ExampleWithDefaults.new + + assert_equal "example", example_using_defaults.type + + example_without_defaults = ExampleWithDefaults.new(type: "not example") + + assert_equal "not example", example_without_defaults.type + end + + it "coerces child models" do + parent = ExampleParent.new(child: { value: "foobar" }) + + assert_kind_of ExampleChild, parent.child + end + + it "uses the api_name to pull the value" do + example = ExampleModel.new({ name: "Inception", yearOfRelease: 2014 }) + + assert_equal 2014, example.year + refute_respond_to example, :yearOfRelease + end + end + + describe "#inspect" do + class SensitiveModel < Seed::Internal::Types::Model + field :username, String + field :password, String + field :client_secret, String + field :access_token, String + field :api_key, String + end + + it "redacts sensitive fields" do + model = SensitiveModel.new( + username: "user123", + password: "secret123", + client_secret: "cs_abc", + access_token: "token_xyz", + api_key: "key_123" + ) + + inspect_output = model.inspect + + assert_includes inspect_output, "username=\"user123\"" + assert_includes inspect_output, "password=[REDACTED]" + assert_includes inspect_output, "client_secret=[REDACTED]" + assert_includes inspect_output, "access_token=[REDACTED]" + assert_includes inspect_output, "api_key=[REDACTED]" + refute_includes inspect_output, "secret123" + refute_includes inspect_output, "cs_abc" + refute_includes inspect_output, "token_xyz" + refute_includes inspect_output, "key_123" + end + + it "does not redact non-sensitive fields" do + example = ExampleModel.new(name: "Inception", rating: 4) + inspect_output = example.inspect + + assert_includes inspect_output, "name=\"Inception\"" + assert_includes inspect_output, "rating=4" + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_union.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_union.rb new file mode 100644 index 000000000000..e4e95c93139f --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_union.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require "test_helper" + +describe Seed::Internal::Types::Union do + class Rectangle < Seed::Internal::Types::Model + literal :type, "square" + + field :area, Float + end + + class Circle < Seed::Internal::Types::Model + literal :type, "circle" + + field :area, Float + end + + class Pineapple < Seed::Internal::Types::Model + literal :type, "pineapple" + + field :area, Float + end + + module Shape + extend Seed::Internal::Types::Union + + discriminant :type + + member -> { Rectangle }, key: "rect" + member -> { Circle }, key: "circle" + end + + module StringOrInteger + extend Seed::Internal::Types::Union + + member String + member Integer + end + + describe "#coerce" do + it "coerces hashes into member models with discriminated unions" do + circle = Shape.coerce({ type: "circle", area: 4.0 }) + + assert_instance_of Circle, circle + end + end + + describe "#type_member?" do + it "defines Model members" do + assert Shape.type_member?(Rectangle) + assert Shape.type_member?(Circle) + refute Shape.type_member?(Pineapple) + end + + it "defines other members" do + assert StringOrInteger.type_member?(String) + assert StringOrInteger.type_member?(Integer) + refute StringOrInteger.type_member?(Float) + refute StringOrInteger.type_member?(Pineapple) + end + end +end diff --git a/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_utils.rb b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_utils.rb new file mode 100644 index 000000000000..29d14621a229 --- /dev/null +++ b/seed/ruby-sdk-v2/basic-auth-optional/test/unit/internal/types/test_utils.rb @@ -0,0 +1,212 @@ +# frozen_string_literal: true + +require "test_helper" + +describe Seed::Internal::Types::Utils do + Utils = Seed::Internal::Types::Utils + + module TestUtils + class M < Seed::Internal::Types::Model + field :value, String + end + + class UnionMemberA < Seed::Internal::Types::Model + literal :type, "A" + field :only_on_a, String + end + + class UnionMemberB < Seed::Internal::Types::Model + literal :type, "B" + field :only_on_b, String + end + + module U + extend Seed::Internal::Types::Union + + discriminant :type + + member -> { UnionMemberA }, key: "A" + member -> { UnionMemberB }, key: "B" + end + + SymbolStringHash = Seed::Internal::Types::Hash[Symbol, String] + SymbolModelHash = -> { Seed::Internal::Types::Hash[Symbol, TestUtils::M] } + end + + describe ".coerce" do + describe "NilClass" do + it "always returns nil" do + assert_nil Utils.coerce(NilClass, "foobar") + assert_nil Utils.coerce(NilClass, 1) + assert_nil Utils.coerce(NilClass, Object.new) + end + end + + describe "String" do + it "coerces from String, Symbol, Numeric, or Boolean" do + assert_equal "foobar", Utils.coerce(String, "foobar") + assert_equal "foobar", Utils.coerce(String, :foobar) + assert_equal "1", Utils.coerce(String, 1) + assert_equal "1.0", Utils.coerce(String, 1.0) + assert_equal "true", Utils.coerce(String, true) + end + + it "passes through value if it cannot be coerced and not strict" do + obj = Object.new + + assert_equal obj, Utils.coerce(String, obj) + end + + it "raises an error if value cannot be coerced and strict" do + assert_raises Seed::Internal::Errors::TypeError do + Utils.coerce(String, Object.new, strict: true) + end + end + end + + describe "Symbol" do + it "coerces from Symbol, String" do + assert_equal :foobar, Utils.coerce(Symbol, :foobar) + assert_equal :foobar, Utils.coerce(Symbol, "foobar") + end + + it "passes through value if it cannot be coerced and not strict" do + obj = Object.new + + assert_equal obj, Utils.coerce(Symbol, obj) + end + + it "raises an error if value cannot be coerced and strict" do + assert_raises Seed::Internal::Errors::TypeError do + Utils.coerce(Symbol, Object.new, strict: true) + end + end + end + + describe "Integer" do + it "coerces from Numeric, String, Time" do + assert_equal 1, Utils.coerce(Integer, 1) + assert_equal 1, Utils.coerce(Integer, 1.0) + assert_equal 1, Utils.coerce(Integer, Complex.rect(1)) + assert_equal 1, Utils.coerce(Integer, Rational(1)) + assert_equal 1, Utils.coerce(Integer, "1") + assert_equal 1_713_916_800, Utils.coerce(Integer, Time.utc(2024, 4, 24)) + end + + it "passes through value if it cannot be coerced and not strict" do + obj = Object.new + + assert_equal obj, Utils.coerce(Integer, obj) + end + + it "raises an error if value cannot be coerced and strict" do + assert_raises Seed::Internal::Errors::TypeError do + Utils.coerce(Integer, Object.new, strict: true) + end + end + end + + describe "Float" do + it "coerces from Numeric, Time" do + assert_in_delta(1.0, Utils.coerce(Float, 1.0)) + assert_in_delta(1.0, Utils.coerce(Float, 1)) + assert_in_delta(1.0, Utils.coerce(Float, Complex.rect(1))) + assert_in_delta(1.0, Utils.coerce(Float, Rational(1))) + assert_in_delta(1_713_916_800.0, Utils.coerce(Integer, Time.utc(2024, 4, 24))) + end + + it "passes through value if it cannot be coerced and not strict" do + obj = Object.new + + assert_equal obj, Utils.coerce(Float, obj) + end + + it "raises an error if value cannot be coerced and strict" do + assert_raises Seed::Internal::Errors::TypeError do + Utils.coerce(Float, Object.new, strict: true) + end + end + end + + describe "Model" do + it "coerces a hash" do + result = Utils.coerce(TestUtils::M, { value: "foobar" }) + + assert_kind_of TestUtils::M, result + assert_equal "foobar", result.value + end + + it "coerces a hash when the target is a type function" do + result = Utils.coerce(-> { TestUtils::M }, { value: "foobar" }) + + assert_kind_of TestUtils::M, result + assert_equal "foobar", result.value + end + + it "will not coerce non-hashes" do + assert_equal "foobar", Utils.coerce(TestUtils::M, "foobar") + end + end + + describe "Enum" do + module ExampleEnum + extend Seed::Internal::Types::Enum + + FOO = :FOO + BAR = :BAR + + finalize! + end + + it "coerces into a Symbol version of the member value" do + assert_equal :FOO, Utils.coerce(ExampleEnum, "FOO") + end + + it "returns given value if not a member" do + assert_equal "NOPE", Utils.coerce(ExampleEnum, "NOPE") + end + end + + describe "Array" do + StringArray = Seed::Internal::Types::Array[String] + ModelArray = -> { Seed::Internal::Types::Array[TestUtils::M] } + UnionArray = -> { Seed::Internal::Types::Array[TestUtils::U] } + + it "coerces an array of literals" do + assert_equal %w[a b c], Utils.coerce(StringArray, %w[a b c]) + assert_equal ["1", "2.0", "true"], Utils.coerce(StringArray, [1, 2.0, true]) + assert_equal ["1", "2.0", "true"], Utils.coerce(StringArray, Set.new([1, 2.0, true])) + end + + it "coerces an array of Models" do + assert_equal [TestUtils::M.new(value: "foobar"), TestUtils::M.new(value: "bizbaz")], + Utils.coerce(ModelArray, [{ value: "foobar" }, { value: "bizbaz" }]) + + assert_equal [TestUtils::M.new(value: "foobar"), TestUtils::M.new(value: "bizbaz")], + Utils.coerce(ModelArray, [TestUtils::M.new(value: "foobar"), TestUtils::M.new(value: "bizbaz")]) + end + + it "coerces an array of model unions" do + assert_equal [TestUtils::UnionMemberA.new(type: "A", only_on_a: "A"), TestUtils::UnionMemberB.new(type: "B", only_on_b: "B")], + Utils.coerce(UnionArray, [{ type: "A", only_on_a: "A" }, { type: "B", only_on_b: "B" }]) + end + + it "returns given value if not an array" do + assert_equal 1, Utils.coerce(StringArray, 1) + end + end + + describe "Hash" do + it "coerces the keys and values" do + ssh_res = Utils.coerce(TestUtils::SymbolStringHash, { "foo" => "bar", "biz" => "2" }) + + assert_equal "bar", ssh_res[:foo] + assert_equal "2", ssh_res[:biz] + + smh_res = Utils.coerce(TestUtils::SymbolModelHash, { "foo" => { "value" => "foo" } }) + + assert_equal TestUtils::M.new(value: "foo"), smh_res[:foo] + end + end + end +end diff --git a/seed/ts-sdk/basic-auth-optional/.fern/metadata.json b/seed/ts-sdk/basic-auth-optional/.fern/metadata.json new file mode 100644 index 000000000000..9da357e1fedb --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/.fern/metadata.json @@ -0,0 +1,7 @@ +{ + "cliVersion": "DUMMY", + "generatorName": "fernapi/fern-typescript-sdk", + "generatorVersion": "latest", + "originGitCommit": "DUMMY", + "sdkVersion": "0.0.1" +} diff --git a/seed/ts-sdk/basic-auth-optional/.github/workflows/ci.yml b/seed/ts-sdk/basic-auth-optional/.github/workflows/ci.yml new file mode 100644 index 000000000000..93fba226cb67 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/.github/workflows/ci.yml @@ -0,0 +1,46 @@ +name: ci + +on: [push] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + compile: + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v6 + + - name: Set up node + uses: actions/setup-node@v6 + + - name: Install pnpm + uses: pnpm/action-setup@v4 + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Compile + run: pnpm build + + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout repo + uses: actions/checkout@v6 + + - name: Set up node + uses: actions/setup-node@v6 + + - name: Install pnpm + uses: pnpm/action-setup@v4 + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Test + run: pnpm test diff --git a/seed/ts-sdk/basic-auth-optional/.gitignore b/seed/ts-sdk/basic-auth-optional/.gitignore new file mode 100644 index 000000000000..72271e049c02 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/.gitignore @@ -0,0 +1,3 @@ +node_modules +.DS_Store +/dist \ No newline at end of file diff --git a/seed/ts-sdk/basic-auth-optional/CONTRIBUTING.md b/seed/ts-sdk/basic-auth-optional/CONTRIBUTING.md new file mode 100644 index 000000000000..fe5bc2f77e0b --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/CONTRIBUTING.md @@ -0,0 +1,133 @@ +# Contributing + +Thanks for your interest in contributing to this SDK! This document provides guidelines for contributing to the project. + +## Getting Started + +### Prerequisites + +- Node.js 20 or higher +- pnpm package manager + +### Installation + +Install the project dependencies: + +```bash +pnpm install +``` + +### Building + +Build the project: + +```bash +pnpm build +``` + +### Testing + +Run the test suite: + +```bash +pnpm test +``` + +Run specific test types: +- `pnpm test:unit` - Run unit tests +- `pnpm test:wire` - Run wire/integration tests + +### Linting and Formatting + +Check code style: + +```bash +pnpm run lint +pnpm run format:check +``` + +Fix code style issues: + +```bash +pnpm run lint:fix +pnpm run format:fix +``` + +Or use the combined check command: + +```bash +pnpm run check:fix +``` + +## About Generated Code + +**Important**: Most files in this SDK are automatically generated by [Fern](https://buildwithfern.com) from the API definition. Direct modifications to generated files will be overwritten the next time the SDK is generated. + +### Generated Files + +The following directories contain generated code: +- `src/api/` - API client classes and types +- `src/serialization/` - Serialization/deserialization logic +- Most TypeScript files in `src/` + +### How to Customize + +If you need to customize the SDK, you have two options: + +#### Option 1: Use `.fernignore` + +For custom code that should persist across SDK regenerations: + +1. Create a `.fernignore` file in the project root +2. Add file patterns for files you want to preserve (similar to `.gitignore` syntax) +3. Add your custom code to those files + +Files listed in `.fernignore` will not be overwritten when the SDK is regenerated. + +For more information, see the [Fern documentation on custom code](https://buildwithfern.com/learn/sdks/overview/custom-code). + +#### Option 2: Contribute to the Generator + +If you want to change how code is generated for all users of this SDK: + +1. The TypeScript SDK generator lives in the [Fern repository](https://github.com/fern-api/fern) +2. Generator code is located at `generators/typescript/sdk/` +3. Follow the [Fern contributing guidelines](https://github.com/fern-api/fern/blob/main/CONTRIBUTING.md) +4. Submit a pull request with your changes to the generator + +This approach is best for: +- Bug fixes in generated code +- New features that would benefit all users +- Improvements to code generation patterns + +## Making Changes + +### Workflow + +1. Create a new branch for your changes +2. Make your modifications +3. Run tests to ensure nothing breaks: `pnpm test` +4. Run linting and formatting: `pnpm run check:fix` +5. Build the project: `pnpm build` +6. Commit your changes with a clear commit message +7. Push your branch and create a pull request + +### Commit Messages + +Write clear, descriptive commit messages that explain what changed and why. + +### Code Style + +This project uses automated code formatting and linting. Run `pnpm run check:fix` before committing to ensure your code meets the project's style guidelines. + +## Questions or Issues? + +If you have questions or run into issues: + +1. Check the [Fern documentation](https://buildwithfern.com) +2. Search existing [GitHub issues](https://github.com/fern-api/fern/issues) +3. Open a new issue if your question hasn't been addressed + +## License + +By contributing to this project, you agree that your contributions will be licensed under the same license as the project. diff --git a/seed/ts-sdk/basic-auth-optional/README.md b/seed/ts-sdk/basic-auth-optional/README.md new file mode 100644 index 000000000000..406691091aa6 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/README.md @@ -0,0 +1,275 @@ +# Seed TypeScript Library + +[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=Seed%2FTypeScript) +[![npm shield](https://img.shields.io/npm/v/@fern/basic-auth-optional)](https://www.npmjs.com/package/@fern/basic-auth-optional) + +The Seed TypeScript library provides convenient access to the Seed APIs from TypeScript. + +## Table of Contents + +- [Installation](#installation) +- [Reference](#reference) +- [Usage](#usage) +- [Exception Handling](#exception-handling) +- [Advanced](#advanced) + - [Subpackage Exports](#subpackage-exports) + - [Additional Headers](#additional-headers) + - [Additional Query String Parameters](#additional-query-string-parameters) + - [Retries](#retries) + - [Timeouts](#timeouts) + - [Aborting Requests](#aborting-requests) + - [Access Raw Response Data](#access-raw-response-data) + - [Logging](#logging) + - [Custom Fetch](#custom-fetch) + - [Runtime Compatibility](#runtime-compatibility) +- [Contributing](#contributing) + +## Installation + +```sh +npm i -s @fern/basic-auth-optional +``` + +## Reference + +A full reference for this library is available [here](./reference.md). + +## Usage + +Instantiate and use the client with the following: + +```typescript +import { SeedBasicAuthOptionalClient } from "@fern/basic-auth-optional"; + +const client = new SeedBasicAuthOptionalClient({ environment: "YOUR_BASE_URL", username: "YOUR_USERNAME", password: "YOUR_PASSWORD" }); +await client.basicAuth.postWithBasicAuth({ + "key": "value" +}); +``` + +## Exception Handling + +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error +will be thrown. + +```typescript +import { SeedBasicAuthOptionalError } from "@fern/basic-auth-optional"; + +try { + await client.basicAuth.postWithBasicAuth(...); +} catch (err) { + if (err instanceof SeedBasicAuthOptionalError) { + console.log(err.statusCode); + console.log(err.message); + console.log(err.body); + console.log(err.rawResponse); + } +} +``` + +## Advanced + +### Subpackage Exports + +This SDK supports direct imports of subpackage clients, which allows JavaScript bundlers to tree-shake and include only the imported subpackage code. This results in much smaller bundle sizes. + +```typescript +import { BasicAuthClient } from '@fern/basic-auth-optional/basicAuth'; + +const client = new BasicAuthClient({...}); +``` + +### Additional Headers + +If you would like to send additional headers as part of the request, use the `headers` request option. + +```typescript +import { SeedBasicAuthOptionalClient } from "@fern/basic-auth-optional"; + +const client = new SeedBasicAuthOptionalClient({ + ... + headers: { + 'X-Custom-Header': 'custom value' + } +}); + +const response = await client.basicAuth.postWithBasicAuth(..., { + headers: { + 'X-Custom-Header': 'custom value' + } +}); +``` + +### Additional Query String Parameters + +If you would like to send additional query string parameters as part of the request, use the `queryParams` request option. + +```typescript +const response = await client.basicAuth.postWithBasicAuth(..., { + queryParams: { + 'customQueryParamKey': 'custom query param value' + } +}); +``` + +### Retries + +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured +retry limit (default: 2). + +A request is deemed retryable when any of the following HTTP status codes is returned: + +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) + +Use the `maxRetries` request option to configure this behavior. + +```typescript +const response = await client.basicAuth.postWithBasicAuth(..., { + maxRetries: 0 // override maxRetries at the request level +}); +``` + +### Timeouts + +The SDK defaults to a 60 second timeout. Use the `timeoutInSeconds` option to configure this behavior. + +```typescript +const response = await client.basicAuth.postWithBasicAuth(..., { + timeoutInSeconds: 30 // override timeout to 30s +}); +``` + +### Aborting Requests + +The SDK allows users to abort requests at any point by passing in an abort signal. + +```typescript +const controller = new AbortController(); +const response = await client.basicAuth.postWithBasicAuth(..., { + abortSignal: controller.signal +}); +controller.abort(); // aborts the request +``` + +### Access Raw Response Data + +The SDK provides access to raw response data, including headers, through the `.withRawResponse()` method. +The `.withRawResponse()` method returns a promise that results to an object with a `data` and a `rawResponse` property. + +```typescript +const { data, rawResponse } = await client.basicAuth.postWithBasicAuth(...).withRawResponse(); + +console.log(data); +console.log(rawResponse.headers['X-My-Header']); +``` + +### Logging + +The SDK supports logging. You can configure the logger by passing in a `logging` object to the client options. + +```typescript +import { SeedBasicAuthOptionalClient, logging } from "@fern/basic-auth-optional"; + +const client = new SeedBasicAuthOptionalClient({ + ... + logging: { + level: logging.LogLevel.Debug, // defaults to logging.LogLevel.Info + logger: new logging.ConsoleLogger(), // defaults to ConsoleLogger + silent: false, // defaults to true, set to false to enable logging + } +}); +``` +The `logging` object can have the following properties: +- `level`: The log level to use. Defaults to `logging.LogLevel.Info`. +- `logger`: The logger to use. Defaults to a `logging.ConsoleLogger`. +- `silent`: Whether to silence the logger. Defaults to `true`. + +The `level` property can be one of the following values: +- `logging.LogLevel.Debug` +- `logging.LogLevel.Info` +- `logging.LogLevel.Warn` +- `logging.LogLevel.Error` + +To provide a custom logger, you can pass in an object that implements the `logging.ILogger` interface. + +
+Custom logger examples + +Here's an example using the popular `winston` logging library. +```ts +import winston from 'winston'; + +const winstonLogger = winston.createLogger({...}); + +const logger: logging.ILogger = { + debug: (msg, ...args) => winstonLogger.debug(msg, ...args), + info: (msg, ...args) => winstonLogger.info(msg, ...args), + warn: (msg, ...args) => winstonLogger.warn(msg, ...args), + error: (msg, ...args) => winstonLogger.error(msg, ...args), +}; +``` + +Here's an example using the popular `pino` logging library. + +```ts +import pino from 'pino'; + +const pinoLogger = pino({...}); + +const logger: logging.ILogger = { + debug: (msg, ...args) => pinoLogger.debug(args, msg), + info: (msg, ...args) => pinoLogger.info(args, msg), + warn: (msg, ...args) => pinoLogger.warn(args, msg), + error: (msg, ...args) => pinoLogger.error(args, msg), +}; +``` +
+ + +### Custom Fetch + +The SDK provides a low-level `fetch` method for making custom HTTP requests while still +benefiting from SDK-level configuration like authentication, retries, timeouts, and logging. +This is useful for calling API endpoints not yet supported in the SDK. + +```typescript +const response = await client.fetch("/v1/custom/endpoint", { + method: "GET", +}, { + timeoutInSeconds: 30, + maxRetries: 3, + headers: { + "X-Custom-Header": "custom-value", + }, +}); + +const data = await response.json(); +``` + +### Runtime Compatibility + + +The SDK works in the following runtimes: + + + +- Node.js 18+ +- Vercel +- Cloudflare Workers +- Deno v1.25+ +- Bun 1.0+ +- React Native + + +## Contributing + +While we value open-source contributions to this SDK, this library is generated programmatically. +Additions made directly to this library would have to be moved over to our generation code, +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening +an issue first to discuss with us! + +On the other hand, contributions to the README are always very welcome! diff --git a/seed/ts-sdk/basic-auth-optional/biome.json b/seed/ts-sdk/basic-auth-optional/biome.json new file mode 100644 index 000000000000..6b89164f9f99 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/biome.json @@ -0,0 +1,74 @@ +{ + "$schema": "https://biomejs.dev/schemas/2.4.10/schema.json", + "root": true, + "vcs": { + "enabled": false + }, + "files": { + "ignoreUnknown": true, + "includes": [ + "**", + "!!dist", + "!!**/dist", + "!!lib", + "!!**/lib", + "!!_tmp_*", + "!!**/_tmp_*", + "!!*.tmp", + "!!**/*.tmp", + "!!.tmp/", + "!!**/.tmp/", + "!!*.log", + "!!**/*.log", + "!!**/.DS_Store", + "!!**/Thumbs.db" + ] + }, + "formatter": { + "enabled": true, + "indentStyle": "space", + "indentWidth": 4, + "lineWidth": 120 + }, + "javascript": { + "formatter": { + "quoteStyle": "double" + } + }, + "assist": { + "enabled": true, + "actions": { + "source": { + "organizeImports": "on" + } + } + }, + "linter": { + "rules": { + "style": { + "useNodejsImportProtocol": "off" + }, + "suspicious": { + "noAssignInExpressions": "warn", + "noUselessEscapeInString": { + "level": "warn", + "fix": "none", + "options": {} + }, + "noThenProperty": "warn", + "useIterableCallbackReturn": "warn", + "noShadowRestrictedNames": "warn", + "noTsIgnore": { + "level": "warn", + "fix": "none", + "options": {} + }, + "noConfusingVoidType": { + "level": "warn", + "fix": "none", + "options": {} + } + } + } + } +} diff --git a/seed/ts-sdk/basic-auth-optional/package.json b/seed/ts-sdk/basic-auth-optional/package.json new file mode 100644 index 000000000000..ef771d898329 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/package.json @@ -0,0 +1,80 @@ +{ + "name": "@fern/basic-auth-optional", + "version": "0.0.1", + "private": false, + "repository": { + "type": "git", + "url": "git+https://github.com/basic-auth-optional/fern.git" + }, + "type": "commonjs", + "main": "./dist/cjs/index.js", + "module": "./dist/esm/index.mjs", + "types": "./dist/cjs/index.d.ts", + "exports": { + ".": { + "import": { + "types": "./dist/esm/index.d.mts", + "default": "./dist/esm/index.mjs" + }, + "require": { + "types": "./dist/cjs/index.d.ts", + "default": "./dist/cjs/index.js" + }, + "default": "./dist/cjs/index.js" + }, + "./basicAuth": { + "import": { + "types": "./dist/esm/api/resources/basicAuth/exports.d.mts", + "default": "./dist/esm/api/resources/basicAuth/exports.mjs" + }, + "require": { + "types": "./dist/cjs/api/resources/basicAuth/exports.d.ts", + "default": "./dist/cjs/api/resources/basicAuth/exports.js" + }, + "default": "./dist/cjs/api/resources/basicAuth/exports.js" + }, + "./package.json": "./package.json" + }, + "files": [ + "dist", + "reference.md", + "README.md", + "LICENSE" + ], + "scripts": { + "format": "biome format --write --skip-parse-errors --no-errors-on-unmatched --max-diagnostics=none", + "format:check": "biome format --skip-parse-errors --no-errors-on-unmatched --max-diagnostics=none", + "lint": "biome lint --skip-parse-errors --no-errors-on-unmatched --max-diagnostics=none", + "lint:fix": "biome lint --fix --unsafe --skip-parse-errors --no-errors-on-unmatched --max-diagnostics=none", + "check": "biome check --skip-parse-errors --no-errors-on-unmatched --max-diagnostics=none", + "check:fix": "biome check --fix --unsafe --skip-parse-errors --no-errors-on-unmatched --max-diagnostics=none", + "build": "pnpm build:cjs && pnpm build:esm", + "build:cjs": "tsc --project ./tsconfig.cjs.json", + "build:esm": "tsc --project ./tsconfig.esm.json && node scripts/rename-to-esm-files.js dist/esm", + "test": "vitest", + "test:unit": "vitest --project unit", + "test:wire": "vitest --project wire" + }, + "dependencies": {}, + "devDependencies": { + "webpack": "^5.105.4", + "ts-loader": "^9.5.4", + "vitest": "^4.1.1", + "msw": "2.11.2", + "@types/node": "^18.19.70", + "typescript": "~5.9.3", + "@biomejs/biome": "2.4.10" + }, + "browser": { + "fs": false, + "os": false, + "path": false, + "stream": false, + "crypto": false + }, + "packageManager": "pnpm@10.33.0", + "engines": { + "node": ">=18.0.0" + }, + "sideEffects": false +} diff --git a/seed/ts-sdk/basic-auth-optional/pnpm-workspace.yaml b/seed/ts-sdk/basic-auth-optional/pnpm-workspace.yaml new file mode 100644 index 000000000000..6e4c395107df --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/pnpm-workspace.yaml @@ -0,0 +1 @@ +packages: ['.'] \ No newline at end of file diff --git a/seed/ts-sdk/basic-auth-optional/reference.md b/seed/ts-sdk/basic-auth-optional/reference.md new file mode 100644 index 000000000000..fb4a7445b23a --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/reference.md @@ -0,0 +1,122 @@ +# Reference +## BasicAuth +
client.basicAuth.getWithBasicAuth() -> boolean +
+
+ +#### 📝 Description + +
+
+ +
+
+ +GET request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```typescript +await client.basicAuth.getWithBasicAuth(); + +``` +
+
+
+
+ +#### ⚙️ Parameters + +
+
+ +
+
+ +**requestOptions:** `BasicAuthClient.RequestOptions` + +
+
+
+
+ + +
+
+
+ +
client.basicAuth.postWithBasicAuth({ ...params }) -> boolean +
+
+ +#### 📝 Description + +
+
+ +
+
+ +POST request with basic auth scheme +
+
+
+
+ +#### 🔌 Usage + +
+
+ +
+
+ +```typescript +await client.basicAuth.postWithBasicAuth({ + "key": "value" +}); + +``` +
+
+
+
+ +#### ⚙️ Parameters + +
+
+ +
+
+ +**request:** `unknown` + +
+
+ +
+
+ +**requestOptions:** `BasicAuthClient.RequestOptions` + +
+
+
+
+ + +
+
+
+ diff --git a/seed/ts-sdk/basic-auth-optional/scripts/rename-to-esm-files.js b/seed/ts-sdk/basic-auth-optional/scripts/rename-to-esm-files.js new file mode 100644 index 000000000000..dc1df1cbbacb --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/scripts/rename-to-esm-files.js @@ -0,0 +1,123 @@ +#!/usr/bin/env node + +const fs = require("fs").promises; +const path = require("path"); + +const extensionMap = { + ".js": ".mjs", + ".d.ts": ".d.mts", +}; +const oldExtensions = Object.keys(extensionMap); + +async function findFiles(rootPath) { + const files = []; + + async function scan(directory) { + const entries = await fs.readdir(directory, { withFileTypes: true }); + + for (const entry of entries) { + const fullPath = path.join(directory, entry.name); + + if (entry.isDirectory()) { + if (entry.name !== "node_modules" && !entry.name.startsWith(".")) { + await scan(fullPath); + } + } else if (entry.isFile()) { + if (oldExtensions.some((ext) => entry.name.endsWith(ext))) { + files.push(fullPath); + } + } + } + } + + await scan(rootPath); + return files; +} + +async function updateFiles(files) { + const updatedFiles = []; + for (const file of files) { + const updated = await updateFileContents(file); + updatedFiles.push(updated); + } + + console.log(`Updated imports in ${updatedFiles.length} files.`); +} + +async function updateFileContents(file) { + const content = await fs.readFile(file, "utf8"); + + let newContent = content; + // Update each extension type defined in the map + for (const [oldExt, newExt] of Object.entries(extensionMap)) { + // Handle static imports/exports + const staticRegex = new RegExp(`(import|export)(.+from\\s+['"])(\\.\\.?\\/[^'"]+)(\\${oldExt})(['"])`, "g"); + newContent = newContent.replace(staticRegex, `$1$2$3${newExt}$5`); + + // Handle dynamic imports (yield import, await import, regular import()) + const dynamicRegex = new RegExp( + `(yield\\s+import|await\\s+import|import)\\s*\\(\\s*['"](\\.\\.\?\\/[^'"]+)(\\${oldExt})['"]\\s*\\)`, + "g", + ); + newContent = newContent.replace(dynamicRegex, `$1("$2${newExt}")`); + } + + if (content !== newContent) { + await fs.writeFile(file, newContent, "utf8"); + return true; + } + return false; +} + +async function renameFiles(files) { + let counter = 0; + for (const file of files) { + const ext = oldExtensions.find((ext) => file.endsWith(ext)); + const newExt = extensionMap[ext]; + + if (newExt) { + const newPath = file.slice(0, -ext.length) + newExt; + await fs.rename(file, newPath); + counter++; + } + } + + console.log(`Renamed ${counter} files.`); +} + +async function main() { + try { + const targetDir = process.argv[2]; + if (!targetDir) { + console.error("Please provide a target directory"); + process.exit(1); + } + + const targetPath = path.resolve(targetDir); + const targetStats = await fs.stat(targetPath); + + if (!targetStats.isDirectory()) { + console.error("The provided path is not a directory"); + process.exit(1); + } + + console.log(`Scanning directory: ${targetDir}`); + + const files = await findFiles(targetDir); + + if (files.length === 0) { + console.log("No matching files found."); + process.exit(0); + } + + console.log(`Found ${files.length} files.`); + await updateFiles(files); + await renameFiles(files); + console.log("\nDone!"); + } catch (error) { + console.error("An error occurred:", error.message); + process.exit(1); + } +} + +main(); diff --git a/seed/ts-sdk/basic-auth-optional/snippet.json b/seed/ts-sdk/basic-auth-optional/snippet.json new file mode 100644 index 000000000000..bb88342ee614 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/snippet.json @@ -0,0 +1,27 @@ +{ + "endpoints": [ + { + "id": { + "path": "/basic-auth", + "method": "GET", + "identifier_override": "endpoint_basic-auth.getWithBasicAuth" + }, + "snippet": { + "type": "typescript", + "client": "import { SeedBasicAuthOptionalClient } from \"@fern/basic-auth-optional\";\n\nconst client = new SeedBasicAuthOptionalClient({ environment: \"YOUR_BASE_URL\", username: \"YOUR_USERNAME\", password: \"YOUR_PASSWORD\" });\nawait client.basicAuth.getWithBasicAuth();\n" + } + }, + { + "id": { + "path": "/basic-auth", + "method": "POST", + "identifier_override": "endpoint_basic-auth.postWithBasicAuth" + }, + "snippet": { + "type": "typescript", + "client": "import { SeedBasicAuthOptionalClient } from \"@fern/basic-auth-optional\";\n\nconst client = new SeedBasicAuthOptionalClient({ environment: \"YOUR_BASE_URL\", username: \"YOUR_USERNAME\", password: \"YOUR_PASSWORD\" });\nawait client.basicAuth.postWithBasicAuth({\n \"key\": \"value\"\n});\n" + } + } + ], + "types": {} +} \ No newline at end of file diff --git a/seed/ts-sdk/basic-auth-optional/src/BaseClient.ts b/seed/ts-sdk/basic-auth-optional/src/BaseClient.ts new file mode 100644 index 000000000000..1ee7625ae074 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/BaseClient.ts @@ -0,0 +1,84 @@ +// This file was auto-generated by Fern from our API Definition. + +import { BasicAuthProvider } from "./auth/BasicAuthProvider.js"; +import { mergeHeaders } from "./core/headers.js"; +import * as core from "./core/index.js"; + +export type BaseClientOptions = { + environment: core.Supplier; + /** Specify a custom URL to connect the client to. */ + baseUrl?: core.Supplier; + /** Additional headers to include in requests. */ + headers?: Record | null | undefined>; + /** The default maximum time to wait for a response in seconds. */ + timeoutInSeconds?: number; + /** The default number of times to retry the request. Defaults to 2. */ + maxRetries?: number; + /** Provide a custom fetch implementation. Useful for platforms that don't have a built-in fetch or need a custom implementation. */ + fetch?: typeof fetch; + /** Configure logging for the client. */ + logging?: core.logging.LogConfig | core.logging.Logger; +} & BasicAuthProvider.AuthOptions; + +export interface BaseRequestOptions { + /** The maximum time to wait for a response in seconds. */ + timeoutInSeconds?: number; + /** The number of times to retry the request. Defaults to 2. */ + maxRetries?: number; + /** A hook to abort the request. */ + abortSignal?: AbortSignal; + /** Additional query string parameters to include in the request. */ + queryParams?: Record; + /** Additional headers to include in the request. */ + headers?: Record | null | undefined>; +} + +export type NormalizedClientOptions = T & { + logging: core.logging.Logger; + authProvider?: core.AuthProvider; +}; + +export type NormalizedClientOptionsWithAuth = + NormalizedClientOptions & { + authProvider: core.AuthProvider; + }; + +export function normalizeClientOptions( + options: T, +): NormalizedClientOptions { + const headers = mergeHeaders( + { + "X-Fern-Language": "JavaScript", + "X-Fern-SDK-Name": "@fern/basic-auth-optional", + "X-Fern-SDK-Version": "0.0.1", + "User-Agent": "@fern/basic-auth-optional/0.0.1", + "X-Fern-Runtime": core.RUNTIME.type, + "X-Fern-Runtime-Version": core.RUNTIME.version, + }, + options?.headers, + ); + + return { + ...options, + logging: core.logging.createLogger(options?.logging), + headers, + } as NormalizedClientOptions; +} + +export function normalizeClientOptionsWithAuth( + options: T, +): NormalizedClientOptionsWithAuth { + const normalized = normalizeClientOptions(options) as NormalizedClientOptionsWithAuth; + const normalizedWithNoOpAuthProvider = withNoOpAuthProvider(normalized); + normalized.authProvider ??= new BasicAuthProvider(normalizedWithNoOpAuthProvider); + return normalized; +} + +function withNoOpAuthProvider( + options: NormalizedClientOptions, +): NormalizedClientOptionsWithAuth { + return { + ...options, + authProvider: new core.NoOpAuthProvider(), + }; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/Client.ts b/seed/ts-sdk/basic-auth-optional/src/Client.ts new file mode 100644 index 000000000000..cabe3df07f65 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/Client.ts @@ -0,0 +1,56 @@ +// This file was auto-generated by Fern from our API Definition. + +import { BasicAuthClient } from "./api/resources/basicAuth/client/Client.js"; +import type { BaseClientOptions, BaseRequestOptions } from "./BaseClient.js"; +import { type NormalizedClientOptionsWithAuth, normalizeClientOptionsWithAuth } from "./BaseClient.js"; +import * as core from "./core/index.js"; + +export declare namespace SeedBasicAuthOptionalClient { + export type Options = BaseClientOptions; + + export interface RequestOptions extends BaseRequestOptions {} +} + +export class SeedBasicAuthOptionalClient { + protected readonly _options: NormalizedClientOptionsWithAuth; + protected _basicAuth: BasicAuthClient | undefined; + + constructor(options: SeedBasicAuthOptionalClient.Options) { + this._options = normalizeClientOptionsWithAuth(options); + } + + public get basicAuth(): BasicAuthClient { + return (this._basicAuth ??= new BasicAuthClient(this._options)); + } + + /** + * Make a passthrough request using the SDK's configured auth, retry, logging, etc. + * This is useful for making requests to endpoints not yet supported in the SDK. + * The input can be a URL string, URL object, or Request object. Relative paths are resolved against the configured base URL. + * + * @param {Request | string | URL} input - The URL, path, or Request object. + * @param {RequestInit} init - Standard fetch RequestInit options. + * @param {core.PassthroughRequest.RequestOptions} requestOptions - Per-request overrides (timeout, retries, headers, abort signal). + * @returns {Promise} A standard Response object. + */ + public async fetch( + input: Request | string | URL, + init?: RequestInit, + requestOptions?: core.PassthroughRequest.RequestOptions, + ): Promise { + return core.makePassthroughRequest( + input, + init, + { + baseUrl: this._options.baseUrl ?? this._options.environment, + headers: this._options.headers, + timeoutInSeconds: this._options.timeoutInSeconds, + maxRetries: this._options.maxRetries, + fetch: this._options.fetch, + logging: this._options.logging, + getAuthHeaders: async () => (await this._options.authProvider.getAuthRequest()).headers, + }, + requestOptions, + ); + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/api/index.ts b/seed/ts-sdk/basic-auth-optional/src/api/index.ts new file mode 100644 index 000000000000..e445af0d831e --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/index.ts @@ -0,0 +1 @@ +export * from "./resources/index.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/client/Client.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/client/Client.ts new file mode 100644 index 000000000000..4bb839aa5426 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/client/Client.ts @@ -0,0 +1,158 @@ +// This file was auto-generated by Fern from our API Definition. + +import type { BaseClientOptions, BaseRequestOptions } from "../../../../BaseClient.js"; +import { type NormalizedClientOptionsWithAuth, normalizeClientOptionsWithAuth } from "../../../../BaseClient.js"; +import { mergeHeaders } from "../../../../core/headers.js"; +import * as core from "../../../../core/index.js"; +import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.js"; +import * as errors from "../../../../errors/index.js"; +import * as SeedBasicAuthOptional from "../../../index.js"; + +export declare namespace BasicAuthClient { + export type Options = BaseClientOptions; + + export interface RequestOptions extends BaseRequestOptions {} +} + +export class BasicAuthClient { + protected readonly _options: NormalizedClientOptionsWithAuth; + + constructor(options: BasicAuthClient.Options) { + this._options = normalizeClientOptionsWithAuth(options); + } + + /** + * GET request with basic auth scheme + * + * @param {BasicAuthClient.RequestOptions} requestOptions - Request-specific configuration. + * + * @throws {@link SeedBasicAuthOptional.UnauthorizedRequest} + * + * @example + * await client.basicAuth.getWithBasicAuth() + */ + public getWithBasicAuth(requestOptions?: BasicAuthClient.RequestOptions): core.HttpResponsePromise { + return core.HttpResponsePromise.fromPromise(this.__getWithBasicAuth(requestOptions)); + } + + private async __getWithBasicAuth( + requestOptions?: BasicAuthClient.RequestOptions, + ): Promise> { + const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest(); + const _headers: core.Fetcher.Args["headers"] = mergeHeaders( + _authRequest.headers, + this._options?.headers, + requestOptions?.headers, + ); + const _response = await core.fetcher({ + url: core.url.join( + (await core.Supplier.get(this._options.baseUrl)) ?? + (await core.Supplier.get(this._options.environment)), + "basic-auth", + ), + method: "GET", + headers: _headers, + queryParameters: requestOptions?.queryParams, + timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000, + maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries, + abortSignal: requestOptions?.abortSignal, + fetchFn: this._options?.fetch, + logging: this._options.logging, + }); + if (_response.ok) { + return { data: _response.body as boolean, rawResponse: _response.rawResponse }; + } + + if (_response.error.reason === "status-code") { + switch (_response.error.statusCode) { + case 401: + throw new SeedBasicAuthOptional.UnauthorizedRequest( + _response.error.body as SeedBasicAuthOptional.UnauthorizedRequestErrorBody, + _response.rawResponse, + ); + default: + throw new errors.SeedBasicAuthOptionalError({ + statusCode: _response.error.statusCode, + body: _response.error.body, + rawResponse: _response.rawResponse, + }); + } + } + + return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/basic-auth"); + } + + /** + * POST request with basic auth scheme + * + * @param {unknown} request + * @param {BasicAuthClient.RequestOptions} requestOptions - Request-specific configuration. + * + * @throws {@link SeedBasicAuthOptional.UnauthorizedRequest} + * @throws {@link SeedBasicAuthOptional.BadRequest} + * + * @example + * await client.basicAuth.postWithBasicAuth({ + * "key": "value" + * }) + */ + public postWithBasicAuth( + request?: unknown, + requestOptions?: BasicAuthClient.RequestOptions, + ): core.HttpResponsePromise { + return core.HttpResponsePromise.fromPromise(this.__postWithBasicAuth(request, requestOptions)); + } + + private async __postWithBasicAuth( + request?: unknown, + requestOptions?: BasicAuthClient.RequestOptions, + ): Promise> { + const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest(); + const _headers: core.Fetcher.Args["headers"] = mergeHeaders( + _authRequest.headers, + this._options?.headers, + requestOptions?.headers, + ); + const _response = await core.fetcher({ + url: core.url.join( + (await core.Supplier.get(this._options.baseUrl)) ?? + (await core.Supplier.get(this._options.environment)), + "basic-auth", + ), + method: "POST", + headers: _headers, + contentType: "application/json", + queryParameters: requestOptions?.queryParams, + requestType: "json", + body: request, + timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000, + maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries, + abortSignal: requestOptions?.abortSignal, + fetchFn: this._options?.fetch, + logging: this._options.logging, + }); + if (_response.ok) { + return { data: _response.body as boolean, rawResponse: _response.rawResponse }; + } + + if (_response.error.reason === "status-code") { + switch (_response.error.statusCode) { + case 401: + throw new SeedBasicAuthOptional.UnauthorizedRequest( + _response.error.body as SeedBasicAuthOptional.UnauthorizedRequestErrorBody, + _response.rawResponse, + ); + case 400: + throw new SeedBasicAuthOptional.BadRequest(_response.rawResponse); + default: + throw new errors.SeedBasicAuthOptionalError({ + statusCode: _response.error.statusCode, + body: _response.error.body, + rawResponse: _response.rawResponse, + }); + } + } + + return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/basic-auth"); + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/client/index.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/client/index.ts new file mode 100644 index 000000000000..cb0ff5c3b541 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/client/index.ts @@ -0,0 +1 @@ +export {}; diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/exports.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/exports.ts new file mode 100644 index 000000000000..1a05c800c26e --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/exports.ts @@ -0,0 +1,4 @@ +// This file was auto-generated by Fern from our API Definition. + +export { BasicAuthClient } from "./client/Client.js"; +export * from "./client/index.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/index.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/index.ts new file mode 100644 index 000000000000..914b8c3c7214 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/basicAuth/index.ts @@ -0,0 +1 @@ +export * from "./client/index.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/BadRequest.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/BadRequest.ts new file mode 100644 index 000000000000..ffd2b844c657 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/BadRequest.ts @@ -0,0 +1,20 @@ +// This file was auto-generated by Fern from our API Definition. + +import type * as core from "../../../../core/index.js"; +import * as errors from "../../../../errors/index.js"; + +export class BadRequest extends errors.SeedBasicAuthOptionalError { + constructor(rawResponse?: core.RawResponse) { + super({ + message: "BadRequest", + statusCode: 400, + rawResponse: rawResponse, + }); + Object.setPrototypeOf(this, new.target.prototype); + if (Error.captureStackTrace) { + Error.captureStackTrace(this, this.constructor); + } + + this.name = this.constructor.name; + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/UnauthorizedRequest.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/UnauthorizedRequest.ts new file mode 100644 index 000000000000..0ec0a68f5cca --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/UnauthorizedRequest.ts @@ -0,0 +1,22 @@ +// This file was auto-generated by Fern from our API Definition. + +import type * as core from "../../../../core/index.js"; +import * as errors from "../../../../errors/index.js"; +import type * as SeedBasicAuthOptional from "../../../index.js"; + +export class UnauthorizedRequest extends errors.SeedBasicAuthOptionalError { + constructor(body: SeedBasicAuthOptional.UnauthorizedRequestErrorBody, rawResponse?: core.RawResponse) { + super({ + message: "UnauthorizedRequest", + statusCode: 401, + body: body, + rawResponse: rawResponse, + }); + Object.setPrototypeOf(this, new.target.prototype); + if (Error.captureStackTrace) { + Error.captureStackTrace(this, this.constructor); + } + + this.name = this.constructor.name; + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/index.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/index.ts new file mode 100644 index 000000000000..3c7bd7d82bc6 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/errors/index.ts @@ -0,0 +1,2 @@ +export * from "./BadRequest.js"; +export * from "./UnauthorizedRequest.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/exports.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/exports.ts new file mode 100644 index 000000000000..94d57674a307 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/exports.ts @@ -0,0 +1,3 @@ +// This file was auto-generated by Fern from our API Definition. + +export * from "./index.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/index.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/index.ts new file mode 100644 index 000000000000..38688e58bd6f --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/index.ts @@ -0,0 +1,2 @@ +export * from "./errors/index.js"; +export * from "./types/index.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/types/UnauthorizedRequestErrorBody.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/types/UnauthorizedRequestErrorBody.ts new file mode 100644 index 000000000000..0334a42b59e9 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/types/UnauthorizedRequestErrorBody.ts @@ -0,0 +1,5 @@ +// This file was auto-generated by Fern from our API Definition. + +export interface UnauthorizedRequestErrorBody { + message: string; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/types/index.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/types/index.ts new file mode 100644 index 000000000000..dda55a4afe1b --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/errors/types/index.ts @@ -0,0 +1 @@ +export * from "./UnauthorizedRequestErrorBody.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/api/resources/index.ts b/seed/ts-sdk/basic-auth-optional/src/api/resources/index.ts new file mode 100644 index 000000000000..7edb3d6070be --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/api/resources/index.ts @@ -0,0 +1,4 @@ +export * as basicAuth from "./basicAuth/index.js"; +export * from "./errors/errors/index.js"; +export * as errors from "./errors/index.js"; +export * from "./errors/types/index.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/auth/BasicAuthProvider.ts b/seed/ts-sdk/basic-auth-optional/src/auth/BasicAuthProvider.ts new file mode 100644 index 000000000000..41cfc59d8e76 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/auth/BasicAuthProvider.ts @@ -0,0 +1,58 @@ +// This file was auto-generated by Fern from our API Definition. + +import * as core from "../core/index.js"; +import * as errors from "../errors/index.js"; + +const USERNAME_PARAM = "username" as const; +const PASSWORD_PARAM = "password" as const; + +export class BasicAuthProvider implements core.AuthProvider { + private readonly options: BasicAuthProvider.Options; + + constructor(options: BasicAuthProvider.Options) { + this.options = options; + } + + public static canCreate(options: Partial): boolean { + return options?.[USERNAME_PARAM] != null || options?.[PASSWORD_PARAM] != null; + } + + public async getAuthRequest({ + endpointMetadata, + }: { + endpointMetadata?: core.EndpointMetadata; + } = {}): Promise { + const username = await core.Supplier.get(this.options[USERNAME_PARAM]); + const password = await core.Supplier.get(this.options[PASSWORD_PARAM]); + if (username == null && password == null) { + throw new errors.SeedBasicAuthOptionalError({ + message: BasicAuthProvider.AUTH_CONFIG_ERROR_MESSAGE, + }); + } + + const authHeader = core.BasicAuth.toAuthorizationHeader({ + username: username, + password: password, + }); + + return { + headers: authHeader != null ? { Authorization: authHeader } : {}, + }; + } +} + +export namespace BasicAuthProvider { + export const AUTH_SCHEME = "Basic" as const; + export const AUTH_CONFIG_ERROR_MESSAGE: string = + "Please provide username and password when initializing the client" as const; + export const AUTH_CONFIG_ERROR_MESSAGE_USERNAME: string = + `Please provide '${USERNAME_PARAM}' when initializing the client` as const; + export const AUTH_CONFIG_ERROR_MESSAGE_PASSWORD: string = + `Please provide '${PASSWORD_PARAM}' when initializing the client` as const; + export type Options = AuthOptions; + export type AuthOptions = { [USERNAME_PARAM]: core.Supplier; [PASSWORD_PARAM]: core.Supplier }; + + export function createInstance(options: Options): core.AuthProvider { + return new BasicAuthProvider(options); + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/auth/index.ts b/seed/ts-sdk/basic-auth-optional/src/auth/index.ts new file mode 100644 index 000000000000..4fe8c31ae96e --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/auth/index.ts @@ -0,0 +1 @@ +export { BasicAuthProvider } from "./BasicAuthProvider.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/auth/AuthProvider.ts b/seed/ts-sdk/basic-auth-optional/src/core/auth/AuthProvider.ts new file mode 100644 index 000000000000..895a50ff30da --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/auth/AuthProvider.ts @@ -0,0 +1,6 @@ +import type { EndpointMetadata } from "../fetcher/EndpointMetadata.js"; +import type { AuthRequest } from "./AuthRequest.js"; + +export interface AuthProvider { + getAuthRequest(arg?: { endpointMetadata?: EndpointMetadata }): Promise; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/auth/AuthRequest.ts b/seed/ts-sdk/basic-auth-optional/src/core/auth/AuthRequest.ts new file mode 100644 index 000000000000..f6218b42211e --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/auth/AuthRequest.ts @@ -0,0 +1,9 @@ +/** + * Request parameters for authentication requests. + */ +export interface AuthRequest { + /** + * The headers to be included in the request. + */ + headers: Record; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/auth/BasicAuth.ts b/seed/ts-sdk/basic-auth-optional/src/core/auth/BasicAuth.ts new file mode 100644 index 000000000000..f34fca5cc4dd --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/auth/BasicAuth.ts @@ -0,0 +1,37 @@ +import { base64Decode, base64Encode } from "../base64.js"; + +export interface BasicAuth { + username?: string; + password?: string; +} + +const BASIC_AUTH_HEADER_PREFIX = /^Basic /i; + +export const BasicAuth = { + toAuthorizationHeader: (basicAuth: BasicAuth | undefined): string | undefined => { + if (basicAuth == null) { + return undefined; + } + const username = basicAuth.username ?? ""; + const password = basicAuth.password ?? ""; + if (username === "" && password === "") { + return undefined; + } + const token = base64Encode(`${username}:${password}`); + return `Basic ${token}`; + }, + fromAuthorizationHeader: (header: string): BasicAuth => { + const credentials = header.replace(BASIC_AUTH_HEADER_PREFIX, ""); + const decoded = base64Decode(credentials); + const [username, ...passwordParts] = decoded.split(":"); + const password = passwordParts.length > 0 ? passwordParts.join(":") : undefined; + + if (username == null || password == null) { + throw new Error("Invalid basic auth"); + } + return { + username, + password, + }; + }, +}; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/auth/BearerToken.ts b/seed/ts-sdk/basic-auth-optional/src/core/auth/BearerToken.ts new file mode 100644 index 000000000000..c44a06c38f06 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/auth/BearerToken.ts @@ -0,0 +1,20 @@ +export type BearerToken = string; + +const BEARER_AUTH_HEADER_PREFIX = /^Bearer /i; + +function toAuthorizationHeader(token: string | undefined): string | undefined { + if (token == null) { + return undefined; + } + return `Bearer ${token}`; +} + +export const BearerToken: { + toAuthorizationHeader: typeof toAuthorizationHeader; + fromAuthorizationHeader: (header: string) => BearerToken; +} = { + toAuthorizationHeader: toAuthorizationHeader, + fromAuthorizationHeader: (header: string): BearerToken => { + return header.replace(BEARER_AUTH_HEADER_PREFIX, "").trim() as BearerToken; + }, +}; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/auth/NoOpAuthProvider.ts b/seed/ts-sdk/basic-auth-optional/src/core/auth/NoOpAuthProvider.ts new file mode 100644 index 000000000000..5b7acfd2bd8b --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/auth/NoOpAuthProvider.ts @@ -0,0 +1,8 @@ +import type { AuthProvider } from "./AuthProvider.js"; +import type { AuthRequest } from "./AuthRequest.js"; + +export class NoOpAuthProvider implements AuthProvider { + public getAuthRequest(): Promise { + return Promise.resolve({ headers: {} }); + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/auth/index.ts b/seed/ts-sdk/basic-auth-optional/src/core/auth/index.ts new file mode 100644 index 000000000000..2215b227709e --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/auth/index.ts @@ -0,0 +1,5 @@ +export type { AuthProvider } from "./AuthProvider.js"; +export type { AuthRequest } from "./AuthRequest.js"; +export { BasicAuth } from "./BasicAuth.js"; +export { BearerToken } from "./BearerToken.js"; +export { NoOpAuthProvider } from "./NoOpAuthProvider.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/base64.ts b/seed/ts-sdk/basic-auth-optional/src/core/base64.ts new file mode 100644 index 000000000000..448a0db638a6 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/base64.ts @@ -0,0 +1,27 @@ +function base64ToBytes(base64: string): Uint8Array { + const binString = atob(base64); + return Uint8Array.from(binString, (m) => m.codePointAt(0)!); +} + +function bytesToBase64(bytes: Uint8Array): string { + const binString = String.fromCodePoint(...bytes); + return btoa(binString); +} + +export function base64Encode(input: string): string { + if (typeof Buffer !== "undefined") { + return Buffer.from(input, "utf8").toString("base64"); + } + + const bytes = new TextEncoder().encode(input); + return bytesToBase64(bytes); +} + +export function base64Decode(input: string): string { + if (typeof Buffer !== "undefined") { + return Buffer.from(input, "base64").toString("utf8"); + } + + const bytes = base64ToBytes(input); + return new TextDecoder().decode(bytes); +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/exports.ts b/seed/ts-sdk/basic-auth-optional/src/core/exports.ts new file mode 100644 index 000000000000..69296d7100d6 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/exports.ts @@ -0,0 +1 @@ +export * from "./logging/exports.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/APIResponse.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/APIResponse.ts new file mode 100644 index 000000000000..97ab83c2b195 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/APIResponse.ts @@ -0,0 +1,23 @@ +import type { RawResponse } from "./RawResponse.js"; + +/** + * The response of an API call. + * It is a successful response or a failed response. + */ +export type APIResponse = SuccessfulResponse | FailedResponse; + +export interface SuccessfulResponse { + ok: true; + body: T; + /** + * @deprecated Use `rawResponse` instead + */ + headers?: Record; + rawResponse: RawResponse; +} + +export interface FailedResponse { + ok: false; + error: T; + rawResponse: RawResponse; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/BinaryResponse.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/BinaryResponse.ts new file mode 100644 index 000000000000..b9e40fb62cc4 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/BinaryResponse.ts @@ -0,0 +1,34 @@ +export type BinaryResponse = { + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/bodyUsed) */ + bodyUsed: Response["bodyUsed"]; + /** + * Returns a ReadableStream of the response body. + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/body) + */ + stream: () => Response["body"]; + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/arrayBuffer) */ + arrayBuffer: () => ReturnType; + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/blob) */ + blob: () => ReturnType; + /** + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/bytes) + * Some versions of the Fetch API may not support this method. + */ + bytes?(): Promise; +}; + +export function getBinaryResponse(response: Response): BinaryResponse { + const binaryResponse: BinaryResponse = { + get bodyUsed() { + return response.bodyUsed; + }, + stream: () => response.body, + arrayBuffer: response.arrayBuffer.bind(response), + blob: response.blob.bind(response), + }; + if ("bytes" in response && typeof response.bytes === "function") { + binaryResponse.bytes = response.bytes.bind(response); + } + + return binaryResponse; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/EndpointMetadata.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/EndpointMetadata.ts new file mode 100644 index 000000000000..998d68f5c20c --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/EndpointMetadata.ts @@ -0,0 +1,13 @@ +export type SecuritySchemeKey = string; +/** + * A collection of security schemes, where the key is the name of the security scheme and the value is the list of scopes required for that scheme. + * All schemes in the collection must be satisfied for authentication to be successful. + */ +export type SecuritySchemeCollection = Record; +export type AuthScope = string; +export type EndpointMetadata = { + /** + * An array of security scheme collections. Each collection represents an alternative way to authenticate. + */ + security?: SecuritySchemeCollection[]; +}; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/EndpointSupplier.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/EndpointSupplier.ts new file mode 100644 index 000000000000..aad81f0d9040 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/EndpointSupplier.ts @@ -0,0 +1,14 @@ +import type { EndpointMetadata } from "./EndpointMetadata.js"; +import type { Supplier } from "./Supplier.js"; + +type EndpointSupplierFn = (arg: { endpointMetadata?: EndpointMetadata }) => T | Promise; +export type EndpointSupplier = Supplier | EndpointSupplierFn; +export const EndpointSupplier = { + get: async (supplier: EndpointSupplier, arg: { endpointMetadata?: EndpointMetadata }): Promise => { + if (typeof supplier === "function") { + return (supplier as EndpointSupplierFn)(arg); + } else { + return supplier; + } + }, +}; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/Fetcher.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/Fetcher.ts new file mode 100644 index 000000000000..764d2e195a41 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/Fetcher.ts @@ -0,0 +1,398 @@ +import { toJson } from "../json.js"; +import { createLogger, type LogConfig, type Logger } from "../logging/logger.js"; +import type { APIResponse } from "./APIResponse.js"; +import { createRequestUrl } from "./createRequestUrl.js"; +import type { EndpointMetadata } from "./EndpointMetadata.js"; +import { EndpointSupplier } from "./EndpointSupplier.js"; +import { getErrorResponseBody } from "./getErrorResponseBody.js"; +import { getFetchFn } from "./getFetchFn.js"; +import { getRequestBody } from "./getRequestBody.js"; +import { getResponseBody } from "./getResponseBody.js"; +import { Headers } from "./Headers.js"; +import { makeRequest } from "./makeRequest.js"; +import { abortRawResponse, toRawResponse, unknownRawResponse } from "./RawResponse.js"; +import { requestWithRetries } from "./requestWithRetries.js"; + +export type FetchFunction = (args: Fetcher.Args) => Promise>; + +export declare namespace Fetcher { + export interface Args { + url: string; + method: string; + contentType?: string; + headers?: Record; + queryParameters?: Record; + body?: unknown; + timeoutMs?: number; + maxRetries?: number; + withCredentials?: boolean; + abortSignal?: AbortSignal; + requestType?: "json" | "file" | "bytes" | "form" | "other"; + responseType?: "json" | "blob" | "sse" | "streaming" | "text" | "arrayBuffer" | "binary-response"; + duplex?: "half"; + endpointMetadata?: EndpointMetadata; + fetchFn?: typeof fetch; + logging?: LogConfig | Logger; + } + + export type Error = FailedStatusCodeError | NonJsonError | BodyIsNullError | TimeoutError | UnknownError; + + export interface FailedStatusCodeError { + reason: "status-code"; + statusCode: number; + body: unknown; + } + + export interface NonJsonError { + reason: "non-json"; + statusCode: number; + rawBody: string; + } + + export interface BodyIsNullError { + reason: "body-is-null"; + statusCode: number; + } + + export interface TimeoutError { + reason: "timeout"; + } + + export interface UnknownError { + reason: "unknown"; + errorMessage: string; + } +} + +const SENSITIVE_HEADERS = new Set([ + "authorization", + "www-authenticate", + "x-api-key", + "api-key", + "apikey", + "x-api-token", + "x-auth-token", + "auth-token", + "cookie", + "set-cookie", + "proxy-authorization", + "proxy-authenticate", + "x-csrf-token", + "x-xsrf-token", + "x-session-token", + "x-access-token", +]); + +function redactHeaders(headers: Headers | Record): Record { + const filtered: Record = {}; + for (const [key, value] of headers instanceof Headers ? headers.entries() : Object.entries(headers)) { + if (SENSITIVE_HEADERS.has(key.toLowerCase())) { + filtered[key] = "[REDACTED]"; + } else { + filtered[key] = value; + } + } + return filtered; +} + +const SENSITIVE_QUERY_PARAMS = new Set([ + "api_key", + "api-key", + "apikey", + "token", + "access_token", + "access-token", + "auth_token", + "auth-token", + "password", + "passwd", + "secret", + "api_secret", + "api-secret", + "apisecret", + "key", + "session", + "session_id", + "session-id", +]); + +function redactQueryParameters(queryParameters?: Record): Record | undefined { + if (queryParameters == null) { + return queryParameters; + } + const redacted: Record = {}; + for (const [key, value] of Object.entries(queryParameters)) { + if (SENSITIVE_QUERY_PARAMS.has(key.toLowerCase())) { + redacted[key] = "[REDACTED]"; + } else { + redacted[key] = value; + } + } + return redacted; +} + +function redactUrl(url: string): string { + const protocolIndex = url.indexOf("://"); + if (protocolIndex === -1) return url; + + const afterProtocol = protocolIndex + 3; + + // Find the first delimiter that marks the end of the authority section + const pathStart = url.indexOf("/", afterProtocol); + let queryStart = url.indexOf("?", afterProtocol); + let fragmentStart = url.indexOf("#", afterProtocol); + + const firstDelimiter = Math.min( + pathStart === -1 ? url.length : pathStart, + queryStart === -1 ? url.length : queryStart, + fragmentStart === -1 ? url.length : fragmentStart, + ); + + // Find the LAST @ before the delimiter (handles multiple @ in credentials) + let atIndex = -1; + for (let i = afterProtocol; i < firstDelimiter; i++) { + if (url[i] === "@") { + atIndex = i; + } + } + + if (atIndex !== -1) { + url = `${url.slice(0, afterProtocol)}[REDACTED]@${url.slice(atIndex + 1)}`; + } + + // Recalculate queryStart since url might have changed + queryStart = url.indexOf("?"); + if (queryStart === -1) return url; + + fragmentStart = url.indexOf("#", queryStart); + const queryEnd = fragmentStart !== -1 ? fragmentStart : url.length; + const queryString = url.slice(queryStart + 1, queryEnd); + + if (queryString.length === 0) return url; + + // FAST PATH: Quick check if any sensitive keywords present + // Using indexOf is faster than regex for simple substring matching + const lower = queryString.toLowerCase(); + const hasSensitive = + lower.includes("token") || + lower.includes("key") || + lower.includes("password") || + lower.includes("passwd") || + lower.includes("secret") || + lower.includes("session") || + lower.includes("auth"); + + if (!hasSensitive) { + return url; + } + + // SLOW PATH: Parse and redact + const redactedParams: string[] = []; + const params = queryString.split("&"); + + for (const param of params) { + const equalIndex = param.indexOf("="); + if (equalIndex === -1) { + redactedParams.push(param); + continue; + } + + const key = param.slice(0, equalIndex); + let shouldRedact = SENSITIVE_QUERY_PARAMS.has(key.toLowerCase()); + + if (!shouldRedact && key.includes("%")) { + try { + const decodedKey = decodeURIComponent(key); + shouldRedact = SENSITIVE_QUERY_PARAMS.has(decodedKey.toLowerCase()); + } catch {} + } + + redactedParams.push(shouldRedact ? `${key}=[REDACTED]` : param); + } + + return url.slice(0, queryStart + 1) + redactedParams.join("&") + url.slice(queryEnd); +} + +async function getHeaders(args: Fetcher.Args): Promise { + const newHeaders: Headers = new Headers(); + + newHeaders.set( + "Accept", + args.responseType === "json" + ? "application/json" + : args.responseType === "text" + ? "text/plain" + : args.responseType === "sse" + ? "text/event-stream" + : "*/*", + ); + if (args.body !== undefined && args.contentType != null) { + newHeaders.set("Content-Type", args.contentType); + } + + if (args.headers == null) { + return newHeaders; + } + + for (const [key, value] of Object.entries(args.headers)) { + const result = await EndpointSupplier.get(value, { endpointMetadata: args.endpointMetadata ?? {} }); + if (typeof result === "string") { + newHeaders.set(key, result); + continue; + } + if (result == null) { + continue; + } + newHeaders.set(key, `${result}`); + } + return newHeaders; +} + +export async function fetcherImpl(args: Fetcher.Args): Promise> { + const url = createRequestUrl(args.url, args.queryParameters); + const requestBody: BodyInit | undefined = await getRequestBody({ + body: args.body, + type: args.requestType ?? "other", + }); + const fetchFn = args.fetchFn ?? (await getFetchFn()); + const headers = await getHeaders(args); + const logger = createLogger(args.logging); + + if (logger.isDebug()) { + const metadata = { + method: args.method, + url: redactUrl(url), + headers: redactHeaders(headers), + queryParameters: redactQueryParameters(args.queryParameters), + hasBody: requestBody != null, + }; + logger.debug("Making HTTP request", metadata); + } + + try { + const response = await requestWithRetries( + async () => + makeRequest( + fetchFn, + url, + args.method, + headers, + requestBody, + args.timeoutMs, + args.abortSignal, + args.withCredentials, + args.duplex, + args.responseType === "streaming" || args.responseType === "sse", + ), + args.maxRetries, + ); + + if (response.status >= 200 && response.status < 400) { + if (logger.isDebug()) { + const metadata = { + method: args.method, + url: redactUrl(url), + statusCode: response.status, + responseHeaders: redactHeaders(response.headers), + }; + logger.debug("HTTP request succeeded", metadata); + } + const body = await getResponseBody(response, args.responseType); + return { + ok: true, + body: body as R, + headers: response.headers, + rawResponse: toRawResponse(response), + }; + } else { + if (logger.isError()) { + const metadata = { + method: args.method, + url: redactUrl(url), + statusCode: response.status, + responseHeaders: redactHeaders(Object.fromEntries(response.headers.entries())), + }; + logger.error("HTTP request failed with error status", metadata); + } + return { + ok: false, + error: { + reason: "status-code", + statusCode: response.status, + body: await getErrorResponseBody(response), + }, + rawResponse: toRawResponse(response), + }; + } + } catch (error) { + if (args.abortSignal?.aborted) { + if (logger.isError()) { + const metadata = { + method: args.method, + url: redactUrl(url), + }; + logger.error("HTTP request was aborted", metadata); + } + return { + ok: false, + error: { + reason: "unknown", + errorMessage: "The user aborted a request", + }, + rawResponse: abortRawResponse, + }; + } else if (error instanceof Error && error.name === "AbortError") { + if (logger.isError()) { + const metadata = { + method: args.method, + url: redactUrl(url), + timeoutMs: args.timeoutMs, + }; + logger.error("HTTP request timed out", metadata); + } + return { + ok: false, + error: { + reason: "timeout", + }, + rawResponse: abortRawResponse, + }; + } else if (error instanceof Error) { + if (logger.isError()) { + const metadata = { + method: args.method, + url: redactUrl(url), + errorMessage: error.message, + }; + logger.error("HTTP request failed with error", metadata); + } + return { + ok: false, + error: { + reason: "unknown", + errorMessage: error.message, + }, + rawResponse: unknownRawResponse, + }; + } + + if (logger.isError()) { + const metadata = { + method: args.method, + url: redactUrl(url), + error: toJson(error), + }; + logger.error("HTTP request failed with unknown error", metadata); + } + return { + ok: false, + error: { + reason: "unknown", + errorMessage: toJson(error), + }, + rawResponse: unknownRawResponse, + }; + } +} + +export const fetcher: FetchFunction = fetcherImpl; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/Headers.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/Headers.ts new file mode 100644 index 000000000000..af841aa24f55 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/Headers.ts @@ -0,0 +1,93 @@ +let Headers: typeof globalThis.Headers; + +if (typeof globalThis.Headers !== "undefined") { + Headers = globalThis.Headers; +} else { + Headers = class Headers implements Headers { + private headers: Map; + + constructor(init?: HeadersInit) { + this.headers = new Map(); + + if (init) { + if (init instanceof Headers) { + init.forEach((value, key) => this.append(key, value)); + } else if (Array.isArray(init)) { + for (const [key, value] of init) { + if (typeof key === "string" && typeof value === "string") { + this.append(key, value); + } else { + throw new TypeError("Each header entry must be a [string, string] tuple"); + } + } + } else { + for (const [key, value] of Object.entries(init)) { + if (typeof value === "string") { + this.append(key, value); + } else { + throw new TypeError("Header values must be strings"); + } + } + } + } + } + + append(name: string, value: string): void { + const key = name.toLowerCase(); + const existing = this.headers.get(key) || []; + this.headers.set(key, [...existing, value]); + } + + delete(name: string): void { + const key = name.toLowerCase(); + this.headers.delete(key); + } + + get(name: string): string | null { + const key = name.toLowerCase(); + const values = this.headers.get(key); + return values ? values.join(", ") : null; + } + + has(name: string): boolean { + const key = name.toLowerCase(); + return this.headers.has(key); + } + + set(name: string, value: string): void { + const key = name.toLowerCase(); + this.headers.set(key, [value]); + } + + forEach(callbackfn: (value: string, key: string, parent: Headers) => void, thisArg?: unknown): void { + const boundCallback = thisArg ? callbackfn.bind(thisArg) : callbackfn; + this.headers.forEach((values, key) => boundCallback(values.join(", "), key, this)); + } + + getSetCookie(): string[] { + return this.headers.get("set-cookie") || []; + } + + *entries(): HeadersIterator<[string, string]> { + for (const [key, values] of this.headers.entries()) { + yield [key, values.join(", ")]; + } + } + + *keys(): HeadersIterator { + yield* this.headers.keys(); + } + + *values(): HeadersIterator { + for (const values of this.headers.values()) { + yield values.join(", "); + } + } + + [Symbol.iterator](): HeadersIterator<[string, string]> { + return this.entries(); + } + }; +} + +export { Headers }; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/HttpResponsePromise.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/HttpResponsePromise.ts new file mode 100644 index 000000000000..692ca7d795f0 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/HttpResponsePromise.ts @@ -0,0 +1,116 @@ +import type { WithRawResponse } from "./RawResponse.js"; + +/** + * A promise that returns the parsed response and lets you retrieve the raw response too. + */ +export class HttpResponsePromise extends Promise { + private innerPromise: Promise>; + private unwrappedPromise: Promise | undefined; + + private constructor(promise: Promise>) { + // Initialize with a no-op to avoid premature parsing + super((resolve) => { + resolve(undefined as unknown as T); + }); + this.innerPromise = promise; + } + + /** + * Creates an `HttpResponsePromise` from a function that returns a promise. + * + * @param fn - A function that returns a promise resolving to a `WithRawResponse` object. + * @param args - Arguments to pass to the function. + * @returns An `HttpResponsePromise` instance. + */ + public static fromFunction Promise>, T>( + fn: F, + ...args: Parameters + ): HttpResponsePromise { + return new HttpResponsePromise(fn(...args)); + } + + /** + * Creates a function that returns an `HttpResponsePromise` from a function that returns a promise. + * + * @param fn - A function that returns a promise resolving to a `WithRawResponse` object. + * @returns A function that returns an `HttpResponsePromise` instance. + */ + public static interceptFunction< + F extends (...args: never[]) => Promise>, + T = Awaited>["data"], + >(fn: F): (...args: Parameters) => HttpResponsePromise { + return (...args: Parameters): HttpResponsePromise => { + return HttpResponsePromise.fromPromise(fn(...args)); + }; + } + + /** + * Creates an `HttpResponsePromise` from an existing promise. + * + * @param promise - A promise resolving to a `WithRawResponse` object. + * @returns An `HttpResponsePromise` instance. + */ + public static fromPromise(promise: Promise>): HttpResponsePromise { + return new HttpResponsePromise(promise); + } + + /** + * Creates an `HttpResponsePromise` from an executor function. + * + * @param executor - A function that takes resolve and reject callbacks to create a promise. + * @returns An `HttpResponsePromise` instance. + */ + public static fromExecutor( + executor: (resolve: (value: WithRawResponse) => void, reject: (reason?: unknown) => void) => void, + ): HttpResponsePromise { + const promise = new Promise>(executor); + return new HttpResponsePromise(promise); + } + + /** + * Creates an `HttpResponsePromise` from a resolved result. + * + * @param result - A `WithRawResponse` object to resolve immediately. + * @returns An `HttpResponsePromise` instance. + */ + public static fromResult(result: WithRawResponse): HttpResponsePromise { + const promise = Promise.resolve(result); + return new HttpResponsePromise(promise); + } + + private unwrap(): Promise { + if (!this.unwrappedPromise) { + this.unwrappedPromise = this.innerPromise.then(({ data }) => data); + } + return this.unwrappedPromise; + } + + /** @inheritdoc */ + public override then( + onfulfilled?: ((value: T) => TResult1 | PromiseLike) | null, + onrejected?: ((reason: unknown) => TResult2 | PromiseLike) | null, + ): Promise { + return this.unwrap().then(onfulfilled, onrejected); + } + + /** @inheritdoc */ + public override catch( + onrejected?: ((reason: unknown) => TResult | PromiseLike) | null, + ): Promise { + return this.unwrap().catch(onrejected); + } + + /** @inheritdoc */ + public override finally(onfinally?: (() => void) | null): Promise { + return this.unwrap().finally(onfinally); + } + + /** + * Retrieves the data and raw response. + * + * @returns A promise resolving to a `WithRawResponse` object. + */ + public async withRawResponse(): Promise> { + return await this.innerPromise; + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/RawResponse.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/RawResponse.ts new file mode 100644 index 000000000000..37fb44e2aa99 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/RawResponse.ts @@ -0,0 +1,61 @@ +import { Headers } from "./Headers.js"; + +/** + * The raw response from the fetch call excluding the body. + */ +export type RawResponse = Omit< + { + [K in keyof Response as Response[K] extends Function ? never : K]: Response[K]; // strips out functions + }, + "ok" | "body" | "bodyUsed" +>; // strips out body and bodyUsed + +/** + * A raw response indicating that the request was aborted. + */ +export const abortRawResponse: RawResponse = { + headers: new Headers(), + redirected: false, + status: 499, + statusText: "Client Closed Request", + type: "error", + url: "", +} as const; + +/** + * A raw response indicating an unknown error. + */ +export const unknownRawResponse: RawResponse = { + headers: new Headers(), + redirected: false, + status: 0, + statusText: "Unknown Error", + type: "error", + url: "", +} as const; + +/** + * Converts a `RawResponse` object into a `RawResponse` by extracting its properties, + * excluding the `body` and `bodyUsed` fields. + * + * @param response - The `RawResponse` object to convert. + * @returns A `RawResponse` object containing the extracted properties of the input response. + */ +export function toRawResponse(response: Response): RawResponse { + return { + headers: response.headers, + redirected: response.redirected, + status: response.status, + statusText: response.statusText, + type: response.type, + url: response.url, + }; +} + +/** + * Creates a `RawResponse` from a standard `Response` object. + */ +export interface WithRawResponse { + readonly data: T; + readonly rawResponse: RawResponse; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/Supplier.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/Supplier.ts new file mode 100644 index 000000000000..867c931c02f4 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/Supplier.ts @@ -0,0 +1,11 @@ +export type Supplier = T | Promise | (() => T | Promise); + +export const Supplier = { + get: async (supplier: Supplier): Promise => { + if (typeof supplier === "function") { + return (supplier as () => T)(); + } else { + return supplier; + } + }, +}; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/createRequestUrl.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/createRequestUrl.ts new file mode 100644 index 000000000000..88e13265e112 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/createRequestUrl.ts @@ -0,0 +1,6 @@ +import { toQueryString } from "../url/qs.js"; + +export function createRequestUrl(baseUrl: string, queryParameters?: Record): string { + const queryString = toQueryString(queryParameters, { arrayFormat: "repeat" }); + return queryString ? `${baseUrl}?${queryString}` : baseUrl; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getErrorResponseBody.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getErrorResponseBody.ts new file mode 100644 index 000000000000..7cf4e623c2f5 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getErrorResponseBody.ts @@ -0,0 +1,33 @@ +import { fromJson } from "../json.js"; +import { getResponseBody } from "./getResponseBody.js"; + +export async function getErrorResponseBody(response: Response): Promise { + let contentType = response.headers.get("Content-Type")?.toLowerCase(); + if (contentType == null || contentType.length === 0) { + return getResponseBody(response); + } + + if (contentType.indexOf(";") !== -1) { + contentType = contentType.split(";")[0]?.trim() ?? ""; + } + switch (contentType) { + case "application/hal+json": + case "application/json": + case "application/ld+json": + case "application/problem+json": + case "application/vnd.api+json": + case "text/json": { + const text = await response.text(); + return text.length > 0 ? fromJson(text) : undefined; + } + default: + if (contentType.startsWith("application/vnd.") && contentType.endsWith("+json")) { + const text = await response.text(); + return text.length > 0 ? fromJson(text) : undefined; + } + + // Fallback to plain text if content type is not recognized + // Even if no body is present, the response will be an empty string + return await response.text(); + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getFetchFn.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getFetchFn.ts new file mode 100644 index 000000000000..9f845b956392 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getFetchFn.ts @@ -0,0 +1,3 @@ +export async function getFetchFn(): Promise { + return fetch; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getHeader.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getHeader.ts new file mode 100644 index 000000000000..50f922b0e87f --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getHeader.ts @@ -0,0 +1,8 @@ +export function getHeader(headers: Record, header: string): string | undefined { + for (const [headerKey, headerValue] of Object.entries(headers)) { + if (headerKey.toLowerCase() === header.toLowerCase()) { + return headerValue; + } + } + return undefined; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getRequestBody.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getRequestBody.ts new file mode 100644 index 000000000000..91d9d81f50e5 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getRequestBody.ts @@ -0,0 +1,20 @@ +import { toJson } from "../json.js"; +import { toQueryString } from "../url/qs.js"; + +export declare namespace GetRequestBody { + interface Args { + body: unknown; + type: "json" | "file" | "bytes" | "form" | "other"; + } +} + +export async function getRequestBody({ body, type }: GetRequestBody.Args): Promise { + if (type === "form") { + return toQueryString(body, { arrayFormat: "repeat", encode: true }); + } + if (type.includes("json")) { + return toJson(body); + } else { + return body as BodyInit; + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getResponseBody.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getResponseBody.ts new file mode 100644 index 000000000000..708d55728f2b --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/getResponseBody.ts @@ -0,0 +1,58 @@ +import { fromJson } from "../json.js"; +import { getBinaryResponse } from "./BinaryResponse.js"; + +export async function getResponseBody(response: Response, responseType?: string): Promise { + switch (responseType) { + case "binary-response": + return getBinaryResponse(response); + case "blob": + return await response.blob(); + case "arrayBuffer": + return await response.arrayBuffer(); + case "sse": + if (response.body == null) { + return { + ok: false, + error: { + reason: "body-is-null", + statusCode: response.status, + }, + }; + } + return response.body; + case "streaming": + if (response.body == null) { + return { + ok: false, + error: { + reason: "body-is-null", + statusCode: response.status, + }, + }; + } + + return response.body; + + case "text": + return await response.text(); + } + + // if responseType is "json" or not specified, try to parse as JSON + const text = await response.text(); + if (text.length > 0) { + try { + const responseBody = fromJson(text); + return responseBody; + } catch (_err) { + return { + ok: false, + error: { + reason: "non-json", + statusCode: response.status, + rawBody: text, + }, + }; + } + } + return undefined; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/index.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/index.ts new file mode 100644 index 000000000000..bd5db362c778 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/index.ts @@ -0,0 +1,13 @@ +export type { APIResponse } from "./APIResponse.js"; +export type { BinaryResponse } from "./BinaryResponse.js"; +export type { EndpointMetadata } from "./EndpointMetadata.js"; +export { EndpointSupplier } from "./EndpointSupplier.js"; +export type { Fetcher, FetchFunction } from "./Fetcher.js"; +export { fetcher } from "./Fetcher.js"; +export { getHeader } from "./getHeader.js"; +export { HttpResponsePromise } from "./HttpResponsePromise.js"; +export type { PassthroughRequest } from "./makePassthroughRequest.js"; +export { makePassthroughRequest } from "./makePassthroughRequest.js"; +export type { RawResponse, WithRawResponse } from "./RawResponse.js"; +export { abortRawResponse, toRawResponse, unknownRawResponse } from "./RawResponse.js"; +export { Supplier } from "./Supplier.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/makePassthroughRequest.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/makePassthroughRequest.ts new file mode 100644 index 000000000000..f5ba761400f8 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/makePassthroughRequest.ts @@ -0,0 +1,189 @@ +import { createLogger, type LogConfig, type Logger } from "../logging/logger.js"; +import { join } from "../url/join.js"; +import { EndpointSupplier } from "./EndpointSupplier.js"; +import { getFetchFn } from "./getFetchFn.js"; +import { makeRequest } from "./makeRequest.js"; +import { requestWithRetries } from "./requestWithRetries.js"; +import { Supplier } from "./Supplier.js"; + +export declare namespace PassthroughRequest { + /** + * Per-request options that can override the SDK client defaults. + */ + export interface RequestOptions { + /** Override the default timeout for this request (in seconds). */ + timeoutInSeconds?: number; + /** Override the default number of retries for this request. */ + maxRetries?: number; + /** Additional headers to include in this request. */ + headers?: Record; + /** Abort signal for this request. */ + abortSignal?: AbortSignal; + } + + /** + * SDK client configuration used by the passthrough fetch method. + */ + export interface ClientOptions { + /** The base URL or environment for the client. */ + environment?: Supplier; + /** Override the base URL. */ + baseUrl?: Supplier; + /** Default headers to include in requests. */ + headers?: Record; + /** Default maximum time to wait for a response in seconds. */ + timeoutInSeconds?: number; + /** Default number of times to retry the request. Defaults to 2. */ + maxRetries?: number; + /** A custom fetch function. */ + fetch?: typeof fetch; + /** Logging configuration. */ + logging?: LogConfig | Logger; + /** A function that returns auth headers. */ + getAuthHeaders?: () => Promise>; + } +} + +/** + * Makes a passthrough HTTP request using the SDK's configuration (auth, retry, logging, etc.) + * while mimicking the standard `fetch` API. + * + * @param input - The URL, path, or Request object. If a relative path, it will be resolved against the configured base URL. + * @param init - Standard RequestInit options (method, headers, body, signal, etc.) + * @param clientOptions - SDK client options (auth, default headers, logging, etc.) + * @param requestOptions - Per-request overrides (timeout, retries, extra headers, abort signal). + * @returns A standard Response object. + */ +export async function makePassthroughRequest( + input: Request | string | URL, + init: RequestInit | undefined, + clientOptions: PassthroughRequest.ClientOptions, + requestOptions?: PassthroughRequest.RequestOptions, +): Promise { + const logger = createLogger(clientOptions.logging); + + // Extract URL and default init properties from Request object if provided + let url: string; + let effectiveInit: RequestInit | undefined = init; + if (input instanceof Request) { + url = input.url; + // If no explicit init provided, extract properties from the Request object + if (init == null) { + effectiveInit = { + method: input.method, + headers: Object.fromEntries(input.headers.entries()), + body: input.body, + signal: input.signal, + credentials: input.credentials, + cache: input.cache as RequestCache, + redirect: input.redirect, + referrer: input.referrer, + integrity: input.integrity, + mode: input.mode, + }; + } + } else { + url = input instanceof URL ? input.toString() : input; + } + + // Resolve the base URL + const baseUrl = + (clientOptions.baseUrl != null ? await Supplier.get(clientOptions.baseUrl) : undefined) ?? + (clientOptions.environment != null ? await Supplier.get(clientOptions.environment) : undefined); + + // Determine the full URL + let fullUrl: string; + if (url.startsWith("http://") || url.startsWith("https://")) { + fullUrl = url; + } else if (baseUrl != null) { + fullUrl = join(baseUrl, url); + } else { + fullUrl = url; + } + + // Merge headers: SDK default headers -> auth headers -> user-provided headers + const mergedHeaders: Record = {}; + + // Apply SDK default headers (resolve suppliers) + if (clientOptions.headers != null) { + for (const [key, value] of Object.entries(clientOptions.headers)) { + const resolved = await EndpointSupplier.get(value, { endpointMetadata: {} }); + if (resolved != null) { + mergedHeaders[key.toLowerCase()] = `${resolved}`; + } + } + } + + // Apply auth headers + if (clientOptions.getAuthHeaders != null) { + const authHeaders = await clientOptions.getAuthHeaders(); + for (const [key, value] of Object.entries(authHeaders)) { + mergedHeaders[key.toLowerCase()] = value; + } + } + + // Apply user-provided headers from init + if (effectiveInit?.headers != null) { + const initHeaders = + effectiveInit.headers instanceof Headers + ? Object.fromEntries(effectiveInit.headers.entries()) + : Array.isArray(effectiveInit.headers) + ? Object.fromEntries(effectiveInit.headers) + : effectiveInit.headers; + for (const [key, value] of Object.entries(initHeaders)) { + if (value != null) { + mergedHeaders[key.toLowerCase()] = value; + } + } + } + + // Apply per-request option headers (highest priority) + if (requestOptions?.headers != null) { + for (const [key, value] of Object.entries(requestOptions.headers)) { + mergedHeaders[key.toLowerCase()] = value; + } + } + + const method = effectiveInit?.method ?? "GET"; + const body = effectiveInit?.body; + const timeoutInSeconds = requestOptions?.timeoutInSeconds ?? clientOptions.timeoutInSeconds; + const timeoutMs = timeoutInSeconds != null ? timeoutInSeconds * 1000 : undefined; + const maxRetries = requestOptions?.maxRetries ?? clientOptions.maxRetries; + const abortSignal = requestOptions?.abortSignal ?? effectiveInit?.signal ?? undefined; + const fetchFn = clientOptions.fetch ?? (await getFetchFn()); + + if (logger.isDebug()) { + logger.debug("Making passthrough HTTP request", { + method, + url: fullUrl, + hasBody: body != null, + }); + } + + const response = await requestWithRetries( + async () => + makeRequest( + fetchFn, + fullUrl, + method, + mergedHeaders, + body ?? undefined, + timeoutMs, + abortSignal, + effectiveInit?.credentials === "include", + undefined, // duplex + false, // disableCache + ), + maxRetries, + ); + + if (logger.isDebug()) { + logger.debug("Passthrough HTTP request completed", { + method, + url: fullUrl, + statusCode: response.status, + }); + } + + return response; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/makeRequest.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/makeRequest.ts new file mode 100644 index 000000000000..360a86df40ad --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/makeRequest.ts @@ -0,0 +1,70 @@ +import { anySignal, getTimeoutSignal } from "./signals.js"; + +/** + * Cached result of checking whether the current runtime supports + * the `cache` option in `Request`. Some runtimes (e.g. Cloudflare Workers) + * throw a TypeError when this option is used. + */ +let _cacheNoStoreSupported: boolean | undefined; +export function isCacheNoStoreSupported(): boolean { + if (_cacheNoStoreSupported != null) { + return _cacheNoStoreSupported; + } + try { + new Request("http://localhost", { cache: "no-store" }); + _cacheNoStoreSupported = true; + } catch { + _cacheNoStoreSupported = false; + } + return _cacheNoStoreSupported; +} + +/** + * Reset the cached result of `isCacheNoStoreSupported`. Exposed for testing only. + */ +export function resetCacheNoStoreSupported(): void { + _cacheNoStoreSupported = undefined; +} + +export const makeRequest = async ( + fetchFn: (url: string, init: RequestInit) => Promise, + url: string, + method: string, + headers: Headers | Record, + requestBody: BodyInit | undefined, + timeoutMs?: number, + abortSignal?: AbortSignal, + withCredentials?: boolean, + duplex?: "half", + disableCache?: boolean, +): Promise => { + const signals: AbortSignal[] = []; + + let timeoutAbortId: ReturnType | undefined; + if (timeoutMs != null) { + const { signal, abortId } = getTimeoutSignal(timeoutMs); + timeoutAbortId = abortId; + signals.push(signal); + } + + if (abortSignal != null) { + signals.push(abortSignal); + } + const newSignals = anySignal(signals); + const response = await fetchFn(url, { + method: method, + headers, + body: requestBody, + signal: newSignals, + credentials: withCredentials ? "include" : undefined, + // @ts-ignore + duplex, + ...(disableCache && isCacheNoStoreSupported() ? { cache: "no-store" as RequestCache } : {}), + }); + + if (timeoutAbortId != null) { + clearTimeout(timeoutAbortId); + } + + return response; +}; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/requestWithRetries.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/requestWithRetries.ts new file mode 100644 index 000000000000..1f689688c4b2 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/requestWithRetries.ts @@ -0,0 +1,64 @@ +const INITIAL_RETRY_DELAY = 1000; // in milliseconds +const MAX_RETRY_DELAY = 60000; // in milliseconds +const DEFAULT_MAX_RETRIES = 2; +const JITTER_FACTOR = 0.2; // 20% random jitter + +function addPositiveJitter(delay: number): number { + const jitterMultiplier = 1 + Math.random() * JITTER_FACTOR; + return delay * jitterMultiplier; +} + +function addSymmetricJitter(delay: number): number { + const jitterMultiplier = 1 + (Math.random() - 0.5) * JITTER_FACTOR; + return delay * jitterMultiplier; +} + +function getRetryDelayFromHeaders(response: Response, retryAttempt: number): number { + const retryAfter = response.headers.get("Retry-After"); + if (retryAfter) { + const retryAfterSeconds = parseInt(retryAfter, 10); + if (!Number.isNaN(retryAfterSeconds) && retryAfterSeconds > 0) { + return Math.min(retryAfterSeconds * 1000, MAX_RETRY_DELAY); + } + + const retryAfterDate = new Date(retryAfter); + if (!Number.isNaN(retryAfterDate.getTime())) { + const delay = retryAfterDate.getTime() - Date.now(); + if (delay > 0) { + return Math.min(Math.max(delay, 0), MAX_RETRY_DELAY); + } + } + } + + const rateLimitReset = response.headers.get("X-RateLimit-Reset"); + if (rateLimitReset) { + const resetTime = parseInt(rateLimitReset, 10); + if (!Number.isNaN(resetTime)) { + const delay = resetTime * 1000 - Date.now(); + if (delay > 0) { + return addPositiveJitter(Math.min(delay, MAX_RETRY_DELAY)); + } + } + } + + return addSymmetricJitter(Math.min(INITIAL_RETRY_DELAY * 2 ** retryAttempt, MAX_RETRY_DELAY)); +} + +export async function requestWithRetries( + requestFn: () => Promise, + maxRetries: number = DEFAULT_MAX_RETRIES, +): Promise { + let response: Response = await requestFn(); + + for (let i = 0; i < maxRetries; ++i) { + if ([408, 429].includes(response.status) || response.status >= 500) { + const delay = getRetryDelayFromHeaders(response, i); + + await new Promise((resolve) => setTimeout(resolve, delay)); + response = await requestFn(); + } else { + break; + } + } + return response!; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/fetcher/signals.ts b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/signals.ts new file mode 100644 index 000000000000..7bd3757ec3a7 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/fetcher/signals.ts @@ -0,0 +1,26 @@ +const TIMEOUT = "timeout"; + +export function getTimeoutSignal(timeoutMs: number): { signal: AbortSignal; abortId: ReturnType } { + const controller = new AbortController(); + const abortId = setTimeout(() => controller.abort(TIMEOUT), timeoutMs); + return { signal: controller.signal, abortId }; +} + +export function anySignal(...args: AbortSignal[] | [AbortSignal[]]): AbortSignal { + const signals = (args.length === 1 && Array.isArray(args[0]) ? args[0] : args) as AbortSignal[]; + + const controller = new AbortController(); + + for (const signal of signals) { + if (signal.aborted) { + controller.abort((signal as any)?.reason); + break; + } + + signal.addEventListener("abort", () => controller.abort((signal as any)?.reason), { + signal: controller.signal, + }); + } + + return controller.signal; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/headers.ts b/seed/ts-sdk/basic-auth-optional/src/core/headers.ts new file mode 100644 index 000000000000..be45c4552a35 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/headers.ts @@ -0,0 +1,33 @@ +export function mergeHeaders(...headersArray: (Record | null | undefined)[]): Record { + const result: Record = {}; + + for (const [key, value] of headersArray + .filter((headers) => headers != null) + .flatMap((headers) => Object.entries(headers))) { + const insensitiveKey = key.toLowerCase(); + if (value != null) { + result[insensitiveKey] = value; + } else if (insensitiveKey in result) { + delete result[insensitiveKey]; + } + } + + return result; +} + +export function mergeOnlyDefinedHeaders( + ...headersArray: (Record | null | undefined)[] +): Record { + const result: Record = {}; + + for (const [key, value] of headersArray + .filter((headers) => headers != null) + .flatMap((headers) => Object.entries(headers))) { + const insensitiveKey = key.toLowerCase(); + if (value != null) { + result[insensitiveKey] = value; + } + } + + return result; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/index.ts b/seed/ts-sdk/basic-auth-optional/src/core/index.ts new file mode 100644 index 000000000000..92290bfadcac --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/index.ts @@ -0,0 +1,6 @@ +export * from "./auth/index.js"; +export * from "./base64.js"; +export * from "./fetcher/index.js"; +export * as logging from "./logging/index.js"; +export * from "./runtime/index.js"; +export * as url from "./url/index.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/json.ts b/seed/ts-sdk/basic-auth-optional/src/core/json.ts new file mode 100644 index 000000000000..c052f3249f4f --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/json.ts @@ -0,0 +1,27 @@ +/** + * Serialize a value to JSON + * @param value A JavaScript value, usually an object or array, to be converted. + * @param replacer A function that transforms the results. + * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read. + * @returns JSON string + */ +export const toJson = ( + value: unknown, + replacer?: (this: unknown, key: string, value: unknown) => unknown, + space?: string | number, +): string => { + return JSON.stringify(value, replacer, space); +}; + +/** + * Parse JSON string to object, array, or other type + * @param text A valid JSON string. + * @param reviver A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is. + * @returns Parsed object, array, or other type + */ +export function fromJson( + text: string, + reviver?: (this: unknown, key: string, value: unknown) => unknown, +): T { + return JSON.parse(text, reviver); +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/logging/exports.ts b/seed/ts-sdk/basic-auth-optional/src/core/logging/exports.ts new file mode 100644 index 000000000000..88f6c00db0cf --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/logging/exports.ts @@ -0,0 +1,19 @@ +import * as logger from "./logger.js"; + +export namespace logging { + /** + * Configuration for logger instances. + */ + export type LogConfig = logger.LogConfig; + export type LogLevel = logger.LogLevel; + export const LogLevel: typeof logger.LogLevel = logger.LogLevel; + export type ILogger = logger.ILogger; + /** + * Console logger implementation that outputs to the console. + */ + export type ConsoleLogger = logger.ConsoleLogger; + /** + * Console logger implementation that outputs to the console. + */ + export const ConsoleLogger: typeof logger.ConsoleLogger = logger.ConsoleLogger; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/logging/index.ts b/seed/ts-sdk/basic-auth-optional/src/core/logging/index.ts new file mode 100644 index 000000000000..d81cc32c40f9 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/logging/index.ts @@ -0,0 +1 @@ +export * from "./logger.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/logging/logger.ts b/seed/ts-sdk/basic-auth-optional/src/core/logging/logger.ts new file mode 100644 index 000000000000..a3f3673cda93 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/logging/logger.ts @@ -0,0 +1,203 @@ +export const LogLevel = { + Debug: "debug", + Info: "info", + Warn: "warn", + Error: "error", +} as const; +export type LogLevel = (typeof LogLevel)[keyof typeof LogLevel]; +const logLevelMap: Record = { + [LogLevel.Debug]: 1, + [LogLevel.Info]: 2, + [LogLevel.Warn]: 3, + [LogLevel.Error]: 4, +}; + +export interface ILogger { + /** + * Logs a debug message. + * @param message - The message to log + * @param args - Additional arguments to log + */ + debug(message: string, ...args: unknown[]): void; + /** + * Logs an info message. + * @param message - The message to log + * @param args - Additional arguments to log + */ + info(message: string, ...args: unknown[]): void; + /** + * Logs a warning message. + * @param message - The message to log + * @param args - Additional arguments to log + */ + warn(message: string, ...args: unknown[]): void; + /** + * Logs an error message. + * @param message - The message to log + * @param args - Additional arguments to log + */ + error(message: string, ...args: unknown[]): void; +} + +/** + * Configuration for logger initialization. + */ +export interface LogConfig { + /** + * Minimum log level to output. + * @default LogLevel.Info + */ + level?: LogLevel; + /** + * Logger implementation to use. + * @default new ConsoleLogger() + */ + logger?: ILogger; + /** + * Whether logging should be silenced. + * @default true + */ + silent?: boolean; +} + +/** + * Default console-based logger implementation. + */ +export class ConsoleLogger implements ILogger { + debug(message: string, ...args: unknown[]): void { + console.debug(message, ...args); + } + info(message: string, ...args: unknown[]): void { + console.info(message, ...args); + } + warn(message: string, ...args: unknown[]): void { + console.warn(message, ...args); + } + error(message: string, ...args: unknown[]): void { + console.error(message, ...args); + } +} + +/** + * Logger class that provides level-based logging functionality. + */ +export class Logger { + private readonly level: number; + private readonly logger: ILogger; + private readonly silent: boolean; + + /** + * Creates a new logger instance. + * @param config - Logger configuration + */ + constructor(config: Required) { + this.level = logLevelMap[config.level]; + this.logger = config.logger; + this.silent = config.silent; + } + + /** + * Checks if a log level should be output based on configuration. + * @param level - The log level to check + * @returns True if the level should be logged + */ + public shouldLog(level: LogLevel): boolean { + return !this.silent && this.level <= logLevelMap[level]; + } + + /** + * Checks if debug logging is enabled. + * @returns True if debug logs should be output + */ + public isDebug(): boolean { + return this.shouldLog(LogLevel.Debug); + } + + /** + * Logs a debug message if debug logging is enabled. + * @param message - The message to log + * @param args - Additional arguments to log + */ + public debug(message: string, ...args: unknown[]): void { + if (this.isDebug()) { + this.logger.debug(message, ...args); + } + } + + /** + * Checks if info logging is enabled. + * @returns True if info logs should be output + */ + public isInfo(): boolean { + return this.shouldLog(LogLevel.Info); + } + + /** + * Logs an info message if info logging is enabled. + * @param message - The message to log + * @param args - Additional arguments to log + */ + public info(message: string, ...args: unknown[]): void { + if (this.isInfo()) { + this.logger.info(message, ...args); + } + } + + /** + * Checks if warning logging is enabled. + * @returns True if warning logs should be output + */ + public isWarn(): boolean { + return this.shouldLog(LogLevel.Warn); + } + + /** + * Logs a warning message if warning logging is enabled. + * @param message - The message to log + * @param args - Additional arguments to log + */ + public warn(message: string, ...args: unknown[]): void { + if (this.isWarn()) { + this.logger.warn(message, ...args); + } + } + + /** + * Checks if error logging is enabled. + * @returns True if error logs should be output + */ + public isError(): boolean { + return this.shouldLog(LogLevel.Error); + } + + /** + * Logs an error message if error logging is enabled. + * @param message - The message to log + * @param args - Additional arguments to log + */ + public error(message: string, ...args: unknown[]): void { + if (this.isError()) { + this.logger.error(message, ...args); + } + } +} + +export function createLogger(config?: LogConfig | Logger): Logger { + if (config == null) { + return defaultLogger; + } + if (config instanceof Logger) { + return config; + } + config = config ?? {}; + config.level ??= LogLevel.Info; + config.logger ??= new ConsoleLogger(); + config.silent ??= true; + return new Logger(config as Required); +} + +const defaultLogger: Logger = new Logger({ + level: LogLevel.Info, + logger: new ConsoleLogger(), + silent: true, +}); diff --git a/seed/ts-sdk/basic-auth-optional/src/core/runtime/index.ts b/seed/ts-sdk/basic-auth-optional/src/core/runtime/index.ts new file mode 100644 index 000000000000..cfab23f9a834 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/runtime/index.ts @@ -0,0 +1 @@ +export { RUNTIME } from "./runtime.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/runtime/runtime.ts b/seed/ts-sdk/basic-auth-optional/src/core/runtime/runtime.ts new file mode 100644 index 000000000000..e6e66b2a7bce --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/runtime/runtime.ts @@ -0,0 +1,134 @@ +interface DenoGlobal { + version: { + deno: string; + }; +} + +interface BunGlobal { + version: string; +} + +declare const Deno: DenoGlobal | undefined; +declare const Bun: BunGlobal | undefined; +declare const EdgeRuntime: string | undefined; +declare const self: typeof globalThis.self & { + importScripts?: unknown; +}; + +/** + * A constant that indicates which environment and version the SDK is running in. + */ +export const RUNTIME: Runtime = evaluateRuntime(); + +export interface Runtime { + type: "browser" | "web-worker" | "deno" | "bun" | "node" | "react-native" | "unknown" | "workerd" | "edge-runtime"; + version?: string; + parsedVersion?: number; +} + +function evaluateRuntime(): Runtime { + /** + * A constant that indicates whether the environment the code is running is a Web Browser. + */ + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; + if (isBrowser) { + return { + type: "browser", + version: window.navigator.userAgent, + }; + } + + /** + * A constant that indicates whether the environment the code is running is Cloudflare. + * https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + */ + const isCloudflare = typeof globalThis !== "undefined" && globalThis?.navigator?.userAgent === "Cloudflare-Workers"; + if (isCloudflare) { + return { + type: "workerd", + }; + } + + /** + * A constant that indicates whether the environment the code is running is Edge Runtime. + * https://vercel.com/docs/functions/runtimes/edge-runtime#check-if-you're-running-on-the-edge-runtime + */ + const isEdgeRuntime = typeof EdgeRuntime === "string"; + if (isEdgeRuntime) { + return { + type: "edge-runtime", + }; + } + + /** + * A constant that indicates whether the environment the code is running is a Web Worker. + */ + const isWebWorker = + typeof self === "object" && + typeof self?.importScripts === "function" && + (self.constructor?.name === "DedicatedWorkerGlobalScope" || + self.constructor?.name === "ServiceWorkerGlobalScope" || + self.constructor?.name === "SharedWorkerGlobalScope"); + if (isWebWorker) { + return { + type: "web-worker", + }; + } + + /** + * A constant that indicates whether the environment the code is running is Deno. + * FYI Deno spoofs process.versions.node, see https://deno.land/std@0.177.0/node/process.ts?s=versions + */ + const isDeno = + typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined"; + if (isDeno) { + return { + type: "deno", + version: Deno.version.deno, + }; + } + + /** + * A constant that indicates whether the environment the code is running is Bun.sh. + */ + const isBun = typeof Bun !== "undefined" && typeof Bun.version !== "undefined"; + if (isBun) { + return { + type: "bun", + version: Bun.version, + }; + } + + /** + * A constant that indicates whether the environment the code is running is in React-Native. + * This check should come before Node.js detection since React Native may have a process polyfill. + * https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Core/setUpNavigator.js + */ + const isReactNative = typeof navigator !== "undefined" && navigator?.product === "ReactNative"; + if (isReactNative) { + return { + type: "react-native", + }; + } + + /** + * A constant that indicates whether the environment the code is running is Node.JS. + * + * We assign `process` to a local variable first to avoid being flagged by + * bundlers that perform static analysis on `process.versions` (e.g. Next.js + * Edge Runtime warns about Node.js APIs even when they are guarded). + */ + const _process = typeof process !== "undefined" ? process : undefined; + const isNode = typeof _process !== "undefined" && typeof _process.versions?.node === "string"; + if (isNode) { + return { + type: "node", + version: _process.versions.node, + parsedVersion: Number(_process.versions.node.split(".")[0]), + }; + } + + return { + type: "unknown", + }; +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/url/encodePathParam.ts b/seed/ts-sdk/basic-auth-optional/src/core/url/encodePathParam.ts new file mode 100644 index 000000000000..19b901244218 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/url/encodePathParam.ts @@ -0,0 +1,18 @@ +export function encodePathParam(param: unknown): string { + if (param === null) { + return "null"; + } + const typeofParam = typeof param; + switch (typeofParam) { + case "undefined": + return "undefined"; + case "string": + case "number": + case "boolean": + break; + default: + param = String(param); + break; + } + return encodeURIComponent(param as string | number | boolean); +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/url/index.ts b/seed/ts-sdk/basic-auth-optional/src/core/url/index.ts new file mode 100644 index 000000000000..f2e0fa2d2221 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/url/index.ts @@ -0,0 +1,3 @@ +export { encodePathParam } from "./encodePathParam.js"; +export { join } from "./join.js"; +export { toQueryString } from "./qs.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/core/url/join.ts b/seed/ts-sdk/basic-auth-optional/src/core/url/join.ts new file mode 100644 index 000000000000..7ca7daef094d --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/url/join.ts @@ -0,0 +1,79 @@ +export function join(base: string, ...segments: string[]): string { + if (!base) { + return ""; + } + + if (segments.length === 0) { + return base; + } + + if (base.includes("://")) { + let url: URL; + try { + url = new URL(base); + } catch { + return joinPath(base, ...segments); + } + + const lastSegment = segments[segments.length - 1]; + const shouldPreserveTrailingSlash = lastSegment?.endsWith("/"); + + for (const segment of segments) { + const cleanSegment = trimSlashes(segment); + if (cleanSegment) { + url.pathname = joinPathSegments(url.pathname, cleanSegment); + } + } + + if (shouldPreserveTrailingSlash && !url.pathname.endsWith("/")) { + url.pathname += "/"; + } + + return url.toString(); + } + + return joinPath(base, ...segments); +} + +function joinPath(base: string, ...segments: string[]): string { + if (segments.length === 0) { + return base; + } + + let result = base; + + const lastSegment = segments[segments.length - 1]; + const shouldPreserveTrailingSlash = lastSegment?.endsWith("/"); + + for (const segment of segments) { + const cleanSegment = trimSlashes(segment); + if (cleanSegment) { + result = joinPathSegments(result, cleanSegment); + } + } + + if (shouldPreserveTrailingSlash && !result.endsWith("/")) { + result += "/"; + } + + return result; +} + +function joinPathSegments(left: string, right: string): string { + if (left.endsWith("/")) { + return left + right; + } + return `${left}/${right}`; +} + +function trimSlashes(str: string): string { + if (!str) return str; + + let start = 0; + let end = str.length; + + if (str.startsWith("/")) start = 1; + if (str.endsWith("/")) end = str.length - 1; + + return start === 0 && end === str.length ? str : str.slice(start, end); +} diff --git a/seed/ts-sdk/basic-auth-optional/src/core/url/qs.ts b/seed/ts-sdk/basic-auth-optional/src/core/url/qs.ts new file mode 100644 index 000000000000..13e89be9d9a6 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/core/url/qs.ts @@ -0,0 +1,74 @@ +interface QueryStringOptions { + arrayFormat?: "indices" | "repeat"; + encode?: boolean; +} + +const defaultQsOptions: Required = { + arrayFormat: "indices", + encode: true, +} as const; + +function encodeValue(value: unknown, shouldEncode: boolean): string { + if (value === undefined) { + return ""; + } + if (value === null) { + return ""; + } + const stringValue = String(value); + return shouldEncode ? encodeURIComponent(stringValue) : stringValue; +} + +function stringifyObject(obj: Record, prefix = "", options: Required): string[] { + const parts: string[] = []; + + for (const [key, value] of Object.entries(obj)) { + const fullKey = prefix ? `${prefix}[${key}]` : key; + + if (value === undefined) { + continue; + } + + if (Array.isArray(value)) { + if (value.length === 0) { + continue; + } + for (let i = 0; i < value.length; i++) { + const item = value[i]; + if (item === undefined) { + continue; + } + if (typeof item === "object" && !Array.isArray(item) && item !== null) { + const arrayKey = options.arrayFormat === "indices" ? `${fullKey}[${i}]` : fullKey; + parts.push(...stringifyObject(item as Record, arrayKey, options)); + } else { + const arrayKey = options.arrayFormat === "indices" ? `${fullKey}[${i}]` : fullKey; + const encodedKey = options.encode ? encodeURIComponent(arrayKey) : arrayKey; + parts.push(`${encodedKey}=${encodeValue(item, options.encode)}`); + } + } + } else if (typeof value === "object" && value !== null) { + if (Object.keys(value as Record).length === 0) { + continue; + } + parts.push(...stringifyObject(value as Record, fullKey, options)); + } else { + const encodedKey = options.encode ? encodeURIComponent(fullKey) : fullKey; + parts.push(`${encodedKey}=${encodeValue(value, options.encode)}`); + } + } + + return parts; +} + +export function toQueryString(obj: unknown, options?: QueryStringOptions): string { + if (obj == null || typeof obj !== "object") { + return ""; + } + + const parts = stringifyObject(obj as Record, "", { + ...defaultQsOptions, + ...options, + }); + return parts.join("&"); +} diff --git a/seed/ts-sdk/basic-auth-optional/src/errors/SeedBasicAuthOptionalError.ts b/seed/ts-sdk/basic-auth-optional/src/errors/SeedBasicAuthOptionalError.ts new file mode 100644 index 000000000000..f632ae40c9ee --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/errors/SeedBasicAuthOptionalError.ts @@ -0,0 +1,58 @@ +// This file was auto-generated by Fern from our API Definition. + +import type * as core from "../core/index.js"; +import { toJson } from "../core/json.js"; + +export class SeedBasicAuthOptionalError extends Error { + public readonly statusCode?: number; + public readonly body?: unknown; + public readonly rawResponse?: core.RawResponse; + + constructor({ + message, + statusCode, + body, + rawResponse, + }: { + message?: string; + statusCode?: number; + body?: unknown; + rawResponse?: core.RawResponse; + }) { + super(buildMessage({ message, statusCode, body })); + Object.setPrototypeOf(this, new.target.prototype); + if (Error.captureStackTrace) { + Error.captureStackTrace(this, this.constructor); + } + + this.name = this.constructor.name; + this.statusCode = statusCode; + this.body = body; + this.rawResponse = rawResponse; + } +} + +function buildMessage({ + message, + statusCode, + body, +}: { + message: string | undefined; + statusCode: number | undefined; + body: unknown | undefined; +}): string { + const lines: string[] = []; + if (message != null) { + lines.push(message); + } + + if (statusCode != null) { + lines.push(`Status code: ${statusCode.toString()}`); + } + + if (body != null) { + lines.push(`Body: ${toJson(body, undefined, 2)}`); + } + + return lines.join("\n"); +} diff --git a/seed/ts-sdk/basic-auth-optional/src/errors/SeedBasicAuthOptionalTimeoutError.ts b/seed/ts-sdk/basic-auth-optional/src/errors/SeedBasicAuthOptionalTimeoutError.ts new file mode 100644 index 000000000000..7f281bfa9188 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/errors/SeedBasicAuthOptionalTimeoutError.ts @@ -0,0 +1,13 @@ +// This file was auto-generated by Fern from our API Definition. + +export class SeedBasicAuthOptionalTimeoutError extends Error { + constructor(message: string) { + super(message); + Object.setPrototypeOf(this, new.target.prototype); + if (Error.captureStackTrace) { + Error.captureStackTrace(this, this.constructor); + } + + this.name = this.constructor.name; + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/errors/handleNonStatusCodeError.ts b/seed/ts-sdk/basic-auth-optional/src/errors/handleNonStatusCodeError.ts new file mode 100644 index 000000000000..03a0bab2eae5 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/errors/handleNonStatusCodeError.ts @@ -0,0 +1,37 @@ +// This file was auto-generated by Fern from our API Definition. + +import type * as core from "../core/index.js"; +import * as errors from "./index.js"; + +export function handleNonStatusCodeError( + error: core.Fetcher.Error, + rawResponse: core.RawResponse, + method: string, + path: string, +): never { + switch (error.reason) { + case "non-json": + throw new errors.SeedBasicAuthOptionalError({ + statusCode: error.statusCode, + body: error.rawBody, + rawResponse: rawResponse, + }); + case "body-is-null": + throw new errors.SeedBasicAuthOptionalError({ + statusCode: error.statusCode, + rawResponse: rawResponse, + }); + case "timeout": + throw new errors.SeedBasicAuthOptionalTimeoutError(`Timeout exceeded when calling ${method} ${path}.`); + case "unknown": + throw new errors.SeedBasicAuthOptionalError({ + message: error.errorMessage, + rawResponse: rawResponse, + }); + default: + throw new errors.SeedBasicAuthOptionalError({ + message: "Unknown error", + rawResponse: rawResponse, + }); + } +} diff --git a/seed/ts-sdk/basic-auth-optional/src/errors/index.ts b/seed/ts-sdk/basic-auth-optional/src/errors/index.ts new file mode 100644 index 000000000000..18a8cc4155de --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/errors/index.ts @@ -0,0 +1,2 @@ +export { SeedBasicAuthOptionalError } from "./SeedBasicAuthOptionalError.js"; +export { SeedBasicAuthOptionalTimeoutError } from "./SeedBasicAuthOptionalTimeoutError.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/exports.ts b/seed/ts-sdk/basic-auth-optional/src/exports.ts new file mode 100644 index 000000000000..7b70ee14fc02 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/exports.ts @@ -0,0 +1 @@ +export * from "./core/exports.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/index.ts b/seed/ts-sdk/basic-auth-optional/src/index.ts new file mode 100644 index 000000000000..ea3be4d2202b --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/index.ts @@ -0,0 +1,5 @@ +export * as SeedBasicAuthOptional from "./api/index.js"; +export type { BaseClientOptions, BaseRequestOptions } from "./BaseClient.js"; +export { SeedBasicAuthOptionalClient } from "./Client.js"; +export { SeedBasicAuthOptionalError, SeedBasicAuthOptionalTimeoutError } from "./errors/index.js"; +export * from "./exports.js"; diff --git a/seed/ts-sdk/basic-auth-optional/src/version.ts b/seed/ts-sdk/basic-auth-optional/src/version.ts new file mode 100644 index 000000000000..b643a3e3ea27 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/src/version.ts @@ -0,0 +1 @@ +export const SDK_VERSION = "0.0.1"; diff --git a/seed/ts-sdk/basic-auth-optional/tests/custom.test.ts b/seed/ts-sdk/basic-auth-optional/tests/custom.test.ts new file mode 100644 index 000000000000..7f5e031c8396 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/custom.test.ts @@ -0,0 +1,13 @@ +/** + * This is a custom test file, if you wish to add more tests + * to your SDK. + * Be sure to mark this file in `.fernignore`. + * + * If you include example requests/responses in your fern definition, + * you will have tests automatically generated for you. + */ +describe("test", () => { + it("default", () => { + expect(true).toBe(true); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/mock-server/MockServer.ts b/seed/ts-sdk/basic-auth-optional/tests/mock-server/MockServer.ts new file mode 100644 index 000000000000..954872157d52 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/mock-server/MockServer.ts @@ -0,0 +1,29 @@ +import type { RequestHandlerOptions } from "msw"; +import type { SetupServer } from "msw/node"; + +import { mockEndpointBuilder } from "./mockEndpointBuilder"; + +export interface MockServerOptions { + baseUrl: string; + server: SetupServer; +} + +export class MockServer { + private readonly server: SetupServer; + public readonly baseUrl: string; + + constructor({ baseUrl, server }: MockServerOptions) { + this.baseUrl = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl; + this.server = server; + } + + public mockEndpoint(options?: RequestHandlerOptions): ReturnType { + const builder = mockEndpointBuilder({ + once: options?.once ?? true, + onBuild: (handler) => { + this.server.use(handler); + }, + }).baseUrl(this.baseUrl); + return builder; + } +} diff --git a/seed/ts-sdk/basic-auth-optional/tests/mock-server/MockServerPool.ts b/seed/ts-sdk/basic-auth-optional/tests/mock-server/MockServerPool.ts new file mode 100644 index 000000000000..d7d891a2d80b --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/mock-server/MockServerPool.ts @@ -0,0 +1,106 @@ +import { setupServer } from "msw/node"; + +import { fromJson, toJson } from "../../src/core/json"; +import { MockServer } from "./MockServer"; +import { randomBaseUrl } from "./randomBaseUrl"; + +const mswServer = setupServer(); +interface MockServerOptions { + baseUrl?: string; +} + +async function formatHttpRequest(request: Request, id?: string): Promise { + try { + const clone = request.clone(); + const headers = [...clone.headers.entries()].map(([k, v]) => `${k}: ${v}`).join("\n"); + + let body = ""; + try { + const contentType = clone.headers.get("content-type"); + if (contentType?.includes("application/json")) { + body = toJson(fromJson(await clone.text()), undefined, 2); + } else if (clone.body) { + body = await clone.text(); + } + } catch (_e) { + body = "(unable to parse body)"; + } + + const title = id ? `### Request ${id} ###\n` : ""; + const firstLine = `${title}${request.method} ${request.url.toString()} HTTP/1.1`; + + return `\n${firstLine}\n${headers}\n\n${body || "(no body)"}\n`; + } catch (e) { + return `Error formatting request: ${e}`; + } +} + +async function formatHttpResponse(response: Response, id?: string): Promise { + try { + const clone = response.clone(); + const headers = [...clone.headers.entries()].map(([k, v]) => `${k}: ${v}`).join("\n"); + + let body = ""; + try { + const contentType = clone.headers.get("content-type"); + if (contentType?.includes("application/json")) { + body = toJson(fromJson(await clone.text()), undefined, 2); + } else if (clone.body) { + body = await clone.text(); + } + } catch (_e) { + body = "(unable to parse body)"; + } + + const title = id ? `### Response for ${id} ###\n` : ""; + const firstLine = `${title}HTTP/1.1 ${response.status} ${response.statusText}`; + + return `\n${firstLine}\n${headers}\n\n${body || "(no body)"}\n`; + } catch (e) { + return `Error formatting response: ${e}`; + } +} + +class MockServerPool { + private servers: MockServer[] = []; + + public createServer(options?: Partial): MockServer { + const baseUrl = options?.baseUrl || randomBaseUrl(); + const server = new MockServer({ baseUrl, server: mswServer }); + this.servers.push(server); + return server; + } + + public getServers(): MockServer[] { + return [...this.servers]; + } + + public listen(): void { + const onUnhandledRequest = process.env.LOG_LEVEL === "debug" ? "warn" : "bypass"; + mswServer.listen({ onUnhandledRequest }); + + if (process.env.LOG_LEVEL === "debug") { + mswServer.events.on("request:start", async ({ request, requestId }) => { + const formattedRequest = await formatHttpRequest(request, requestId); + console.debug(`request:start\n${formattedRequest}`); + }); + + mswServer.events.on("request:unhandled", async ({ request, requestId }) => { + const formattedRequest = await formatHttpRequest(request, requestId); + console.debug(`request:unhandled\n${formattedRequest}`); + }); + + mswServer.events.on("response:mocked", async ({ request, response, requestId }) => { + const formattedResponse = await formatHttpResponse(response, requestId); + console.debug(`response:mocked\n${formattedResponse}`); + }); + } + } + + public close(): void { + this.servers = []; + mswServer.close(); + } +} + +export const mockServerPool: MockServerPool = new MockServerPool(); diff --git a/seed/ts-sdk/basic-auth-optional/tests/mock-server/mockEndpointBuilder.ts b/seed/ts-sdk/basic-auth-optional/tests/mock-server/mockEndpointBuilder.ts new file mode 100644 index 000000000000..3e8540a3ba5a --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/mock-server/mockEndpointBuilder.ts @@ -0,0 +1,234 @@ +import { type DefaultBodyType, type HttpHandler, HttpResponse, type HttpResponseResolver, http } from "msw"; + +import { url } from "../../src/core"; +import { toJson } from "../../src/core/json"; +import { type WithFormUrlEncodedOptions, withFormUrlEncoded } from "./withFormUrlEncoded"; +import { withHeaders } from "./withHeaders"; +import { type WithJsonOptions, withJson } from "./withJson"; + +type HttpMethod = "all" | "get" | "post" | "put" | "delete" | "patch" | "options" | "head"; + +interface MethodStage { + baseUrl(baseUrl: string): MethodStage; + all(path: string): RequestHeadersStage; + get(path: string): RequestHeadersStage; + post(path: string): RequestHeadersStage; + put(path: string): RequestHeadersStage; + delete(path: string): RequestHeadersStage; + patch(path: string): RequestHeadersStage; + options(path: string): RequestHeadersStage; + head(path: string): RequestHeadersStage; +} + +interface RequestHeadersStage extends RequestBodyStage, ResponseStage { + header(name: string, value: string): RequestHeadersStage; + headers(headers: Record): RequestBodyStage; +} + +interface RequestBodyStage extends ResponseStage { + jsonBody(body: unknown, options?: WithJsonOptions): ResponseStage; + formUrlEncodedBody(body: unknown, options?: WithFormUrlEncodedOptions): ResponseStage; +} + +interface ResponseStage { + respondWith(): ResponseStatusStage; +} +interface ResponseStatusStage { + statusCode(statusCode: number): ResponseHeaderStage; +} + +interface ResponseHeaderStage extends ResponseBodyStage, BuildStage { + header(name: string, value: string): ResponseHeaderStage; + headers(headers: Record): ResponseHeaderStage; +} + +interface ResponseBodyStage { + jsonBody(body: unknown): BuildStage; + sseBody(body: string): BuildStage; +} + +interface BuildStage { + build(): HttpHandler; +} + +export interface HttpHandlerBuilderOptions { + onBuild?: (handler: HttpHandler) => void; + once?: boolean; +} + +class RequestBuilder implements MethodStage, RequestHeadersStage, RequestBodyStage, ResponseStage { + private method: HttpMethod = "get"; + private _baseUrl: string = ""; + private path: string = "/"; + private readonly predicates: ((resolver: HttpResponseResolver) => HttpResponseResolver)[] = []; + private readonly handlerOptions?: HttpHandlerBuilderOptions; + + constructor(options?: HttpHandlerBuilderOptions) { + this.handlerOptions = options; + } + + baseUrl(baseUrl: string): MethodStage { + this._baseUrl = baseUrl; + return this; + } + + all(path: string): RequestHeadersStage { + this.method = "all"; + this.path = path; + return this; + } + + get(path: string): RequestHeadersStage { + this.method = "get"; + this.path = path; + return this; + } + + post(path: string): RequestHeadersStage { + this.method = "post"; + this.path = path; + return this; + } + + put(path: string): RequestHeadersStage { + this.method = "put"; + this.path = path; + return this; + } + + delete(path: string): RequestHeadersStage { + this.method = "delete"; + this.path = path; + return this; + } + + patch(path: string): RequestHeadersStage { + this.method = "patch"; + this.path = path; + return this; + } + + options(path: string): RequestHeadersStage { + this.method = "options"; + this.path = path; + return this; + } + + head(path: string): RequestHeadersStage { + this.method = "head"; + this.path = path; + return this; + } + + header(name: string, value: string): RequestHeadersStage { + this.predicates.push((resolver) => withHeaders({ [name]: value }, resolver)); + return this; + } + + headers(headers: Record): RequestBodyStage { + this.predicates.push((resolver) => withHeaders(headers, resolver)); + return this; + } + + jsonBody(body: unknown, options?: WithJsonOptions): ResponseStage { + if (body === undefined) { + throw new Error("Undefined is not valid JSON. Do not call jsonBody if you want an empty body."); + } + this.predicates.push((resolver) => withJson(body, resolver, options)); + return this; + } + + formUrlEncodedBody(body: unknown, options?: WithFormUrlEncodedOptions): ResponseStage { + if (body === undefined) { + throw new Error( + "Undefined is not valid for form-urlencoded. Do not call formUrlEncodedBody if you want an empty body.", + ); + } + this.predicates.push((resolver) => withFormUrlEncoded(body, resolver, options)); + return this; + } + + respondWith(): ResponseStatusStage { + return new ResponseBuilder(this.method, this.buildUrl(), this.predicates, this.handlerOptions); + } + + private buildUrl(): string { + return url.join(this._baseUrl, this.path); + } +} + +class ResponseBuilder implements ResponseStatusStage, ResponseHeaderStage, ResponseBodyStage, BuildStage { + private readonly method: HttpMethod; + private readonly url: string; + private readonly requestPredicates: ((resolver: HttpResponseResolver) => HttpResponseResolver)[]; + private readonly handlerOptions?: HttpHandlerBuilderOptions; + + private responseStatusCode: number = 200; + private responseHeaders: Record = {}; + private responseBody: DefaultBodyType = undefined; + + constructor( + method: HttpMethod, + url: string, + requestPredicates: ((resolver: HttpResponseResolver) => HttpResponseResolver)[], + options?: HttpHandlerBuilderOptions, + ) { + this.method = method; + this.url = url; + this.requestPredicates = requestPredicates; + this.handlerOptions = options; + } + + public statusCode(code: number): ResponseHeaderStage { + this.responseStatusCode = code; + return this; + } + + public header(name: string, value: string): ResponseHeaderStage { + this.responseHeaders[name] = value; + return this; + } + + public headers(headers: Record): ResponseHeaderStage { + this.responseHeaders = { ...this.responseHeaders, ...headers }; + return this; + } + + public jsonBody(body: unknown): BuildStage { + if (body === undefined) { + throw new Error("Undefined is not valid JSON. Do not call jsonBody if you expect an empty body."); + } + this.responseBody = toJson(body); + return this; + } + + public sseBody(body: string): BuildStage { + this.responseHeaders["Content-Type"] = "text/event-stream"; + this.responseBody = body; + return this; + } + + public build(): HttpHandler { + const responseResolver: HttpResponseResolver = () => { + const response = new HttpResponse(this.responseBody, { + status: this.responseStatusCode, + headers: this.responseHeaders, + }); + // if no Content-Type header is set, delete the default text content type that is set + if (Object.keys(this.responseHeaders).some((key) => key.toLowerCase() === "content-type") === false) { + response.headers.delete("Content-Type"); + } + return response; + }; + + const finalResolver = this.requestPredicates.reduceRight((acc, predicate) => predicate(acc), responseResolver); + + const handler = http[this.method](this.url, finalResolver, this.handlerOptions); + this.handlerOptions?.onBuild?.(handler); + return handler; + } +} + +export function mockEndpointBuilder(options?: HttpHandlerBuilderOptions): MethodStage { + return new RequestBuilder(options); +} diff --git a/seed/ts-sdk/basic-auth-optional/tests/mock-server/randomBaseUrl.ts b/seed/ts-sdk/basic-auth-optional/tests/mock-server/randomBaseUrl.ts new file mode 100644 index 000000000000..031aa6408aca --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/mock-server/randomBaseUrl.ts @@ -0,0 +1,4 @@ +export function randomBaseUrl(): string { + const randomString = Math.random().toString(36).substring(2, 15); + return `http://${randomString}.localhost`; +} diff --git a/seed/ts-sdk/basic-auth-optional/tests/mock-server/setup.ts b/seed/ts-sdk/basic-auth-optional/tests/mock-server/setup.ts new file mode 100644 index 000000000000..aeb3a95af7dc --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/mock-server/setup.ts @@ -0,0 +1,10 @@ +import { afterAll, beforeAll } from "vitest"; + +import { mockServerPool } from "./MockServerPool"; + +beforeAll(() => { + mockServerPool.listen(); +}); +afterAll(() => { + mockServerPool.close(); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/mock-server/withFormUrlEncoded.ts b/seed/ts-sdk/basic-auth-optional/tests/mock-server/withFormUrlEncoded.ts new file mode 100644 index 000000000000..2b23448e3102 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/mock-server/withFormUrlEncoded.ts @@ -0,0 +1,104 @@ +import { type HttpResponseResolver, passthrough } from "msw"; + +import { toJson } from "../../src/core/json"; + +export interface WithFormUrlEncodedOptions { + /** + * List of field names to ignore when comparing request bodies. + * This is useful for pagination cursor fields that change between requests. + */ + ignoredFields?: string[]; +} + +/** + * Creates a request matcher that validates if the request form-urlencoded body exactly matches the expected object + * @param expectedBody - The exact body object to match against + * @param resolver - Response resolver to execute if body matches + * @param options - Optional configuration including fields to ignore + */ +export function withFormUrlEncoded( + expectedBody: unknown, + resolver: HttpResponseResolver, + options?: WithFormUrlEncodedOptions, +): HttpResponseResolver { + const ignoredFields = options?.ignoredFields ?? []; + return async (args) => { + const { request } = args; + + let clonedRequest: Request; + let bodyText: string | undefined; + let actualBody: Record; + try { + clonedRequest = request.clone(); + bodyText = await clonedRequest.text(); + if (bodyText === "") { + // Empty body is valid if expected body is also empty + const isExpectedEmpty = + expectedBody != null && + typeof expectedBody === "object" && + Object.keys(expectedBody as Record).length === 0; + if (!isExpectedEmpty) { + console.error("Request body is empty, expected a form-urlencoded body."); + return passthrough(); + } + actualBody = {}; + } else { + const params = new URLSearchParams(bodyText); + actualBody = {}; + for (const [key, value] of params.entries()) { + actualBody[key] = value; + } + } + } catch (error) { + console.error(`Error processing form-urlencoded request body:\n\tError: ${error}\n\tBody: ${bodyText}`); + return passthrough(); + } + + const mismatches = findMismatches(actualBody, expectedBody); + const filteredMismatches = Object.keys(mismatches).filter((key) => !ignoredFields.includes(key)); + if (filteredMismatches.length > 0) { + console.error("Form-urlencoded body mismatch:", toJson(mismatches, undefined, 2)); + return passthrough(); + } + + return resolver(args); + }; +} + +function findMismatches(actual: any, expected: any): Record { + const mismatches: Record = {}; + + if (typeof actual !== typeof expected) { + return { value: { actual, expected } }; + } + + if (typeof actual !== "object" || actual === null || expected === null) { + if (actual !== expected) { + return { value: { actual, expected } }; + } + return {}; + } + + const actualKeys = Object.keys(actual); + const expectedKeys = Object.keys(expected); + + const allKeys = new Set([...actualKeys, ...expectedKeys]); + + for (const key of allKeys) { + if (!expectedKeys.includes(key)) { + if (actual[key] === undefined) { + continue; + } + mismatches[key] = { actual: actual[key], expected: undefined }; + } else if (!actualKeys.includes(key)) { + if (expected[key] === undefined) { + continue; + } + mismatches[key] = { actual: undefined, expected: expected[key] }; + } else if (actual[key] !== expected[key]) { + mismatches[key] = { actual: actual[key], expected: expected[key] }; + } + } + + return mismatches; +} diff --git a/seed/ts-sdk/basic-auth-optional/tests/mock-server/withHeaders.ts b/seed/ts-sdk/basic-auth-optional/tests/mock-server/withHeaders.ts new file mode 100644 index 000000000000..6599d2b4a92d --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/mock-server/withHeaders.ts @@ -0,0 +1,70 @@ +import { type HttpResponseResolver, passthrough } from "msw"; + +/** + * Creates a request matcher that validates if request headers match specified criteria + * @param expectedHeaders - Headers to match against + * @param resolver - Response resolver to execute if headers match + */ +export function withHeaders( + expectedHeaders: Record boolean)>, + resolver: HttpResponseResolver, +): HttpResponseResolver { + return (args) => { + const { request } = args; + const { headers } = request; + + const mismatches: Record< + string, + { actual: string | null; expected: string | RegExp | ((value: string) => boolean) } + > = {}; + + for (const [key, expectedValue] of Object.entries(expectedHeaders)) { + const actualValue = headers.get(key); + + if (actualValue === null) { + mismatches[key] = { actual: null, expected: expectedValue }; + continue; + } + + if (typeof expectedValue === "function") { + if (!expectedValue(actualValue)) { + mismatches[key] = { actual: actualValue, expected: expectedValue }; + } + } else if (expectedValue instanceof RegExp) { + if (!expectedValue.test(actualValue)) { + mismatches[key] = { actual: actualValue, expected: expectedValue }; + } + } else if (expectedValue !== actualValue) { + mismatches[key] = { actual: actualValue, expected: expectedValue }; + } + } + + if (Object.keys(mismatches).length > 0) { + const formattedMismatches = formatHeaderMismatches(mismatches); + console.error("Header mismatch:", formattedMismatches); + return passthrough(); + } + + return resolver(args); + }; +} + +function formatHeaderMismatches( + mismatches: Record boolean) }>, +): Record { + const formatted: Record = {}; + + for (const [key, { actual, expected }] of Object.entries(mismatches)) { + formatted[key] = { + actual, + expected: + expected instanceof RegExp + ? expected.toString() + : typeof expected === "function" + ? "[Function]" + : expected, + }; + } + + return formatted; +} diff --git a/seed/ts-sdk/basic-auth-optional/tests/mock-server/withJson.ts b/seed/ts-sdk/basic-auth-optional/tests/mock-server/withJson.ts new file mode 100644 index 000000000000..3e8800a0c374 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/mock-server/withJson.ts @@ -0,0 +1,173 @@ +import { type HttpResponseResolver, passthrough } from "msw"; + +import { fromJson, toJson } from "../../src/core/json"; + +export interface WithJsonOptions { + /** + * List of field names to ignore when comparing request bodies. + * This is useful for pagination cursor fields that change between requests. + */ + ignoredFields?: string[]; +} + +/** + * Creates a request matcher that validates if the request JSON body exactly matches the expected object + * @param expectedBody - The exact body object to match against + * @param resolver - Response resolver to execute if body matches + * @param options - Optional configuration including fields to ignore + */ +export function withJson( + expectedBody: unknown, + resolver: HttpResponseResolver, + options?: WithJsonOptions, +): HttpResponseResolver { + const ignoredFields = options?.ignoredFields ?? []; + return async (args) => { + const { request } = args; + + let clonedRequest: Request; + let bodyText: string | undefined; + let actualBody: unknown; + try { + clonedRequest = request.clone(); + bodyText = await clonedRequest.text(); + if (bodyText === "") { + console.error("Request body is empty, expected a JSON object."); + return passthrough(); + } + actualBody = fromJson(bodyText); + } catch (error) { + console.error(`Error processing request body:\n\tError: ${error}\n\tBody: ${bodyText}`); + return passthrough(); + } + + const mismatches = findMismatches(actualBody, expectedBody); + const filteredMismatches = Object.keys(mismatches).filter((key) => !ignoredFields.includes(key)); + if (filteredMismatches.length > 0) { + console.error("JSON body mismatch:", toJson(mismatches, undefined, 2)); + return passthrough(); + } + + return resolver(args); + }; +} + +function findMismatches(actual: any, expected: any): Record { + const mismatches: Record = {}; + + if (typeof actual !== typeof expected) { + if (areEquivalent(actual, expected)) { + return {}; + } + return { value: { actual, expected } }; + } + + if (typeof actual !== "object" || actual === null || expected === null) { + if (actual !== expected) { + if (areEquivalent(actual, expected)) { + return {}; + } + return { value: { actual, expected } }; + } + return {}; + } + + if (Array.isArray(actual) && Array.isArray(expected)) { + if (actual.length !== expected.length) { + return { length: { actual: actual.length, expected: expected.length } }; + } + + const arrayMismatches: Record = {}; + for (let i = 0; i < actual.length; i++) { + const itemMismatches = findMismatches(actual[i], expected[i]); + if (Object.keys(itemMismatches).length > 0) { + for (const [mismatchKey, mismatchValue] of Object.entries(itemMismatches)) { + arrayMismatches[`[${i}]${mismatchKey === "value" ? "" : `.${mismatchKey}`}`] = mismatchValue; + } + } + } + return arrayMismatches; + } + + const actualKeys = Object.keys(actual); + const expectedKeys = Object.keys(expected); + + const allKeys = new Set([...actualKeys, ...expectedKeys]); + + for (const key of allKeys) { + if (!expectedKeys.includes(key)) { + if (actual[key] === undefined) { + continue; // Skip undefined values in actual + } + mismatches[key] = { actual: actual[key], expected: undefined }; + } else if (!actualKeys.includes(key)) { + if (expected[key] === undefined) { + continue; // Skip undefined values in expected + } + mismatches[key] = { actual: undefined, expected: expected[key] }; + } else if ( + typeof actual[key] === "object" && + actual[key] !== null && + typeof expected[key] === "object" && + expected[key] !== null + ) { + const nestedMismatches = findMismatches(actual[key], expected[key]); + if (Object.keys(nestedMismatches).length > 0) { + for (const [nestedKey, nestedValue] of Object.entries(nestedMismatches)) { + mismatches[`${key}${nestedKey === "value" ? "" : `.${nestedKey}`}`] = nestedValue; + } + } + } else if (actual[key] !== expected[key]) { + if (areEquivalent(actual[key], expected[key])) { + continue; + } + mismatches[key] = { actual: actual[key], expected: expected[key] }; + } + } + + return mismatches; +} + +function areEquivalent(actual: unknown, expected: unknown): boolean { + if (actual === expected) { + return true; + } + if (isEquivalentBigInt(actual, expected)) { + return true; + } + if (isEquivalentDatetime(actual, expected)) { + return true; + } + return false; +} + +function isEquivalentBigInt(actual: unknown, expected: unknown) { + if (typeof actual === "number") { + actual = BigInt(actual); + } + if (typeof expected === "number") { + expected = BigInt(expected); + } + if (typeof actual === "bigint" && typeof expected === "bigint") { + return actual === expected; + } + return false; +} + +function isEquivalentDatetime(str1: unknown, str2: unknown): boolean { + if (typeof str1 !== "string" || typeof str2 !== "string") { + return false; + } + const isoDatePattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/; + if (!isoDatePattern.test(str1) || !isoDatePattern.test(str2)) { + return false; + } + + try { + const date1 = new Date(str1).getTime(); + const date2 = new Date(str2).getTime(); + return date1 === date2; + } catch { + return false; + } +} diff --git a/seed/ts-sdk/basic-auth-optional/tests/setup.ts b/seed/ts-sdk/basic-auth-optional/tests/setup.ts new file mode 100644 index 000000000000..a5651f81ba10 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/setup.ts @@ -0,0 +1,80 @@ +import { expect } from "vitest"; + +interface CustomMatchers { + toContainHeaders(expectedHeaders: Record): R; +} + +declare module "vitest" { + interface Assertion extends CustomMatchers {} + interface AsymmetricMatchersContaining extends CustomMatchers {} +} + +expect.extend({ + toContainHeaders(actual: unknown, expectedHeaders: Record) { + const isHeaders = actual instanceof Headers; + const isPlainObject = typeof actual === "object" && actual !== null && !Array.isArray(actual); + + if (!isHeaders && !isPlainObject) { + throw new TypeError("Received value must be an instance of Headers or a plain object!"); + } + + if (typeof expectedHeaders !== "object" || expectedHeaders === null || Array.isArray(expectedHeaders)) { + throw new TypeError("Expected headers must be a plain object!"); + } + + const missingHeaders: string[] = []; + const mismatchedHeaders: Array<{ key: string; expected: string; actual: string | null }> = []; + + for (const [key, value] of Object.entries(expectedHeaders)) { + let actualValue: string | null = null; + + if (isHeaders) { + // Headers.get() is already case-insensitive + actualValue = (actual as Headers).get(key); + } else { + // For plain objects, do case-insensitive lookup + const actualObj = actual as Record; + const lowerKey = key.toLowerCase(); + const foundKey = Object.keys(actualObj).find((k) => k.toLowerCase() === lowerKey); + actualValue = foundKey ? actualObj[foundKey] : null; + } + + if (actualValue === null || actualValue === undefined) { + missingHeaders.push(key); + } else if (actualValue !== value) { + mismatchedHeaders.push({ key, expected: value, actual: actualValue }); + } + } + + const pass = missingHeaders.length === 0 && mismatchedHeaders.length === 0; + + const actualType = isHeaders ? "Headers" : "object"; + + if (pass) { + return { + message: () => `expected ${actualType} not to contain ${this.utils.printExpected(expectedHeaders)}`, + pass: true, + }; + } else { + const messages: string[] = []; + + if (missingHeaders.length > 0) { + messages.push(`Missing headers: ${this.utils.printExpected(missingHeaders.join(", "))}`); + } + + if (mismatchedHeaders.length > 0) { + const mismatches = mismatchedHeaders.map( + ({ key, expected, actual }) => + `${key}: expected ${this.utils.printExpected(expected)} but got ${this.utils.printReceived(actual)}`, + ); + messages.push(mismatches.join("\n")); + } + + return { + message: () => + `expected ${actualType} to contain ${this.utils.printExpected(expectedHeaders)}\n\n${messages.join("\n")}`, + pass: false, + }; + } + }, +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/tsconfig.json b/seed/ts-sdk/basic-auth-optional/tests/tsconfig.json new file mode 100644 index 000000000000..ac39744de7b2 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../tsconfig.base.json", + "compilerOptions": { + "outDir": null, + "rootDir": "..", + "types": ["vitest/globals"] + }, + "include": ["../src", "../tests"], + "exclude": [] +} diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/auth/BasicAuth.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/auth/BasicAuth.test.ts new file mode 100644 index 000000000000..8c82c1b723db --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/auth/BasicAuth.test.ts @@ -0,0 +1,112 @@ +import { BasicAuth } from "../../../src/core/auth/BasicAuth"; + +describe("BasicAuth", () => { + interface ToHeaderTestCase { + description: string; + input: { username?: string; password?: string }; + expected: string | undefined; + } + + interface FromHeaderTestCase { + description: string; + input: string; + expected: { username: string; password: string }; + } + + interface ErrorTestCase { + description: string; + input: string; + expectedError: string; + } + + describe("toAuthorizationHeader", () => { + const toHeaderTests: ToHeaderTestCase[] = [ + { + description: "correctly converts to header with both username and password", + input: { username: "username", password: "password" }, + expected: "Basic dXNlcm5hbWU6cGFzc3dvcmQ=", + }, + { + description: "encodes username only with trailing colon", + input: { username: "username" }, + expected: "Basic dXNlcm5hbWU6", + }, + { + description: "encodes password only with leading colon", + input: { password: "password" }, + expected: "Basic OnBhc3N3b3Jk", + }, + { + description: "returns undefined when neither provided", + input: {}, + expected: undefined, + }, + { + description: "returns undefined when both are empty strings", + input: { username: "", password: "" }, + expected: undefined, + }, + ]; + + toHeaderTests.forEach(({ description, input, expected }) => { + it(description, () => { + expect(BasicAuth.toAuthorizationHeader(input)).toBe(expected); + }); + }); + }); + + describe("fromAuthorizationHeader", () => { + const fromHeaderTests: FromHeaderTestCase[] = [ + { + description: "correctly parses header", + input: "Basic dXNlcm5hbWU6cGFzc3dvcmQ=", + expected: { username: "username", password: "password" }, + }, + { + description: "handles password with colons", + input: "Basic dXNlcjpwYXNzOndvcmQ=", + expected: { username: "user", password: "pass:word" }, + }, + { + description: "handles empty username and password (just colon)", + input: "Basic Og==", + expected: { username: "", password: "" }, + }, + { + description: "handles empty username", + input: "Basic OnBhc3N3b3Jk", + expected: { username: "", password: "password" }, + }, + { + description: "handles empty password", + input: "Basic dXNlcm5hbWU6", + expected: { username: "username", password: "" }, + }, + ]; + + fromHeaderTests.forEach(({ description, input, expected }) => { + it(description, () => { + expect(BasicAuth.fromAuthorizationHeader(input)).toEqual(expected); + }); + }); + + const errorTests: ErrorTestCase[] = [ + { + description: "throws error for completely empty credentials", + input: "Basic ", + expectedError: "Invalid basic auth", + }, + { + description: "throws error for credentials without colon", + input: "Basic dXNlcm5hbWU=", + expectedError: "Invalid basic auth", + }, + ]; + + errorTests.forEach(({ description, input, expectedError }) => { + it(description, () => { + expect(() => BasicAuth.fromAuthorizationHeader(input)).toThrow(expectedError); + }); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/auth/BearerToken.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/auth/BearerToken.test.ts new file mode 100644 index 000000000000..7757b87cb97e --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/auth/BearerToken.test.ts @@ -0,0 +1,14 @@ +import { BearerToken } from "../../../src/core/auth/BearerToken"; + +describe("BearerToken", () => { + describe("toAuthorizationHeader", () => { + it("correctly converts to header", () => { + expect(BearerToken.toAuthorizationHeader("my-token")).toBe("Bearer my-token"); + }); + }); + describe("fromAuthorizationHeader", () => { + it("correctly parses header", () => { + expect(BearerToken.fromAuthorizationHeader("Bearer my-token")).toBe("my-token"); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/base64.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/base64.test.ts new file mode 100644 index 000000000000..939594ca277b --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/base64.test.ts @@ -0,0 +1,53 @@ +import { base64Decode, base64Encode } from "../../src/core/base64"; + +describe("base64", () => { + describe("base64Encode", () => { + it("should encode ASCII strings", () => { + expect(base64Encode("hello")).toBe("aGVsbG8="); + expect(base64Encode("")).toBe(""); + }); + + it("should encode UTF-8 strings", () => { + expect(base64Encode("café")).toBe("Y2Fmw6k="); + expect(base64Encode("🎉")).toBe("8J+OiQ=="); + }); + + it("should handle basic auth credentials", () => { + expect(base64Encode("username:password")).toBe("dXNlcm5hbWU6cGFzc3dvcmQ="); + }); + }); + + describe("base64Decode", () => { + it("should decode ASCII strings", () => { + expect(base64Decode("aGVsbG8=")).toBe("hello"); + expect(base64Decode("")).toBe(""); + }); + + it("should decode UTF-8 strings", () => { + expect(base64Decode("Y2Fmw6k=")).toBe("café"); + expect(base64Decode("8J+OiQ==")).toBe("🎉"); + }); + + it("should handle basic auth credentials", () => { + expect(base64Decode("dXNlcm5hbWU6cGFzc3dvcmQ=")).toBe("username:password"); + }); + }); + + describe("round-trip encoding", () => { + const testStrings = [ + "hello world", + "test@example.com", + "café", + "username:password", + "user@domain.com:super$ecret123!", + ]; + + testStrings.forEach((testString) => { + it(`should round-trip encode/decode: "${testString}"`, () => { + const encoded = base64Encode(testString); + const decoded = base64Decode(encoded); + expect(decoded).toBe(testString); + }); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/Fetcher.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/Fetcher.test.ts new file mode 100644 index 000000000000..6c17624228bb --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/Fetcher.test.ts @@ -0,0 +1,262 @@ +import fs from "fs"; +import { join } from "path"; +import stream from "stream"; +import type { BinaryResponse } from "../../../src/core"; +import { type Fetcher, fetcherImpl } from "../../../src/core/fetcher/Fetcher"; + +describe("Test fetcherImpl", () => { + it("should handle successful request", async () => { + const mockArgs: Fetcher.Args = { + url: "https://httpbin.org/post", + method: "POST", + headers: { "X-Test": "x-test-header" }, + body: { data: "test" }, + contentType: "application/json", + requestType: "json", + maxRetries: 0, + responseType: "json", + }; + + global.fetch = vi.fn().mockResolvedValue( + new Response(JSON.stringify({ data: "test" }), { + status: 200, + statusText: "OK", + }), + ); + + const result = await fetcherImpl(mockArgs); + expect(result.ok).toBe(true); + if (result.ok) { + expect(result.body).toEqual({ data: "test" }); + } + + expect(global.fetch).toHaveBeenCalledWith( + "https://httpbin.org/post", + expect.objectContaining({ + method: "POST", + headers: expect.toContainHeaders({ "X-Test": "x-test-header" }), + body: JSON.stringify({ data: "test" }), + }), + ); + }); + + it("should send octet stream", async () => { + const url = "https://httpbin.org/post/file"; + const mockArgs: Fetcher.Args = { + url, + method: "POST", + headers: { "X-Test": "x-test-header" }, + contentType: "application/octet-stream", + requestType: "bytes", + maxRetries: 0, + responseType: "json", + body: fs.createReadStream(join(__dirname, "test-file.txt")), + }; + + global.fetch = vi.fn().mockResolvedValue( + new Response(JSON.stringify({ data: "test" }), { + status: 200, + statusText: "OK", + }), + ); + + const result = await fetcherImpl(mockArgs); + + expect(global.fetch).toHaveBeenCalledWith( + url, + expect.objectContaining({ + method: "POST", + headers: expect.toContainHeaders({ "X-Test": "x-test-header" }), + body: expect.any(fs.ReadStream), + }), + ); + expect(result.ok).toBe(true); + if (result.ok) { + expect(result.body).toEqual({ data: "test" }); + } + }); + + it("should receive file as stream", async () => { + const url = "https://httpbin.org/post/file"; + const mockArgs: Fetcher.Args = { + url, + method: "GET", + headers: { "X-Test": "x-test-header" }, + maxRetries: 0, + responseType: "binary-response", + }; + + global.fetch = vi.fn().mockResolvedValue( + new Response( + stream.Readable.toWeb(fs.createReadStream(join(__dirname, "test-file.txt"))) as ReadableStream, + { + status: 200, + statusText: "OK", + }, + ), + ); + + const result = await fetcherImpl(mockArgs); + + expect(global.fetch).toHaveBeenCalledWith( + url, + expect.objectContaining({ + method: "GET", + headers: expect.toContainHeaders({ "X-Test": "x-test-header" }), + }), + ); + expect(result.ok).toBe(true); + if (result.ok) { + const body = result.body as BinaryResponse; + expect(body).toBeDefined(); + expect(body.bodyUsed).toBe(false); + expect(typeof body.stream).toBe("function"); + const stream = body.stream(); + expect(stream).toBeInstanceOf(ReadableStream); + const readableStream = stream as ReadableStream; + const reader = readableStream.getReader(); + const { value } = await reader.read(); + const decoder = new TextDecoder(); + const streamContent = decoder.decode(value); + expect(streamContent.trim()).toBe("This is a test file!"); + expect(body.bodyUsed).toBe(true); + } + }); + + it("should receive file as blob", async () => { + const url = "https://httpbin.org/post/file"; + const mockArgs: Fetcher.Args = { + url, + method: "GET", + headers: { "X-Test": "x-test-header" }, + maxRetries: 0, + responseType: "binary-response", + }; + + global.fetch = vi.fn().mockResolvedValue( + new Response( + stream.Readable.toWeb(fs.createReadStream(join(__dirname, "test-file.txt"))) as ReadableStream, + { + status: 200, + statusText: "OK", + }, + ), + ); + + const result = await fetcherImpl(mockArgs); + + expect(global.fetch).toHaveBeenCalledWith( + url, + expect.objectContaining({ + method: "GET", + headers: expect.toContainHeaders({ "X-Test": "x-test-header" }), + }), + ); + expect(result.ok).toBe(true); + if (result.ok) { + const body = result.body as BinaryResponse; + expect(body).toBeDefined(); + expect(body.bodyUsed).toBe(false); + expect(typeof body.blob).toBe("function"); + const blob = await body.blob(); + expect(blob).toBeInstanceOf(Blob); + const reader = blob.stream().getReader(); + const { value } = await reader.read(); + const decoder = new TextDecoder(); + const streamContent = decoder.decode(value); + expect(streamContent.trim()).toBe("This is a test file!"); + expect(body.bodyUsed).toBe(true); + } + }); + + it("should receive file as arraybuffer", async () => { + const url = "https://httpbin.org/post/file"; + const mockArgs: Fetcher.Args = { + url, + method: "GET", + headers: { "X-Test": "x-test-header" }, + maxRetries: 0, + responseType: "binary-response", + }; + + global.fetch = vi.fn().mockResolvedValue( + new Response( + stream.Readable.toWeb(fs.createReadStream(join(__dirname, "test-file.txt"))) as ReadableStream, + { + status: 200, + statusText: "OK", + }, + ), + ); + + const result = await fetcherImpl(mockArgs); + + expect(global.fetch).toHaveBeenCalledWith( + url, + expect.objectContaining({ + method: "GET", + headers: expect.toContainHeaders({ "X-Test": "x-test-header" }), + }), + ); + expect(result.ok).toBe(true); + if (result.ok) { + const body = result.body as BinaryResponse; + expect(body).toBeDefined(); + expect(body.bodyUsed).toBe(false); + expect(typeof body.arrayBuffer).toBe("function"); + const arrayBuffer = await body.arrayBuffer(); + expect(arrayBuffer).toBeInstanceOf(ArrayBuffer); + const decoder = new TextDecoder(); + const streamContent = decoder.decode(new Uint8Array(arrayBuffer)); + expect(streamContent.trim()).toBe("This is a test file!"); + expect(body.bodyUsed).toBe(true); + } + }); + + it("should receive file as bytes", async () => { + const url = "https://httpbin.org/post/file"; + const mockArgs: Fetcher.Args = { + url, + method: "GET", + headers: { "X-Test": "x-test-header" }, + maxRetries: 0, + responseType: "binary-response", + }; + + global.fetch = vi.fn().mockResolvedValue( + new Response( + stream.Readable.toWeb(fs.createReadStream(join(__dirname, "test-file.txt"))) as ReadableStream, + { + status: 200, + statusText: "OK", + }, + ), + ); + + const result = await fetcherImpl(mockArgs); + + expect(global.fetch).toHaveBeenCalledWith( + url, + expect.objectContaining({ + method: "GET", + headers: expect.toContainHeaders({ "X-Test": "x-test-header" }), + }), + ); + expect(result.ok).toBe(true); + if (result.ok) { + const body = result.body as BinaryResponse; + expect(body).toBeDefined(); + expect(body.bodyUsed).toBe(false); + expect(typeof body.bytes).toBe("function"); + if (!body.bytes) { + return; + } + const bytes = await body.bytes(); + expect(bytes).toBeInstanceOf(Uint8Array); + const decoder = new TextDecoder(); + const streamContent = decoder.decode(bytes); + expect(streamContent.trim()).toBe("This is a test file!"); + expect(body.bodyUsed).toBe(true); + } + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/HttpResponsePromise.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/HttpResponsePromise.test.ts new file mode 100644 index 000000000000..2ec008e581d8 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/HttpResponsePromise.test.ts @@ -0,0 +1,143 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; + +import { HttpResponsePromise } from "../../../src/core/fetcher/HttpResponsePromise"; +import type { RawResponse, WithRawResponse } from "../../../src/core/fetcher/RawResponse"; + +describe("HttpResponsePromise", () => { + const mockRawResponse: RawResponse = { + headers: new Headers(), + redirected: false, + status: 200, + statusText: "OK", + type: "basic" as ResponseType, + url: "https://example.com", + }; + const mockData = { id: "123", name: "test" }; + const mockWithRawResponse: WithRawResponse = { + data: mockData, + rawResponse: mockRawResponse, + }; + + describe("fromFunction", () => { + it("should create an HttpResponsePromise from a function", async () => { + const mockFn = vi + .fn<(arg1: string, arg2: string) => Promise>>() + .mockResolvedValue(mockWithRawResponse); + + const responsePromise = HttpResponsePromise.fromFunction(mockFn, "arg1", "arg2"); + + const result = await responsePromise; + expect(result).toEqual(mockData); + expect(mockFn).toHaveBeenCalledWith("arg1", "arg2"); + + const resultWithRawResponse = await responsePromise.withRawResponse(); + expect(resultWithRawResponse).toEqual({ + data: mockData, + rawResponse: mockRawResponse, + }); + }); + }); + + describe("fromPromise", () => { + it("should create an HttpResponsePromise from a promise", async () => { + const promise = Promise.resolve(mockWithRawResponse); + + const responsePromise = HttpResponsePromise.fromPromise(promise); + + const result = await responsePromise; + expect(result).toEqual(mockData); + + const resultWithRawResponse = await responsePromise.withRawResponse(); + expect(resultWithRawResponse).toEqual({ + data: mockData, + rawResponse: mockRawResponse, + }); + }); + }); + + describe("fromExecutor", () => { + it("should create an HttpResponsePromise from an executor function", async () => { + const responsePromise = HttpResponsePromise.fromExecutor((resolve) => { + resolve(mockWithRawResponse); + }); + + const result = await responsePromise; + expect(result).toEqual(mockData); + + const resultWithRawResponse = await responsePromise.withRawResponse(); + expect(resultWithRawResponse).toEqual({ + data: mockData, + rawResponse: mockRawResponse, + }); + }); + }); + + describe("fromResult", () => { + it("should create an HttpResponsePromise from a result", async () => { + const responsePromise = HttpResponsePromise.fromResult(mockWithRawResponse); + + const result = await responsePromise; + expect(result).toEqual(mockData); + + const resultWithRawResponse = await responsePromise.withRawResponse(); + expect(resultWithRawResponse).toEqual({ + data: mockData, + rawResponse: mockRawResponse, + }); + }); + }); + + describe("Promise methods", () => { + let responsePromise: HttpResponsePromise; + + beforeEach(() => { + responsePromise = HttpResponsePromise.fromResult(mockWithRawResponse); + }); + + it("should support then() method", async () => { + const result = await responsePromise.then((data) => ({ + ...data, + modified: true, + })); + + expect(result).toEqual({ + ...mockData, + modified: true, + }); + }); + + it("should support catch() method", async () => { + const errorResponsePromise = HttpResponsePromise.fromExecutor((_, reject) => { + reject(new Error("Test error")); + }); + + const catchSpy = vi.fn(); + await errorResponsePromise.catch(catchSpy); + + expect(catchSpy).toHaveBeenCalled(); + const error = catchSpy.mock.calls[0]?.[0]; + expect(error).toBeInstanceOf(Error); + expect((error as Error).message).toBe("Test error"); + }); + + it("should support finally() method", async () => { + const finallySpy = vi.fn(); + await responsePromise.finally(finallySpy); + + expect(finallySpy).toHaveBeenCalled(); + }); + }); + + describe("withRawResponse", () => { + it("should return both data and raw response", async () => { + const responsePromise = HttpResponsePromise.fromResult(mockWithRawResponse); + + const result = await responsePromise.withRawResponse(); + + expect(result).toEqual({ + data: mockData, + rawResponse: mockRawResponse, + }); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/RawResponse.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/RawResponse.test.ts new file mode 100644 index 000000000000..375ee3f38064 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/RawResponse.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from "vitest"; + +import { toRawResponse } from "../../../src/core/fetcher/RawResponse"; + +describe("RawResponse", () => { + describe("toRawResponse", () => { + it("should convert Response to RawResponse by removing body, bodyUsed, and ok properties", () => { + const mockHeaders = new Headers({ "content-type": "application/json" }); + const mockResponse = { + body: "test body", + bodyUsed: false, + ok: true, + headers: mockHeaders, + redirected: false, + status: 200, + statusText: "OK", + type: "basic" as ResponseType, + url: "https://example.com", + }; + + const result = toRawResponse(mockResponse as unknown as Response); + + expect("body" in result).toBe(false); + expect("bodyUsed" in result).toBe(false); + expect("ok" in result).toBe(false); + expect(result.headers).toBe(mockHeaders); + expect(result.redirected).toBe(false); + expect(result.status).toBe(200); + expect(result.statusText).toBe("OK"); + expect(result.type).toBe("basic"); + expect(result.url).toBe("https://example.com"); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/createRequestUrl.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/createRequestUrl.test.ts new file mode 100644 index 000000000000..a92f1b5e81d1 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/createRequestUrl.test.ts @@ -0,0 +1,163 @@ +import { createRequestUrl } from "../../../src/core/fetcher/createRequestUrl"; + +describe("Test createRequestUrl", () => { + const BASE_URL = "https://api.example.com"; + + interface TestCase { + description: string; + baseUrl: string; + queryParams?: Record; + expected: string; + } + + const testCases: TestCase[] = [ + { + description: "should return the base URL when no query parameters are provided", + baseUrl: BASE_URL, + expected: BASE_URL, + }, + { + description: "should append simple query parameters", + baseUrl: BASE_URL, + queryParams: { key: "value", another: "param" }, + expected: "https://api.example.com?key=value&another=param", + }, + { + description: "should handle array query parameters", + baseUrl: BASE_URL, + queryParams: { items: ["a", "b", "c"] }, + expected: "https://api.example.com?items=a&items=b&items=c", + }, + { + description: "should handle object query parameters", + baseUrl: BASE_URL, + queryParams: { filter: { name: "John", age: 30 } }, + expected: "https://api.example.com?filter%5Bname%5D=John&filter%5Bage%5D=30", + }, + { + description: "should handle mixed types of query parameters", + baseUrl: BASE_URL, + queryParams: { + simple: "value", + array: ["x", "y"], + object: { key: "value" }, + }, + expected: "https://api.example.com?simple=value&array=x&array=y&object%5Bkey%5D=value", + }, + { + description: "should handle empty query parameters object", + baseUrl: BASE_URL, + queryParams: {}, + expected: BASE_URL, + }, + { + description: "should encode special characters in query parameters", + baseUrl: BASE_URL, + queryParams: { special: "a&b=c d" }, + expected: "https://api.example.com?special=a%26b%3Dc%20d", + }, + { + description: "should handle numeric values", + baseUrl: BASE_URL, + queryParams: { count: 42, price: 19.99, active: 1, inactive: 0 }, + expected: "https://api.example.com?count=42&price=19.99&active=1&inactive=0", + }, + { + description: "should handle boolean values", + baseUrl: BASE_URL, + queryParams: { enabled: true, disabled: false }, + expected: "https://api.example.com?enabled=true&disabled=false", + }, + { + description: "should handle null and undefined values", + baseUrl: BASE_URL, + queryParams: { + valid: "value", + nullValue: null, + undefinedValue: undefined, + emptyString: "", + }, + expected: "https://api.example.com?valid=value&nullValue=&emptyString=", + }, + { + description: "should handle deeply nested objects", + baseUrl: BASE_URL, + queryParams: { + user: { + profile: { + name: "John", + settings: { theme: "dark" }, + }, + }, + }, + expected: + "https://api.example.com?user%5Bprofile%5D%5Bname%5D=John&user%5Bprofile%5D%5Bsettings%5D%5Btheme%5D=dark", + }, + { + description: "should handle arrays of objects", + baseUrl: BASE_URL, + queryParams: { + users: [ + { name: "John", age: 30 }, + { name: "Jane", age: 25 }, + ], + }, + expected: + "https://api.example.com?users%5Bname%5D=John&users%5Bage%5D=30&users%5Bname%5D=Jane&users%5Bage%5D=25", + }, + { + description: "should handle mixed arrays", + baseUrl: BASE_URL, + queryParams: { + mixed: ["string", 42, true, { key: "value" }], + }, + expected: "https://api.example.com?mixed=string&mixed=42&mixed=true&mixed%5Bkey%5D=value", + }, + { + description: "should handle empty arrays", + baseUrl: BASE_URL, + queryParams: { emptyArray: [] }, + expected: BASE_URL, + }, + { + description: "should handle empty objects", + baseUrl: BASE_URL, + queryParams: { emptyObject: {} }, + expected: BASE_URL, + }, + { + description: "should handle special characters in keys", + baseUrl: BASE_URL, + queryParams: { "key with spaces": "value", "key[with]brackets": "value" }, + expected: "https://api.example.com?key%20with%20spaces=value&key%5Bwith%5Dbrackets=value", + }, + { + description: "should handle URL with existing query parameters", + baseUrl: "https://api.example.com?existing=param", + queryParams: { new: "value" }, + expected: "https://api.example.com?existing=param?new=value", + }, + { + description: "should handle complex nested structures", + baseUrl: BASE_URL, + queryParams: { + filters: { + status: ["active", "pending"], + category: { + type: "electronics", + subcategories: ["phones", "laptops"], + }, + }, + sort: { field: "name", direction: "asc" }, + }, + expected: + "https://api.example.com?filters%5Bstatus%5D=active&filters%5Bstatus%5D=pending&filters%5Bcategory%5D%5Btype%5D=electronics&filters%5Bcategory%5D%5Bsubcategories%5D=phones&filters%5Bcategory%5D%5Bsubcategories%5D=laptops&sort%5Bfield%5D=name&sort%5Bdirection%5D=asc", + }, + ]; + + testCases.forEach(({ description, baseUrl, queryParams, expected }) => { + it(description, () => { + expect(createRequestUrl(baseUrl, queryParams)).toBe(expected); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/getRequestBody.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/getRequestBody.test.ts new file mode 100644 index 000000000000..8a6c3a57e211 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/getRequestBody.test.ts @@ -0,0 +1,129 @@ +import { getRequestBody } from "../../../src/core/fetcher/getRequestBody"; +import { RUNTIME } from "../../../src/core/runtime"; + +describe("Test getRequestBody", () => { + interface TestCase { + description: string; + input: any; + type: "json" | "form" | "file" | "bytes" | "other"; + expected: any; + skipCondition?: () => boolean; + } + + const testCases: TestCase[] = [ + { + description: "should stringify body if not FormData in Node environment", + input: { key: "value" }, + type: "json", + expected: '{"key":"value"}', + skipCondition: () => RUNTIME.type !== "node", + }, + { + description: "should stringify body if not FormData in browser environment", + input: { key: "value" }, + type: "json", + expected: '{"key":"value"}', + skipCondition: () => RUNTIME.type !== "browser", + }, + { + description: "should return the Uint8Array", + input: new Uint8Array([1, 2, 3]), + type: "bytes", + expected: new Uint8Array([1, 2, 3]), + }, + { + description: "should serialize objects for form-urlencoded content type", + input: { username: "johndoe", email: "john@example.com" }, + type: "form", + expected: "username=johndoe&email=john%40example.com", + }, + { + description: "should serialize complex nested objects and arrays for form-urlencoded content type", + input: { + user: { + profile: { + name: "John Doe", + settings: { + theme: "dark", + notifications: true, + }, + }, + tags: ["admin", "user"], + contacts: [ + { type: "email", value: "john@example.com" }, + { type: "phone", value: "+1234567890" }, + ], + }, + filters: { + status: ["active", "pending"], + metadata: { + created: "2024-01-01", + categories: ["electronics", "books"], + }, + }, + preferences: ["notifications", "updates"], + }, + type: "form", + expected: + "user%5Bprofile%5D%5Bname%5D=John%20Doe&" + + "user%5Bprofile%5D%5Bsettings%5D%5Btheme%5D=dark&" + + "user%5Bprofile%5D%5Bsettings%5D%5Bnotifications%5D=true&" + + "user%5Btags%5D=admin&" + + "user%5Btags%5D=user&" + + "user%5Bcontacts%5D%5Btype%5D=email&" + + "user%5Bcontacts%5D%5Bvalue%5D=john%40example.com&" + + "user%5Bcontacts%5D%5Btype%5D=phone&" + + "user%5Bcontacts%5D%5Bvalue%5D=%2B1234567890&" + + "filters%5Bstatus%5D=active&" + + "filters%5Bstatus%5D=pending&" + + "filters%5Bmetadata%5D%5Bcreated%5D=2024-01-01&" + + "filters%5Bmetadata%5D%5Bcategories%5D=electronics&" + + "filters%5Bmetadata%5D%5Bcategories%5D=books&" + + "preferences=notifications&" + + "preferences=updates", + }, + { + description: "should return the input for pre-serialized form-urlencoded strings", + input: "key=value&another=param", + type: "other", + expected: "key=value&another=param", + }, + { + description: "should JSON stringify objects", + input: { key: "value" }, + type: "json", + expected: '{"key":"value"}', + }, + ]; + + testCases.forEach(({ description, input, type, expected, skipCondition }) => { + it(description, async () => { + if (skipCondition?.()) { + return; + } + + const result = await getRequestBody({ + body: input, + type, + }); + + if (input instanceof Uint8Array) { + expect(result).toBe(input); + } else { + expect(result).toBe(expected); + } + }); + }); + + it("should return FormData in browser environment", async () => { + if (RUNTIME.type === "browser") { + const formData = new FormData(); + formData.append("key", "value"); + const result = await getRequestBody({ + body: formData, + type: "file", + }); + expect(result).toBe(formData); + } + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/getResponseBody.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/getResponseBody.test.ts new file mode 100644 index 000000000000..ad6be7fc2c9b --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/getResponseBody.test.ts @@ -0,0 +1,97 @@ +import { getResponseBody } from "../../../src/core/fetcher/getResponseBody"; + +import { RUNTIME } from "../../../src/core/runtime"; + +describe("Test getResponseBody", () => { + interface SimpleTestCase { + description: string; + responseData: string | Record; + responseType?: "blob" | "sse" | "streaming" | "text"; + expected: any; + skipCondition?: () => boolean; + } + + const simpleTestCases: SimpleTestCase[] = [ + { + description: "should handle text response type", + responseData: "test text", + responseType: "text", + expected: "test text", + }, + { + description: "should handle JSON response", + responseData: { key: "value" }, + expected: { key: "value" }, + }, + { + description: "should handle empty response", + responseData: "", + expected: undefined, + }, + { + description: "should handle non-JSON response", + responseData: "invalid json", + expected: { + ok: false, + error: { + reason: "non-json", + statusCode: 200, + rawBody: "invalid json", + }, + }, + }, + ]; + + simpleTestCases.forEach(({ description, responseData, responseType, expected, skipCondition }) => { + it(description, async () => { + if (skipCondition?.()) { + return; + } + + const mockResponse = new Response( + typeof responseData === "string" ? responseData : JSON.stringify(responseData), + ); + const result = await getResponseBody(mockResponse, responseType); + expect(result).toEqual(expected); + }); + }); + + it("should handle blob response type", async () => { + const mockBlob = new Blob(["test"], { type: "text/plain" }); + const mockResponse = new Response(mockBlob); + const result = await getResponseBody(mockResponse, "blob"); + // @ts-expect-error + expect(result.constructor.name).toBe("Blob"); + }); + + it("should handle sse response type", async () => { + if (RUNTIME.type === "node") { + const mockStream = new ReadableStream(); + const mockResponse = new Response(mockStream); + const result = await getResponseBody(mockResponse, "sse"); + expect(result).toBe(mockStream); + } + }); + + it("should handle streaming response type", async () => { + const encoder = new TextEncoder(); + const testData = "test stream data"; + const mockStream = new ReadableStream({ + start(controller) { + controller.enqueue(encoder.encode(testData)); + controller.close(); + }, + }); + + const mockResponse = new Response(mockStream); + const result = (await getResponseBody(mockResponse, "streaming")) as ReadableStream; + + expect(result).toBeInstanceOf(ReadableStream); + + const reader = result.getReader(); + const decoder = new TextDecoder(); + const { value } = await reader.read(); + const streamContent = decoder.decode(value); + expect(streamContent).toBe(testData); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/logging.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/logging.test.ts new file mode 100644 index 000000000000..366c9b6ced61 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/logging.test.ts @@ -0,0 +1,517 @@ +import { fetcherImpl } from "../../../src/core/fetcher/Fetcher"; + +function createMockLogger() { + return { + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }; +} + +function mockSuccessResponse(data: unknown = { data: "test" }, status = 200, statusText = "OK") { + global.fetch = vi.fn().mockResolvedValue( + new Response(JSON.stringify(data), { + status, + statusText, + }), + ); +} + +function mockErrorResponse(data: unknown = { error: "Error" }, status = 404, statusText = "Not Found") { + global.fetch = vi.fn().mockResolvedValue( + new Response(JSON.stringify(data), { + status, + statusText, + }), + ); +} + +describe("Fetcher Logging Integration", () => { + describe("Request Logging", () => { + it("should log successful request at debug level", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "POST", + headers: { "Content-Type": "application/json" }, + body: { test: "data" }, + contentType: "application/json", + requestType: "json", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + method: "POST", + url: "https://example.com/api", + headers: expect.toContainHeaders({ + "Content-Type": "application/json", + }), + hasBody: true, + }), + ); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "HTTP request succeeded", + expect.objectContaining({ + method: "POST", + url: "https://example.com/api", + statusCode: 200, + }), + ); + }); + + it("should not log debug messages at info level for successful requests", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "info", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).not.toHaveBeenCalled(); + expect(mockLogger.info).not.toHaveBeenCalled(); + }); + + it("should log request with body flag", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "POST", + body: { data: "test" }, + contentType: "application/json", + requestType: "json", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + hasBody: true, + }), + ); + }); + + it("should log request without body flag", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + hasBody: false, + }), + ); + }); + + it("should not log when silent mode is enabled", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: true, + }, + }); + + expect(mockLogger.debug).not.toHaveBeenCalled(); + expect(mockLogger.info).not.toHaveBeenCalled(); + expect(mockLogger.warn).not.toHaveBeenCalled(); + expect(mockLogger.error).not.toHaveBeenCalled(); + }); + + it("should not log when no logging config is provided", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + }); + + expect(mockLogger.debug).not.toHaveBeenCalled(); + }); + }); + + describe("Error Logging", () => { + it("should log 4xx errors at error level", async () => { + const mockLogger = createMockLogger(); + mockErrorResponse({ error: "Not found" }, 404, "Not Found"); + + const result = await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "error", + logger: mockLogger, + silent: false, + }, + }); + + expect(result.ok).toBe(false); + expect(mockLogger.error).toHaveBeenCalledWith( + "HTTP request failed with error status", + expect.objectContaining({ + method: "GET", + url: "https://example.com/api", + statusCode: 404, + }), + ); + }); + + it("should log 5xx errors at error level", async () => { + const mockLogger = createMockLogger(); + mockErrorResponse({ error: "Internal error" }, 500, "Internal Server Error"); + + const result = await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "error", + logger: mockLogger, + silent: false, + }, + }); + + expect(result.ok).toBe(false); + expect(mockLogger.error).toHaveBeenCalledWith( + "HTTP request failed with error status", + expect.objectContaining({ + method: "GET", + url: "https://example.com/api", + statusCode: 500, + }), + ); + }); + + it("should log aborted request errors", async () => { + const mockLogger = createMockLogger(); + + const abortController = new AbortController(); + abortController.abort(); + + global.fetch = vi.fn().mockRejectedValue(new Error("Aborted")); + + const result = await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + abortSignal: abortController.signal, + maxRetries: 0, + logging: { + level: "error", + logger: mockLogger, + silent: false, + }, + }); + + expect(result.ok).toBe(false); + expect(mockLogger.error).toHaveBeenCalledWith( + "HTTP request was aborted", + expect.objectContaining({ + method: "GET", + url: "https://example.com/api", + }), + ); + }); + + it("should log timeout errors", async () => { + const mockLogger = createMockLogger(); + + const timeoutError = new Error("Request timeout"); + timeoutError.name = "AbortError"; + + global.fetch = vi.fn().mockRejectedValue(timeoutError); + + const result = await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "error", + logger: mockLogger, + silent: false, + }, + }); + + expect(result.ok).toBe(false); + expect(mockLogger.error).toHaveBeenCalledWith( + "HTTP request timed out", + expect.objectContaining({ + method: "GET", + url: "https://example.com/api", + timeoutMs: undefined, + }), + ); + }); + + it("should log unknown errors", async () => { + const mockLogger = createMockLogger(); + + const unknownError = new Error("Unknown error"); + + global.fetch = vi.fn().mockRejectedValue(unknownError); + + const result = await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "error", + logger: mockLogger, + silent: false, + }, + }); + + expect(result.ok).toBe(false); + expect(mockLogger.error).toHaveBeenCalledWith( + "HTTP request failed with error", + expect.objectContaining({ + method: "GET", + url: "https://example.com/api", + errorMessage: "Unknown error", + }), + ); + }); + }); + + describe("Logging with Redaction", () => { + it("should redact sensitive data in error logs", async () => { + const mockLogger = createMockLogger(); + mockErrorResponse({ error: "Unauthorized" }, 401, "Unauthorized"); + + await fetcherImpl({ + url: "https://example.com/api?api_key=secret", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "error", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.error).toHaveBeenCalledWith( + "HTTP request failed with error status", + expect.objectContaining({ + url: "https://example.com/api?api_key=[REDACTED]", + }), + ); + }); + }); + + describe("Different HTTP Methods", () => { + it("should log GET requests", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + method: "GET", + }), + ); + }); + + it("should log POST requests", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse({ data: "test" }, 201, "Created"); + + await fetcherImpl({ + url: "https://example.com/api", + method: "POST", + body: { data: "test" }, + contentType: "application/json", + requestType: "json", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + method: "POST", + }), + ); + }); + + it("should log PUT requests", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "PUT", + body: { data: "test" }, + contentType: "application/json", + requestType: "json", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + method: "PUT", + }), + ); + }); + + it("should log DELETE requests", async () => { + const mockLogger = createMockLogger(); + global.fetch = vi.fn().mockResolvedValue( + new Response(null, { + status: 200, + statusText: "OK", + }), + ); + + await fetcherImpl({ + url: "https://example.com/api", + method: "DELETE", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + method: "DELETE", + }), + ); + }); + }); + + describe("Status Code Logging", () => { + it("should log 2xx success status codes", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse({ data: "test" }, 201, "Created"); + + await fetcherImpl({ + url: "https://example.com/api", + method: "POST", + body: { data: "test" }, + contentType: "application/json", + requestType: "json", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "HTTP request succeeded", + expect.objectContaining({ + statusCode: 201, + }), + ); + }); + + it("should log 3xx redirect status codes as success", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse({ data: "test" }, 301, "Moved Permanently"); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "HTTP request succeeded", + expect.objectContaining({ + statusCode: 301, + }), + ); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/makePassthroughRequest.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/makePassthroughRequest.test.ts new file mode 100644 index 000000000000..1850d1fda959 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/makePassthroughRequest.test.ts @@ -0,0 +1,398 @@ +import type { Mock } from "vitest"; +import { makePassthroughRequest } from "../../../src/core/fetcher/makePassthroughRequest"; + +describe("makePassthroughRequest", () => { + let mockFetch: Mock; + + beforeEach(() => { + mockFetch = vi.fn(); + mockFetch.mockResolvedValue(new Response(JSON.stringify({ ok: true }), { status: 200 })); + }); + + describe("URL resolution", () => { + it("should use absolute URL directly", async () => { + await makePassthroughRequest("https://api.example.com/v1/users", undefined, { + fetch: mockFetch, + }); + const [calledUrl] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe("https://api.example.com/v1/users"); + }); + + it("should resolve relative path against baseUrl", async () => { + await makePassthroughRequest("/v1/users", undefined, { + baseUrl: "https://api.example.com", + fetch: mockFetch, + }); + const [calledUrl] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe("https://api.example.com/v1/users"); + }); + + it("should resolve relative path against environment when baseUrl is not set", async () => { + await makePassthroughRequest("/v1/users", undefined, { + environment: "https://env.example.com", + fetch: mockFetch, + }); + const [calledUrl] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe("https://env.example.com/v1/users"); + }); + + it("should prefer baseUrl over environment", async () => { + await makePassthroughRequest("/v1/users", undefined, { + baseUrl: "https://base.example.com", + environment: "https://env.example.com", + fetch: mockFetch, + }); + const [calledUrl] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe("https://base.example.com/v1/users"); + }); + + it("should pass relative URL through as-is when no baseUrl or environment", async () => { + await makePassthroughRequest("/v1/users", undefined, { + fetch: mockFetch, + }); + const [calledUrl] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe("/v1/users"); + }); + + it("should resolve baseUrl supplier", async () => { + await makePassthroughRequest("/v1/users", undefined, { + baseUrl: () => "https://dynamic.example.com", + fetch: mockFetch, + }); + const [calledUrl] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe("https://dynamic.example.com/v1/users"); + }); + + it("should ignore absolute URL even when baseUrl is set", async () => { + await makePassthroughRequest("https://other.example.com/path", undefined, { + baseUrl: "https://base.example.com", + fetch: mockFetch, + }); + const [calledUrl] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe("https://other.example.com/path"); + }); + + it("should accept a URL object", async () => { + await makePassthroughRequest(new URL("https://api.example.com/v1/users"), undefined, { + fetch: mockFetch, + }); + const [calledUrl] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe("https://api.example.com/v1/users"); + }); + }); + + describe("header merge order", () => { + it("should merge headers in correct priority: SDK defaults < auth < init < requestOptions", async () => { + await makePassthroughRequest( + "https://api.example.com", + { + headers: { "X-Custom": "from-init", Authorization: "from-init" }, + }, + { + headers: { + "X-Custom": "from-sdk", + "X-SDK-Only": "sdk-value", + Authorization: "from-sdk", + }, + getAuthHeaders: async () => ({ + Authorization: "Bearer auth-token", + "X-Auth-Only": "auth-value", + }), + fetch: mockFetch, + }, + { + headers: { Authorization: "from-request-options" }, + }, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + const headers = calledOptions.headers; + + // requestOptions.headers wins for Authorization (highest priority) + expect(headers.authorization).toBe("from-request-options"); + // init.headers wins over SDK defaults for X-Custom + expect(headers["x-custom"]).toBe("from-init"); + // SDK-only header is preserved + expect(headers["x-sdk-only"]).toBe("sdk-value"); + // Auth-only header is preserved + expect(headers["x-auth-only"]).toBe("auth-value"); + }); + + it("should lowercase all header keys", async () => { + await makePassthroughRequest( + "https://api.example.com", + { + headers: { "Content-Type": "application/json" }, + }, + { + headers: { "X-Fern-Language": "JavaScript" }, + fetch: mockFetch, + }, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + const headers = calledOptions.headers; + expect(headers["content-type"]).toBe("application/json"); + expect(headers["x-fern-language"]).toBe("JavaScript"); + expect(headers["Content-Type"]).toBeUndefined(); + expect(headers["X-Fern-Language"]).toBeUndefined(); + }); + + it("should handle Headers object in init", async () => { + const initHeaders = new Headers(); + initHeaders.set("X-From-Headers-Object", "value"); + await makePassthroughRequest("https://api.example.com", { headers: initHeaders }, { fetch: mockFetch }); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.headers["x-from-headers-object"]).toBe("value"); + }); + + it("should handle array-style headers in init", async () => { + await makePassthroughRequest( + "https://api.example.com", + { headers: [["X-Array-Header", "array-value"]] }, + { fetch: mockFetch }, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.headers["x-array-header"]).toBe("array-value"); + }); + + it("should skip null SDK default header values", async () => { + await makePassthroughRequest("https://api.example.com", undefined, { + headers: { "X-Present": "value", "X-Null": null }, + fetch: mockFetch, + }); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.headers["x-present"]).toBe("value"); + expect(calledOptions.headers["x-null"]).toBeUndefined(); + }); + }); + + describe("auth headers", () => { + it("should include auth headers when getAuthHeaders is provided", async () => { + await makePassthroughRequest("https://api.example.com", undefined, { + getAuthHeaders: async () => ({ Authorization: "Bearer my-token" }), + fetch: mockFetch, + }); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.headers.authorization).toBe("Bearer my-token"); + }); + + it("should work without auth headers", async () => { + await makePassthroughRequest("https://api.example.com", undefined, { + fetch: mockFetch, + }); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.headers.authorization).toBeUndefined(); + }); + + it("should allow init headers to override auth headers", async () => { + await makePassthroughRequest( + "https://api.example.com", + { headers: { Authorization: "Bearer override" } }, + { + getAuthHeaders: async () => ({ Authorization: "Bearer sdk-auth" }), + fetch: mockFetch, + }, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.headers.authorization).toBe("Bearer override"); + }); + }); + + describe("method and body", () => { + it("should default to GET when no method specified", async () => { + await makePassthroughRequest("https://api.example.com", undefined, { + fetch: mockFetch, + }); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.method).toBe("GET"); + }); + + it("should use the method from init", async () => { + await makePassthroughRequest( + "https://api.example.com", + { method: "POST", body: JSON.stringify({ key: "value" }) }, + { fetch: mockFetch }, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.method).toBe("POST"); + expect(calledOptions.body).toBe(JSON.stringify({ key: "value" })); + }); + + it("should pass body as undefined when not provided", async () => { + await makePassthroughRequest("https://api.example.com", { method: "GET" }, { fetch: mockFetch }); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.body).toBeUndefined(); + }); + }); + + describe("timeout and retries", () => { + it("should use requestOptions timeout over client timeout", async () => { + await makePassthroughRequest( + "https://api.example.com", + undefined, + { timeoutInSeconds: 30, fetch: mockFetch }, + { timeoutInSeconds: 10 }, + ); + // The timeout is passed to makeRequest which converts to ms + // We verify via the signal timing behavior (indirectly tested through makeRequest) + expect(mockFetch).toHaveBeenCalledTimes(1); + }); + + it("should use client timeout when requestOptions timeout is not set", async () => { + await makePassthroughRequest("https://api.example.com", undefined, { + timeoutInSeconds: 30, + fetch: mockFetch, + }); + expect(mockFetch).toHaveBeenCalledTimes(1); + }); + + it("should use requestOptions maxRetries over client maxRetries", async () => { + mockFetch.mockResolvedValue(new Response("", { status: 500 })); + vi.spyOn(global, "setTimeout").mockImplementation((callback: (args: void) => void) => { + process.nextTick(callback); + return null as any; + }); + + await makePassthroughRequest( + "https://api.example.com", + undefined, + { maxRetries: 5, fetch: mockFetch }, + { maxRetries: 1 }, + ); + // 1 initial + 1 retry = 2 calls + expect(mockFetch).toHaveBeenCalledTimes(2); + + vi.restoreAllMocks(); + }); + }); + + describe("abort signal", () => { + it("should use requestOptions.abortSignal over init.signal", async () => { + const initController = new AbortController(); + const requestController = new AbortController(); + + await makePassthroughRequest( + "https://api.example.com", + { signal: initController.signal }, + { fetch: mockFetch }, + { abortSignal: requestController.signal }, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + // The signal passed to makeRequest is combined with timeout signal via anySignal, + // but the requestOptions.abortSignal should be the one that's used (not init.signal) + expect(calledOptions.signal).toBeDefined(); + }); + + it("should use init.signal when requestOptions.abortSignal is not set", async () => { + const initController = new AbortController(); + + await makePassthroughRequest( + "https://api.example.com", + { signal: initController.signal }, + { fetch: mockFetch }, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.signal).toBeDefined(); + }); + }); + + describe("credentials", () => { + it("should pass credentials include when set", async () => { + await makePassthroughRequest("https://api.example.com", { credentials: "include" }, { fetch: mockFetch }); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.credentials).toBe("include"); + }); + + it("should not pass credentials when not set to include", async () => { + await makePassthroughRequest( + "https://api.example.com", + { credentials: "same-origin" }, + { + fetch: mockFetch, + }, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.credentials).toBeUndefined(); + }); + }); + + describe("response", () => { + it("should return the Response object from fetch", async () => { + const mockResponse = new Response(JSON.stringify({ data: "test" }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + mockFetch.mockResolvedValue(mockResponse); + + const response = await makePassthroughRequest("https://api.example.com", undefined, { + fetch: mockFetch, + }); + expect(response).toBe(mockResponse); + expect(response.status).toBe(200); + }); + + it("should return error responses without throwing", async () => { + const errorResponse = new Response("Not Found", { status: 404 }); + mockFetch.mockResolvedValue(errorResponse); + + const response = await makePassthroughRequest("https://api.example.com", undefined, { + fetch: mockFetch, + }); + expect(response.status).toBe(404); + }); + }); + + describe("Request object input", () => { + it("should extract URL from Request object", async () => { + const request = new Request("https://api.example.com/v1/resource", { method: "POST" }); + await makePassthroughRequest(request, undefined, { + fetch: mockFetch, + }); + const [calledUrl, calledOptions] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe("https://api.example.com/v1/resource"); + expect(calledOptions.method).toBe("POST"); + }); + + it("should extract headers from Request object when no init provided", async () => { + const request = new Request("https://api.example.com", { + headers: { "X-From-Request": "request-value" }, + }); + await makePassthroughRequest(request, undefined, { + fetch: mockFetch, + }); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.headers["x-from-request"]).toBe("request-value"); + }); + + it("should use explicit init over Request object properties", async () => { + const request = new Request("https://api.example.com", { + method: "POST", + headers: { "X-From-Request": "request-value" }, + }); + await makePassthroughRequest( + request, + { method: "PUT", headers: { "X-From-Init": "init-value" } }, + { fetch: mockFetch }, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.method).toBe("PUT"); + expect(calledOptions.headers["x-from-init"]).toBe("init-value"); + // Request headers should NOT be present since explicit init was provided + expect(calledOptions.headers["x-from-request"]).toBeUndefined(); + }); + }); + + describe("SDK default header suppliers", () => { + it("should resolve supplier functions for SDK default headers", async () => { + await makePassthroughRequest("https://api.example.com", undefined, { + headers: { + "X-Static": "static-value", + "X-Dynamic": () => "dynamic-value", + }, + fetch: mockFetch, + }); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.headers["x-static"]).toBe("static-value"); + expect(calledOptions.headers["x-dynamic"]).toBe("dynamic-value"); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/makeRequest.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/makeRequest.test.ts new file mode 100644 index 000000000000..bde194554dd8 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/makeRequest.test.ts @@ -0,0 +1,158 @@ +import type { Mock } from "vitest"; +import { + isCacheNoStoreSupported, + makeRequest, + resetCacheNoStoreSupported, +} from "../../../src/core/fetcher/makeRequest"; + +describe("Test makeRequest", () => { + const mockPostUrl = "https://httpbin.org/post"; + const mockGetUrl = "https://httpbin.org/get"; + const mockHeaders = { "Content-Type": "application/json" }; + const mockBody = JSON.stringify({ key: "value" }); + + let mockFetch: Mock; + + beforeEach(() => { + mockFetch = vi.fn(); + mockFetch.mockResolvedValue(new Response(JSON.stringify({ test: "successful" }), { status: 200 })); + resetCacheNoStoreSupported(); + }); + + it("should handle POST request correctly", async () => { + const response = await makeRequest(mockFetch, mockPostUrl, "POST", mockHeaders, mockBody); + const responseBody = await response.json(); + expect(responseBody).toEqual({ test: "successful" }); + expect(mockFetch).toHaveBeenCalledTimes(1); + const [calledUrl, calledOptions] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe(mockPostUrl); + expect(calledOptions).toEqual( + expect.objectContaining({ + method: "POST", + headers: mockHeaders, + body: mockBody, + credentials: undefined, + }), + ); + expect(calledOptions.signal).toBeDefined(); + expect(calledOptions.signal).toBeInstanceOf(AbortSignal); + }); + + it("should handle GET request correctly", async () => { + const response = await makeRequest(mockFetch, mockGetUrl, "GET", mockHeaders, undefined); + const responseBody = await response.json(); + expect(responseBody).toEqual({ test: "successful" }); + expect(mockFetch).toHaveBeenCalledTimes(1); + const [calledUrl, calledOptions] = mockFetch.mock.calls[0]; + expect(calledUrl).toBe(mockGetUrl); + expect(calledOptions).toEqual( + expect.objectContaining({ + method: "GET", + headers: mockHeaders, + body: undefined, + credentials: undefined, + }), + ); + expect(calledOptions.signal).toBeDefined(); + expect(calledOptions.signal).toBeInstanceOf(AbortSignal); + }); + + it("should not include cache option when disableCache is not set", async () => { + await makeRequest(mockFetch, mockGetUrl, "GET", mockHeaders, undefined); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.cache).toBeUndefined(); + }); + + it("should not include cache option when disableCache is false", async () => { + await makeRequest( + mockFetch, + mockGetUrl, + "GET", + mockHeaders, + undefined, + undefined, + undefined, + undefined, + undefined, + false, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.cache).toBeUndefined(); + }); + + it("should include cache: no-store when disableCache is true and runtime supports it", async () => { + // In Node.js test environment, Request supports the cache option + expect(isCacheNoStoreSupported()).toBe(true); + await makeRequest( + mockFetch, + mockGetUrl, + "GET", + mockHeaders, + undefined, + undefined, + undefined, + undefined, + undefined, + true, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.cache).toBe("no-store"); + }); + + it("should cache the result of isCacheNoStoreSupported", () => { + const first = isCacheNoStoreSupported(); + const second = isCacheNoStoreSupported(); + expect(first).toBe(second); + }); + + it("should reset cache detection state with resetCacheNoStoreSupported", () => { + // First call caches the result + const first = isCacheNoStoreSupported(); + expect(first).toBe(true); + + // Reset clears the cache + resetCacheNoStoreSupported(); + + // After reset, it should re-detect (and still return true in Node.js) + const second = isCacheNoStoreSupported(); + expect(second).toBe(true); + }); + + it("should not include cache option when runtime does not support it (e.g. Cloudflare Workers)", async () => { + // Mock Request constructor to throw when cache option is passed, + // simulating runtimes like Cloudflare Workers + const OriginalRequest = globalThis.Request; + globalThis.Request = class MockRequest { + constructor(_url: string, init?: RequestInit) { + if (init?.cache != null) { + throw new TypeError("The 'cache' field on 'RequestInitializerDict' is not implemented."); + } + } + } as unknown as typeof Request; + + try { + // Reset so the detection runs fresh with the mocked Request + resetCacheNoStoreSupported(); + expect(isCacheNoStoreSupported()).toBe(false); + + await makeRequest( + mockFetch, + mockGetUrl, + "GET", + mockHeaders, + undefined, + undefined, + undefined, + undefined, + undefined, + true, + ); + const [, calledOptions] = mockFetch.mock.calls[0]; + expect(calledOptions.cache).toBeUndefined(); + } finally { + // Restore original Request + globalThis.Request = OriginalRequest; + resetCacheNoStoreSupported(); + } + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/redacting.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/redacting.test.ts new file mode 100644 index 000000000000..d599376b9bcf --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/redacting.test.ts @@ -0,0 +1,1115 @@ +import { fetcherImpl } from "../../../src/core/fetcher/Fetcher"; + +function createMockLogger() { + return { + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }; +} + +function mockSuccessResponse(data: unknown = { data: "test" }, status = 200, statusText = "OK") { + global.fetch = vi.fn().mockResolvedValue( + new Response(JSON.stringify(data), { + status, + statusText, + }), + ); +} + +describe("Redacting Logic", () => { + describe("Header Redaction", () => { + it("should redact authorization header", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + headers: { Authorization: "Bearer secret-token-12345" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + headers: expect.toContainHeaders({ + Authorization: "[REDACTED]", + }), + }), + ); + }); + + it("should redact api-key header (case-insensitive)", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + headers: { "X-API-KEY": "secret-api-key" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + headers: expect.toContainHeaders({ + "X-API-KEY": "[REDACTED]", + }), + }), + ); + }); + + it("should redact cookie header", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + headers: { Cookie: "session=abc123; token=xyz789" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + headers: expect.toContainHeaders({ + Cookie: "[REDACTED]", + }), + }), + ); + }); + + it("should redact x-auth-token header", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + headers: { "x-auth-token": "auth-token-12345" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + headers: expect.toContainHeaders({ + "x-auth-token": "[REDACTED]", + }), + }), + ); + }); + + it("should redact proxy-authorization header", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + headers: { "Proxy-Authorization": "Basic credentials" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + headers: expect.toContainHeaders({ + "Proxy-Authorization": "[REDACTED]", + }), + }), + ); + }); + + it("should redact x-csrf-token header", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + headers: { "X-CSRF-Token": "csrf-token-abc" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + headers: expect.toContainHeaders({ + "X-CSRF-Token": "[REDACTED]", + }), + }), + ); + }); + + it("should redact www-authenticate header", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + headers: { "WWW-Authenticate": "Bearer realm=example" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + headers: expect.toContainHeaders({ + "WWW-Authenticate": "[REDACTED]", + }), + }), + ); + }); + + it("should redact x-session-token header", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + headers: { "X-Session-Token": "session-token-xyz" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + headers: expect.toContainHeaders({ + "X-Session-Token": "[REDACTED]", + }), + }), + ); + }); + + it("should not redact non-sensitive headers", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + headers: { + "Content-Type": "application/json", + "User-Agent": "Test/1.0", + Accept: "application/json", + }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + headers: expect.toContainHeaders({ + "Content-Type": "application/json", + "User-Agent": "Test/1.0", + Accept: "application/json", + }), + }), + ); + }); + + it("should redact multiple sensitive headers at once", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + headers: { + Authorization: "Bearer token", + "X-API-Key": "api-key", + Cookie: "session=123", + "Content-Type": "application/json", + }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + headers: expect.toContainHeaders({ + Authorization: "[REDACTED]", + "X-API-Key": "[REDACTED]", + Cookie: "[REDACTED]", + "Content-Type": "application/json", + }), + }), + ); + }); + }); + + describe("Response Header Redaction", () => { + it("should redact Set-Cookie in response headers", async () => { + const mockLogger = createMockLogger(); + + const mockHeaders = new Headers(); + mockHeaders.set("Set-Cookie", "session=abc123; HttpOnly; Secure"); + mockHeaders.set("Content-Type", "application/json"); + + global.fetch = vi.fn().mockResolvedValue( + new Response(JSON.stringify({ data: "test" }), { + status: 200, + statusText: "OK", + headers: mockHeaders, + }), + ); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "HTTP request succeeded", + expect.objectContaining({ + responseHeaders: expect.toContainHeaders({ + "set-cookie": "[REDACTED]", + "content-type": "application/json", + }), + }), + ); + }); + + it("should redact authorization in response headers", async () => { + const mockLogger = createMockLogger(); + + const mockHeaders = new Headers(); + mockHeaders.set("Authorization", "Bearer token-123"); + mockHeaders.set("Content-Type", "application/json"); + + global.fetch = vi.fn().mockResolvedValue( + new Response(JSON.stringify({ data: "test" }), { + status: 200, + statusText: "OK", + headers: mockHeaders, + }), + ); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "HTTP request succeeded", + expect.objectContaining({ + responseHeaders: expect.toContainHeaders({ + authorization: "[REDACTED]", + "content-type": "application/json", + }), + }), + ); + }); + + it("should redact response headers in error responses", async () => { + const mockLogger = createMockLogger(); + + const mockHeaders = new Headers(); + mockHeaders.set("WWW-Authenticate", "Bearer realm=example"); + mockHeaders.set("Content-Type", "application/json"); + + global.fetch = vi.fn().mockResolvedValue( + new Response(JSON.stringify({ error: "Unauthorized" }), { + status: 401, + statusText: "Unauthorized", + headers: mockHeaders, + }), + ); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "error", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.error).toHaveBeenCalledWith( + "HTTP request failed with error status", + expect.objectContaining({ + responseHeaders: expect.toContainHeaders({ + "www-authenticate": "[REDACTED]", + "content-type": "application/json", + }), + }), + ); + }); + }); + + describe("Query Parameter Redaction", () => { + it("should redact api_key query parameter", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + queryParameters: { api_key: "secret-key" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + queryParameters: expect.objectContaining({ + api_key: "[REDACTED]", + }), + }), + ); + }); + + it("should redact token query parameter", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + queryParameters: { token: "secret-token" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + queryParameters: expect.objectContaining({ + token: "[REDACTED]", + }), + }), + ); + }); + + it("should redact access_token query parameter", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + queryParameters: { access_token: "secret-access-token" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + queryParameters: expect.objectContaining({ + access_token: "[REDACTED]", + }), + }), + ); + }); + + it("should redact password query parameter", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + queryParameters: { password: "secret-password" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + queryParameters: expect.objectContaining({ + password: "[REDACTED]", + }), + }), + ); + }); + + it("should redact secret query parameter", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + queryParameters: { secret: "secret-value" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + queryParameters: expect.objectContaining({ + secret: "[REDACTED]", + }), + }), + ); + }); + + it("should redact session_id query parameter", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + queryParameters: { session_id: "session-123" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + queryParameters: expect.objectContaining({ + session_id: "[REDACTED]", + }), + }), + ); + }); + + it("should not redact non-sensitive query parameters", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + queryParameters: { + page: "1", + limit: "10", + sort: "name", + }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + queryParameters: expect.objectContaining({ + page: "1", + limit: "10", + sort: "name", + }), + }), + ); + }); + + it("should not redact parameters containing 'auth' substring like 'author'", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + queryParameters: { + author: "john", + authenticate: "false", + authorization_level: "user", + }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + queryParameters: expect.objectContaining({ + author: "john", + authenticate: "false", + authorization_level: "user", + }), + }), + ); + }); + + it("should handle undefined query parameters", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + queryParameters: undefined, + }), + ); + }); + + it("should redact case-insensitive query parameters", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + queryParameters: { API_KEY: "secret-key", Token: "secret-token" }, + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + queryParameters: expect.objectContaining({ + API_KEY: "[REDACTED]", + Token: "[REDACTED]", + }), + }), + ); + }); + }); + + describe("URL Redaction", () => { + it("should redact credentials in URL", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://user:password@example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://[REDACTED]@example.com/api", + }), + ); + }); + + it("should redact api_key in query string", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?api_key=secret-key&page=1", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?api_key=[REDACTED]&page=1", + }), + ); + }); + + it("should redact token in query string", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?token=secret-token", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?token=[REDACTED]", + }), + ); + }); + + it("should redact password in query string", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?username=user&password=secret", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?username=user&password=[REDACTED]", + }), + ); + }); + + it("should not redact non-sensitive query strings", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?page=1&limit=10&sort=name", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?page=1&limit=10&sort=name", + }), + ); + }); + + it("should not redact URL parameters containing 'auth' substring like 'author'", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?author=john&authenticate=false&page=1", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?author=john&authenticate=false&page=1", + }), + ); + }); + + it("should handle URL with fragment", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?token=secret#section", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?token=[REDACTED]#section", + }), + ); + }); + + it("should redact URL-encoded query parameters", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?api%5Fkey=secret", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?api%5Fkey=[REDACTED]", + }), + ); + }); + + it("should handle URL without query string", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api", + }), + ); + }); + + it("should handle empty query string", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?", + }), + ); + }); + + it("should redact multiple sensitive parameters in URL", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?api_key=secret1&token=secret2&page=1", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?api_key=[REDACTED]&token=[REDACTED]&page=1", + }), + ); + }); + + it("should redact both credentials and query parameters", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://user:pass@example.com/api?token=secret", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://[REDACTED]@example.com/api?token=[REDACTED]", + }), + ); + }); + + it("should use fast path for URLs without sensitive keywords", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?page=1&limit=10&sort=name&filter=value", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?page=1&limit=10&sort=name&filter=value", + }), + ); + }); + + it("should handle query parameter without value", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?flag&token=secret", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?flag&token=[REDACTED]", + }), + ); + }); + + it("should handle URL with multiple @ symbols in credentials", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://user@example.com:pass@host.com/api", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://[REDACTED]@host.com/api", + }), + ); + }); + + it("should handle URL with @ in query parameter but not in credentials", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://example.com/api?email=user@example.com", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://example.com/api?email=user@example.com", + }), + ); + }); + + it("should handle URL with both credentials and @ in path", async () => { + const mockLogger = createMockLogger(); + mockSuccessResponse(); + + await fetcherImpl({ + url: "https://user:pass@example.com/users/@username", + method: "GET", + responseType: "json", + maxRetries: 0, + logging: { + level: "debug", + logger: mockLogger, + silent: false, + }, + }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + "Making HTTP request", + expect.objectContaining({ + url: "https://[REDACTED]@example.com/users/@username", + }), + ); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/requestWithRetries.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/requestWithRetries.test.ts new file mode 100644 index 000000000000..d22661367f4e --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/requestWithRetries.test.ts @@ -0,0 +1,230 @@ +import type { Mock, MockInstance } from "vitest"; +import { requestWithRetries } from "../../../src/core/fetcher/requestWithRetries"; + +describe("requestWithRetries", () => { + let mockFetch: Mock; + let originalMathRandom: typeof Math.random; + let setTimeoutSpy: MockInstance; + + beforeEach(() => { + mockFetch = vi.fn(); + originalMathRandom = Math.random; + + Math.random = vi.fn(() => 0.5); + + vi.useFakeTimers({ + toFake: [ + "setTimeout", + "clearTimeout", + "setInterval", + "clearInterval", + "setImmediate", + "clearImmediate", + "Date", + "performance", + "requestAnimationFrame", + "cancelAnimationFrame", + "requestIdleCallback", + "cancelIdleCallback", + ], + }); + }); + + afterEach(() => { + Math.random = originalMathRandom; + vi.clearAllMocks(); + vi.clearAllTimers(); + }); + + it("should retry on retryable status codes", async () => { + setTimeoutSpy = vi.spyOn(global, "setTimeout").mockImplementation((callback: (args: void) => void) => { + process.nextTick(callback); + return null as any; + }); + + const retryableStatuses = [408, 429, 500, 502]; + let callCount = 0; + + mockFetch.mockImplementation(async () => { + if (callCount < retryableStatuses.length) { + return new Response("", { status: retryableStatuses[callCount++] }); + } + return new Response("", { status: 200 }); + }); + + const responsePromise = requestWithRetries(() => mockFetch(), retryableStatuses.length); + await vi.runAllTimersAsync(); + const response = await responsePromise; + + expect(mockFetch).toHaveBeenCalledTimes(retryableStatuses.length + 1); + expect(response.status).toBe(200); + }); + + it("should respect maxRetries limit", async () => { + setTimeoutSpy = vi.spyOn(global, "setTimeout").mockImplementation((callback: (args: void) => void) => { + process.nextTick(callback); + return null as any; + }); + + const maxRetries = 2; + mockFetch.mockResolvedValue(new Response("", { status: 500 })); + + const responsePromise = requestWithRetries(() => mockFetch(), maxRetries); + await vi.runAllTimersAsync(); + const response = await responsePromise; + + expect(mockFetch).toHaveBeenCalledTimes(maxRetries + 1); + expect(response.status).toBe(500); + }); + + it("should not retry on success status codes", async () => { + setTimeoutSpy = vi.spyOn(global, "setTimeout").mockImplementation((callback: (args: void) => void) => { + process.nextTick(callback); + return null as any; + }); + + const successStatuses = [200, 201, 202]; + + for (const status of successStatuses) { + mockFetch.mockReset(); + setTimeoutSpy.mockClear(); + mockFetch.mockResolvedValueOnce(new Response("", { status })); + + const responsePromise = requestWithRetries(() => mockFetch(), 3); + await vi.runAllTimersAsync(); + await responsePromise; + + expect(mockFetch).toHaveBeenCalledTimes(1); + expect(setTimeoutSpy).not.toHaveBeenCalled(); + } + }); + + interface RetryHeaderTestCase { + description: string; + headerName: string; + headerValue: string | (() => string); + expectedDelayMin: number; + expectedDelayMax: number; + } + + const retryHeaderTests: RetryHeaderTestCase[] = [ + { + description: "should respect retry-after header with seconds value", + headerName: "retry-after", + headerValue: "5", + expectedDelayMin: 4000, + expectedDelayMax: 6000, + }, + { + description: "should respect retry-after header with HTTP date value", + headerName: "retry-after", + headerValue: () => new Date(Date.now() + 3000).toUTCString(), + expectedDelayMin: 2000, + expectedDelayMax: 4000, + }, + { + description: "should respect x-ratelimit-reset header", + headerName: "x-ratelimit-reset", + headerValue: () => Math.floor((Date.now() + 4000) / 1000).toString(), + expectedDelayMin: 3000, + expectedDelayMax: 6000, + }, + ]; + + retryHeaderTests.forEach(({ description, headerName, headerValue, expectedDelayMin, expectedDelayMax }) => { + it(description, async () => { + setTimeoutSpy = vi.spyOn(global, "setTimeout").mockImplementation((callback: (args: void) => void) => { + process.nextTick(callback); + return null as any; + }); + + const value = typeof headerValue === "function" ? headerValue() : headerValue; + mockFetch + .mockResolvedValueOnce( + new Response("", { + status: 429, + headers: new Headers({ [headerName]: value }), + }), + ) + .mockResolvedValueOnce(new Response("", { status: 200 })); + + const responsePromise = requestWithRetries(() => mockFetch(), 1); + await vi.runAllTimersAsync(); + const response = await responsePromise; + + expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), expect.any(Number)); + const actualDelay = setTimeoutSpy.mock.calls[0][1]; + expect(actualDelay).toBeGreaterThan(expectedDelayMin); + expect(actualDelay).toBeLessThan(expectedDelayMax); + expect(response.status).toBe(200); + }); + }); + + it("should apply correct exponential backoff with jitter", async () => { + setTimeoutSpy = vi.spyOn(global, "setTimeout").mockImplementation((callback: (args: void) => void) => { + process.nextTick(callback); + return null as any; + }); + + mockFetch.mockResolvedValue(new Response("", { status: 500 })); + const maxRetries = 3; + const expectedDelays = [1000, 2000, 4000]; + + const responsePromise = requestWithRetries(() => mockFetch(), maxRetries); + await vi.runAllTimersAsync(); + await responsePromise; + + expect(setTimeoutSpy).toHaveBeenCalledTimes(expectedDelays.length); + + expectedDelays.forEach((delay, index) => { + expect(setTimeoutSpy).toHaveBeenNthCalledWith(index + 1, expect.any(Function), delay); + }); + + expect(mockFetch).toHaveBeenCalledTimes(maxRetries + 1); + }); + + it("should handle concurrent retries independently", async () => { + setTimeoutSpy = vi.spyOn(global, "setTimeout").mockImplementation((callback: (args: void) => void) => { + process.nextTick(callback); + return null as any; + }); + + mockFetch + .mockResolvedValueOnce(new Response("", { status: 500 })) + .mockResolvedValueOnce(new Response("", { status: 500 })) + .mockResolvedValueOnce(new Response("", { status: 200 })) + .mockResolvedValueOnce(new Response("", { status: 200 })); + + const promise1 = requestWithRetries(() => mockFetch(), 1); + const promise2 = requestWithRetries(() => mockFetch(), 1); + + await vi.runAllTimersAsync(); + const [response1, response2] = await Promise.all([promise1, promise2]); + + expect(response1.status).toBe(200); + expect(response2.status).toBe(200); + }); + + it("should cap delay at MAX_RETRY_DELAY for large header values", async () => { + setTimeoutSpy = vi.spyOn(global, "setTimeout").mockImplementation((callback: (args: void) => void) => { + process.nextTick(callback); + return null as any; + }); + + mockFetch + .mockResolvedValueOnce( + new Response("", { + status: 429, + headers: new Headers({ "retry-after": "120" }), // 120 seconds = 120000ms > MAX_RETRY_DELAY (60000ms) + }), + ) + .mockResolvedValueOnce(new Response("", { status: 200 })); + + const responsePromise = requestWithRetries(() => mockFetch(), 1); + await vi.runAllTimersAsync(); + const response = await responsePromise; + + expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 60000); + expect(response.status).toBe(200); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/signals.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/signals.test.ts new file mode 100644 index 000000000000..d7b6d1e63caa --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/signals.test.ts @@ -0,0 +1,69 @@ +import { anySignal, getTimeoutSignal } from "../../../src/core/fetcher/signals"; + +describe("Test getTimeoutSignal", () => { + beforeEach(() => { + vi.useFakeTimers(); + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it("should return an object with signal and abortId", () => { + const { signal, abortId } = getTimeoutSignal(1000); + + expect(signal).toBeDefined(); + expect(abortId).toBeDefined(); + expect(signal).toBeInstanceOf(AbortSignal); + expect(signal.aborted).toBe(false); + }); + + it("should create a signal that aborts after the specified timeout", () => { + const timeoutMs = 5000; + const { signal } = getTimeoutSignal(timeoutMs); + + expect(signal.aborted).toBe(false); + + vi.advanceTimersByTime(timeoutMs - 1); + expect(signal.aborted).toBe(false); + + vi.advanceTimersByTime(1); + expect(signal.aborted).toBe(true); + }); +}); + +describe("Test anySignal", () => { + it("should return an AbortSignal", () => { + const signal = anySignal(new AbortController().signal); + expect(signal).toBeInstanceOf(AbortSignal); + }); + + it("should abort when any of the input signals is aborted", () => { + const controller1 = new AbortController(); + const controller2 = new AbortController(); + const signal = anySignal(controller1.signal, controller2.signal); + + expect(signal.aborted).toBe(false); + controller1.abort(); + expect(signal.aborted).toBe(true); + }); + + it("should handle an array of signals", () => { + const controller1 = new AbortController(); + const controller2 = new AbortController(); + const signal = anySignal([controller1.signal, controller2.signal]); + + expect(signal.aborted).toBe(false); + controller2.abort(); + expect(signal.aborted).toBe(true); + }); + + it("should abort immediately if one of the input signals is already aborted", () => { + const controller1 = new AbortController(); + const controller2 = new AbortController(); + controller1.abort(); + + const signal = anySignal(controller1.signal, controller2.signal); + expect(signal.aborted).toBe(true); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/test-file.txt b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/test-file.txt new file mode 100644 index 000000000000..c66d471e359c --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/fetcher/test-file.txt @@ -0,0 +1 @@ +This is a test file! diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/logging/logger.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/logging/logger.test.ts new file mode 100644 index 000000000000..2e0b5fe5040c --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/logging/logger.test.ts @@ -0,0 +1,454 @@ +import { ConsoleLogger, createLogger, Logger, LogLevel } from "../../../src/core/logging/logger"; + +function createMockLogger() { + return { + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + }; +} + +describe("Logger", () => { + describe("LogLevel", () => { + it("should have correct log levels", () => { + expect(LogLevel.Debug).toBe("debug"); + expect(LogLevel.Info).toBe("info"); + expect(LogLevel.Warn).toBe("warn"); + expect(LogLevel.Error).toBe("error"); + }); + }); + + describe("ConsoleLogger", () => { + let consoleLogger: ConsoleLogger; + let consoleSpy: { + debug: ReturnType; + info: ReturnType; + warn: ReturnType; + error: ReturnType; + }; + + beforeEach(() => { + consoleLogger = new ConsoleLogger(); + consoleSpy = { + debug: vi.spyOn(console, "debug").mockImplementation(() => {}), + info: vi.spyOn(console, "info").mockImplementation(() => {}), + warn: vi.spyOn(console, "warn").mockImplementation(() => {}), + error: vi.spyOn(console, "error").mockImplementation(() => {}), + }; + }); + + afterEach(() => { + consoleSpy.debug.mockRestore(); + consoleSpy.info.mockRestore(); + consoleSpy.warn.mockRestore(); + consoleSpy.error.mockRestore(); + }); + + it("should log debug messages", () => { + consoleLogger.debug("debug message", { data: "test" }); + expect(consoleSpy.debug).toHaveBeenCalledWith("debug message", { data: "test" }); + }); + + it("should log info messages", () => { + consoleLogger.info("info message", { data: "test" }); + expect(consoleSpy.info).toHaveBeenCalledWith("info message", { data: "test" }); + }); + + it("should log warn messages", () => { + consoleLogger.warn("warn message", { data: "test" }); + expect(consoleSpy.warn).toHaveBeenCalledWith("warn message", { data: "test" }); + }); + + it("should log error messages", () => { + consoleLogger.error("error message", { data: "test" }); + expect(consoleSpy.error).toHaveBeenCalledWith("error message", { data: "test" }); + }); + + it("should handle multiple arguments", () => { + consoleLogger.debug("message", "arg1", "arg2", { key: "value" }); + expect(consoleSpy.debug).toHaveBeenCalledWith("message", "arg1", "arg2", { key: "value" }); + }); + }); + + describe("Logger with level filtering", () => { + let mockLogger: { + debug: ReturnType; + info: ReturnType; + warn: ReturnType; + error: ReturnType; + }; + + beforeEach(() => { + mockLogger = createMockLogger(); + }); + + describe("Debug level", () => { + it("should log all levels when set to debug", () => { + const logger = new Logger({ + level: LogLevel.Debug, + logger: mockLogger, + silent: false, + }); + + logger.debug("debug"); + logger.info("info"); + logger.warn("warn"); + logger.error("error"); + + expect(mockLogger.debug).toHaveBeenCalledWith("debug"); + expect(mockLogger.info).toHaveBeenCalledWith("info"); + expect(mockLogger.warn).toHaveBeenCalledWith("warn"); + expect(mockLogger.error).toHaveBeenCalledWith("error"); + }); + + it("should report correct level checks", () => { + const logger = new Logger({ + level: LogLevel.Debug, + logger: mockLogger, + silent: false, + }); + + expect(logger.isDebug()).toBe(true); + expect(logger.isInfo()).toBe(true); + expect(logger.isWarn()).toBe(true); + expect(logger.isError()).toBe(true); + }); + }); + + describe("Info level", () => { + it("should log info, warn, and error when set to info", () => { + const logger = new Logger({ + level: LogLevel.Info, + logger: mockLogger, + silent: false, + }); + + logger.debug("debug"); + logger.info("info"); + logger.warn("warn"); + logger.error("error"); + + expect(mockLogger.debug).not.toHaveBeenCalled(); + expect(mockLogger.info).toHaveBeenCalledWith("info"); + expect(mockLogger.warn).toHaveBeenCalledWith("warn"); + expect(mockLogger.error).toHaveBeenCalledWith("error"); + }); + + it("should report correct level checks", () => { + const logger = new Logger({ + level: LogLevel.Info, + logger: mockLogger, + silent: false, + }); + + expect(logger.isDebug()).toBe(false); + expect(logger.isInfo()).toBe(true); + expect(logger.isWarn()).toBe(true); + expect(logger.isError()).toBe(true); + }); + }); + + describe("Warn level", () => { + it("should log warn and error when set to warn", () => { + const logger = new Logger({ + level: LogLevel.Warn, + logger: mockLogger, + silent: false, + }); + + logger.debug("debug"); + logger.info("info"); + logger.warn("warn"); + logger.error("error"); + + expect(mockLogger.debug).not.toHaveBeenCalled(); + expect(mockLogger.info).not.toHaveBeenCalled(); + expect(mockLogger.warn).toHaveBeenCalledWith("warn"); + expect(mockLogger.error).toHaveBeenCalledWith("error"); + }); + + it("should report correct level checks", () => { + const logger = new Logger({ + level: LogLevel.Warn, + logger: mockLogger, + silent: false, + }); + + expect(logger.isDebug()).toBe(false); + expect(logger.isInfo()).toBe(false); + expect(logger.isWarn()).toBe(true); + expect(logger.isError()).toBe(true); + }); + }); + + describe("Error level", () => { + it("should only log error when set to error", () => { + const logger = new Logger({ + level: LogLevel.Error, + logger: mockLogger, + silent: false, + }); + + logger.debug("debug"); + logger.info("info"); + logger.warn("warn"); + logger.error("error"); + + expect(mockLogger.debug).not.toHaveBeenCalled(); + expect(mockLogger.info).not.toHaveBeenCalled(); + expect(mockLogger.warn).not.toHaveBeenCalled(); + expect(mockLogger.error).toHaveBeenCalledWith("error"); + }); + + it("should report correct level checks", () => { + const logger = new Logger({ + level: LogLevel.Error, + logger: mockLogger, + silent: false, + }); + + expect(logger.isDebug()).toBe(false); + expect(logger.isInfo()).toBe(false); + expect(logger.isWarn()).toBe(false); + expect(logger.isError()).toBe(true); + }); + }); + + describe("Silent mode", () => { + it("should not log anything when silent is true", () => { + const logger = new Logger({ + level: LogLevel.Debug, + logger: mockLogger, + silent: true, + }); + + logger.debug("debug"); + logger.info("info"); + logger.warn("warn"); + logger.error("error"); + + expect(mockLogger.debug).not.toHaveBeenCalled(); + expect(mockLogger.info).not.toHaveBeenCalled(); + expect(mockLogger.warn).not.toHaveBeenCalled(); + expect(mockLogger.error).not.toHaveBeenCalled(); + }); + + it("should report all level checks as false when silent", () => { + const logger = new Logger({ + level: LogLevel.Debug, + logger: mockLogger, + silent: true, + }); + + expect(logger.isDebug()).toBe(false); + expect(logger.isInfo()).toBe(false); + expect(logger.isWarn()).toBe(false); + expect(logger.isError()).toBe(false); + }); + }); + + describe("shouldLog", () => { + it("should correctly determine if level should be logged", () => { + const logger = new Logger({ + level: LogLevel.Info, + logger: mockLogger, + silent: false, + }); + + expect(logger.shouldLog(LogLevel.Debug)).toBe(false); + expect(logger.shouldLog(LogLevel.Info)).toBe(true); + expect(logger.shouldLog(LogLevel.Warn)).toBe(true); + expect(logger.shouldLog(LogLevel.Error)).toBe(true); + }); + + it("should return false for all levels when silent", () => { + const logger = new Logger({ + level: LogLevel.Debug, + logger: mockLogger, + silent: true, + }); + + expect(logger.shouldLog(LogLevel.Debug)).toBe(false); + expect(logger.shouldLog(LogLevel.Info)).toBe(false); + expect(logger.shouldLog(LogLevel.Warn)).toBe(false); + expect(logger.shouldLog(LogLevel.Error)).toBe(false); + }); + }); + + describe("Multiple arguments", () => { + it("should pass multiple arguments to logger", () => { + const logger = new Logger({ + level: LogLevel.Debug, + logger: mockLogger, + silent: false, + }); + + logger.debug("message", "arg1", { key: "value" }, 123); + expect(mockLogger.debug).toHaveBeenCalledWith("message", "arg1", { key: "value" }, 123); + }); + }); + }); + + describe("createLogger", () => { + it("should return default logger when no config provided", () => { + const logger = createLogger(); + expect(logger).toBeInstanceOf(Logger); + }); + + it("should return same logger instance when Logger is passed", () => { + const customLogger = new Logger({ + level: LogLevel.Debug, + logger: new ConsoleLogger(), + silent: false, + }); + + const result = createLogger(customLogger); + expect(result).toBe(customLogger); + }); + + it("should create logger with custom config", () => { + const mockLogger = createMockLogger(); + + const logger = createLogger({ + level: LogLevel.Warn, + logger: mockLogger, + silent: false, + }); + + expect(logger).toBeInstanceOf(Logger); + logger.warn("test"); + expect(mockLogger.warn).toHaveBeenCalledWith("test"); + }); + + it("should use default values for missing config", () => { + const logger = createLogger({}); + expect(logger).toBeInstanceOf(Logger); + }); + + it("should override default level", () => { + const mockLogger = createMockLogger(); + + const logger = createLogger({ + level: LogLevel.Debug, + logger: mockLogger, + silent: false, + }); + + logger.debug("test"); + expect(mockLogger.debug).toHaveBeenCalledWith("test"); + }); + + it("should override default silent mode", () => { + const mockLogger = createMockLogger(); + + const logger = createLogger({ + logger: mockLogger, + silent: false, + }); + + logger.info("test"); + expect(mockLogger.info).toHaveBeenCalledWith("test"); + }); + + it("should use provided logger implementation", () => { + const customLogger = createMockLogger(); + + const logger = createLogger({ + logger: customLogger, + level: LogLevel.Debug, + silent: false, + }); + + logger.debug("test"); + expect(customLogger.debug).toHaveBeenCalledWith("test"); + }); + + it("should default to silent: true", () => { + const mockLogger = createMockLogger(); + + const logger = createLogger({ + logger: mockLogger, + level: LogLevel.Debug, + }); + + logger.debug("test"); + expect(mockLogger.debug).not.toHaveBeenCalled(); + }); + }); + + describe("Default logger", () => { + it("should have silent: true by default", () => { + const logger = createLogger(); + expect(logger.shouldLog(LogLevel.Info)).toBe(false); + }); + + it("should not log when using default logger", () => { + const logger = createLogger(); + + logger.info("test"); + expect(logger.isInfo()).toBe(false); + }); + }); + + describe("Edge cases", () => { + it("should handle empty message", () => { + const mockLogger = createMockLogger(); + + const logger = new Logger({ + level: LogLevel.Debug, + logger: mockLogger, + silent: false, + }); + + logger.debug(""); + expect(mockLogger.debug).toHaveBeenCalledWith(""); + }); + + it("should handle no arguments", () => { + const mockLogger = createMockLogger(); + + const logger = new Logger({ + level: LogLevel.Debug, + logger: mockLogger, + silent: false, + }); + + logger.debug("message"); + expect(mockLogger.debug).toHaveBeenCalledWith("message"); + }); + + it("should handle complex objects", () => { + const mockLogger = createMockLogger(); + + const logger = new Logger({ + level: LogLevel.Debug, + logger: mockLogger, + silent: false, + }); + + const complexObject = { + nested: { key: "value" }, + array: [1, 2, 3], + fn: () => "test", + }; + + logger.debug("message", complexObject); + expect(mockLogger.debug).toHaveBeenCalledWith("message", complexObject); + }); + + it("should handle errors as arguments", () => { + const mockLogger = createMockLogger(); + + const logger = new Logger({ + level: LogLevel.Error, + logger: mockLogger, + silent: false, + }); + + const error = new Error("Test error"); + logger.error("Error occurred", error); + expect(mockLogger.error).toHaveBeenCalledWith("Error occurred", error); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/url/join.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/url/join.test.ts new file mode 100644 index 000000000000..123488f084ea --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/url/join.test.ts @@ -0,0 +1,284 @@ +import { join } from "../../../src/core/url/index"; + +describe("join", () => { + interface TestCase { + description: string; + base: string; + segments: string[]; + expected: string; + } + + describe("basic functionality", () => { + const basicTests: TestCase[] = [ + { description: "should return empty string for empty base", base: "", segments: [], expected: "" }, + { + description: "should return empty string for empty base with path", + base: "", + segments: ["path"], + expected: "", + }, + { + description: "should handle single segment", + base: "base", + segments: ["segment"], + expected: "base/segment", + }, + { + description: "should handle single segment with trailing slash on base", + base: "base/", + segments: ["segment"], + expected: "base/segment", + }, + { + description: "should handle single segment with leading slash", + base: "base", + segments: ["/segment"], + expected: "base/segment", + }, + { + description: "should handle single segment with both slashes", + base: "base/", + segments: ["/segment"], + expected: "base/segment", + }, + { + description: "should handle multiple segments", + base: "base", + segments: ["path1", "path2", "path3"], + expected: "base/path1/path2/path3", + }, + { + description: "should handle multiple segments with slashes", + base: "base/", + segments: ["/path1/", "/path2/", "/path3/"], + expected: "base/path1/path2/path3/", + }, + ]; + + basicTests.forEach(({ description, base, segments, expected }) => { + it(description, () => { + expect(join(base, ...segments)).toBe(expected); + }); + }); + }); + + describe("URL handling", () => { + const urlTests: TestCase[] = [ + { + description: "should handle absolute URLs", + base: "https://example.com", + segments: ["api", "v1"], + expected: "https://example.com/api/v1", + }, + { + description: "should handle absolute URLs with slashes", + base: "https://example.com/", + segments: ["/api/", "/v1/"], + expected: "https://example.com/api/v1/", + }, + { + description: "should handle absolute URLs with base path", + base: "https://example.com/base", + segments: ["api", "v1"], + expected: "https://example.com/base/api/v1", + }, + { + description: "should preserve URL query parameters", + base: "https://example.com?query=1", + segments: ["api"], + expected: "https://example.com/api?query=1", + }, + { + description: "should preserve URL fragments", + base: "https://example.com#fragment", + segments: ["api"], + expected: "https://example.com/api#fragment", + }, + { + description: "should preserve URL query and fragments", + base: "https://example.com?query=1#fragment", + segments: ["api"], + expected: "https://example.com/api?query=1#fragment", + }, + { + description: "should handle http protocol", + base: "http://example.com", + segments: ["api"], + expected: "http://example.com/api", + }, + { + description: "should handle ftp protocol", + base: "ftp://example.com", + segments: ["files"], + expected: "ftp://example.com/files", + }, + { + description: "should handle ws protocol", + base: "ws://example.com", + segments: ["socket"], + expected: "ws://example.com/socket", + }, + { + description: "should fallback to path joining for malformed URLs", + base: "not-a-url://", + segments: ["path"], + expected: "not-a-url:///path", + }, + ]; + + urlTests.forEach(({ description, base, segments, expected }) => { + it(description, () => { + expect(join(base, ...segments)).toBe(expected); + }); + }); + }); + + describe("edge cases", () => { + const edgeCaseTests: TestCase[] = [ + { + description: "should handle empty segments", + base: "base", + segments: ["", "path"], + expected: "base/path", + }, + { + description: "should handle null segments", + base: "base", + segments: [null as any, "path"], + expected: "base/path", + }, + { + description: "should handle undefined segments", + base: "base", + segments: [undefined as any, "path"], + expected: "base/path", + }, + { + description: "should handle segments with only single slash", + base: "base", + segments: ["/", "path"], + expected: "base/path", + }, + { + description: "should handle segments with only double slash", + base: "base", + segments: ["//", "path"], + expected: "base/path", + }, + { + description: "should handle base paths with trailing slashes", + base: "base/", + segments: ["path"], + expected: "base/path", + }, + { + description: "should handle complex nested paths", + base: "api/v1/", + segments: ["/users/", "/123/", "/profile"], + expected: "api/v1/users/123/profile", + }, + ]; + + edgeCaseTests.forEach(({ description, base, segments, expected }) => { + it(description, () => { + expect(join(base, ...segments)).toBe(expected); + }); + }); + }); + + describe("real-world scenarios", () => { + const realWorldTests: TestCase[] = [ + { + description: "should handle API endpoint construction", + base: "https://api.example.com/v1", + segments: ["users", "123", "posts"], + expected: "https://api.example.com/v1/users/123/posts", + }, + { + description: "should handle file path construction", + base: "/var/www", + segments: ["html", "assets", "images"], + expected: "/var/www/html/assets/images", + }, + { + description: "should handle relative path construction", + base: "../parent", + segments: ["child", "grandchild"], + expected: "../parent/child/grandchild", + }, + { + description: "should handle Windows-style paths", + base: "C:\\Users", + segments: ["Documents", "file.txt"], + expected: "C:\\Users/Documents/file.txt", + }, + ]; + + realWorldTests.forEach(({ description, base, segments, expected }) => { + it(description, () => { + expect(join(base, ...segments)).toBe(expected); + }); + }); + }); + + describe("performance scenarios", () => { + it("should handle many segments efficiently", () => { + const segments = Array(100).fill("segment"); + const result = join("base", ...segments); + expect(result).toBe(`base/${segments.join("/")}`); + }); + + it("should handle long URLs", () => { + const longPath = "a".repeat(1000); + expect(join("https://example.com", longPath)).toBe(`https://example.com/${longPath}`); + }); + }); + + describe("trailing slash preservation", () => { + const trailingSlashTests: TestCase[] = [ + { + description: + "should preserve trailing slash on final result when base has trailing slash and no segments", + base: "https://api.example.com/", + segments: [], + expected: "https://api.example.com/", + }, + { + description: "should preserve trailing slash on v1 path", + base: "https://api.example.com/v1/", + segments: [], + expected: "https://api.example.com/v1/", + }, + { + description: "should preserve trailing slash when last segment has trailing slash", + base: "https://api.example.com", + segments: ["users/"], + expected: "https://api.example.com/users/", + }, + { + description: "should preserve trailing slash with relative path", + base: "api/v1", + segments: ["users/"], + expected: "api/v1/users/", + }, + { + description: "should preserve trailing slash with multiple segments", + base: "https://api.example.com", + segments: ["v1", "collections/"], + expected: "https://api.example.com/v1/collections/", + }, + { + description: "should preserve trailing slash with base path", + base: "base", + segments: ["path1", "path2/"], + expected: "base/path1/path2/", + }, + ]; + + trailingSlashTests.forEach(({ description, base, segments, expected }) => { + it(description, () => { + expect(join(base, ...segments)).toBe(expected); + }); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/unit/url/qs.test.ts b/seed/ts-sdk/basic-auth-optional/tests/unit/url/qs.test.ts new file mode 100644 index 000000000000..42cdffb9e5ea --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/unit/url/qs.test.ts @@ -0,0 +1,278 @@ +import { toQueryString } from "../../../src/core/url/index"; + +describe("Test qs toQueryString", () => { + interface BasicTestCase { + description: string; + input: any; + expected: string; + } + + describe("Basic functionality", () => { + const basicTests: BasicTestCase[] = [ + { description: "should return empty string for null", input: null, expected: "" }, + { description: "should return empty string for undefined", input: undefined, expected: "" }, + { description: "should return empty string for string primitive", input: "hello", expected: "" }, + { description: "should return empty string for number primitive", input: 42, expected: "" }, + { description: "should return empty string for true boolean", input: true, expected: "" }, + { description: "should return empty string for false boolean", input: false, expected: "" }, + { description: "should handle empty objects", input: {}, expected: "" }, + { + description: "should handle simple key-value pairs", + input: { name: "John", age: 30 }, + expected: "name=John&age=30", + }, + ]; + + basicTests.forEach(({ description, input, expected }) => { + it(description, () => { + expect(toQueryString(input)).toBe(expected); + }); + }); + }); + + describe("Array handling", () => { + interface ArrayTestCase { + description: string; + input: any; + options?: { arrayFormat?: "repeat" | "indices" }; + expected: string; + } + + const arrayTests: ArrayTestCase[] = [ + { + description: "should handle arrays with indices format (default)", + input: { items: ["a", "b", "c"] }, + expected: "items%5B0%5D=a&items%5B1%5D=b&items%5B2%5D=c", + }, + { + description: "should handle arrays with repeat format", + input: { items: ["a", "b", "c"] }, + options: { arrayFormat: "repeat" }, + expected: "items=a&items=b&items=c", + }, + { + description: "should handle empty arrays", + input: { items: [] }, + expected: "", + }, + { + description: "should handle arrays with mixed types", + input: { mixed: ["string", 42, true, false] }, + expected: "mixed%5B0%5D=string&mixed%5B1%5D=42&mixed%5B2%5D=true&mixed%5B3%5D=false", + }, + { + description: "should handle arrays with objects", + input: { users: [{ name: "John" }, { name: "Jane" }] }, + expected: "users%5B0%5D%5Bname%5D=John&users%5B1%5D%5Bname%5D=Jane", + }, + { + description: "should handle arrays with objects in repeat format", + input: { users: [{ name: "John" }, { name: "Jane" }] }, + options: { arrayFormat: "repeat" }, + expected: "users%5Bname%5D=John&users%5Bname%5D=Jane", + }, + ]; + + arrayTests.forEach(({ description, input, options, expected }) => { + it(description, () => { + expect(toQueryString(input, options)).toBe(expected); + }); + }); + }); + + describe("Nested objects", () => { + const nestedTests: BasicTestCase[] = [ + { + description: "should handle nested objects", + input: { user: { name: "John", age: 30 } }, + expected: "user%5Bname%5D=John&user%5Bage%5D=30", + }, + { + description: "should handle deeply nested objects", + input: { user: { profile: { name: "John", settings: { theme: "dark" } } } }, + expected: "user%5Bprofile%5D%5Bname%5D=John&user%5Bprofile%5D%5Bsettings%5D%5Btheme%5D=dark", + }, + { + description: "should handle empty nested objects", + input: { user: {} }, + expected: "", + }, + ]; + + nestedTests.forEach(({ description, input, expected }) => { + it(description, () => { + expect(toQueryString(input)).toBe(expected); + }); + }); + }); + + describe("Encoding", () => { + interface EncodingTestCase { + description: string; + input: any; + options?: { encode?: boolean }; + expected: string; + } + + const encodingTests: EncodingTestCase[] = [ + { + description: "should encode by default", + input: { name: "John Doe", email: "john@example.com" }, + expected: "name=John%20Doe&email=john%40example.com", + }, + { + description: "should not encode when encode is false", + input: { name: "John Doe", email: "john@example.com" }, + options: { encode: false }, + expected: "name=John Doe&email=john@example.com", + }, + { + description: "should encode special characters in keys", + input: { "user name": "John", "email[primary]": "john@example.com" }, + expected: "user%20name=John&email%5Bprimary%5D=john%40example.com", + }, + { + description: "should not encode special characters in keys when encode is false", + input: { "user name": "John", "email[primary]": "john@example.com" }, + options: { encode: false }, + expected: "user name=John&email[primary]=john@example.com", + }, + ]; + + encodingTests.forEach(({ description, input, options, expected }) => { + it(description, () => { + expect(toQueryString(input, options)).toBe(expected); + }); + }); + }); + + describe("Mixed scenarios", () => { + interface MixedTestCase { + description: string; + input: any; + options?: { arrayFormat?: "repeat" | "indices" }; + expected: string; + } + + const mixedTests: MixedTestCase[] = [ + { + description: "should handle complex nested structures", + input: { + filters: { + status: ["active", "pending"], + category: { + type: "electronics", + subcategories: ["phones", "laptops"], + }, + }, + sort: { field: "name", direction: "asc" }, + }, + expected: + "filters%5Bstatus%5D%5B0%5D=active&filters%5Bstatus%5D%5B1%5D=pending&filters%5Bcategory%5D%5Btype%5D=electronics&filters%5Bcategory%5D%5Bsubcategories%5D%5B0%5D=phones&filters%5Bcategory%5D%5Bsubcategories%5D%5B1%5D=laptops&sort%5Bfield%5D=name&sort%5Bdirection%5D=asc", + }, + { + description: "should handle complex nested structures with repeat format", + input: { + filters: { + status: ["active", "pending"], + category: { + type: "electronics", + subcategories: ["phones", "laptops"], + }, + }, + sort: { field: "name", direction: "asc" }, + }, + options: { arrayFormat: "repeat" }, + expected: + "filters%5Bstatus%5D=active&filters%5Bstatus%5D=pending&filters%5Bcategory%5D%5Btype%5D=electronics&filters%5Bcategory%5D%5Bsubcategories%5D=phones&filters%5Bcategory%5D%5Bsubcategories%5D=laptops&sort%5Bfield%5D=name&sort%5Bdirection%5D=asc", + }, + { + description: "should handle arrays with null/undefined values", + input: { items: ["a", null, "c", undefined, "e"] }, + expected: "items%5B0%5D=a&items%5B1%5D=&items%5B2%5D=c&items%5B4%5D=e", + }, + { + description: "should handle objects with null/undefined values", + input: { name: "John", age: null, email: undefined, active: true }, + expected: "name=John&age=&active=true", + }, + ]; + + mixedTests.forEach(({ description, input, options, expected }) => { + it(description, () => { + expect(toQueryString(input, options)).toBe(expected); + }); + }); + }); + + describe("Edge cases", () => { + const edgeCaseTests: BasicTestCase[] = [ + { + description: "should handle numeric keys", + input: { "0": "zero", "1": "one" }, + expected: "0=zero&1=one", + }, + { + description: "should handle boolean values in objects", + input: { enabled: true, disabled: false }, + expected: "enabled=true&disabled=false", + }, + { + description: "should handle empty strings", + input: { name: "", description: "test" }, + expected: "name=&description=test", + }, + { + description: "should handle zero values", + input: { count: 0, price: 0.0 }, + expected: "count=0&price=0", + }, + { + description: "should handle arrays with empty strings", + input: { items: ["a", "", "c"] }, + expected: "items%5B0%5D=a&items%5B1%5D=&items%5B2%5D=c", + }, + ]; + + edgeCaseTests.forEach(({ description, input, expected }) => { + it(description, () => { + expect(toQueryString(input)).toBe(expected); + }); + }); + }); + + describe("Options combinations", () => { + interface OptionsTestCase { + description: string; + input: any; + options?: { arrayFormat?: "repeat" | "indices"; encode?: boolean }; + expected: string; + } + + const optionsTests: OptionsTestCase[] = [ + { + description: "should respect both arrayFormat and encode options", + input: { items: ["a & b", "c & d"] }, + options: { arrayFormat: "repeat", encode: false }, + expected: "items=a & b&items=c & d", + }, + { + description: "should use default options when none provided", + input: { items: ["a", "b"] }, + expected: "items%5B0%5D=a&items%5B1%5D=b", + }, + { + description: "should merge provided options with defaults", + input: { items: ["a", "b"], name: "John Doe" }, + options: { encode: false }, + expected: "items[0]=a&items[1]=b&name=John Doe", + }, + ]; + + optionsTests.forEach(({ description, input, options, expected }) => { + it(description, () => { + expect(toQueryString(input, options)).toBe(expected); + }); + }); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tests/wire/.gitkeep b/seed/ts-sdk/basic-auth-optional/tests/wire/.gitkeep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/seed/ts-sdk/basic-auth-optional/tests/wire/basicAuth.test.ts b/seed/ts-sdk/basic-auth-optional/tests/wire/basicAuth.test.ts new file mode 100644 index 000000000000..325548c10c6b --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tests/wire/basicAuth.test.ts @@ -0,0 +1,114 @@ +// This file was auto-generated by Fern from our API Definition. + +import * as SeedBasicAuthOptional from "../../src/api/index"; +import { SeedBasicAuthOptionalClient } from "../../src/Client"; +import { mockServerPool } from "../mock-server/MockServerPool"; + +describe("BasicAuthClient", () => { + test("getWithBasicAuth (1)", async () => { + const server = mockServerPool.createServer(); + const client = new SeedBasicAuthOptionalClient({ + maxRetries: 0, + username: "test", + password: "test", + environment: server.baseUrl, + }); + + const rawResponseBody = true; + + server.mockEndpoint().get("/basic-auth").respondWith().statusCode(200).jsonBody(rawResponseBody).build(); + + const response = await client.basicAuth.getWithBasicAuth(); + expect(response).toEqual(true); + }); + + test("getWithBasicAuth (2)", async () => { + const server = mockServerPool.createServer(); + const client = new SeedBasicAuthOptionalClient({ + maxRetries: 0, + username: "test", + password: "test", + environment: server.baseUrl, + }); + + const rawResponseBody = { message: "message" }; + + server.mockEndpoint().get("/basic-auth").respondWith().statusCode(401).jsonBody(rawResponseBody).build(); + + await expect(async () => { + return await client.basicAuth.getWithBasicAuth(); + }).rejects.toThrow(SeedBasicAuthOptional.UnauthorizedRequest); + }); + + test("postWithBasicAuth (1)", async () => { + const server = mockServerPool.createServer(); + const client = new SeedBasicAuthOptionalClient({ + maxRetries: 0, + username: "test", + password: "test", + environment: server.baseUrl, + }); + const rawRequestBody = { key: "value" }; + const rawResponseBody = true; + + server + .mockEndpoint() + .post("/basic-auth") + .jsonBody(rawRequestBody) + .respondWith() + .statusCode(200) + .jsonBody(rawResponseBody) + .build(); + + const response = await client.basicAuth.postWithBasicAuth({ + key: "value", + }); + expect(response).toEqual(true); + }); + + test("postWithBasicAuth (2)", async () => { + const server = mockServerPool.createServer(); + const client = new SeedBasicAuthOptionalClient({ + maxRetries: 0, + username: "test", + password: "test", + environment: server.baseUrl, + }); + const rawRequestBody = { key: "value" }; + const rawResponseBody = { message: "message" }; + + server + .mockEndpoint() + .post("/basic-auth") + .jsonBody(rawRequestBody) + .respondWith() + .statusCode(401) + .jsonBody(rawResponseBody) + .build(); + + await expect(async () => { + return await client.basicAuth.postWithBasicAuth({ + key: "value", + }); + }).rejects.toThrow(SeedBasicAuthOptional.UnauthorizedRequest); + }); + + test("postWithBasicAuth (3)", async () => { + const server = mockServerPool.createServer(); + const client = new SeedBasicAuthOptionalClient({ + maxRetries: 0, + username: "test", + password: "test", + environment: server.baseUrl, + }); + const rawRequestBody = { key: "value" }; + + server.mockEndpoint().post("/basic-auth").jsonBody(rawRequestBody).respondWith().statusCode(400).build(); + + await expect(async () => { + return await client.basicAuth.postWithBasicAuth({ + key: "value", + }); + }).rejects.toThrow(SeedBasicAuthOptional.BadRequest); + }); +}); diff --git a/seed/ts-sdk/basic-auth-optional/tsconfig.base.json b/seed/ts-sdk/basic-auth-optional/tsconfig.base.json new file mode 100644 index 000000000000..93a92c0630b5 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tsconfig.base.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "extendedDiagnostics": true, + "strict": true, + "target": "ES6", + "moduleResolution": "node", + "esModuleInterop": true, + "skipLibCheck": true, + "declaration": true, + "outDir": "dist", + "rootDir": "src", + "isolatedModules": true, + "isolatedDeclarations": true + }, + "include": ["src"], + "exclude": [] +} diff --git a/seed/ts-sdk/basic-auth-optional/tsconfig.cjs.json b/seed/ts-sdk/basic-auth-optional/tsconfig.cjs.json new file mode 100644 index 000000000000..5c11446f5984 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tsconfig.cjs.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "module": "CommonJS", + "outDir": "dist/cjs" + }, + "include": ["src"], + "exclude": [] +} diff --git a/seed/ts-sdk/basic-auth-optional/tsconfig.esm.json b/seed/ts-sdk/basic-auth-optional/tsconfig.esm.json new file mode 100644 index 000000000000..6ce909748b2c --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tsconfig.esm.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "module": "esnext", + "outDir": "dist/esm", + "verbatimModuleSyntax": true + }, + "include": ["src"], + "exclude": [] +} diff --git a/seed/ts-sdk/basic-auth-optional/tsconfig.json b/seed/ts-sdk/basic-auth-optional/tsconfig.json new file mode 100644 index 000000000000..d77fdf00d259 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "./tsconfig.cjs.json" +} diff --git a/seed/ts-sdk/basic-auth-optional/vitest.config.mts b/seed/ts-sdk/basic-auth-optional/vitest.config.mts new file mode 100644 index 000000000000..0dee5a752d39 --- /dev/null +++ b/seed/ts-sdk/basic-auth-optional/vitest.config.mts @@ -0,0 +1,32 @@ +import { defineConfig } from "vitest/config"; +export default defineConfig({ + test: { + typecheck: { + enabled: true, + tsconfig: "./tests/tsconfig.json", + }, + projects: [ + { + test: { + globals: true, + name: "unit", + environment: "node", + root: "./tests", + include: ["**/*.test.{js,ts,jsx,tsx}"], + exclude: ["wire/**"], + setupFiles: ["./setup.ts"], + }, + }, + { + test: { + globals: true, + name: "wire", + environment: "node", + root: "./tests/wire", + setupFiles: ["../setup.ts", "../mock-server/setup.ts"], + }, + }, + ], + passWithNoTests: true, + }, +}); diff --git a/test-definitions/fern/apis/basic-auth-optional/definition/api.yml b/test-definitions/fern/apis/basic-auth-optional/definition/api.yml new file mode 100644 index 000000000000..8b1d72b0b769 --- /dev/null +++ b/test-definitions/fern/apis/basic-auth-optional/definition/api.yml @@ -0,0 +1,12 @@ +name: basic-auth-optional +auth: Basic +auth-schemes: + Basic: + scheme: basic + username: + name: username + password: + name: password + omit: true +error-discrimination: + strategy: status-code diff --git a/test-definitions/fern/apis/basic-auth-optional/definition/basic-auth.yml b/test-definitions/fern/apis/basic-auth-optional/definition/basic-auth.yml new file mode 100644 index 000000000000..6a21fd56c9f9 --- /dev/null +++ b/test-definitions/fern/apis/basic-auth-optional/definition/basic-auth.yml @@ -0,0 +1,39 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/fern-api/fern/main/fern.schema.json + +imports: + errors: ./errors.yml + +service: + auth: false + base-path: "" + endpoints: + getWithBasicAuth: + auth: true + docs: GET request with basic auth scheme + path: /basic-auth + method: GET + response: + boolean + examples: + - response: + body: true + errors: + - errors.UnauthorizedRequest + + postWithBasicAuth: + auth: true + docs: POST request with basic auth scheme + path: /basic-auth + method: POST + request: + name: PostWithBasicAuth + body: unknown + response: boolean + examples: + - request: + key: "value" + response: + body: true + errors: + - errors.UnauthorizedRequest + - errors.BadRequest diff --git a/test-definitions/fern/apis/basic-auth-optional/definition/errors.yml b/test-definitions/fern/apis/basic-auth-optional/definition/errors.yml new file mode 100644 index 000000000000..cdd6a9667031 --- /dev/null +++ b/test-definitions/fern/apis/basic-auth-optional/definition/errors.yml @@ -0,0 +1,11 @@ +errors: + UnauthorizedRequest: + status-code: 401 + type: UnauthorizedRequestErrorBody + BadRequest: + status-code: 400 + +types: + UnauthorizedRequestErrorBody: + properties: + message: string diff --git a/test-definitions/fern/apis/basic-auth-optional/generators.yml b/test-definitions/fern/apis/basic-auth-optional/generators.yml new file mode 100644 index 000000000000..b30d6ed97cd2 --- /dev/null +++ b/test-definitions/fern/apis/basic-auth-optional/generators.yml @@ -0,0 +1,22 @@ +# yaml-language-server: $schema=https://schema.buildwithfern.dev/generators-yml.json +groups: + php-sdk: + generators: + - name: fernapi/fern-php-sdk + version: latest + ir-version: v61 + github: + token: ${GITHUB_TOKEN} + mode: push + uri: fern-api/php-sdk-tests + branch: basic-auth-optional + go-sdk: + generators: + - name: fernapi/fern-go-sdk + version: latest + ir-version: v61 + github: + token: ${GITHUB_TOKEN} + mode: push + uri: fern-api/go-sdk-tests + branch: basic-auth-optional From 69540a397209e8eed42ba9993dad2c1353fa510d Mon Sep 17 00:00:00 2001 From: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com> Date: Tue, 31 Mar 2026 21:29:28 +0000 Subject: [PATCH 5/6] fix: make TypeScript AuthOptions type optional when usernameOmit/passwordOmit is set Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../BasicAuthProviderGenerator.ts | 12 ++++----- .../src/auth/BasicAuthProvider.ts | 5 +++- .../basic-auth/src/core/auth/BasicAuth.ts | 11 +++++--- .../tests/unit/auth/BasicAuth.test.ts | 26 ++++++++++++++++--- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts b/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts index e63b4c6f5629..ecae7df08b38 100644 --- a/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts +++ b/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts @@ -91,23 +91,23 @@ export class BasicAuthProviderGenerator implements AuthProviderGenerator { public getAuthOptionsProperties(context: SdkContext): OptionalKind[] | undefined { const hasUsernameEnv = this.authScheme.usernameEnvVar != null; const hasPasswordEnv = this.authScheme.passwordEnvVar != null; - const isUsernameOptional = !this.isAuthMandatory || hasUsernameEnv; - const isPasswordOptional = !this.isAuthMandatory || hasPasswordEnv; + const isUsernameOptional = !this.isAuthMandatory || hasUsernameEnv || this.authScheme.usernameOmit === true; + const isPasswordOptional = !this.isAuthMandatory || hasPasswordEnv || this.authScheme.passwordOmit === true; - // When there's an env var fallback, use Supplier | undefined because the supplier itself can be undefined + // When there's an env var fallback or omit flag, use Supplier | undefined because the supplier itself can be undefined // When there's no env var fallback, use Supplier directly. const stringType = ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword); const supplierType = context.coreUtilities.fetcher.SupplierOrEndpointSupplier._getReferenceToType(stringType); - // For env var fallback: prop?: Supplier | undefined - const usernamePropertyType = hasUsernameEnv + // For env var fallback or omit: prop?: Supplier | undefined + const usernamePropertyType = hasUsernameEnv || this.authScheme.usernameOmit === true ? ts.factory.createUnionTypeNode([ supplierType, ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword) ]) : supplierType; - const passwordPropertyType = hasPasswordEnv + const passwordPropertyType = hasPasswordEnv || this.authScheme.passwordOmit === true ? ts.factory.createUnionTypeNode([ supplierType, ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword) diff --git a/seed/ts-sdk/basic-auth-optional/src/auth/BasicAuthProvider.ts b/seed/ts-sdk/basic-auth-optional/src/auth/BasicAuthProvider.ts index 41cfc59d8e76..42a7704af880 100644 --- a/seed/ts-sdk/basic-auth-optional/src/auth/BasicAuthProvider.ts +++ b/seed/ts-sdk/basic-auth-optional/src/auth/BasicAuthProvider.ts @@ -50,7 +50,10 @@ export namespace BasicAuthProvider { export const AUTH_CONFIG_ERROR_MESSAGE_PASSWORD: string = `Please provide '${PASSWORD_PARAM}' when initializing the client` as const; export type Options = AuthOptions; - export type AuthOptions = { [USERNAME_PARAM]: core.Supplier; [PASSWORD_PARAM]: core.Supplier }; + export type AuthOptions = { + [USERNAME_PARAM]: core.Supplier; + [PASSWORD_PARAM]?: core.Supplier | undefined; + }; export function createInstance(options: Options): core.AuthProvider { return new BasicAuthProvider(options); diff --git a/seed/ts-sdk/basic-auth/src/core/auth/BasicAuth.ts b/seed/ts-sdk/basic-auth/src/core/auth/BasicAuth.ts index a64235910062..f34fca5cc4dd 100644 --- a/seed/ts-sdk/basic-auth/src/core/auth/BasicAuth.ts +++ b/seed/ts-sdk/basic-auth/src/core/auth/BasicAuth.ts @@ -1,8 +1,8 @@ import { base64Decode, base64Encode } from "../base64.js"; export interface BasicAuth { - username: string; - password: string; + username?: string; + password?: string; } const BASIC_AUTH_HEADER_PREFIX = /^Basic /i; @@ -12,7 +12,12 @@ export const BasicAuth = { if (basicAuth == null) { return undefined; } - const token = base64Encode(`${basicAuth.username}:${basicAuth.password}`); + const username = basicAuth.username ?? ""; + const password = basicAuth.password ?? ""; + if (username === "" && password === "") { + return undefined; + } + const token = base64Encode(`${username}:${password}`); return `Basic ${token}`; }, fromAuthorizationHeader: (header: string): BasicAuth => { diff --git a/seed/ts-sdk/basic-auth/tests/unit/auth/BasicAuth.test.ts b/seed/ts-sdk/basic-auth/tests/unit/auth/BasicAuth.test.ts index 9b5123364c47..8c82c1b723db 100644 --- a/seed/ts-sdk/basic-auth/tests/unit/auth/BasicAuth.test.ts +++ b/seed/ts-sdk/basic-auth/tests/unit/auth/BasicAuth.test.ts @@ -3,8 +3,8 @@ import { BasicAuth } from "../../../src/core/auth/BasicAuth"; describe("BasicAuth", () => { interface ToHeaderTestCase { description: string; - input: { username: string; password: string }; - expected: string; + input: { username?: string; password?: string }; + expected: string | undefined; } interface FromHeaderTestCase { @@ -22,10 +22,30 @@ describe("BasicAuth", () => { describe("toAuthorizationHeader", () => { const toHeaderTests: ToHeaderTestCase[] = [ { - description: "correctly converts to header", + description: "correctly converts to header with both username and password", input: { username: "username", password: "password" }, expected: "Basic dXNlcm5hbWU6cGFzc3dvcmQ=", }, + { + description: "encodes username only with trailing colon", + input: { username: "username" }, + expected: "Basic dXNlcm5hbWU6", + }, + { + description: "encodes password only with leading colon", + input: { password: "password" }, + expected: "Basic OnBhc3N3b3Jk", + }, + { + description: "returns undefined when neither provided", + input: {}, + expected: undefined, + }, + { + description: "returns undefined when both are empty strings", + input: { username: "", password: "" }, + expected: undefined, + }, ]; toHeaderTests.forEach(({ description, input, expected }) => { From 176ccd444bbc17c4eb38ba0c5e9b1655339c52e0 Mon Sep 17 00:00:00 2001 From: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com> Date: Tue, 31 Mar 2026 21:51:06 +0000 Subject: [PATCH 6/6] fix: biome formatting and update IR-to-JSON-schema snapshot for basic-auth-optional Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../BasicAuthProviderGenerator.ts | 28 ++++++++++--------- ...e_errors_UnauthorizedRequestErrorBody.json | 13 +++++++++ 2 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 packages/cli/fern-definition/ir-to-jsonschema/src/__test__/__snapshots__/basic-auth-optional/type_errors_UnauthorizedRequestErrorBody.json diff --git a/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts b/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts index ecae7df08b38..f18d3c644fe0 100644 --- a/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts +++ b/generators/typescript/sdk/client-class-generator/src/auth-provider/BasicAuthProviderGenerator.ts @@ -100,19 +100,21 @@ export class BasicAuthProviderGenerator implements AuthProviderGenerator { const supplierType = context.coreUtilities.fetcher.SupplierOrEndpointSupplier._getReferenceToType(stringType); // For env var fallback or omit: prop?: Supplier | undefined - const usernamePropertyType = hasUsernameEnv || this.authScheme.usernameOmit === true - ? ts.factory.createUnionTypeNode([ - supplierType, - ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword) - ]) - : supplierType; - - const passwordPropertyType = hasPasswordEnv || this.authScheme.passwordOmit === true - ? ts.factory.createUnionTypeNode([ - supplierType, - ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword) - ]) - : supplierType; + const usernamePropertyType = + hasUsernameEnv || this.authScheme.usernameOmit === true + ? ts.factory.createUnionTypeNode([ + supplierType, + ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword) + ]) + : supplierType; + + const passwordPropertyType = + hasPasswordEnv || this.authScheme.passwordOmit === true + ? ts.factory.createUnionTypeNode([ + supplierType, + ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword) + ]) + : supplierType; return [ { diff --git a/packages/cli/fern-definition/ir-to-jsonschema/src/__test__/__snapshots__/basic-auth-optional/type_errors_UnauthorizedRequestErrorBody.json b/packages/cli/fern-definition/ir-to-jsonschema/src/__test__/__snapshots__/basic-auth-optional/type_errors_UnauthorizedRequestErrorBody.json new file mode 100644 index 000000000000..f50ccac10d76 --- /dev/null +++ b/packages/cli/fern-definition/ir-to-jsonschema/src/__test__/__snapshots__/basic-auth-optional/type_errors_UnauthorizedRequestErrorBody.json @@ -0,0 +1,13 @@ +{ + "type": "object", + "properties": { + "message": { + "type": "string" + } + }, + "required": [ + "message" + ], + "additionalProperties": false, + "definitions": {} +} \ No newline at end of file