Skip to content

Commit b94e766

Browse files
committed
String type for ContentType
1 parent 85be2f4 commit b94e766

File tree

22 files changed

+231
-187
lines changed

22 files changed

+231
-187
lines changed

src/RestSharp.Serializers.CsvHelper/CsvHelperSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public class CsvHelperSerializer : IDeserializer, IRestSerializer, ISerializer {
1616

1717
public string[] AcceptedContentTypes => new[] { TextCsvContentType, "application/x-download" };
1818

19-
public SupportsContentType SupportsContentType => x => Array.IndexOf(AcceptedContentTypes, x) != -1 || x.Contains("csv");
19+
public SupportsContentType SupportsContentType => x => Array.IndexOf(AcceptedContentTypes, x) != -1 || x.Value.Contains("csv");
2020

2121
public DataFormat DataFormat => DataFormat.None;
2222

23-
public string ContentType { get; set; } = TextCsvContentType;
23+
public ContentType ContentType { get; set; } = TextCsvContentType;
2424

2525
public CsvHelperSerializer() => _configuration = new CsvConfiguration(CultureInfo.InvariantCulture);
2626

src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class JsonNetSerializer : IRestSerializer, ISerializer, IDeserializer {
3434
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
3535
};
3636

37-
[ThreadStatic] static WriterBuffer? _writerBuffer;
37+
[ThreadStatic] static WriterBuffer? writerBuffer;
3838

3939
readonly JsonSerializer _serializer;
4040

@@ -52,7 +52,7 @@ public class JsonNetSerializer : IRestSerializer, ISerializer, IDeserializer {
5252
public string? Serialize(object? obj) {
5353
if (obj == null) return null;
5454

55-
using var writerBuffer = _writerBuffer ??= new WriterBuffer(_serializer);
55+
using var writerBuffer = JsonNetSerializer.writerBuffer ??= new WriterBuffer(_serializer);
5656

5757
_serializer.Serialize(writerBuffer.GetJsonTextWriter(), obj, obj.GetType());
5858

@@ -73,11 +73,11 @@ public class JsonNetSerializer : IRestSerializer, ISerializer, IDeserializer {
7373
public ISerializer Serializer => this;
7474
public IDeserializer Deserializer => this;
7575

76-
public string[] AcceptedContentTypes => Serializers.ContentType.JsonAccept;
76+
public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
7777

78-
public string ContentType { get; set; } = "application/json";
78+
public ContentType ContentType { get; set; } = ContentType.Json;
7979

80-
public SupportsContentType SupportsContentType => contentType => contentType.Contains("json");
80+
public SupportsContentType SupportsContentType => contentType => contentType.Value.Contains("json");
8181

8282
public DataFormat DataFormat => DataFormat.Json;
8383
}

src/RestSharp.Serializers.Xml/XmlSerializer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public class XmlSerializer : IXmlSerializer, IWithRootElement, IWithDateFormat {
2727
/// <summary>
2828
/// Default constructor, does not specify namespace
2929
/// </summary>
30-
public XmlSerializer() => ContentType = Serializers.ContentType.Xml;
30+
public XmlSerializer() { }
3131

3232
/// <summary>
3333
/// Specify the namespaced to be used when serializing
3434
/// </summary>
3535
/// <param name="namespace">XML namespace</param>
36-
public XmlSerializer(string @namespace) : this() => Namespace = @namespace;
36+
public XmlSerializer(string @namespace) => Namespace = @namespace;
3737

3838
/// <summary>
3939
/// Serialize the object as XML
@@ -103,7 +103,7 @@ public string Serialize(object obj) {
103103
/// <summary>
104104
/// Content type for serialized content
105105
/// </summary>
106-
public string ContentType { get; set; }
106+
public ContentType ContentType { get; set; } = ContentType.Xml;
107107

108108
void Map(XContainer root, object obj) {
109109
var objType = obj.GetType();

src/RestSharp/ContentType.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
using System.Net.Http.Headers;
16+
17+
namespace RestSharp;
18+
19+
public delegate bool SupportsContentType(ContentType contentType);
20+
21+
public class ContentType : IEquatable<ContentType> {
22+
ContentType(string contentType) {
23+
var ct = Ensure.NotEmptyString(contentType, nameof(contentType));
24+
if (!ct.Contains('/')) throw new ArgumentException("Invalid content type string", nameof(contentType));
25+
26+
_value = ct;
27+
}
28+
29+
public static readonly ContentType Json = "application/json";
30+
public static readonly ContentType Xml = "application/xml";
31+
public static readonly ContentType Plain = "text/plain";
32+
public static readonly ContentType Binary = "application/octet-stream";
33+
public static readonly ContentType GZip = "application/x-gzip";
34+
public static readonly ContentType FormUrlEncoded = "application/x-www-form-urlencoded";
35+
public static readonly ContentType Undefined = "undefined/undefined";
36+
37+
public string Value => _value == Undefined._value ? Plain._value : _value;
38+
39+
public static ContentType FromDataFormat(DataFormat dataFormat) => DataFormatMap[dataFormat];
40+
41+
public override string ToString() => Value;
42+
43+
public static implicit operator ContentType(string? contentType) => contentType == null ? Undefined : new(contentType);
44+
45+
public static implicit operator string(ContentType contentType) => contentType.Value;
46+
47+
public ContentType Or(ContentType? contentType) => Equals(Undefined) ? (contentType ?? Plain) : this;
48+
49+
public string OrValue(string? contentType) => Equals(Undefined) ? (contentType ?? Plain.Value) : Value;
50+
51+
public MediaTypeHeaderValue AsMediaTypeHeaderValue => MediaTypeHeaderValue.Parse(Value);
52+
53+
static readonly Dictionary<DataFormat, ContentType> DataFormatMap =
54+
new() {
55+
{ DataFormat.Xml, Xml },
56+
{ DataFormat.Json, Json },
57+
{ DataFormat.None, Plain },
58+
{ DataFormat.Binary, Binary }
59+
};
60+
61+
public static readonly string[] JsonAccept = {
62+
"application/json", "text/json", "text/x-json", "text/javascript"
63+
};
64+
65+
public static readonly string[] XmlAccept = {
66+
"application/xml", "text/xml"
67+
};
68+
69+
readonly string _value;
70+
71+
public bool Equals(ContentType? other) {
72+
if (ReferenceEquals(null, other)) return false;
73+
if (ReferenceEquals(this, other)) return true;
74+
75+
return _value == other._value;
76+
}
77+
78+
public override bool Equals(object? obj) {
79+
if (ReferenceEquals(null, obj)) return false;
80+
if (ReferenceEquals(this, obj)) return true;
81+
if (obj.GetType() != this.GetType()) return false;
82+
83+
return Equals((ContentType)obj);
84+
}
85+
86+
public override int GetHashCode() => _value.GetHashCode();
87+
}

src/RestSharp/KnownHeaders.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static class KnownHeaders {
3535
public const string Host = "Host";
3636
public const string Cookie = "Cookie";
3737
public const string SetCookie = "Set-Cookie";
38+
public const string UserAgent = "User-Agent";
3839

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

src/RestSharp/Parameters/BodyParameter.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace RestSharp;
1717

1818
public record BodyParameter : Parameter {
19-
public BodyParameter(string? name, object value, string contentType, DataFormat dataFormat = DataFormat.None)
19+
public BodyParameter(string? name, object value, ContentType contentType, DataFormat dataFormat = DataFormat.None)
2020
: base(name, Ensure.NotNull(value, nameof(value)), ParameterType.RequestBody, false) {
2121
if (dataFormat == DataFormat.Binary && value is not byte[]) {
2222
throw new ArgumentException("Binary data format needs a byte array as value");
@@ -26,7 +26,7 @@ public BodyParameter(string? name, object value, string contentType, DataFormat
2626
DataFormat = dataFormat;
2727
}
2828

29-
public BodyParameter(object value, string contentType, DataFormat dataFormat = DataFormat.None)
29+
public BodyParameter(object value, ContentType contentType, DataFormat dataFormat = DataFormat.None)
3030
: this("", value, contentType, dataFormat) { }
3131

3232
/// <summary>
@@ -41,20 +41,20 @@ public BodyParameter(object value, string contentType, DataFormat dataFormat = D
4141
}
4242

4343
public record XmlParameter : BodyParameter {
44-
public XmlParameter(string name, object value, string? xmlNamespace = null, string contentType = Serializers.ContentType.Xml)
45-
: base(name, value, contentType, DataFormat.Xml)
44+
public XmlParameter(string name, object value, string? xmlNamespace = null, ContentType? contentType = null)
45+
: base(name, value, contentType ?? ContentType.Xml, DataFormat.Xml)
4646
=> XmlNamespace = xmlNamespace;
4747

48-
public XmlParameter(object value, string? xmlNamespace = null, string contentType = Serializers.ContentType.Xml)
48+
public XmlParameter(object value, string? xmlNamespace = null, ContentType? contentType = null)
4949
: this("", value, xmlNamespace, contentType) { }
5050

5151
public string? XmlNamespace { get; }
5252
}
5353

5454
public record JsonParameter : BodyParameter {
55-
public JsonParameter(string name, object value, string contentType = Serializers.ContentType.Json)
56-
: base(name, value, contentType, DataFormat.Json) { }
55+
public JsonParameter(string name, object value, ContentType? contentType = null)
56+
: base(name, value, contentType ?? ContentType.Json, DataFormat.Json) { }
5757

58-
public JsonParameter(object value, string contentType = Serializers.ContentType.Json)
58+
public JsonParameter(object value, ContentType? contentType = null)
5959
: this("", value, contentType) { }
6060
}

src/RestSharp/Parameters/FileParameter.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public record FileParameter {
3232
/// <summary>
3333
/// MIME content type of file
3434
/// </summary>
35-
public string? ContentType { get; }
35+
public ContentType ContentType { get; }
3636

3737
/// <summary>
3838
/// Provides raw data for file
@@ -41,12 +41,12 @@ public record FileParameter {
4141

4242
public FileParameterOptions Options { get; }
4343

44-
FileParameter(string name, string fileName, Func<Stream> getFile, string? contentType, FileParameterOptions options) {
44+
FileParameter(string name, string fileName, Func<Stream> getFile, ContentType? contentType, FileParameterOptions options) {
4545
Name = name;
4646
FileName = fileName;
4747
GetFile = getFile;
4848
Options = options;
49-
ContentType = contentType ?? Serializers.ContentType.Binary;
49+
ContentType = contentType ?? ContentType.Binary;
5050
}
5151

5252
/// <summary>
@@ -58,7 +58,13 @@ public record FileParameter {
5858
/// <param name="contentType">The content type to use in the request.</param>
5959
/// <param name="options">File parameter options</param>
6060
/// <returns>The <see cref="FileParameter" /></returns>
61-
public static FileParameter Create(string name, byte[] data, string filename, string? contentType = null, FileParameterOptions? options = null) {
61+
public static FileParameter Create(
62+
string name,
63+
byte[] data,
64+
string filename,
65+
ContentType? contentType = null,
66+
FileParameterOptions? options = null
67+
) {
6268
return new FileParameter(name, filename, GetFile, contentType, options ?? new FileParameterOptions());
6369

6470
Stream GetFile() {
@@ -83,12 +89,17 @@ public static FileParameter Create(
8389
string name,
8490
Func<Stream> getFile,
8591
string fileName,
86-
string? contentType = null,
92+
ContentType? contentType = null,
8793
FileParameterOptions? options = null
8894
)
89-
=> new(name, fileName, getFile, contentType ?? Serializers.ContentType.Binary, options ?? new FileParameterOptions());
95+
=> new(name, fileName, getFile, contentType, options ?? new FileParameterOptions());
9096

91-
public static FileParameter FromFile(string fullPath, string? name = null, string? contentType = null, FileParameterOptions? options = null) {
97+
public static FileParameter FromFile(
98+
string fullPath,
99+
string? name = null,
100+
ContentType? contentType = null,
101+
FileParameterOptions? options = null
102+
) {
92103
if (!File.Exists(Ensure.NotEmptyString(fullPath, nameof(fullPath)))) throw new FileNotFoundException("File not found", fullPath);
93104

94105
var fileName = Path.GetFileName(fullPath);

src/RestSharp/Parameters/Parameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public abstract record Parameter(string? Name, object? Value, ParameterType Type
2121
/// <summary>
2222
/// MIME content type of the parameter
2323
/// </summary>
24-
public string ContentType { get; protected init; } = "text/plain";
24+
public ContentType ContentType { get; protected init; } = ContentType.Undefined;
2525

2626
/// <summary>
2727
/// Return a human-readable representation of this parameter

src/RestSharp/Parameters/ParametersCollection.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,7 @@ public ParametersCollection AddParameters(ParametersCollection parameters) {
4949

5050
public ParametersCollection GetParameters(ParameterType parameterType) => new(_parameters.Where(x => x.Type == parameterType));
5151

52-
public ParametersCollection GetParameters<T>() => new(_parameters.Where(x => x is T));
53-
54-
internal ParametersCollection GetQueryParameters(Method method) {
55-
Func<Parameter, bool> condition =
56-
!IsPost(method)
57-
? p => p.Type is ParameterType.GetOrPost or ParameterType.QueryString
58-
: p => p.Type is ParameterType.QueryString;
59-
60-
return new ParametersCollection(_parameters.Where(p => condition(p)));
61-
}
62-
63-
internal ParametersCollection? GetContentParameters(Method method)
64-
=> IsPost(method) ? new ParametersCollection(GetParameters<GetOrPostParameter>()) : null;
65-
66-
static bool IsPost(Method method) => method is Method.Post or Method.Put or Method.Patch;
52+
public IEnumerable<T> GetParameters<T>() where T : class => _parameters.OfType<T>();
6753

6854
public IEnumerator<Parameter> GetEnumerator() => _parameters.GetEnumerator();
6955

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
namespace RestSharp;
17+
18+
static class ParametersCollectionExtensions {
19+
internal static IEnumerable<Parameter> GetQueryParameters(this ParametersCollection parameters, Method method) {
20+
Func<Parameter, bool> condition =
21+
!IsPost(method)
22+
? p => p.Type is ParameterType.GetOrPost or ParameterType.QueryString
23+
: p => p.Type is ParameterType.QueryString;
24+
25+
return parameters.Where(p => condition(p));
26+
}
27+
28+
internal static IEnumerable<GetOrPostParameter> GetContentParameters(this ParametersCollection parameters, Method method)
29+
=> IsPost(method) ? parameters.GetParameters<GetOrPostParameter>() : Enumerable.Empty<GetOrPostParameter>();
30+
31+
static bool IsPost(Method method) => method is Method.Post or Method.Put or Method.Patch;
32+
}

0 commit comments

Comments
 (0)