Skip to content

[C#] add httpUserAgent option, add configurable user-agent support to C# #2371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public class Generate implements Runnable {

@Option(name = {"--release-version"}, title = "release version", description = CodegenConstants.RELEASE_VERSION_DESC)
private String releaseVersion;

@Option(name = {"--http-user-agent"}, title = "http user agent", description = CodegenConstants.HTTP_USER_AGENT)
private String httpUserAgent;

@Override
public void run() {
Expand Down Expand Up @@ -210,6 +213,10 @@ public void run() {
if (isNotEmpty(releaseVersion)) {
configurator.setReleaseVersion(releaseVersion);
}

if (isNotEmpty(httpUserAgent)) {
configurator.setHttpUserAgent(httpUserAgent);
}

applySystemPropertiesKvp(systemProperties, configurator);
applyInstantiationTypesKvp(instantiationTypes, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,8 @@ public interface CodegenConfig {

String getReleaseVersion();

void setHttpUserAgent(String httpUserAgent);

String getHttpUserAgent();

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ public static enum MODEL_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case
public static final String RELEASE_VERSION = "releaseVersion";
public static final String RELEASE_VERSION_DESC= "Release version, e.g. 1.2.5, default to 0.1.0.";

public static final String HTTP_USER_AGENT = "httpUserAgent";
public static final String HTTP_USER_AGENT_DESC = "HTTP user agent, e.g. codegen_csharp_api_client, default to 'Swagger-Codegen/{releaseVersion}}/{language}'";

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class DefaultCodegen {
protected Boolean sortParamsByRequiredFlag = true;
protected Boolean ensureUniqueParams = true;
protected String gitUserId, gitRepoId, releaseNote, releaseVersion;
protected String httpUserAgent;

public List<CliOption> cliOptions() {
return cliOptions;
Expand Down Expand Up @@ -2431,6 +2432,24 @@ public String getReleaseVersion() {
return releaseVersion;
}

/**
* Set HTTP user agent.
*
* @param httpUserAgent HTTP user agent
*/
public void setHttpUserAgent(String httpUserAgent) {
this.httpUserAgent = httpUserAgent;
}

/**
* HTTP user agent
*
* @return HTTP user agent
*/
public String getHttpUserAgent() {
return httpUserAgent;
}

@SuppressWarnings("static-method")
protected CliOption buildLibraryCliOption(Map<String, String> supportedLibraries) {
StringBuilder sb = new StringBuilder("library template (sub-template) to use:");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class CodegenConfigurator {
private String gitRepoId="YOUR_GIT_REPO_ID";
private String releaseNote="Minor update";
private String releaseVersion="0.1.0";
private String httpUserAgent;

private final Map<String, String> dynamicProperties = new HashMap<String, String>(); //the map that holds the JsonAnySetter/JsonAnyGetter values

Expand Down Expand Up @@ -334,6 +335,15 @@ public CodegenConfigurator setReleaseVersion(String releaseVersion) {
this.releaseVersion = releaseVersion;
return this;
}

public String getHttpUserAgent() {
return httpUserAgent;
}

public CodegenConfigurator setHttpUserAgent(String httpUserAgent) {
this.httpUserAgent= httpUserAgent;
return this;
}

public ClientOptInput toClientOptInput() {

Expand Down Expand Up @@ -366,6 +376,7 @@ public ClientOptInput toClientOptInput() {
checkAndSetAdditionalProperty(gitRepoId, CodegenConstants.GIT_REPO_ID);
checkAndSetAdditionalProperty(releaseVersion, CodegenConstants.RELEASE_VERSION);
checkAndSetAdditionalProperty(releaseNote, CodegenConstants.RELEASE_NOTE);
checkAndSetAdditionalProperty(httpUserAgent, CodegenConstants.HTTP_USER_AGENT);

handleDynamicProperties(config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ namespace {{packageName}}.Client
String contentType)
{
var request = new RestRequest(path, method);

// add user agent header
request.AddHeader("User-Agent", Configuration.HttpUserAgent);

// add path parameter, if any
foreach(var param in pathParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ namespace {{packageName}}.Client
Dictionary<String, String> apiKeyPrefix = null,
string tempFolderPath = null,
string dateTimeFormat = null,
int timeout = 100000
int timeout = 100000,
string httpUserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{packageVersion}}/csharp{{/httpUserAgent}}"
)
{
setApiClientUsingDefault(apiClient);

Username = username;
Password = password;
AccessToken = accessToken;
HttpUserAgent = httpUserAgent;

if (defaultHeader != null)
DefaultHeader = defaultHeader;
Expand Down Expand Up @@ -146,6 +148,12 @@ namespace {{packageName}}.Client
_defaultHeaderMap.Add(key, value);
}

/// <summary>
/// Gets or sets the HTTP user agent.
/// </summary>
/// <value>Http user agent.</value>
public String HttpUserAgent { get; set; }

/// <summary>
/// Gets or sets the username (HTTP basic authentication).
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions modules/swagger-codegen/src/main/resources/csharp/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ namespace {{packageName}}.Api
public {{classname}}(String basePath)
{
this.Configuration = new Configuration(new ApiClient(basePath));

// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
}

/// <summary>
Expand All @@ -96,6 +102,12 @@ namespace {{packageName}}.Api
this.Configuration = Configuration.Default;
else
this.Configuration = configuration;

// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,12 @@ public class PetApi : IPetApi
public PetApi(String basePath)
{
this.Configuration = new Configuration(new ApiClient(basePath));

// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
}

/// <summary>
Expand All @@ -555,6 +561,12 @@ public PetApi(Configuration configuration = null)
this.Configuration = Configuration.Default;
else
this.Configuration = configuration;

// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ public class StoreApi : IStoreApi
public StoreApi(String basePath)
{
this.Configuration = new Configuration(new ApiClient(basePath));

// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
}

/// <summary>
Expand All @@ -307,6 +313,12 @@ public StoreApi(Configuration configuration = null)
this.Configuration = Configuration.Default;
else
this.Configuration = configuration;

// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@ public class UserApi : IUserApi
public UserApi(String basePath)
{
this.Configuration = new Configuration(new ApiClient(basePath));

// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
}

/// <summary>
Expand All @@ -407,6 +413,12 @@ public UserApi(Configuration configuration = null)
this.Configuration = Configuration.Default;
else
this.Configuration = configuration;

// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ private RestRequest PrepareRequest(
String contentType)
{
var request = new RestRequest(path, method);

// add user agent header
request.AddHeader("User-Agent", Configuration.HttpUserAgent);

// add path parameter, if any
foreach(var param in pathParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ public Configuration(ApiClient apiClient = null,
Dictionary<String, String> apiKeyPrefix = null,
string tempFolderPath = null,
string dateTimeFormat = null,
int timeout = 100000
int timeout = 100000,
string httpUserAgent = "Swagger-Codegen/1.0.0/csharp"
)
{
setApiClientUsingDefault(apiClient);

Username = username;
Password = password;
AccessToken = accessToken;
HttpUserAgent = httpUserAgent;

if (defaultHeader != null)
DefaultHeader = defaultHeader;
Expand Down Expand Up @@ -146,6 +148,12 @@ public void AddDefaultHeader(string key, string value)
_defaultHeaderMap.Add(key, value);
}

/// <summary>
/// Gets or sets the HTTP user agent.
/// </summary>
/// <value>Http user agent.</value>
public String HttpUserAgent { get; set; }

/// <summary>
/// Gets or sets the username (HTTP basic authentication).
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs">
<Files>
<File FileName="TestPet.cs" Line="189" Column="4" />
<File FileName="TestPet.cs" Line="69" Column="32" />
<File FileName="TestOrder.cs" Line="1" Column="1" />
</Files>
<Pads>
<Pad Id="MonoDevelop.NUnit.TestPad">
<State name="__root__">
<Node name="SwaggerClientTest" expanded="True" />
</State>
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
Expand Down