Skip to content

Commit

Permalink
Add new argument to sent User-Agent header
Browse files Browse the repository at this point in the history
  • Loading branch information
en-milie committed Oct 9, 2023
1 parent f8e42bd commit 1a05c59
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/endava/cats/args/ApiArguments.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.endava.cats.args;

import jakarta.inject.Singleton;
import lombok.Getter;
import lombok.Setter;
import picocli.CommandLine;

import jakarta.inject.Singleton;

/**
* Holds all arguments related to API details.
*/
Expand All @@ -32,6 +31,10 @@ public class ApiArguments {
defaultValue = "10")
private int readTimeout = 10;

@CommandLine.Option(names = {"--userAgent"},
description = "The user agent to be set in the User-Agent HTTP header. Default: @|bold,underline cats/${app.version}|@")
private String userAgent;

@Setter
@CommandLine.Option(names = {"-c", "--contract"},
description = "The OpenAPI contract")
Expand All @@ -53,4 +56,10 @@ public void validateRequired(CommandLine.Model.CommandSpec spec) {
throw new CommandLine.ParameterException(spec.commandLine(), "Missing required option --server=<server>");
}
}

public void setUserAgent(String version) {
if (userAgent == null) {
userAgent = "cats/" + version;
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/endava/cats/command/CatsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.Schema;
import jakarta.inject.Inject;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import picocli.AutoComplete;
Expand Down Expand Up @@ -122,6 +123,7 @@ public class CatsCommand implements Runnable, CommandLine.IExitCodeGenerator {
@Inject
VersionChecker versionChecker;

@Getter
@ConfigProperty(name = "quarkus.application.version", defaultValue = "1.0.0")
String appVersion;

Expand Down Expand Up @@ -306,6 +308,7 @@ void doEarlyOperations() throws IOException {
this.processLogLevelArgument();
filesArguments.loadConfig();
apiArguments.validateRequired(spec);
apiArguments.setUserAgent(appVersion);
}

private void processLogLevelArgument() {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/endava/cats/io/ServiceCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ private void addMandatoryHeaders(ServiceData data, List<KeyValuePair<String, Obj
data.getHeaders().forEach(header -> headers.add(new KeyValuePair<>(header.getName(), header.getValue())));
addIfNotPresent(HttpHeaders.ACCEPT, processingArguments.getDefaultContentType(), data, headers);
addIfNotPresent(HttpHeaders.CONTENT_TYPE, data.getContentType(), data, headers);
addIfNotPresent(HttpHeaders.USER_AGENT, apiArguments.getUserAgent(), data, headers);
}

private void addIfNotPresent(String headerName, String headerValue, ServiceData data, List<KeyValuePair<String, Object>> headers) {
Expand Down

0 comments on commit 1a05c59

Please sign in to comment.