|
| 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 | +} |
0 commit comments