Skip to content
Draft
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
32 changes: 32 additions & 0 deletions src/RestSharp/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) .NET Foundation and Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Globalization;

namespace RestSharp.Extensions;

static class ObjectExtensions {
/// <summary>
/// Converts a value to its string representation using the specified culture for IFormattable types.
/// </summary>
/// <typeparam name="T">The type of value to convert</typeparam>
/// <param name="value">The value to convert</param>
/// <param name="culture">The culture to use for formatting. If null, uses the current culture.</param>
/// <returns>String representation using the specified culture, or null if value is null</returns>
internal static string? ToStringWithCulture<T>(this T value, CultureInfo? culture) => value switch {
null => null,
IFormattable f => f.ToString(null, culture),
_ => value.ToString()
};
}
7 changes: 7 additions & 0 deletions src/RestSharp/Options/RestClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
//

using System.Globalization;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Reflection;
Expand Down Expand Up @@ -230,4 +231,10 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
/// Custom function to encode a string for use in a URL query.
/// </summary>
public Func<string, Encoding, string> EncodeQuery { get; set; } = (s, encoding) => s.UrlEncode(encoding)!;

/// <summary>
/// Culture to use for formatting IFormattable parameter values. Default is null which uses the current culture.
/// Set to <see cref="CultureInfo.InvariantCulture"/> to ensure consistent formatting across different locales.
/// </summary>
public CultureInfo? CultureForParameters { get; set; }
}
71 changes: 71 additions & 0 deletions src/RestSharp/Request/RestRequestExtensions.Culture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) .NET Foundation and Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using RestSharp.Extensions;

namespace RestSharp;

public static partial class RestRequestExtensions {
/// <summary>
/// Adds a HTTP parameter to the request (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT).
/// The value will be converted to string using the culture specified in <see cref="RestClientOptions.CultureForParameters"/>.
/// </summary>
/// <param name="client">The RestClient instance containing the culture settings</param>
/// <param name="request">The request to add the parameter to</param>
/// <param name="name">Name of the parameter</param>
/// <param name="value">Value of the parameter</param>
/// <param name="encode">Encode the value or not, default true</param>
/// <returns>This request</returns>
public static RestRequest AddParameter<T>(this RestRequest request, IRestClient client, string name, T value, bool encode = true) where T : struct
=> request.AddParameter(name, value.ToStringWithCulture(client.Options.CultureForParameters), encode);

/// <summary>
/// Adds or updates a HTTP parameter to the request (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT).
/// The value will be converted to string using the culture specified in <see cref="RestClientOptions.CultureForParameters"/>.
/// </summary>
/// <param name="client">The RestClient instance containing the culture settings</param>
/// <param name="request">The request to add the parameter to</param>
/// <param name="name">Name of the parameter</param>
/// <param name="value">Value of the parameter</param>
/// <param name="encode">Encode the value or not, default true</param>
/// <returns>This request</returns>
public static RestRequest AddOrUpdateParameter<T>(this RestRequest request, IRestClient client, string name, T value, bool encode = true) where T : struct
=> request.AddOrUpdateParameter(name, value.ToStringWithCulture(client.Options.CultureForParameters), encode);

/// <summary>
/// Adds a query string parameter to the request.
/// The value will be converted to string using the culture specified in <see cref="RestClientOptions.CultureForParameters"/>.
/// </summary>
/// <param name="client">The RestClient instance containing the culture settings</param>
/// <param name="request">The request to add the parameter to</param>
/// <param name="name">Parameter name</param>
/// <param name="value">Parameter value</param>
/// <param name="encode">Encode the value or not, default true</param>
/// <returns></returns>
public static RestRequest AddQueryParameter<T>(this RestRequest request, IRestClient client, string name, T value, bool encode = true) where T : struct
=> request.AddQueryParameter(name, value.ToStringWithCulture(client.Options.CultureForParameters), encode);

/// <summary>
/// Adds a URL segment parameter to the request.
/// The value will be converted to string using the culture specified in <see cref="RestClientOptions.CultureForParameters"/>.
/// </summary>
/// <param name="client">The RestClient instance containing the culture settings</param>
/// <param name="request">The request to add the parameter to</param>
/// <param name="name">Name of the parameter; must be matching a placeholder in the resource URL as {name}</param>
/// <param name="value">Value of the parameter</param>
/// <param name="encode">Encode the value or not, default true</param>
/// <returns></returns>
public static RestRequest AddUrlSegment<T>(this RestRequest request, IRestClient client, string name, T value, bool encode = true) where T : struct
=> request.AddUrlSegment(name, value.ToStringWithCulture(client.Options.CultureForParameters), encode);
}
41 changes: 41 additions & 0 deletions src/RestSharp/RestClient.Extensions.Params.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.
//

using RestSharp.Extensions;

namespace RestSharp;

public static partial class RestClientExtensions {
Expand All @@ -38,6 +40,17 @@ public IRestClient AddDefaultParameter(Parameter parameter) {
public IRestClient AddDefaultParameter(string name, string value)
=> client.AddDefaultParameter(new GetOrPostParameter(name, value));

/// <summary>
/// Adds a default HTTP parameter (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)
/// Used on every request made by this client instance. The value will be formatted using the culture
/// specified in <see cref="RestClientOptions.CultureForParameters"/>.
/// </summary>
/// <param name="name">Name of the parameter</param>
/// <param name="value">Value of the parameter</param>
/// <returns>This request</returns>
public IRestClient AddDefaultParameter<T>(string name, T value) where T : struct
=> client.AddDefaultParameter(new GetOrPostParameter(name, value.ToStringWithCulture(client.Options.CultureForParameters)));

/// <summary>
/// Adds a default parameter to the client options. There are four types of parameters:
/// - GetOrPost: Either a QueryString value or encoded form value based on method
Expand Down Expand Up @@ -82,6 +95,16 @@ public IRestClient AddDefaultHeaders(Dictionary<string, string> headers) {
public IRestClient AddDefaultUrlSegment(string name, string value)
=> client.AddDefaultParameter(new UrlSegmentParameter(name, value));

/// <summary>
/// Adds a default URL segment parameter to the RestClient. Used on every request made by this client instance.
/// The value will be formatted using the culture specified in <see cref="RestClientOptions.CultureForParameters"/>.
/// </summary>
/// <param name="name">Name of the segment to add</param>
/// <param name="value">Value of the segment to add</param>
/// <returns></returns>
public IRestClient AddDefaultUrlSegment<T>(string name, T value) where T : struct
=> client.AddDefaultParameter(new UrlSegmentParameter(name, value.ToStringWithCulture(client.Options.CultureForParameters)));

/// <summary>
/// Adds a default URL query parameter to the RestClient. Used on every request made by this client instance.
/// </summary>
Expand All @@ -90,5 +113,23 @@ public IRestClient AddDefaultUrlSegment(string name, string value)
/// <returns></returns>
public IRestClient AddDefaultQueryParameter(string name, string value)
=> client.AddDefaultParameter(new QueryParameter(name, value));

/// <summary>
/// Adds a default URL query parameter to the RestClient. Used on every request made by this client instance.
/// The value will be formatted using the culture specified in <see cref="RestClientOptions.CultureForParameters"/>.
/// </summary>
/// <param name="name">Name of the query parameter to add</param>
/// <param name="value">Value of the query parameter to add</param>
/// <returns></returns>
public IRestClient AddDefaultQueryParameter<T>(string name, T value) where T : struct
=> client.AddDefaultParameter(new QueryParameter(name, value.ToStringWithCulture(client.Options.CultureForParameters)));

/// <summary>
/// Formats the value using the culture specified in <see cref="RestClientOptions.CultureForParameters"/>.
/// </summary>
/// <param name="value">Value to format</param>
/// <returns>String representation of the value using the client's culture setting</returns>
public string? FormatValue<T>(T value) where T : struct
=> value.ToStringWithCulture(client.Options.CultureForParameters);
}
}
Loading
Loading