Skip to content

Commit 99b76af

Browse files
Matthias Gessingercommonsensesoftware
authored andcommitted
Add DeprecationPolicy class
1 parent 1cb11b0 commit 99b76af

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Asp.Versioning;
4+
5+
/// <summary>
6+
/// Represents an API version deprecation policy.
7+
/// </summary>
8+
public class DeprecationPolicy
9+
{
10+
private readonly LinkList links;
11+
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="DeprecationPolicy"/> class.
14+
/// </summary>
15+
public DeprecationPolicy()
16+
{
17+
links = new DeprecationLinkList();
18+
}
19+
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="DeprecationPolicy"/> class.
22+
/// </summary>
23+
/// <param name="date">The date and time when the API version will be deprecated.</param>
24+
/// <param name="link">The optional link which provides information about the deprecation policy.</param>
25+
public DeprecationPolicy( DateTimeOffset date, LinkHeaderValue? link = default )
26+
: this()
27+
{
28+
Date = date;
29+
30+
if ( link is not null )
31+
{
32+
links.Add( link );
33+
}
34+
}
35+
36+
/// <summary>
37+
/// Initializes a new instance of the <see cref="DeprecationPolicy"/> class.
38+
/// </summary>
39+
/// <param name="link">The link which provides information about the deprecation policy.</param>
40+
public DeprecationPolicy( LinkHeaderValue link )
41+
: this()
42+
{
43+
links.Add( link );
44+
}
45+
46+
/// <summary>
47+
/// Gets the date and time when the API version will be deprecated.
48+
/// </summary>
49+
/// <value>The date and time when the API version will be deprecated, if any.</value>
50+
public DateTimeOffset? Date { get; }
51+
52+
/// <summary>
53+
/// Gets a value indicating whether the deprecation policy has any associated links.
54+
/// </summary>
55+
/// <value>True if the deprecation policy has associated links; otherwise, false.</value>
56+
public bool HasLinks => links.Count > 0;
57+
58+
/// <summary>
59+
/// Gets a read-only list of links that provide information about the deprecation policy.
60+
/// </summary>
61+
/// <value>A read-only list of HTTP links.</value>
62+
/// <remarks>If a link is provided, generally only one link is necessary; however, additional
63+
/// links might be provided for different languages or different formats such as a HTML page
64+
/// or a JSON file.</remarks>
65+
public IList<LinkHeaderValue> Links => links;
66+
67+
internal sealed class DeprecationLinkList : LinkList
68+
{
69+
protected override void EnsureRelationType( LinkHeaderValue item )
70+
{
71+
if ( !item.RelationType.Equals( "deprecation", StringComparison.OrdinalIgnoreCase ) )
72+
{
73+
throw new ArgumentException( SR.InvalidDeprecationRelationType, nameof( item ) );
74+
}
75+
}
76+
}
77+
}

src/Abstractions/src/Asp.Versioning.Abstractions/SR.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Abstractions/src/Asp.Versioning.Abstractions/SR.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,7 @@
144144
<data name="InvalidSunsetRelationType" xml:space="preserve">
145145
<value>The relation type for a sunset policy link must be "sunset".</value>
146146
</data>
147+
<data name="InvalidDeprecationRelationType" xml:space="preserve">
148+
<value>The relation type for a deprecation policy link must be "deprecation".</value>
149+
</data>
147150
</root>

0 commit comments

Comments
 (0)