4
4
using System . Collections . Generic ;
5
5
using System . ComponentModel ;
6
6
using System . IO ;
7
+ using System . Linq ;
7
8
using System . Net . Http ;
8
9
using System . Text ;
9
10
using System . Text . Json ;
@@ -94,12 +95,9 @@ public async Task<string> ExecuteCodeAsync([Description("The valid Python code t
94
95
95
96
var requestBody = new SessionsPythonCodeExecutionProperties ( this . _settings , code ) ;
96
97
97
- using var request = new HttpRequestMessage ( HttpMethod . Post , $ "{ this . _poolManagementEndpoint } /executions?identifier={ this . _settings . SessionId } &api-version={ ApiVersion } ")
98
- {
99
- Content = new StringContent ( JsonSerializer . Serialize ( requestBody ) , Encoding . UTF8 , "application/json" )
100
- } ;
98
+ using var content = new StringContent ( JsonSerializer . Serialize ( requestBody ) , Encoding . UTF8 , "application/json" ) ;
101
99
102
- using var response = await httpClient . SendWithSuccessCheckAsync ( request , CancellationToken . None ) . ConfigureAwait ( false ) ;
100
+ using var response = await this . SendAsync ( httpClient , HttpMethod . Post , "executions" , content ) . ConfigureAwait ( false ) ;
103
101
104
102
var responseContent = JsonSerializer . Deserialize < JsonElement > ( await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ) ;
105
103
@@ -139,15 +137,13 @@ public async Task<SessionsRemoteFileMetadata> UploadFileAsync(
139
137
await this . AddHeadersAsync ( httpClient ) . ConfigureAwait ( false ) ;
140
138
141
139
using var fileContent = new ByteArrayContent ( File . ReadAllBytes ( localFilePath ) ) ;
142
- using var request = new HttpRequestMessage ( HttpMethod . Post , $ "{ this . _poolManagementEndpoint } files?identifier={ this . _settings . SessionId } &api-version={ ApiVersion } ")
140
+
141
+ using var multipartFormDataContent = new MultipartFormDataContent ( )
143
142
{
144
- Content = new MultipartFormDataContent
145
- {
146
- { fileContent , "file" , remoteFileName } ,
147
- }
143
+ { fileContent , "file" , remoteFileName } ,
148
144
} ;
149
145
150
- using var response = await httpClient . SendWithSuccessCheckAsync ( request , CancellationToken . None ) . ConfigureAwait ( false ) ;
146
+ using var response = await this . SendAsync ( httpClient , HttpMethod . Post , "files" , multipartFormDataContent ) . ConfigureAwait ( false ) ;
151
147
152
148
var stringContent = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
153
149
@@ -172,9 +168,7 @@ public async Task<byte[]> DownloadFileAsync(
172
168
using var httpClient = this . _httpClientFactory . CreateClient ( ) ;
173
169
await this . AddHeadersAsync ( httpClient ) . ConfigureAwait ( false ) ;
174
170
175
- using var request = new HttpRequestMessage ( HttpMethod . Get , $ "{ this . _poolManagementEndpoint } /files/{ Uri . EscapeDataString ( remoteFileName ) } /content?identifier={ this . _settings . SessionId } &api-version={ ApiVersion } ") ;
176
-
177
- using var response = await httpClient . SendWithSuccessCheckAsync ( request , CancellationToken . None ) . ConfigureAwait ( false ) ;
171
+ using var response = await this . SendAsync ( httpClient , HttpMethod . Get , $ "files/{ Uri . EscapeDataString ( remoteFileName ) } /content") . ConfigureAwait ( false ) ;
178
172
179
173
var fileContent = await response . Content . ReadAsByteArrayAsync ( ) . ConfigureAwait ( false ) ;
180
174
@@ -205,9 +199,7 @@ public async Task<IReadOnlyList<SessionsRemoteFileMetadata>> ListFilesAsync()
205
199
using var httpClient = this . _httpClientFactory . CreateClient ( ) ;
206
200
await this . AddHeadersAsync ( httpClient ) . ConfigureAwait ( false ) ;
207
201
208
- using var request = new HttpRequestMessage ( HttpMethod . Get , $ "{ this . _poolManagementEndpoint } /files?identifier={ this . _settings . SessionId } &api-version={ ApiVersion } ") ;
209
-
210
- using var response = await httpClient . SendWithSuccessCheckAsync ( request , CancellationToken . None ) . ConfigureAwait ( false ) ;
202
+ using var response = await this . SendAsync ( httpClient , HttpMethod . Get , "files" ) . ConfigureAwait ( false ) ;
211
203
212
204
var jsonElementResult = JsonSerializer . Deserialize < JsonElement > ( await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ) ;
213
205
@@ -262,6 +254,36 @@ private async Task AddHeadersAsync(HttpClient httpClient)
262
254
}
263
255
}
264
256
257
+ /// <summary>
258
+ /// Sends an HTTP request to the specified path with the specified method and content.
259
+ /// </summary>
260
+ /// <param name="httpClient">The HTTP client to use.</param>
261
+ /// <param name="method">The HTTP method to use.</param>
262
+ /// <param name="path">The path to send the request to.</param>
263
+ /// <param name="httpContent">The content to send with the request.</param>
264
+ /// <returns>The HTTP response message.</returns>
265
+ private async Task < HttpResponseMessage > SendAsync ( HttpClient httpClient , HttpMethod method , string path , HttpContent ? httpContent = null )
266
+ {
267
+ // The query string is the same for all operations
268
+ var pathWithQueryString = $ "{ path } ?identifier={ this . _settings . SessionId } &api-version={ ApiVersion } ";
269
+
270
+ var uri = new Uri ( this . _poolManagementEndpoint , pathWithQueryString ) ;
271
+
272
+ // If a list of allowed domains has been provided, the host of the provided
273
+ // uri is checked to verify it is in the allowed domain list.
274
+ if ( ! this . _settings . AllowedDomains ? . Contains ( uri . Host ) ?? false )
275
+ {
276
+ throw new InvalidOperationException ( "Sending requests to the provided location is not allowed." ) ;
277
+ }
278
+
279
+ using var request = new HttpRequestMessage ( method , uri )
280
+ {
281
+ Content = httpContent ,
282
+ } ;
283
+
284
+ return await httpClient . SendWithSuccessCheckAsync ( request , CancellationToken . None ) . ConfigureAwait ( false ) ;
285
+ }
286
+
265
287
#if NET
266
288
[ GeneratedRegex ( @"^(\s|`)*(?i:python)?\s*" , RegexOptions . ExplicitCapture ) ]
267
289
private static partial Regex RemoveLeadingWhitespaceBackticksPython ( ) ;
0 commit comments