5
5
using System . Linq ;
6
6
using System . Net ;
7
7
using System . Net . Http ;
8
- using System . Text ;
9
8
using System . Threading ;
10
9
using System . Threading . Tasks ;
11
10
@@ -43,8 +42,7 @@ internal ContainerOperations(DockerClient client)
43
42
}
44
43
45
44
IQueryString queryParameters = new QueryString < ContainersListParameters > ( parameters ) ;
46
- var response = await _client . MakeRequestAsync ( _client . NoErrorHandlers , HttpMethod . Get , "containers/json" , queryParameters , cancellationToken ) . ConfigureAwait ( false ) ;
47
- return _client . JsonSerializer . DeserializeObject < ContainerListResponse [ ] > ( response . Body ) ;
45
+ return await _client . MakeRequestAsync < ContainerListResponse [ ] > ( _client . NoErrorHandlers , HttpMethod . Get , "containers/json" , queryParameters , cancellationToken ) . ConfigureAwait ( false ) ;
48
46
}
49
47
50
48
public async Task < CreateContainerResponse > CreateContainerAsync ( CreateContainerParameters parameters , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -62,8 +60,7 @@ internal ContainerOperations(DockerClient client)
62
60
}
63
61
64
62
var data = new JsonRequestContent < CreateContainerParameters > ( parameters , _client . JsonSerializer ) ;
65
- var response = await _client . MakeRequestAsync ( new [ ] { NoSuchImageHandler } , HttpMethod . Post , "containers/create" , qs , data , cancellationToken ) . ConfigureAwait ( false ) ;
66
- return _client . JsonSerializer . DeserializeObject < CreateContainerResponse > ( response . Body ) ;
63
+ return await _client . MakeRequestAsync < CreateContainerResponse > ( new [ ] { NoSuchImageHandler } , HttpMethod . Post , "containers/create" , qs , data , cancellationToken ) . ConfigureAwait ( false ) ;
67
64
}
68
65
69
66
public async Task < ContainerInspectResponse > InspectContainerAsync ( string id , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -73,8 +70,7 @@ internal ContainerOperations(DockerClient client)
73
70
throw new ArgumentNullException ( nameof ( id ) ) ;
74
71
}
75
72
76
- var response = await _client . MakeRequestAsync ( new [ ] { NoSuchContainerHandler } , HttpMethod . Get , $ "containers/{ id } /json", cancellationToken ) . ConfigureAwait ( false ) ;
77
- return _client . JsonSerializer . DeserializeObject < ContainerInspectResponse > ( response . Body ) ;
73
+ return await _client . MakeRequestAsync < ContainerInspectResponse > ( new [ ] { NoSuchContainerHandler } , HttpMethod . Get , $ "containers/{ id } /json", cancellationToken ) . ConfigureAwait ( false ) ;
78
74
}
79
75
80
76
public async Task < ContainerProcessesResponse > ListProcessesAsync ( string id , ContainerListProcessesParameters parameters , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -90,8 +86,7 @@ internal ContainerOperations(DockerClient client)
90
86
}
91
87
92
88
IQueryString queryParameters = new QueryString < ContainerListProcessesParameters > ( parameters ) ;
93
- var response = await _client . MakeRequestAsync ( new [ ] { NoSuchContainerHandler } , HttpMethod . Get , $ "containers/{ id } /top", queryParameters , cancellationToken ) . ConfigureAwait ( false ) ;
94
- return _client . JsonSerializer . DeserializeObject < ContainerProcessesResponse > ( response . Body ) ;
89
+ return await _client . MakeRequestAsync < ContainerProcessesResponse > ( new [ ] { NoSuchContainerHandler } , HttpMethod . Get , $ "containers/{ id } /top", queryParameters , cancellationToken ) . ConfigureAwait ( false ) ;
95
90
}
96
91
97
92
public Task < Stream > GetContainerLogsAsync ( string id , ContainerLogsParameters parameters , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -145,8 +140,7 @@ public Task GetContainerLogsAsync(string id, ContainerLogsParameters parameters,
145
140
throw new ArgumentNullException ( nameof ( id ) ) ;
146
141
}
147
142
148
- var response = await _client . MakeRequestAsync ( new [ ] { NoSuchContainerHandler } , HttpMethod . Get , $ "containers/{ id } /changes", cancellationToken ) . ConfigureAwait ( false ) ;
149
- return _client . JsonSerializer . DeserializeObject < ContainerFileSystemChangeResponse [ ] > ( response . Body ) ;
143
+ return await _client . MakeRequestAsync < ContainerFileSystemChangeResponse [ ] > ( new [ ] { NoSuchContainerHandler } , HttpMethod . Get , $ "containers/{ id } /changes", cancellationToken ) . ConfigureAwait ( false ) ;
150
144
}
151
145
152
146
public Task < Stream > ExportContainerAsync ( string id , CancellationToken cancellationToken )
@@ -208,8 +202,9 @@ public Task<Stream> GetContainerStatsAsync(string id, ContainerStatsParameters p
208
202
}
209
203
210
204
var queryParams = parameters == null ? null : new QueryString < ContainerStartParameters > ( parameters ) ;
211
- var response = await _client . MakeRequestAsync ( new [ ] { NoSuchContainerHandler } , HttpMethod . Post , $ "containers/{ id } /start", queryParams , cancellationToken ) . ConfigureAwait ( false ) ;
212
- return response . StatusCode != HttpStatusCode . NotModified ;
205
+ bool ? result = null ;
206
+ await _client . MakeRequestAsync ( new [ ] { NoSuchContainerHandler , ( statusCode , _ ) => result = statusCode != HttpStatusCode . NotModified } , HttpMethod . Post , $ "containers/{ id } /start", queryParams , cancellationToken ) . ConfigureAwait ( false ) ;
207
+ return result ?? throw new InvalidOperationException ( ) ;
213
208
}
214
209
215
210
public async Task < bool > StopContainerAsync ( string id , ContainerStopParameters parameters , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -227,8 +222,9 @@ public Task<Stream> GetContainerStatsAsync(string id, ContainerStatsParameters p
227
222
IQueryString queryParameters = new QueryString < ContainerStopParameters > ( parameters ) ;
228
223
// since specified wait timespan can be greater than HttpClient's default, we set the
229
224
// client timeout to infinite and provide a cancellation token.
230
- var response = await _client . MakeRequestAsync ( new [ ] { NoSuchContainerHandler } , HttpMethod . Post , $ "containers/{ id } /stop", queryParameters , null , null , TimeSpan . FromMilliseconds ( Timeout . Infinite ) , cancellationToken ) . ConfigureAwait ( false ) ;
231
- return response . StatusCode != HttpStatusCode . NotModified ;
225
+ bool ? result = null ;
226
+ await _client . MakeRequestAsync ( new [ ] { NoSuchContainerHandler , ( statusCode , _ ) => result = statusCode != HttpStatusCode . NotModified } , HttpMethod . Post , $ "containers/{ id } /stop", queryParameters , null , null , TimeSpan . FromMilliseconds ( Timeout . Infinite ) , cancellationToken ) . ConfigureAwait ( false ) ;
227
+ return result ?? throw new InvalidOperationException ( ) ;
232
228
}
233
229
234
230
public Task RestartContainerAsync ( string id , ContainerRestartParameters parameters , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -327,8 +323,7 @@ public Task RenameContainerAsync(string id, ContainerRenameParameters parameters
327
323
throw new ArgumentNullException ( nameof ( id ) ) ;
328
324
}
329
325
330
- var response = await _client . MakeRequestAsync ( new [ ] { NoSuchContainerHandler } , HttpMethod . Post , $ "containers/{ id } /wait", null , null , null , TimeSpan . FromMilliseconds ( Timeout . Infinite ) , cancellationToken ) . ConfigureAwait ( false ) ;
331
- return _client . JsonSerializer . DeserializeObject < ContainerWaitResponse > ( response . Body ) ;
326
+ return await _client . MakeRequestAsync < ContainerWaitResponse > ( new [ ] { NoSuchContainerHandler } , HttpMethod . Post , $ "containers/{ id } /wait", null , null , null , TimeSpan . FromMilliseconds ( Timeout . Infinite ) , cancellationToken ) . ConfigureAwait ( false ) ;
332
327
}
333
328
334
329
public Task RemoveContainerAsync ( string id , ContainerRemoveParameters parameters , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -367,9 +362,7 @@ public Task RenameContainerAsync(string id, ContainerRenameParameters parameters
367
362
368
363
var bytes = Convert . FromBase64String ( statHeader ) ;
369
364
370
- var stat = Encoding . UTF8 . GetString ( bytes , 0 , bytes . Length ) ;
371
-
372
- var pathStat = _client . JsonSerializer . DeserializeObject < ContainerPathStatResponse > ( stat ) ;
365
+ var pathStat = _client . JsonSerializer . Deserialize < ContainerPathStatResponse > ( bytes ) ;
373
366
374
367
return new GetArchiveFromContainerResponse
375
368
{
@@ -399,8 +392,7 @@ public Task RenameContainerAsync(string id, ContainerRenameParameters parameters
399
392
public async Task < ContainersPruneResponse > PruneContainersAsync ( ContainersPruneParameters parameters , CancellationToken cancellationToken )
400
393
{
401
394
var queryParameters = parameters == null ? null : new QueryString < ContainersPruneParameters > ( parameters ) ;
402
- var response = await _client . MakeRequestAsync ( _client . NoErrorHandlers , HttpMethod . Post , "containers/prune" , queryParameters , cancellationToken ) . ConfigureAwait ( false ) ;
403
- return _client . JsonSerializer . DeserializeObject < ContainersPruneResponse > ( response . Body ) ;
395
+ return await _client . MakeRequestAsync < ContainersPruneResponse > ( _client . NoErrorHandlers , HttpMethod . Post , "containers/prune" , queryParameters , cancellationToken ) . ConfigureAwait ( false ) ;
404
396
}
405
397
406
398
public async Task < ContainerUpdateResponse > UpdateContainerAsync ( string id , ContainerUpdateParameters parameters , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -416,8 +408,7 @@ public async Task<ContainersPruneResponse> PruneContainersAsync(ContainersPruneP
416
408
}
417
409
418
410
var data = new JsonRequestContent < ContainerUpdateParameters > ( parameters , _client . JsonSerializer ) ;
419
- var response = await _client . MakeRequestAsync ( new [ ] { NoSuchContainerHandler } , HttpMethod . Post , $ "containers/{ id } /update", null , data , cancellationToken ) ;
420
- return _client . JsonSerializer . DeserializeObject < ContainerUpdateResponse > ( response . Body ) ;
411
+ return await _client . MakeRequestAsync < ContainerUpdateResponse > ( new [ ] { NoSuchContainerHandler } , HttpMethod . Post , $ "containers/{ id } /update", null , data , cancellationToken ) ;
421
412
}
422
413
}
423
414
}
0 commit comments