Skip to content

Set Accept header when format set on Cat APIs #4269

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 2 commits into from
Dec 17, 2019
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 @@ -128,7 +128,7 @@ public string TypeLowLevel
}


public string InitializerGenerator(string type, string name, string key, string setter, params string[] doc) =>
CodeGenerator.Property(type, name, key, setter, Obsolete, doc);
public string InitializerGenerator(string @namespace, string type, string name, string key, string setter, params string[] doc) =>
CodeGenerator.Property(@namespace, type, name, key, setter, Obsolete, doc);
}
}
14 changes: 12 additions & 2 deletions src/CodeGeneration/ApiGenerator/Generator/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@ namespace ApiGenerator.Generator
//TODO this should be in views and models
public static class CodeGenerator
{
public static string CatFormatPropertyGenerator(string type, string name, string key, string setter) =>
$"public {type} {name} {{ "
+ $" get => Q<{type}>(\"{key}\");"
+ $" set {{ Q(\"{key}\", {setter}); SetAcceptHeader({setter}); }}"
+ $"}}";

public static string PropertyGenerator(string type, string name, string key, string setter) =>
$"public {type} {name} {{ get => Q<{type}>(\"{key}\"); set => Q(\"{key}\", {setter}); }}";

public static string Property(string type, string name, string key, string setter, string obsolete, params string[] doc)
public static string Property(string @namespace, string type, string name, string key, string setter, string obsolete, params string[] doc)
{
var components = new List<string>();
foreach (var d in RenderDocumentation(doc)) A(d);
if (!string.IsNullOrWhiteSpace(obsolete)) A($"[Obsolete(\"Scheduled to be removed in 7.0, {obsolete}\")]");

A(PropertyGenerator(type, name, key, setter));
var generated = @namespace != null && @namespace == "Cat" && name == "Format"
? CatFormatPropertyGenerator(type, name, key, setter)
: PropertyGenerator(type, name, key, setter);

A(generated);
return string.Join($"{Environment.NewLine}\t\t", components);

void A(string s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Nest
{
@foreach (var common in RestApiSpec.CommonApiQueryParameters.Values)
{
<text> @Raw(common.InitializerGenerator(common.TypeHighLevel, common.ClsName, common.QueryStringKey, "value", common.DescriptionHighLevel.ToArray()))
<text> @Raw(common.InitializerGenerator(null, common.TypeHighLevel, common.ClsName, common.QueryStringKey, "value", common.DescriptionHighLevel.ToArray()))
</text>
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
continue;
}
var doc = param.DescriptionHighLevel.ToArray();
<text> @Raw(param.InitializerGenerator(param.TypeHighLevel, param.ClsName, original, param.SetterHighLevel, doc))
<text> @Raw(param.InitializerGenerator(r.CsharpNames.Namespace, param.TypeHighLevel, param.ClsName, original, param.SetterHighLevel, doc))
</text>
}
@if (names.DescriptorNotFoundInCodebase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Elasticsearch.Net@(ns)
public override HttpMethod DefaultHttpMethod => HttpMethod.@r.HttpMethod;
@foreach (var param in r.Params)
{
<text> @Raw(param.InitializerGenerator(param.TypeLowLevel, param.ClsName, param.QueryStringKey, param.SetterLowLevel, param.Description))
<text> @Raw(param.InitializerGenerator(r.CsharpNames.Namespace, param.TypeLowLevel, param.ClsName, param.QueryStringKey, param.SetterLowLevel, param.Description))
</text>
}
}</text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ public interface IRequestParameters
/// Gets the stringified representation of a query string value as it would be sent to Elasticsearch.
/// </summary>
string GetResolvedQueryStringValue(string n, IConnectionConfigurationValues s);

/// <summary>
/// Gets the HTTP Accept Header value from the shortened name. If the shortened name is not recognized,
/// <c>null</c> is returned.
/// </summary>
string AcceptHeaderFromFormat(string format);
}
}
114 changes: 95 additions & 19 deletions src/Elasticsearch.Net/Api/RequestParameters/RequestParameters.Cat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public class CatAliasesRequestParameters : RequestParameters<CatAliasesRequestPa
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -86,7 +90,11 @@ public class CatAllocationRequestParameters : RequestParameters<CatAllocationReq
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -140,7 +148,11 @@ public class CatCountRequestParameters : RequestParameters<CatCountRequestParame
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -201,7 +213,11 @@ public string[] Fields
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -255,7 +271,11 @@ public class CatHealthRequestParameters : RequestParameters<CatHealthRequestPara
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -335,7 +355,11 @@ public class CatIndicesRequestParameters : RequestParameters<CatIndicesRequestPa
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -410,7 +434,11 @@ public class CatMasterRequestParameters : RequestParameters<CatMasterRequestPara
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -464,7 +492,11 @@ public class CatNodeAttributesRequestParameters : RequestParameters<CatNodeAttri
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -518,7 +550,11 @@ public class CatNodesRequestParameters : RequestParameters<CatNodesRequestParame
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Return the full node ID instead of the shortened version (default: false)</summary>
Expand Down Expand Up @@ -579,7 +615,11 @@ public class CatPendingTasksRequestParameters : RequestParameters<CatPendingTask
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -633,7 +673,11 @@ public class CatPluginsRequestParameters : RequestParameters<CatPluginsRequestPa
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -701,7 +745,11 @@ public bool? Detailed
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -755,7 +803,11 @@ public class CatRepositoriesRequestParameters : RequestParameters<CatRepositorie
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -809,7 +861,11 @@ public class CatSegmentsRequestParameters : RequestParameters<CatSegmentsRequest
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -849,7 +905,11 @@ public class CatShardsRequestParameters : RequestParameters<CatShardsRequestPara
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -903,7 +963,11 @@ public class CatSnapshotsRequestParameters : RequestParameters<CatSnapshotsReque
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -971,7 +1035,11 @@ public bool? Detailed
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -1028,7 +1096,11 @@ public class CatTemplatesRequestParameters : RequestParameters<CatTemplatesReque
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down Expand Up @@ -1082,7 +1154,11 @@ public class CatThreadPoolRequestParameters : RequestParameters<CatThreadPoolReq
public string Format
{
get => Q<string>("format");
set => Q("format", value);
set
{
Q("format", value);
SetAcceptHeader(value);
}
}

///<summary>Comma-separated list of column names to display</summary>
Expand Down
32 changes: 30 additions & 2 deletions src/Elasticsearch.Net/Api/RequestParameters/RequestParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public abstract class RequestParameters<T> : IRequestParameters where T : Reques
public TOut GetQueryStringValue<TOut>(string name)
{
if (!ContainsQueryString(name))
return default(TOut);
return default;

var value = Self.QueryString[name];
if (value == null)
return default(TOut);
return default;

return (TOut)value;
}
Expand All @@ -58,5 +58,33 @@ private void RemoveQueryString(string name)

Self.QueryString.Remove(name);
}

protected void SetAcceptHeader(string format)
{
if (RequestConfiguration == null)
RequestConfiguration = new RequestConfiguration();

RequestConfiguration.Accept = AcceptHeaderFromFormat(format);
}

/// <inheritdoc />
public string AcceptHeaderFromFormat(string format)
{
if (format == null)
return null;

var lowerFormat = format.ToLowerInvariant();

switch(lowerFormat)
{
case "smile":
case "yaml":
case "cbor":
case "json":
return $"application/{lowerFormat}";
default:
return null;
}
}
}
}
1 change: 1 addition & 0 deletions src/Elasticsearch.Net/Transport/Pipeline/RequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ IMemoryStreamFactory memoryStreamFactory
public string ProxyAddress { get; }
public SecureString ProxyPassword { get; }
public string ProxyUsername { get; }
// TODO: rename to ContentType in 8.0.0
public string RequestMimeType { get; }
public TimeSpan RequestTimeout { get; }
public string RunAs { get; }
Expand Down
Loading