Skip to content

Commit

Permalink
chore(experimentalIdentityAndAuth): refactor HttpAuthScheme propert…
Browse files Browse the repository at this point in the history
…ies to builders (#941)
  • Loading branch information
Steven Yuan authored Sep 18, 2023
1 parent 5b871b3 commit abdf6ce
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@
package software.amazon.smithy.typescript.codegen;

import java.util.function.Consumer;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.SmithyUnstableApi;
import software.amazon.smithy.utils.ToSmithyBuilder;

/**
* Definition of a Config field.
*
* Currently used to populate the ClientDefaults interface in `experimentalIdentityAndAuth`.
*
* @param name name of the config field
* @param source writer for the type of the config field
* @param type whether the config field is main or auxiliary
* @param inputType writer for the input type of the config field
* @param resolvedType writer for the resolved type of the config field
* @param docs writer for the docs of the config field
*/
@SmithyUnstableApi
public final record ConfigField(
String name,
Type type,
Consumer<TypeScriptWriter> source,
Consumer<TypeScriptWriter> inputType,
Consumer<TypeScriptWriter> resolvedType,
Consumer<TypeScriptWriter> docs
) {
) implements ToSmithyBuilder<ConfigField> {
/**
* Defines the type of the config field.
*/
Expand All @@ -38,4 +43,61 @@ public enum Type {
*/
AUXILIARY
}

public static Builder builder() {
return new Builder();
}

@Override
public Builder toBuilder() {
return builder()
.name(name)
.type(type)
.inputType(inputType)
.resolvedType(resolvedType)
.docs(docs);
}

public static final class Builder implements SmithyBuilder<ConfigField> {
private String name;
private Type type;
private Consumer<TypeScriptWriter> inputType;
private Consumer<TypeScriptWriter> resolvedType;
private Consumer<TypeScriptWriter> docs;

@Override
public ConfigField build() {
return new ConfigField(
SmithyBuilder.requiredState("name", name),
SmithyBuilder.requiredState("type", type),
SmithyBuilder.requiredState("inputType", inputType),
SmithyBuilder.requiredState("resolvedType", resolvedType),
SmithyBuilder.requiredState("docs", docs));
}

public Builder name(String name) {
this.name = name;
return this;
}

public Builder type(Type type) {
this.type = type;
return this;
}

public Builder inputType(Consumer<TypeScriptWriter> inputType) {
this.inputType = inputType;
return this;
}

public Builder resolvedType(Consumer<TypeScriptWriter> resolvedType) {
this.resolvedType = resolvedType;
return this;
}

public Builder docs(Consumer<TypeScriptWriter> docs) {
this.docs = docs;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private void generateClientDefaults() {
httpAuthIntegration.getHttpAuthScheme().ifPresent(authScheme -> {
for (ConfigField configField : authScheme.getConfigFields()) {
writer.writeDocs(() -> writer.write("$C", configField.docs()));
writer.write("$L?: $C;\n", configField.name(), configField.source());
writer.write("$L?: $C;\n", configField.name(), configField.inputType());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import java.util.function.Function;
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.SmithyUnstableApi;
import software.amazon.smithy.utils.ToSmithyBuilder;

/**
* Definition of an HttpAuthOptionProperty.
Expand All @@ -24,7 +26,7 @@ public final record HttpAuthOptionProperty(
String name,
Type type,
Function<Trait, Consumer<TypeScriptWriter>> source
) {
) implements ToSmithyBuilder<HttpAuthOptionProperty> {
/**
* Defines the type of the auth option property.
*/
Expand All @@ -38,4 +40,45 @@ public enum Type {
*/
SIGNING
}

public static Builder builder() {
return new Builder();
}

@Override
public SmithyBuilder<HttpAuthOptionProperty> toBuilder() {
return builder()
.name(name)
.type(type)
.source(source);
}

public static final class Builder implements SmithyBuilder<HttpAuthOptionProperty> {
private String name;
private Type type;
private Function<Trait, Consumer<TypeScriptWriter>> source;

@Override
public HttpAuthOptionProperty build() {
return new HttpAuthOptionProperty(
SmithyBuilder.requiredState("name", name),
SmithyBuilder.requiredState("type", type),
SmithyBuilder.requiredState("source", source));
}

public Builder name(String name) {
this.name = name;
return this;
}

public Builder type(Type type) {
this.type = type;
return this;
}

public Builder source(Function<Trait, Consumer<TypeScriptWriter>> source) {
this.source = source;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import java.util.function.Consumer;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.SmithyUnstableApi;
import software.amazon.smithy.utils.ToSmithyBuilder;

/**
* Definition of an HttpAuthSchemeParameter.
Expand All @@ -23,4 +25,46 @@ public final record HttpAuthSchemeParameter(
String name,
Consumer<TypeScriptWriter> type,
Consumer<TypeScriptWriter> source
) {}
) implements ToSmithyBuilder<HttpAuthSchemeParameter> {

public static Builder builder() {
return new Builder();
}

@Override
public SmithyBuilder<HttpAuthSchemeParameter> toBuilder() {
return builder()
.name(name)
.type(type)
.source(source);
}

public static final class Builder implements SmithyBuilder<HttpAuthSchemeParameter> {
private String name;
private Consumer<TypeScriptWriter> type;
private Consumer<TypeScriptWriter> source;

@Override
public HttpAuthSchemeParameter build() {
return new HttpAuthSchemeParameter(
SmithyBuilder.requiredState("name", name),
SmithyBuilder.requiredState("type", type),
SmithyBuilder.requiredState("source", source));
}

public Builder name(String name) {
this.name = name;
return this;
}

public Builder type(Consumer<TypeScriptWriter> type) {
this.type = type;
return this;
}

public Builder source(Consumer<TypeScriptWriter> source) {
this.source = source;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,61 @@ public Optional<HttpAuthScheme> getHttpAuthScheme() {
return Optional.of(HttpAuthScheme.builder()
.schemeId(HttpApiKeyAuthTrait.ID)
.applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol())
.addConfigField(new ConfigField("apiKey", ConfigField.Type.MAIN, w -> {
w.addImport("ApiKeyIdentity", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("ApiKeyIdentityProvider", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.write("ApiKeyIdentity | ApiKeyIdentityProvider");
}, w -> w.write("The API key to use when making requests.")))
.addHttpAuthOptionProperty(new HttpAuthOptionProperty(
"name", HttpAuthOptionProperty.Type.SIGNING, t -> w -> {
HttpApiKeyAuthTrait httpApiKeyAuthTrait = (HttpApiKeyAuthTrait) t;
w.write("$S", httpApiKeyAuthTrait.getName());
}))
.addHttpAuthOptionProperty(new HttpAuthOptionProperty(
"in", HttpAuthOptionProperty.Type.SIGNING, t -> w -> {
w.addImport("HttpApiKeyAuthLocation", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
HttpApiKeyAuthTrait httpApiKeyAuthTrait = (HttpApiKeyAuthTrait) t;
if (httpApiKeyAuthTrait.getIn().equals(Location.HEADER)) {
w.write("HttpApiKeyAuthLocation.HEADER");
} else if (httpApiKeyAuthTrait.getIn().equals(Location.QUERY)) {
w.write("HttpApiKeyAuthLocation.QUERY");
} else {
throw new CodegenException("Encountered invalid `in` property on `@httpApiKeyAuth`: "
+ httpApiKeyAuthTrait.getIn());
}
}))
.addHttpAuthOptionProperty(new HttpAuthOptionProperty(
"scheme", HttpAuthOptionProperty.Type.SIGNING, t -> w -> {
HttpApiKeyAuthTrait httpApiKeyAuthTrait = (HttpApiKeyAuthTrait) t;
httpApiKeyAuthTrait.getScheme().ifPresentOrElse(
s -> w.write(s),
() -> w.write("undefined"));
}))
.addConfigField(ConfigField.builder()
.name("apiKey")
.type(ConfigField.Type.MAIN)
.docs(w -> w.write("The API key to use when making requests."))
.inputType(w -> {
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("ApiKeyIdentity", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("ApiKeyIdentityProvider", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.write("ApiKeyIdentity | ApiKeyIdentityProvider");
})
.resolvedType(w -> {
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("ApiKeyIdentityProvider", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.write("ApiKeyIdentityProvider");
})
.build())
.addHttpAuthOptionProperty(HttpAuthOptionProperty.builder()
.name("name")
.type(HttpAuthOptionProperty.Type.SIGNING)
.source(t -> w -> {
HttpApiKeyAuthTrait httpApiKeyAuthTrait = (HttpApiKeyAuthTrait) t;
w.write("$S", httpApiKeyAuthTrait.getName());
})
.build())
.addHttpAuthOptionProperty(HttpAuthOptionProperty.builder()
.name("in")
.type(HttpAuthOptionProperty.Type.SIGNING)
.source(t -> w -> {
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("HttpApiKeyAuthLocation", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
HttpApiKeyAuthTrait httpApiKeyAuthTrait = (HttpApiKeyAuthTrait) t;
if (httpApiKeyAuthTrait.getIn().equals(Location.HEADER)) {
w.write("HttpApiKeyAuthLocation.HEADER");
} else if (httpApiKeyAuthTrait.getIn().equals(Location.QUERY)) {
w.write("HttpApiKeyAuthLocation.QUERY");
} else {
throw new CodegenException("Encountered invalid `in` property on `@httpApiKeyAuth`: "
+ httpApiKeyAuthTrait.getIn());
}
})
.build())
.addHttpAuthOptionProperty(HttpAuthOptionProperty.builder()
.name("scheme")
.type(HttpAuthOptionProperty.Type.SIGNING)
.source(t -> w -> {
HttpApiKeyAuthTrait httpApiKeyAuthTrait = (HttpApiKeyAuthTrait) t;
httpApiKeyAuthTrait.getScheme().ifPresentOrElse(
s -> w.write(s),
() -> w.write("undefined"));
})
.build())
.putDefaultSigner(LanguageTarget.SHARED, HTTP_API_KEY_AUTH_SIGNER)
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,25 @@ public Optional<HttpAuthScheme> getHttpAuthScheme() {
return Optional.of(HttpAuthScheme.builder()
.schemeId(HttpBearerAuthTrait.ID)
.applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol())
.addConfigField(new ConfigField("token", Type.MAIN, w -> {
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("TokenIdentity", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("TokenIdentityProvider", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.write("TokenIdentity | TokenIdentityProvider");
}, w -> w.write("The token used to authenticate requests.")))
.addConfigField(ConfigField.builder()
.name("token")
.type(Type.MAIN)
.docs(w -> w.write("The token used to authenticate requests."))
.inputType(w -> {
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("TokenIdentity", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("TokenIdentityProvider", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.write("TokenIdentity | TokenIdentityProvider");
})
.resolvedType(w -> {
w.addDependency(TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.addImport("TokenIdentityProvider", null,
TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH);
w.write("TokenIdentityProvider");
})
.build())
.putDefaultSigner(LanguageTarget.SHARED, HTTP_BEARER_AUTH_SIGNER)
.build());
}
Expand Down
Loading

0 comments on commit abdf6ce

Please sign in to comment.