Skip to content

Commit 7be39f5

Browse files
committed
Interface, configuration
1 parent a4473c3 commit 7be39f5

37 files changed

+848
-610
lines changed

src/RestSharp.Serializers.CsvHelper/RestClientExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace RestSharp.Serializers.CsvHelper;
44

55
[PublicAPI]
66
public static class RestClientExtensions {
7-
public static RestClient UseCsvHelper(this RestClient client) => client.UseSerializer<CsvHelperSerializer>();
7+
public static SerializerConfig UseCsvHelper(this SerializerConfig config) => config.UseSerializer<CsvHelperSerializer>();
88

9-
public static RestClient UseCsvHelper(this RestClient client, CsvConfiguration configuration)
10-
=> client.UseSerializer(() => new CsvHelperSerializer(configuration));
9+
public static SerializerConfig UseCsvHelper(this SerializerConfig config, CsvConfiguration configuration)
10+
=> config.UseSerializer(() => new CsvHelperSerializer(configuration));
1111
}

src/RestSharp.Serializers.NewtonsoftJson/RestClientExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ public static class RestClientExtensions {
1919
/// <summary>
2020
/// Use Newtonsoft.Json serializer with default settings
2121
/// </summary>
22-
/// <param name="client"></param>
22+
/// <param name="config"></param>
2323
/// <returns></returns>
24-
public static RestClient UseNewtonsoftJson(this RestClient client) => client.UseSerializer(() => new JsonNetSerializer());
24+
public static SerializerConfig UseNewtonsoftJson(this SerializerConfig config) => config.UseSerializer(() => new JsonNetSerializer());
2525

2626
/// <summary>
2727
/// Use Newtonsoft.Json serializer with custom settings
2828
/// </summary>
29-
/// <param name="client"></param>
29+
/// <param name="config"></param>
3030
/// <param name="settings">Newtonsoft.Json serializer settings</param>
3131
/// <returns></returns>
32-
public static RestClient UseNewtonsoftJson(this RestClient client, JsonSerializerSettings settings)
33-
=> client.UseSerializer(() => new JsonNetSerializer(settings));
32+
public static SerializerConfig UseNewtonsoftJson(this SerializerConfig config, JsonSerializerSettings settings)
33+
=> config.UseSerializer(() => new JsonNetSerializer(settings));
3434
}

src/RestSharp.Serializers.Xml/XmlSerializerClientExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ namespace RestSharp.Serializers.Xml;
1616

1717
[PublicAPI]
1818
public static class XmlSerializerClientExtensions {
19-
public static RestClient UseXmlSerializer(
20-
this RestClient restClient,
21-
string? xmlNamespace = null,
22-
string? rootElement = null,
23-
bool useAttributeDeserializer = false
19+
public static SerializerConfig UseXmlSerializer(
20+
this SerializerConfig config,
21+
string? xmlNamespace = null,
22+
string? rootElement = null,
23+
bool useAttributeDeserializer = false
2424
) {
2525
var xmlSerializer = new XmlSerializer {
2626
Namespace = xmlNamespace,
@@ -33,6 +33,6 @@ public static RestClient UseXmlSerializer(
3333
.WithXmlSerializer(xmlSerializer)
3434
.WithXmlDeserializer(xmlDeserializer);
3535

36-
return restClient.UseSerializer(() => serializer);
36+
return config.UseSerializer(() => serializer);
3737
}
38-
}
38+
}

src/RestSharp/Ensure.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
namespace RestSharp;
1616

1717
static class Ensure {
18-
public static T NotNull<T>(T? value, string name)
19-
=> value ?? throw new ArgumentNullException(name);
18+
public static T NotNull<T>(T? value, [InvokerParameterName] string name) => value ?? throw new ArgumentNullException(name);
2019

21-
public static string NotEmpty(string? value, string name)
20+
public static string NotEmpty(string? value, [InvokerParameterName] string name)
2221
=> string.IsNullOrWhiteSpace(value) ? throw new ArgumentNullException(name) : value!;
2322

24-
public static string NotEmptyString(object? value, string name) {
23+
public static string NotEmptyString(object? value, [InvokerParameterName] string name) {
2524
var s = value as string ?? value?.ToString();
2625
return string.IsNullOrWhiteSpace(s) ? throw new ArgumentNullException(name) : s!;
2726
}
28-
}
27+
}

src/RestSharp/Extensions/StringExtensions.cs

Lines changed: 54 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
using System.Text.RegularExpressions;
1818
using System.Web;
1919

20-
namespace RestSharp.Extensions;
20+
namespace RestSharp.Extensions;
2121

2222
static class StringExtensions {
2323
static readonly Regex IsUpperCaseRegex = new(@"^[A-Z]+$");
@@ -33,20 +33,19 @@ static class StringExtensions {
3333
static readonly Regex AddSpacesRegex1 = new(@"[-\s]");
3434
static readonly Regex AddSpacesRegex2 = new(@"([a-z\d])([A-Z])");
3535
static readonly Regex AddSpacesRegex3 = new(@"([A-Z]+)([A-Z][a-z])");
36-
public static string UrlDecode(this string input) => HttpUtility.UrlDecode(input);
36+
37+
internal static string UrlDecode(this string input) => HttpUtility.UrlDecode(input);
3738

3839
/// <summary>
3940
/// Uses Uri.EscapeDataString() based on recommendations on MSDN
4041
/// http://blogs.msdn.com/b/yangxind/archive/2006/11/09/don-t-use-net-system-uri-unescapedatastring-in-url-decoding.aspx
4142
/// </summary>
42-
public static string UrlEncode(this string input) {
43+
internal static string UrlEncode(this string input) {
4344
const int maxLength = 32766;
4445

45-
if (input == null)
46-
throw new ArgumentNullException(nameof(input));
46+
if (input == null) throw new ArgumentNullException(nameof(input));
4747

48-
if (input.Length <= maxLength)
49-
return Uri.EscapeDataString(input);
48+
if (input.Length <= maxLength) return Uri.EscapeDataString(input);
5049

5150
var sb = new StringBuilder(input.Length * 2);
5251
var index = 0;
@@ -67,37 +66,18 @@ public static string UrlEncode(this string input) {
6766
return sb.ToString();
6867
}
6968

70-
public static string? UrlEncode(this string input, Encoding encoding) {
69+
internal static string? UrlEncode(this string input, Encoding encoding) {
7170
var encoded = HttpUtility.UrlEncode(input, encoding);
7271
return encoded?.Replace("+", "%20");
7372
}
7473

75-
/// <summary>
76-
/// Remove underscores from a string
77-
/// </summary>
78-
/// <param name="input">String to process</param>
79-
/// <returns>string</returns>
80-
public static string RemoveUnderscoresAndDashes(this string input) => input.Replace("_", "").Replace("-", "");
74+
internal static string RemoveUnderscoresAndDashes(this string input) => input.Replace("_", "").Replace("-", "");
8175

82-
/// <summary>
83-
/// Converts a string to pascal case
84-
/// </summary>
85-
/// <param name="lowercaseAndUnderscoredWord">String to convert</param>
86-
/// <param name="culture"></param>
87-
/// <returns>string</returns>
88-
public static string ToPascalCase(this string lowercaseAndUnderscoredWord, CultureInfo culture)
76+
internal static string ToPascalCase(this string lowercaseAndUnderscoredWord, CultureInfo culture)
8977
=> ToPascalCase(lowercaseAndUnderscoredWord, true, culture);
9078

91-
/// <summary>
92-
/// Converts a string to pascal case with the option to remove underscores
93-
/// </summary>
94-
/// <param name="text">String to convert</param>
95-
/// <param name="removeUnderscores">Option to remove underscores</param>
96-
/// <param name="culture"></param>
97-
/// <returns></returns>
98-
public static string ToPascalCase(this string text, bool removeUnderscores, CultureInfo culture) {
99-
if (string.IsNullOrEmpty(text))
100-
return text;
79+
internal static string ToPascalCase(this string text, bool removeUnderscores, CultureInfo culture) {
80+
if (string.IsNullOrEmpty(text)) return text;
10181

10282
text = text.Replace('_', ' ');
10383

@@ -113,69 +93,17 @@ string CaseWord(string word) {
11393
var restOfWord = word.Substring(1);
11494
var firstChar = char.ToUpper(word[0], culture);
11595

116-
if (restOfWord.IsUpperCase())
117-
restOfWord = restOfWord.ToLower(culture);
96+
if (restOfWord.IsUpperCase()) restOfWord = restOfWord.ToLower(culture);
11897

11998
return string.Concat(firstChar, restOfWord);
12099
}
121100
}
122101

123-
/// <summary>
124-
/// Converts a string to camel case
125-
/// </summary>
126-
/// <param name="lowercaseAndUnderscoredWord">String to convert</param>
127-
/// <param name="culture"></param>
128-
/// <returns>String</returns>
129-
public static string ToCamelCase(this string lowercaseAndUnderscoredWord, CultureInfo culture)
102+
internal static string ToCamelCase(this string lowercaseAndUnderscoredWord, CultureInfo culture)
130103
=> MakeInitialLowerCase(ToPascalCase(lowercaseAndUnderscoredWord, culture), culture);
131104

132-
static string MakeInitialLowerCase(this string word, CultureInfo culture)
133-
=> string.Concat(word.Substring(0, 1).ToLower(culture), word.Substring(1));
134-
135-
static string AddUnderscores(this string pascalCasedWord)
136-
=> AddUnderscoresRegex1.Replace(
137-
AddUnderscoresRegex2.Replace(
138-
AddUnderscoresRegex3.Replace(pascalCasedWord, "$1_$2"),
139-
"$1_$2"
140-
),
141-
"_"
142-
);
143-
144-
static string AddDashes(this string pascalCasedWord)
145-
=> AddDashesRegex1.Replace(
146-
AddDashesRegex2.Replace(
147-
AddDashesRegex3.Replace(pascalCasedWord, "$1-$2"),
148-
"$1-$2"
149-
),
150-
"-"
151-
);
152-
153-
static bool IsUpperCase(this string inputString) => IsUpperCaseRegex.IsMatch(inputString);
154-
155-
static string AddUnderscorePrefix(this string pascalCasedWord) => $"_{pascalCasedWord}";
156-
157-
static string AddSpaces(this string pascalCasedWord)
158-
=> AddSpacesRegex1.Replace(
159-
AddSpacesRegex2.Replace(
160-
AddSpacesRegex3.Replace(pascalCasedWord, "$1 $2"),
161-
"$1 $2"
162-
),
163-
" "
164-
);
165-
166-
internal static bool IsEmpty(this string? value) => string.IsNullOrWhiteSpace(value);
167-
168-
internal static bool IsNotEmpty(this string? value) => !string.IsNullOrWhiteSpace(value);
169-
170-
/// <summary>
171-
/// Return possible variants of a name for name matching.
172-
/// </summary>
173-
/// <param name="name">String to convert</param>
174-
/// <param name="culture">The culture to use for conversion</param>
175-
/// <returns>IEnumerable&lt;string&gt;</returns>
176-
public static IEnumerable<string> GetNameVariants(this string name, CultureInfo culture) {
177-
if (string.IsNullOrEmpty(name))
178-
yield break;
105+
internal static IEnumerable<string> GetNameVariants(this string name, CultureInfo culture) {
106+
if (string.IsNullOrEmpty(name)) yield break;
179107

180108
yield return name;
181109

@@ -216,8 +144,46 @@ public static IEnumerable<string> GetNameVariants(this string name, CultureInfo
216144
yield return name.AddSpaces().ToLower(culture);
217145
}
218146

147+
internal static bool IsEmpty(this string? value) => string.IsNullOrWhiteSpace(value);
148+
149+
internal static bool IsNotEmpty(this string? value) => !string.IsNullOrWhiteSpace(value);
150+
219151
internal static string JoinToString<T>(this IEnumerable<T> collection, string separator, Func<T, string> getString)
220152
=> JoinToString(collection.Select(getString), separator);
221153

222154
internal static string JoinToString(this IEnumerable<string> strings, string separator) => string.Join(separator, strings);
223-
}
155+
156+
static string MakeInitialLowerCase(this string word, CultureInfo culture)
157+
=> string.Concat(word.Substring(0, 1).ToLower(culture), word.Substring(1));
158+
159+
static string AddUnderscores(this string pascalCasedWord)
160+
=> AddUnderscoresRegex1.Replace(
161+
AddUnderscoresRegex2.Replace(
162+
AddUnderscoresRegex3.Replace(pascalCasedWord, "$1_$2"),
163+
"$1_$2"
164+
),
165+
"_"
166+
);
167+
168+
static string AddDashes(this string pascalCasedWord)
169+
=> AddDashesRegex1.Replace(
170+
AddDashesRegex2.Replace(
171+
AddDashesRegex3.Replace(pascalCasedWord, "$1-$2"),
172+
"$1-$2"
173+
),
174+
"-"
175+
);
176+
177+
static bool IsUpperCase(this string inputString) => IsUpperCaseRegex.IsMatch(inputString);
178+
179+
static string AddUnderscorePrefix(this string pascalCasedWord) => $"_{pascalCasedWord}";
180+
181+
static string AddSpaces(this string pascalCasedWord)
182+
=> AddSpacesRegex1.Replace(
183+
AddSpacesRegex2.Replace(
184+
AddSpacesRegex3.Replace(pascalCasedWord, "$1 $2"),
185+
"$1 $2"
186+
),
187+
" "
188+
);
189+
}

src/RestSharp/IRestClient.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) .NET Foundation and Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
using RestSharp.Serializers;
17+
18+
namespace RestSharp;
19+
20+
public interface IRestClient : IDisposable {
21+
IRestClientOptions Options { get; }
22+
23+
RestSerializers Serializers { get; }
24+
25+
/// <summary>
26+
/// Executes the request asynchronously, authenticating if needed
27+
/// </summary>
28+
/// <param name="request">Request to be executed</param>
29+
/// <param name="cancellationToken">Cancellation token</param>
30+
Task<RestResponse> ExecuteAsync(RestRequest request, CancellationToken cancellationToken = default);
31+
32+
/// <summary>
33+
/// A specialized method to download files as streams.
34+
/// </summary>
35+
/// <param name="request">Pre-configured request instance.</param>
36+
/// <param name="cancellationToken"></param>
37+
/// <returns>The downloaded stream.</returns>
38+
Task<Stream?> DownloadStreamAsync(RestRequest request, CancellationToken cancellationToken = default);
39+
}

src/RestSharp/KnownHeaders.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ public static class KnownHeaders {
3030
public const string ContentLocation = "Content-Location";
3131
public const string ContentRange = "Content-Range";
3232
public const string ContentType = "Content-Type";
33-
public const string Cookie = "Cookie";
3433
public const string LastModified = "Last-Modified";
3534
public const string ContentMD5 = "Content-MD5";
3635
public const string Host = "Host";
36+
public const string Cookie = "Cookie";
37+
public const string SetCookie = "Set-Cookie";
3738

3839
internal static readonly string[] ContentHeaders = {
3940
Allow, Expires, ContentDisposition, ContentEncoding, ContentLanguage, ContentLength, ContentLocation, ContentRange, ContentType, ContentMD5,

0 commit comments

Comments
 (0)