Skip to content
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
2 changes: 1 addition & 1 deletion Activout.RestClient.Json/SystemTextJsonDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class SystemTextJsonDefaults
/// </summary>
public static readonly MediaType[] MediaTypes =
[
MediaType.ValueOf("application/json")
new MediaType("application/json")
];

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class NewtonsoftJsonDefaults
{
public static readonly MediaType[] SupportedMediaTypes =
[
MediaType.ValueOf("application/json")
new MediaType("application/json")
];

public static readonly JsonConverter[] DefaultJsonConverters =
Expand Down
4 changes: 2 additions & 2 deletions Activout.RestClient.Xml/XmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static class XmlHelper
{
public static readonly MediaType[] SupportedMediaTypes =
[
MediaType.ValueOf("application/xml"),
MediaType.ValueOf("text/xml")
new MediaType("application/xml"),
new MediaType("text/xml")
];
}
2 changes: 1 addition & 1 deletion Activout.RestClient/Implementation/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class RequestHandler
private const string DefaultHttpContentType = "application/octet-stream";

// https://tools.ietf.org/html/rfc7578#section-4.4
private static readonly MediaType DefaultPartContentType = MediaType.ValueOf("text/plain");
private static readonly MediaType DefaultPartContentType = new MediaType("text/plain");

private readonly Type _actualReturnType;
private readonly int _bodyArgumentIndex = -1;
Expand Down
2 changes: 1 addition & 1 deletion Activout.RestClient/Implementation/RestClientContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class RestClientContext
{
private static readonly Collection<MediaType> JsonMediaTypeCollection = new Collection<MediaType>
{
MediaType.ValueOf("application/json")
new MediaType("application/json")
};

public RestClientContext()
Expand Down
49 changes: 16 additions & 33 deletions Activout.RestClient/MediaType.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,28 @@
#nullable disable
using System;
using System.Net.Http.Headers;

namespace Activout.RestClient
namespace Activout.RestClient;

public record MediaType
{
public sealed class MediaType
public static MediaType? ValueOf(string? value)
{
public static MediaType ValueOf(string value)
{
return value == null ? null : new MediaType(value);
}

public string Value { get; }

public MediaType(string value)
{
Value = value ?? throw new ArgumentNullException(nameof(value));
if (!MediaTypeHeaderValue.TryParse(Value, out _))
{
throw new ArgumentException(nameof(value), "Failed to parse MediaType: " + value);
}
}

public override string ToString()
{
return Value;
}
return value == null ? null : new MediaType(value);
}

private bool Equals(MediaType other)
{
return Value == other.Value;
}
public string Value { get; }

public override bool Equals(object obj)
public MediaType(string value)
{
Value = value;
if (!MediaTypeHeaderValue.TryParse(Value, out _))
{
return ReferenceEquals(this, obj) || obj is MediaType other && Equals(other);
throw new ArgumentException("Failed to parse MediaType: " + value, nameof(value));
}
}

public override int GetHashCode()
{
return (Value != null ? Value.GetHashCode() : 0);
}
public override string ToString()
{
return Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class ByteArrayDeserializer : IDeserializer
{
public IReadOnlyCollection<MediaType> SupportedMediaTypes => new[]
{
MediaType.ValueOf("application/octet-stream")
new MediaType("application/octet-stream")
};

public int Order { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FormUrlEncodedSerializer : ISerializer
{
public IReadOnlyCollection<MediaType> SupportedMediaTypes => new[]
{
MediaType.ValueOf("application/x-www-form-urlencoded")
new MediaType("application/x-www-form-urlencoded")
};

public int Order { get; set; }
Expand Down
Loading