8
8
using System . Net . Http ;
9
9
using System . Net . Http . Headers ;
10
10
using System . Text . Json . Serialization ;
11
+ using System . IO ;
11
12
12
13
namespace PnP . PowerShell . Commands . Base
13
14
{
14
15
[ Cmdlet ( VerbsLifecycle . Invoke , "PnPGraphMethod" , DefaultParameterSetName = ParameterSet_TOCONSOLE ) ]
15
16
public class InvokeGraphMethod : PnPGraphCmdlet
16
17
{
18
+ private const string ParameterSet_TOSTREAM = "Out to stream" ;
17
19
private const string ParameterSet_TOFILE = "Out to file" ;
18
20
private const string ParameterSet_TOCONSOLE = "Out to console" ;
19
21
@@ -25,6 +27,7 @@ public class InvokeGraphMethod : PnPGraphCmdlet
25
27
26
28
[ Parameter ( Mandatory = true , Position = 0 , ValueFromPipeline = true , ParameterSetName = ParameterSet_TOFILE ) ]
27
29
[ Parameter ( Mandatory = true , Position = 0 , ValueFromPipeline = true , ParameterSetName = ParameterSet_TOCONSOLE ) ]
30
+ [ Parameter ( Mandatory = true , Position = 0 , ValueFromPipeline = true , ParameterSetName = ParameterSet_TOSTREAM ) ]
28
31
public string Url
29
32
{
30
33
get { return _url ; }
@@ -48,16 +51,19 @@ public string Url
48
51
49
52
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOFILE ) ]
50
53
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOCONSOLE ) ]
54
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOSTREAM ) ]
51
55
public object Content ;
52
56
53
57
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOFILE ) ]
54
58
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOCONSOLE ) ]
59
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOSTREAM ) ]
55
60
public string ContentType = "application/json" ;
56
61
57
62
IDictionary < string , string > additionalHeaders = null ;
58
63
59
64
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOFILE ) ]
60
65
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOCONSOLE ) ]
66
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOSTREAM ) ]
61
67
public IDictionary < string , string > AdditionalHeaders
62
68
{
63
69
get
@@ -81,6 +87,7 @@ public IDictionary<string, string> AdditionalHeaders
81
87
82
88
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOFILE ) ]
83
89
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOCONSOLE ) ]
90
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOSTREAM ) ]
84
91
public SwitchParameter ConsistencyLevelEventual ;
85
92
86
93
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_TOCONSOLE ) ]
@@ -92,6 +99,9 @@ public IDictionary<string, string> AdditionalHeaders
92
99
[ Parameter ( Mandatory = true , ParameterSetName = ParameterSet_TOFILE ) ]
93
100
public string OutFile ;
94
101
102
+ [ Parameter ( Mandatory = true , ParameterSetName = ParameterSet_TOSTREAM ) ]
103
+ public SwitchParameter OutStream ;
104
+
95
105
protected override void ExecuteCmdlet ( )
96
106
{
97
107
try
@@ -133,7 +143,7 @@ private void SendRequest()
133
143
switch ( Method )
134
144
{
135
145
case HttpRequestMethod . Get :
136
- if ( string . IsNullOrWhiteSpace ( OutFile ) )
146
+ if ( ParameterSetName == ParameterSet_TOCONSOLE )
137
147
{
138
148
GetRequestWithPaging ( ) ;
139
149
}
@@ -283,24 +293,46 @@ private void DeleteRequest()
283
293
284
294
private void HandleResponse ( HttpResponseMessage response )
285
295
{
286
- if ( ParameterSpecified ( nameof ( OutFile ) ) )
296
+ switch ( ParameterSetName )
287
297
{
288
- var responseStream = response . Content . ReadAsStreamAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
298
+ case ParameterSet_TOCONSOLE :
299
+ var result = response . Content . ReadAsStringAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
289
300
290
- WriteVerbose ( $ "Writing { responseStream . Length } bytes response to { OutFile } ") ;
301
+ WriteVerbose ( $ "Returning { result . Length } characters response") ;
291
302
292
- using var fileStream = new System . IO . FileStream ( OutFile , System . IO . FileMode . Create , System . IO . FileAccess . Write ) ;
293
- responseStream . CopyTo ( fileStream ) ;
294
- fileStream . Close ( ) ;
295
- return ;
296
- }
297
- else
298
- {
299
- var result = response . Content . ReadAsStringAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
303
+ WriteGraphResult ( result ) ;
304
+ break ;
305
+
306
+ case ParameterSet_TOFILE :
307
+ using ( var responseStreamForFile = response . Content . ReadAsStream ( ) )
308
+ {
309
+ WriteVerbose ( $ "Writing { responseStreamForFile . Length } bytes response to { OutFile } ") ;
310
+
311
+ using ( var fileStream = new System . IO . FileStream ( OutFile , System . IO . FileMode . Create , System . IO . FileAccess . Write ) )
312
+ {
313
+ responseStreamForFile . CopyTo ( fileStream ) ;
314
+ fileStream . Close ( ) ;
315
+ }
316
+ }
317
+ break ;
318
+
319
+ case ParameterSet_TOSTREAM :
320
+ var responseStream = response . Content . ReadAsStream ( ) ;
321
+
322
+ WriteVerbose ( $ "Writing { responseStream . Length } bytes response to outputstream") ;
323
+
324
+ var memoryStream = new System . IO . MemoryStream ( ) ;
325
+ responseStream . CopyTo ( memoryStream ) ;
326
+ memoryStream . Position = 0 ;
327
+
328
+ responseStream . Close ( ) ;
329
+ responseStream . Dispose ( ) ;
330
+
331
+ WriteObject ( memoryStream ) ;
332
+ break ;
300
333
301
- WriteVerbose ( $ "Returning { result . Length } characters response") ;
302
-
303
- WriteGraphResult ( result ) ;
334
+ default :
335
+ throw new Exception ( $ "Parameter set { ParameterSetName } not supported") ;
304
336
}
305
337
}
306
338
}
0 commit comments