-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuoteClient.cs
96 lines (87 loc) · 5.47 KB
/
QuoteClient.cs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
namespace JollyQuotes
{
/// <summary>
/// <see cref="QuoteResolver{T}"/> that provides special handling of <see cref="IResourceResolver"/>s of type <see cref="JollyQuotes.HttpResolver"/>.
/// </summary>
/// <typeparam name="T">Type of <see cref="IQuote"/> this class can generate.</typeparam>
public abstract partial class QuoteClient<T> : QuoteResolver<T> where T : class, IQuote
{
/// <summary>
/// <see cref="HttpClient"/> that is used to resolve the requested resources or <see langword="null"/> if <see cref="IsHttpBased"/> is <see langword="false"/>.
/// </summary>
public HttpClient? BaseClient => HttpResolver?.BaseClient;
/// <summary>
/// Returns the <see cref="QuoteResolver{T}.Resolver"/> as a <see cref="JollyQuotes.HttpResolver"/>.
/// </summary>
public HttpResolver? HttpResolver => Resolver as HttpResolver;
/// <summary>
/// Determines whether the <see cref="QuoteResolver{T}.Resolver"/> is a <see cref="JollyQuotes.HttpResolver"/>.
/// </summary>
[MemberNotNullWhen(true, nameof(HttpResolver), nameof(BaseClient))]
public bool IsHttpBased => Resolver is HttpResolver;
/// <summary>
/// Initializes a new instance of the <see cref="QuoteClient{T}"/> class with a <paramref name="source"/> specified.
/// </summary>
/// <param name="source">Source of the quotes, e.g. a link, file name or raw text.</param>
/// <param name="includeBaseAddress">Determines whether the specified <paramref name="source"/> should be applied to <see cref="HttpClient.BaseAddress"/>.</param>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="UriFormatException">Invalid format of the <paramref name="source"/>.</exception>
protected QuoteClient(string source, bool includeBaseAddress = true) : base(Internals.CreateResolver(source, includeBaseAddress), source)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="QuoteClient{T}"/> class with a specified <paramref name="uri"/>
/// that is used as a <see cref="QuoteGenerator{T}.Source"/> of the quote resources
/// and <see cref="HttpClient.BaseAddress"/> of the underlaying <see cref="BaseClient"/>.
/// </summary>
/// <param name="uri">
/// <see cref="Uri"/> that is used a <see cref="QuoteGenerator{T}.Source"/> of the quote resources
/// and <see cref="HttpClient.BaseAddress"/> of the underlaying <see cref="BaseClient"/>.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="uri"/> is <see langword="null"/>.</exception>
protected QuoteClient(Uri uri) : base(Internals.CreateResolver(uri), uri.ToString())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="QuoteClient{T}"/> class with an underlaying <paramref name="client"/> specified.
/// </summary>
/// <param name="client">Underlaying client that is used to access required resources.</param>
/// <exception cref="ArgumentNullException"><paramref name="client"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><see cref="HttpClient.BaseAddress"/> of <paramref name="client"/> cannot be <see langword="null"/> or empty when no source specified.</exception>
protected QuoteClient(HttpClient client) : base(new HttpResolver(client), client.RetrieveSourceFromClient())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="QuoteClient{T}"/> class with an underlaying <paramref name="resolver"/> specified.
/// </summary>
/// <param name="resolver"><see cref="JollyQuotes.HttpResolver"/> that is used to access the requested resources.</param>
/// <exception cref="ArgumentNullException"><paramref name="resolver"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><see cref="HttpClient.BaseAddress"/> of <paramref name="resolver"/> cannot be <see langword="null"/> or empty when no source specified.</exception>
protected QuoteClient(HttpResolver resolver) : base(resolver, resolver.BaseClient.RetrieveSourceFromClient())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="QuoteClient{T}"/> class with an underlaying <paramref name="resolver"/> and <paramref name="source"/> specified.
/// </summary>
/// <param name="resolver"><see cref="IResourceResolver"/> that is used to access the requested resources.</param>
/// <param name="source">Source of the quotes, e.g. a link, file name or raw text.</param>
/// <exception cref="ArgumentNullException"><paramref name="resolver"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
protected QuoteClient(IResourceResolver resolver, string source) : base(resolver, source)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="QuoteClient{T}"/> class with an underlaying <paramref name="client"/> and <paramref name="source"/> specified.
/// </summary>
/// <param name="client">Underlaying client that is used to access required resources.</param>
/// <param name="source">Source of the quotes, e.g. a link, file name or raw text.</param>
/// <exception cref="ArgumentNullException"><paramref name="client"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
protected QuoteClient(HttpClient client, string source) : base(new HttpResolver(client), source)
{
}
}
}