Merge pull request #2371 from wing328/csharp_user_agent

[C#] add httpUserAgent option, add configurable user-agent support to C#
This commit is contained in:
wing328 2016-03-14 11:03:57 +08:00
commit 84564ae231
14 changed files with 124 additions and 3 deletions

View File

@ -118,6 +118,9 @@ public class Generate implements Runnable {
@Option(name = {"--release-version"}, title = "release version", description = CodegenConstants.RELEASE_VERSION_DESC) @Option(name = {"--release-version"}, title = "release version", description = CodegenConstants.RELEASE_VERSION_DESC)
private String releaseVersion; private String releaseVersion;
@Option(name = {"--http-user-agent"}, title = "http user agent", description = CodegenConstants.HTTP_USER_AGENT)
private String httpUserAgent;
@Override @Override
public void run() { public void run() {
@ -211,6 +214,10 @@ public class Generate implements Runnable {
configurator.setReleaseVersion(releaseVersion); configurator.setReleaseVersion(releaseVersion);
} }
if (isNotEmpty(httpUserAgent)) {
configurator.setHttpUserAgent(httpUserAgent);
}
applySystemPropertiesKvp(systemProperties, configurator); applySystemPropertiesKvp(systemProperties, configurator);
applyInstantiationTypesKvp(instantiationTypes, configurator); applyInstantiationTypesKvp(instantiationTypes, configurator);
applyImportMappingsKvp(importMappings, configurator); applyImportMappingsKvp(importMappings, configurator);

View File

@ -184,4 +184,8 @@ public interface CodegenConfig {
String getReleaseVersion(); String getReleaseVersion();
void setHttpUserAgent(String httpUserAgent);
String getHttpUserAgent();
} }

View File

@ -103,4 +103,7 @@ public class CodegenConstants {
public static final String RELEASE_VERSION = "releaseVersion"; 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 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}'";
} }

View File

@ -84,6 +84,7 @@ public class DefaultCodegen {
protected Boolean sortParamsByRequiredFlag = true; protected Boolean sortParamsByRequiredFlag = true;
protected Boolean ensureUniqueParams = true; protected Boolean ensureUniqueParams = true;
protected String gitUserId, gitRepoId, releaseNote, releaseVersion; protected String gitUserId, gitRepoId, releaseNote, releaseVersion;
protected String httpUserAgent;
public List<CliOption> cliOptions() { public List<CliOption> cliOptions() {
return cliOptions; return cliOptions;
@ -2431,6 +2432,24 @@ public class DefaultCodegen {
return releaseVersion; 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") @SuppressWarnings("static-method")
protected CliOption buildLibraryCliOption(Map<String, String> supportedLibraries) { protected CliOption buildLibraryCliOption(Map<String, String> supportedLibraries) {
StringBuilder sb = new StringBuilder("library template (sub-template) to use:"); StringBuilder sb = new StringBuilder("library template (sub-template) to use:");

View File

@ -64,6 +64,7 @@ public class CodegenConfigurator {
private String gitRepoId="YOUR_GIT_REPO_ID"; private String gitRepoId="YOUR_GIT_REPO_ID";
private String releaseNote="Minor update"; private String releaseNote="Minor update";
private String releaseVersion="0.1.0"; 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 private final Map<String, String> dynamicProperties = new HashMap<String, String>(); //the map that holds the JsonAnySetter/JsonAnyGetter values
@ -335,6 +336,15 @@ public class CodegenConfigurator {
return this; return this;
} }
public String getHttpUserAgent() {
return httpUserAgent;
}
public CodegenConfigurator setHttpUserAgent(String httpUserAgent) {
this.httpUserAgent= httpUserAgent;
return this;
}
public ClientOptInput toClientOptInput() { public ClientOptInput toClientOptInput() {
Validate.notEmpty(lang, "language must be specified"); Validate.notEmpty(lang, "language must be specified");
@ -366,6 +376,7 @@ public class CodegenConfigurator {
checkAndSetAdditionalProperty(gitRepoId, CodegenConstants.GIT_REPO_ID); checkAndSetAdditionalProperty(gitRepoId, CodegenConstants.GIT_REPO_ID);
checkAndSetAdditionalProperty(releaseVersion, CodegenConstants.RELEASE_VERSION); checkAndSetAdditionalProperty(releaseVersion, CodegenConstants.RELEASE_VERSION);
checkAndSetAdditionalProperty(releaseNote, CodegenConstants.RELEASE_NOTE); checkAndSetAdditionalProperty(releaseNote, CodegenConstants.RELEASE_NOTE);
checkAndSetAdditionalProperty(httpUserAgent, CodegenConstants.HTTP_USER_AGENT);
handleDynamicProperties(config); handleDynamicProperties(config);

View File

@ -85,6 +85,9 @@ namespace {{packageName}}.Client
{ {
var request = new RestRequest(path, method); var request = new RestRequest(path, method);
// add user agent header
request.AddHeader("User-Agent", Configuration.HttpUserAgent);
// add path parameter, if any // add path parameter, if any
foreach(var param in pathParams) foreach(var param in pathParams)
request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment); request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment);

View File

@ -34,7 +34,8 @@ namespace {{packageName}}.Client
Dictionary<String, String> apiKeyPrefix = null, Dictionary<String, String> apiKeyPrefix = null,
string tempFolderPath = null, string tempFolderPath = null,
string dateTimeFormat = null, string dateTimeFormat = null,
int timeout = 100000 int timeout = 100000,
string httpUserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{packageVersion}}/csharp{{/httpUserAgent}}"
) )
{ {
setApiClientUsingDefault(apiClient); setApiClientUsingDefault(apiClient);
@ -42,6 +43,7 @@ namespace {{packageName}}.Client
Username = username; Username = username;
Password = password; Password = password;
AccessToken = accessToken; AccessToken = accessToken;
HttpUserAgent = httpUserAgent;
if (defaultHeader != null) if (defaultHeader != null)
DefaultHeader = defaultHeader; DefaultHeader = defaultHeader;
@ -146,6 +148,12 @@ namespace {{packageName}}.Client
_defaultHeaderMap.Add(key, 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> /// <summary>
/// Gets or sets the username (HTTP basic authentication). /// Gets or sets the username (HTTP basic authentication).
/// </summary> /// </summary>

View File

@ -82,6 +82,12 @@ namespace {{packageName}}.Api
public {{classname}}(String basePath) public {{classname}}(String basePath)
{ {
this.Configuration = new Configuration(new ApiClient(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> /// <summary>
@ -96,6 +102,12 @@ namespace {{packageName}}.Api
this.Configuration = Configuration.Default; this.Configuration = Configuration.Default;
else else
this.Configuration = configuration; this.Configuration = configuration;
// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
} }
/// <summary> /// <summary>

View File

@ -541,6 +541,12 @@ namespace IO.Swagger.Api
public PetApi(String basePath) public PetApi(String basePath)
{ {
this.Configuration = new Configuration(new ApiClient(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> /// <summary>
@ -555,6 +561,12 @@ namespace IO.Swagger.Api
this.Configuration = Configuration.Default; this.Configuration = Configuration.Default;
else else
this.Configuration = configuration; this.Configuration = configuration;
// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
} }
/// <summary> /// <summary>

View File

@ -293,6 +293,12 @@ namespace IO.Swagger.Api
public StoreApi(String basePath) public StoreApi(String basePath)
{ {
this.Configuration = new Configuration(new ApiClient(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> /// <summary>
@ -307,6 +313,12 @@ namespace IO.Swagger.Api
this.Configuration = Configuration.Default; this.Configuration = Configuration.Default;
else else
this.Configuration = configuration; this.Configuration = configuration;
// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
} }
/// <summary> /// <summary>

View File

@ -393,6 +393,12 @@ namespace IO.Swagger.Api
public UserApi(String basePath) public UserApi(String basePath)
{ {
this.Configuration = new Configuration(new ApiClient(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> /// <summary>
@ -407,6 +413,12 @@ namespace IO.Swagger.Api
this.Configuration = Configuration.Default; this.Configuration = Configuration.Default;
else else
this.Configuration = configuration; this.Configuration = configuration;
// ensure API client has configuration ready
if (Configuration.ApiClient.Configuration == null)
{
this.Configuration.ApiClient.Configuration = this.Configuration;
}
} }
/// <summary> /// <summary>

View File

@ -85,6 +85,9 @@ namespace IO.Swagger.Client
{ {
var request = new RestRequest(path, method); var request = new RestRequest(path, method);
// add user agent header
request.AddHeader("User-Agent", Configuration.HttpUserAgent);
// add path parameter, if any // add path parameter, if any
foreach(var param in pathParams) foreach(var param in pathParams)
request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment); request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment);

View File

@ -34,7 +34,8 @@ namespace IO.Swagger.Client
Dictionary<String, String> apiKeyPrefix = null, Dictionary<String, String> apiKeyPrefix = null,
string tempFolderPath = null, string tempFolderPath = null,
string dateTimeFormat = null, string dateTimeFormat = null,
int timeout = 100000 int timeout = 100000,
string httpUserAgent = "Swagger-Codegen/1.0.0/csharp"
) )
{ {
setApiClientUsingDefault(apiClient); setApiClientUsingDefault(apiClient);
@ -42,6 +43,7 @@ namespace IO.Swagger.Client
Username = username; Username = username;
Password = password; Password = password;
AccessToken = accessToken; AccessToken = accessToken;
HttpUserAgent = httpUserAgent;
if (defaultHeader != null) if (defaultHeader != null)
DefaultHeader = defaultHeader; DefaultHeader = defaultHeader;
@ -146,6 +148,12 @@ namespace IO.Swagger.Client
_defaultHeaderMap.Add(key, 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> /// <summary>
/// Gets or sets the username (HTTP basic authentication). /// Gets or sets the username (HTTP basic authentication).
/// </summary> /// </summary>

View File

@ -2,9 +2,16 @@
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs"> <MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs">
<Files> <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" /> <File FileName="TestOrder.cs" Line="1" Column="1" />
</Files> </Files>
<Pads>
<Pad Id="MonoDevelop.NUnit.TestPad">
<State name="__root__">
<Node name="SwaggerClientTest" expanded="True" />
</State>
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench> </MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore /> <BreakpointStore />