-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathHttpClientDocumentResolver.cs
More file actions
201 lines (174 loc) · 6.27 KB
/
Copy pathHttpClientDocumentResolver.cs
File metadata and controls
201 lines (174 loc) · 6.27 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// <copyright file="HttpClientDocumentResolver.cs" company="Endjin Limited">
// Copyright (c) Endjin Limited. All rights reserved.
// </copyright>
using System.Text.Json;
using Corvus.Json.CodeGeneration.DocumentResolvers;
namespace Corvus.Json.CodeGeneration;
/// <summary>
/// Resolves a <see cref="JsonDocument"/> from an HTTP endpoint.
/// </summary>
public class HttpClientDocumentResolver : IDocumentResolver
{
private static readonly ReadOnlyMemory<char> LocalHost = "localhost".AsMemory();
private readonly HttpClient httpClient;
private readonly Dictionary<string, JsonDocument> documents = [];
private readonly IDocumentStreamPreProcessor? preProcessor;
private readonly bool supportLocalhost;
private bool disposedValue;
/// <summary>
/// Initializes a new instance of the <see cref="HttpClientDocumentResolver"/> class.
/// </summary>
/// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/> to use to resolve the uri.</param>
/// <param name="supportLocalhost">If true, we support resolving from localhost, otherwise false.</param>
public HttpClientDocumentResolver(IHttpClientFactory httpClientFactory, bool supportLocalhost = false)
{
this.httpClient = httpClientFactory.CreateClient();
this.supportLocalhost = supportLocalhost;
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpClientDocumentResolver"/> class.
/// </summary>
/// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/> to use to resolve the uri.</param>
/// <param name="preProcessor">The document stream pre-processor.</param>
/// <param name="supportLocalhost">If true, we support resolving from localhost, otherwise false.</param>
public HttpClientDocumentResolver(IHttpClientFactory httpClientFactory, IDocumentStreamPreProcessor preProcessor, bool supportLocalhost = false)
{
this.httpClient = httpClientFactory.CreateClient();
this.preProcessor = preProcessor;
this.supportLocalhost = supportLocalhost;
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpClientDocumentResolver"/> class.
/// </summary>
/// <param name="httpClient">The <see cref="HttpClient"/> to use to resolve the uri.</param>
/// <param name="preProcessor">The document stream pre-processor.</param>
public HttpClientDocumentResolver(HttpClient httpClient, IDocumentStreamPreProcessor preProcessor)
{
this.httpClient = httpClient;
this.preProcessor = preProcessor;
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpClientDocumentResolver"/> class.
/// </summary>
/// <param name="httpClient">The <see cref="HttpClient"/> to use to resolve the uri.</param>
public HttpClientDocumentResolver(HttpClient httpClient)
{
this.httpClient = httpClient;
}
/// <inheritdoc/>
public bool AddDocument(string uri, JsonDocument document)
{
this.CheckDisposed();
return this.documents.TryAdd(uri, document);
}
/// <inheritdoc/>
public bool AddDocument(IMetaSchema metaSchema)
=> this.AddDocument(metaSchema.Uri, metaSchema.Document);
/// <inheritdoc/>
public async ValueTask<JsonElement?> TryResolve(JsonReference reference)
{
this.CheckDisposed();
if (!this.supportLocalhost)
{
if (IsLocalHost(reference))
{
return default;
}
}
string uri = reference.Uri.ToString();
if (this.documents.TryGetValue(uri, out JsonDocument? result))
{
return JsonPointerUtilities.ResolvePointer(result, reference.Fragment);
}
try
{
#if NET8_0_OR_GREATER
await using Stream inputStream = await this.httpClient.GetStreamAsync(uri);
#else
using Stream inputStream = await this.httpClient.GetStreamAsync(uri);
#endif
Stream processedStream;
if (this.preProcessor is IDocumentStreamPreProcessor preProcessor)
{
processedStream = preProcessor.Process(inputStream);
}
else
{
processedStream = inputStream;
}
try
{
result = await JsonDocument.ParseAsync(processedStream);
this.documents.Add(uri, result);
if (JsonPointerUtilities.TryResolvePointer(result, reference.Fragment, out JsonElement? element))
{
return element;
}
return default;
}
finally
{
if (processedStream != inputStream)
{
processedStream.Dispose();
}
}
}
catch (Exception)
{
return default;
}
static bool IsLocalHost(JsonReference reference)
{
JsonReferenceBuilder builder = reference.AsBuilder();
return builder.Host.SequenceEqual(LocalHost.Span);
}
}
/// <inheritdoc/>
public void Reset()
{
this.CheckDisposed();
this.DisposeDocumentsAndClear();
}
/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put clean-up code in 'Dispose(bool disposing)' method
this.Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Implements the dispose pattern.
/// </summary>
/// <param name="disposing">True if we are disposing.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
this.DisposeDocumentsAndClear();
}
this.disposedValue = true;
}
}
private void DisposeDocumentsAndClear()
{
foreach (KeyValuePair<string, JsonDocument> document in this.documents)
{
document.Value.Dispose();
}
this.documents.Clear();
}
private void CheckDisposed()
{
#if NET8_0_OR_GREATER
ObjectDisposedException.ThrowIf(this.disposedValue, this);
#else
if (this.disposedValue)
{
throw new ObjectDisposedException(nameof(CompoundDocumentResolver));
}
#endif
}
}