-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathOwnerDetailsUriTemplateResourceV3.cs
More file actions
62 lines (53 loc) · 2.33 KB
/
OwnerDetailsUriTemplateResourceV3.cs
File metadata and controls
62 lines (53 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using NuGet.Protocol.Core.Types;
namespace NuGet.Protocol.Resources
{
/// <summary>Owner Details Uri Template for NuGet V3 HTTP feeds.</summary>
/// <remarks>Not intended to be created directly. Use <see cref="SourceRepository.GetResourceAsync{T}(CancellationToken)"/>
/// with <see cref="OwnerDetailsUriTemplateResourceV3"/> for T, and typecast to this class.
public class OwnerDetailsUriTemplateResourceV3 : INuGetResource
{
private readonly string _template;
private OwnerDetailsUriTemplateResourceV3(string template)
{
_template = template ?? throw new ArgumentNullException(nameof(template));
}
/// <summary>
/// Creates the specified Owner Details Uri template provided by the server if it exists and is valid.
/// </summary>
/// <param name="uriTemplate">The Absolute Uri template provided by the server.</param>
/// <returns>A valid Owner Details Uri template, or null.</returns>
public static OwnerDetailsUriTemplateResourceV3? CreateOrNull(Uri uriTemplate)
{
if (uriTemplate is null)
{
throw new ArgumentNullException(nameof(uriTemplate));
}
if (uriTemplate.OriginalString.Length == 0
|| !uriTemplate.IsAbsoluteUri
|| !uriTemplate.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
{
return null;
}
return new OwnerDetailsUriTemplateResourceV3(uriTemplate.OriginalString);
}
/// <summary>
/// Gets a URL for viewing package Owner URL outside of Visual Studio. The URL will not be verified to exist.
/// </summary>
/// <param name="owner">The owner username.</param>
/// <returns>The first URL from the resource, with the URI template applied.</returns>
public Uri GetUri(string owner)
{
var uriString = _template
#if NETCOREAPP
.Replace("{owner}", owner, StringComparison.OrdinalIgnoreCase);
#else
.Replace("{owner}", owner);
#endif
return new Uri(uriString);
}
}
}