5
5
using System . IO ;
6
6
using System . Net . Http ;
7
7
using System . Security ;
8
+ using System . Text . Json . Nodes ;
8
9
using System . Threading ;
9
10
using System . Threading . Tasks ;
10
11
using Microsoft . OpenApi . Interfaces ;
@@ -29,13 +30,32 @@ public OpenApiDocument Parse(string input, out OpenApiDiagnostic diagnostic, Ope
29
30
30
31
/// <inheritdoc/>
31
32
public OpenApiDocument Read ( string url , out OpenApiDiagnostic diagnostic , OpenApiReaderSettings settings = null )
33
+ {
34
+ var stream = GetStream ( url ) ;
35
+ return Read ( stream , out diagnostic , settings ) ;
36
+ }
37
+
38
+ /// <inheritdoc/>
39
+ public OpenApiDocument Read ( Stream stream , out OpenApiDiagnostic diagnostic , OpenApiReaderSettings settings = null )
40
+ {
41
+ return new OpenApiStreamReader ( settings ) . Read ( stream , out diagnostic ) ;
42
+ }
43
+
44
+ /// <inheritdoc/>
45
+ public OpenApiDocument Read ( TextReader input , out OpenApiDiagnostic diagnostic , OpenApiReaderSettings settings = null )
46
+ {
47
+ return new OpenApiTextReaderReader ( settings ) . Read ( input , out diagnostic ) ;
48
+ }
49
+
50
+ /// <inheritdoc/>
51
+ public async Task < ReadResult > ReadAsync ( string url , OpenApiReaderSettings settings = null , CancellationToken cancellationToken = default )
32
52
{
33
53
Stream stream ;
34
54
if ( url . StartsWith ( "http" , StringComparison . OrdinalIgnoreCase ) || url . StartsWith ( "https" , StringComparison . OrdinalIgnoreCase ) )
35
55
{
36
56
try
37
57
{
38
- stream = _httpClient . GetStreamAsync ( new Uri ( url ) ) . GetAwaiter ( ) . GetResult ( ) ;
58
+ stream = await _httpClient . GetStreamAsync ( new Uri ( url ) ) ;
39
59
}
40
60
catch ( HttpRequestException ex )
41
61
{
@@ -63,30 +83,96 @@ SecurityException or
63
83
}
64
84
}
65
85
66
- return Read ( stream , out diagnostic , settings ) ;
86
+ return await ReadAsync ( stream , settings , cancellationToken ) ;
67
87
}
68
88
69
- /// <inheritdoc/>
70
- public OpenApiDocument Read ( Stream stream , out OpenApiDiagnostic diagnostic , OpenApiReaderSettings settings = null )
89
+ /// <inheritdoc/>
90
+ public async Task < ReadResult > ReadAsync ( Stream stream , OpenApiReaderSettings settings = null , CancellationToken cancellationToken = default )
71
91
{
72
- return new OpenApiStreamReader ( settings ) . Read ( stream , out diagnostic ) ;
92
+ return await new OpenApiStreamReader ( settings ) . ReadAsync ( stream , cancellationToken ) ;
73
93
}
74
94
75
95
/// <inheritdoc/>
76
- public OpenApiDocument Read ( TextReader input , out OpenApiDiagnostic diagnostic , OpenApiReaderSettings settings = null )
96
+ public async Task < ReadResult > ReadAsync ( TextReader input ,
97
+ OpenApiReaderSettings settings = null ,
98
+ CancellationToken cancellationToken = default )
77
99
{
78
- return new OpenApiTextReaderReader ( settings ) . Read ( input , out diagnostic ) ;
100
+ return await new OpenApiTextReaderReader ( settings ) . ReadAsync ( input , cancellationToken ) ;
101
+ }
102
+
103
+
104
+ /// <summary>
105
+ /// Takes in an input URL and parses it into an Open API document
106
+ /// </summary>
107
+ /// <param name="url">The path to the Open API file</param>
108
+ /// <param name="version">The OpenAPI specification version.</param>
109
+ /// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
110
+ /// <param name="settings">The Reader settings to be used during parsing.</param>
111
+ /// <returns></returns>
112
+ /// <exception cref="ArgumentException"></exception>
113
+ public T Read < T > ( string url ,
114
+ OpenApiSpecVersion version ,
115
+ out OpenApiDiagnostic diagnostic ,
116
+ OpenApiReaderSettings settings = null ) where T : IOpenApiElement
117
+ {
118
+ settings ??= new OpenApiReaderSettings ( ) ;
119
+ var stream = GetStream ( url ) ;
120
+ return Read < T > ( stream , version , out diagnostic , settings ) ;
79
121
}
80
122
81
123
/// <inheritdoc/>
82
- public async Task < ReadResult > ReadAsync ( string url , OpenApiReaderSettings settings = null , CancellationToken cancellationToken = default )
124
+ public T Read < T > ( Stream input ,
125
+ OpenApiSpecVersion version ,
126
+ out OpenApiDiagnostic diagnostic ,
127
+ OpenApiReaderSettings settings = null ) where T : IOpenApiElement
128
+ {
129
+ return new OpenApiStreamReader ( settings ) . ReadFragment < T > ( input , version , out diagnostic ) ;
130
+ }
131
+
132
+ /// <inheritdoc/>
133
+ public T Read < T > ( TextReader input ,
134
+ OpenApiSpecVersion version ,
135
+ out OpenApiDiagnostic diagnostic ,
136
+ OpenApiReaderSettings settings = null ) where T : IOpenApiElement
137
+ {
138
+ return new OpenApiTextReaderReader ( settings ) . ReadFragment < T > ( input , version , out diagnostic ) ;
139
+ }
140
+
141
+ /// <inheritdoc/>
142
+ public T Read < T > ( JsonNode input ,
143
+ OpenApiSpecVersion version ,
144
+ out OpenApiDiagnostic diagnostic ,
145
+ OpenApiReaderSettings settings = null ) where T : IOpenApiElement
146
+ {
147
+ return new OpenApiYamlDocumentReader ( settings ) . ReadFragment < T > ( input , version , out diagnostic ) ;
148
+ }
149
+
150
+ /// <summary>
151
+ /// Parses an input string into an Open API document.
152
+ /// </summary>
153
+ /// <param name="input"></param>
154
+ /// <param name="version"></param>
155
+ /// <param name="diagnostic"></param>
156
+ /// <param name="settings"></param>
157
+ /// <returns></returns>
158
+ public T Parse < T > ( string input ,
159
+ OpenApiSpecVersion version ,
160
+ out OpenApiDiagnostic diagnostic ,
161
+ OpenApiReaderSettings settings = null ) where T : IOpenApiElement
162
+ {
163
+ settings ??= new OpenApiReaderSettings ( ) ;
164
+ using var reader = new StringReader ( input ) ;
165
+ return Read < T > ( reader , version , out diagnostic , settings ) ;
166
+ }
167
+
168
+ private Stream GetStream ( string url )
83
169
{
84
170
Stream stream ;
85
171
if ( url . StartsWith ( "http" , StringComparison . OrdinalIgnoreCase ) || url . StartsWith ( "https" , StringComparison . OrdinalIgnoreCase ) )
86
172
{
87
173
try
88
174
{
89
- stream = await _httpClient . GetStreamAsync ( new Uri ( url ) ) ;
175
+ stream = _httpClient . GetStreamAsync ( new Uri ( url ) ) . GetAwaiter ( ) . GetResult ( ) ;
90
176
}
91
177
catch ( HttpRequestException ex )
92
178
{
@@ -114,21 +200,7 @@ SecurityException or
114
200
}
115
201
}
116
202
117
- return await ReadAsync ( stream , settings , cancellationToken ) ;
118
- }
119
-
120
- /// <inheritdoc/>
121
- public async Task < ReadResult > ReadAsync ( Stream stream , OpenApiReaderSettings settings = null , CancellationToken cancellationToken = default )
122
- {
123
- return await new OpenApiStreamReader ( settings ) . ReadAsync ( stream , cancellationToken ) ;
124
- }
125
-
126
- /// <inheritdoc/>
127
- public async Task < ReadResult > ReadAsync ( TextReader input ,
128
- OpenApiReaderSettings settings = null ,
129
- CancellationToken cancellationToken = default )
130
- {
131
- return await new OpenApiTextReaderReader ( settings ) . ReadAsync ( input , cancellationToken ) ;
132
- }
203
+ return stream ;
204
+ }
133
205
}
134
206
}
0 commit comments