mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-05-13 00:21:18 +00:00
add httpUserAgent option, add customized user-agent support to C#
This commit is contained in:
@@ -184,4 +184,8 @@ public interface CodegenConfig {
|
||||
|
||||
String getReleaseVersion();
|
||||
|
||||
void setHttpUserAgent(String httpUserAgent);
|
||||
|
||||
String getHttpUserAgent();
|
||||
|
||||
}
|
||||
|
||||
@@ -103,4 +103,7 @@ public class CodegenConstants {
|
||||
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}'";
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -2431,6 +2432,24 @@ public class DefaultCodegen {
|
||||
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:");
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -334,6 +335,15 @@ public class CodegenConfigurator {
|
||||
this.releaseVersion = releaseVersion;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getHttpUserAgent() {
|
||||
return httpUserAgent;
|
||||
}
|
||||
|
||||
public CodegenConfigurator setHttpUserAgent(String httpUserAgent) {
|
||||
this.httpUserAgent= httpUserAgent;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ClientOptInput toClientOptInput() {
|
||||
|
||||
@@ -366,6 +376,7 @@ public class CodegenConfigurator {
|
||||
checkAndSetAdditionalProperty(gitRepoId, CodegenConstants.GIT_REPO_ID);
|
||||
checkAndSetAdditionalProperty(releaseVersion, CodegenConstants.RELEASE_VERSION);
|
||||
checkAndSetAdditionalProperty(releaseNote, CodegenConstants.RELEASE_NOTE);
|
||||
checkAndSetAdditionalProperty(httpUserAgent, CodegenConstants.HTTP_USER_AGENT);
|
||||
|
||||
handleDynamicProperties(config);
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -34,7 +34,8 @@ 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);
|
||||
@@ -42,6 +43,7 @@ namespace {{packageName}}.Client
|
||||
Username = username;
|
||||
Password = password;
|
||||
AccessToken = accessToken;
|
||||
HttpUserAgent = httpUserAgent;
|
||||
|
||||
if (defaultHeader != null)
|
||||
DefaultHeader = defaultHeader;
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user