This repository was archived by the owner on Jul 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathCromwellApiClient.cs
More file actions
331 lines (276 loc) · 13.3 KB
/
CromwellApiClient.cs
File metadata and controls
331 lines (276 loc) · 13.3 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using TriggerService;
[assembly: InternalsVisibleTo("TriggerService.Tests")]
namespace CromwellApiClient
{
public class CromwellApiClient : ICromwellApiClient
{
private const string Version = "v1";
private static readonly string basePath = $"/api/workflows/{Version}";
private static readonly HttpClient httpClient = new();
private readonly string url;
public CromwellApiClient(IOptions<CromwellApiClientOptions> cromwellApiClientOptions)
{
ArgumentException.ThrowIfNullOrEmpty(cromwellApiClientOptions.Value.BaseUrl, nameof(cromwellApiClientOptions.Value.BaseUrl));
Common.NewtonsoftJsonSafeInit.SetDefaultSettings();
if (string.IsNullOrWhiteSpace(cromwellApiClientOptions.Value.BaseUrl))
{
throw new ArgumentException(null, nameof(cromwellApiClientOptions.Value.BaseUrl));
}
url = $"{cromwellApiClientOptions.Value.BaseUrl.TrimEnd('/')}{basePath}";
}
public string GetUrl()
=> url;
public async Task<GetLogsResponse> GetLogsAsync(Guid id)
=> await GetAsync<GetLogsResponse>($"/{id}/logs");
public async Task<GetOutputsResponse> GetOutputsAsync(Guid id)
=> new() { Id = id, Json = await GetAsyncWithMediaType($"/{id}/outputs", "application/json") };
public async Task<GetMetadataResponse> GetMetadataAsync(Guid id)
=> new() { Id = id, Json = await GetAsyncWithMediaType($"/{id}/metadata?expandSubWorkflows=true", "application/json") };
public async Task<GetStatusResponse> GetStatusAsync(Guid id)
=> await GetAsync<GetStatusResponse>($"/{id}/status");
public async Task<GetTimingResponse> GetTimingAsync(Guid id)
=> new() { Id = id, Html = await GetAsyncWithMediaType($"/{id}/timing", "text/html") };
public async Task<PostAbortResponse> PostAbortAsync(Guid id)
=> await PostAsync<PostAbortResponse>($"/{id}/abort", id);
public async Task<PostWorkflowResponse> PostWorkflowAsync(
string workflowUrl,
List<string> workflowInputsFilename,
List<byte[]> workflowInputsData,
string workflowOptionsFilename = null,
byte[] workflowOptionsData = null,
string workflowDependenciesFilename = null,
byte[] workflowDependenciesData = null)
{
var files = AccumulatePostFiles(
workflowInputsFilename,
workflowInputsData,
workflowOptionsFilename,
workflowOptionsData,
workflowDependenciesFilename,
workflowDependenciesData);
var parameters = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("workflowUrl", workflowUrl) };
return await PostAsync<PostWorkflowResponse>(string.Empty, files, parameters);
}
internal static List<FileToPost> AccumulatePostFiles(
List<string> workflowInputsFilename,
List<byte[]> workflowInputsData,
string workflowOptionsFilename = null,
byte[] workflowOptionsData = null,
string workflowDependenciesFilename = null,
byte[] workflowDependenciesData = null)
{
var files = new List<FileToPost>();
for (var i = 0; i < workflowInputsFilename.Count; i++)
{
var parameterName = i == 0 ? "workflowInputs" : "workflowInputs_" + (i + 1);
files.Add(new(workflowInputsFilename[i], workflowInputsData[i], parameterName, removeTabs: true));
}
if (workflowOptionsFilename is not null && workflowOptionsData is not null)
{
files.Add(new(workflowOptionsFilename, workflowOptionsData, "workflowOptions", removeTabs: true));
}
if (workflowDependenciesFilename is not null && workflowDependenciesData is not null)
{
files.Add(new(workflowDependenciesFilename, workflowDependenciesData, "workflowDependencies"));
}
return files;
}
public async Task<PostQueryResponse> PostQueryAsync(string queryJson)
=> await PostAsync<PostQueryResponse>("/query", queryJson);
private string GetApiUrl(string path)
=> $"{url}{path}";
private async Task<T> GetAsync<T>(string path)
{
HttpResponseMessage response = null;
var url = string.Empty;
try
{
url = GetApiUrl(path);
response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsAsync<T>();
}
catch (HttpRequestException httpRequestException)
{
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine($"URL: {url}");
messageBuilder.AppendLine($"StatusCode: {response?.StatusCode}");
messageBuilder.AppendLine($"Exception message: {httpRequestException.Message}");
await AppendResponseBodyAsync(response, messageBuilder);
throw new CromwellApiException(messageBuilder.ToString(), httpRequestException, response?.StatusCode);
}
catch (Exception exc)
{
throw new CromwellApiException(exc.Message, exc, response?.StatusCode);
}
}
private async Task<string> GetAsyncWithMediaType(string path, string mediaType)
{
HttpResponseMessage response = null;
var url = string.Empty;
try
{
url = GetApiUrl(path);
var request = new HttpRequestMessage()
{
RequestUri = new(url),
Method = HttpMethod.Get,
};
request.Headers.Accept.Add(new(mediaType));
response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
catch (HttpRequestException httpRequestException)
{
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine($"URL: {url}");
messageBuilder.AppendLine($"StatusCode: {response?.StatusCode}");
messageBuilder.AppendLine($"Exception message: {httpRequestException.Message}");
await AppendResponseBodyAsync(response, messageBuilder);
throw new CromwellApiException(messageBuilder.ToString(), httpRequestException, response?.StatusCode);
}
catch (Exception exc)
{
throw new CromwellApiException(exc.Message, exc, response?.StatusCode);
}
}
private async Task<T> PostAsync<T>(string path, Guid id)
{
HttpResponseMessage response = null;
var url = string.Empty;
try
{
url = GetApiUrl(path);
var content = new FormUrlEncodedContent(new KeyValuePair<string, string>[] { new("id", id.ToString()) });
response = await httpClient.PostAsync(url, content);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsAsync<T>();
}
catch (HttpRequestException httpRequestException)
{
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine($"URL: {url}");
messageBuilder.AppendLine($"StatusCode: {response?.StatusCode}");
messageBuilder.AppendLine($"Exception message: {httpRequestException.Message}");
await AppendResponseBodyAsync(response, messageBuilder);
throw new CromwellApiException(messageBuilder.ToString(), httpRequestException, response?.StatusCode);
}
catch (Exception exc)
{
throw new CromwellApiException(exc.Message, exc, response?.StatusCode);
}
}
private async Task<T> PostAsync<T>(string path, string body)
{
HttpResponseMessage response = null;
var url = string.Empty;
try
{
url = GetApiUrl(path);
response = await httpClient.PostAsync(url, new StringContent(body, Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsAsync<T>();
}
catch (HttpRequestException httpRequestException)
{
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine($"URL: {url}");
messageBuilder.AppendLine($"StatusCode: {response?.StatusCode}");
messageBuilder.AppendLine($"Exception message: {httpRequestException.Message}");
await AppendResponseBodyAsync(response, messageBuilder);
throw new CromwellApiException(messageBuilder.ToString(), httpRequestException, response?.StatusCode);
}
catch (Exception exc)
{
throw new CromwellApiException(exc.Message, exc, response?.StatusCode);
}
}
private async Task<T> PostAsync<T>(string path, IEnumerable<FileToPost> files, IEnumerable<KeyValuePair<string, string>> parameters = null)
{
HttpResponseMessage response = null;
var url = string.Empty;
try
{
url = GetApiUrl(path);
using var formContent = new MultipartFormDataContent(Guid.NewGuid().ToString());
formContent.Headers.ContentType.MediaType = "multipart/form-data";
foreach (var parameter in parameters)
{
formContent.Add(new StringContent(parameter.Value), parameter.Key);
}
foreach (var file in files)
{
formContent.Add(new ByteArrayContent(file.Data), file.ParameterName, file.Filename);
}
var req = formContent.ToString();
response = await httpClient.PostAsync(url, formContent);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsAsync<T>();
}
catch (HttpRequestException httpRequestException)
{
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine($"URL: {url}");
messageBuilder.AppendLine($"StatusCode: {response?.StatusCode}");
messageBuilder.AppendLine($"Exception message: {httpRequestException.Message}");
await AppendResponseBodyAsync(response, messageBuilder);
throw new CromwellApiException(messageBuilder.ToString(), httpRequestException, response?.StatusCode);
}
catch (Exception exc)
{
throw new CromwellApiException(exc.Message, exc, response?.StatusCode);
}
}
private static async Task AppendResponseBodyAsync(HttpResponseMessage response, StringBuilder messageBuilder)
{
try
{
// Attempt to append the response body for additional error info
var contents = await response.Content.ReadAsStringAsync();
messageBuilder.AppendLine(contents);
}
catch
{
// Ignore exceptions. Retrieve extra error info only if possible
}
}
internal class FileToPost
{
public string ParameterName { get; set; }
public string Filename { get; set; }
public byte[] Data { get; set; }
internal FileToPost(string filename, byte[] data, string parameterName, bool removeTabs = false)
{
this.Filename = filename;
this.ParameterName = parameterName;
this.Data = removeTabs ? EncodeToUtf8AndRemoveTabsAndDecode(data) : data;
}
/// <summary>
/// Encodes a byte array to Utf8, removes tabs, and decodes back to a byte array.
/// As of 1/10/2020, Cromwell has a bug that requires tabs to be removed from JSON data
/// https://github.com/broadinstitute/cromwell/issues/3487
/// </summary>
/// <param name="data">The byte array of the file</param>
/// <returns>A new byte array of the file</returns>
private static byte[] EncodeToUtf8AndRemoveTabsAndDecode(byte[] data)
{
if (data?.Length == 0)
{
return data;
}
return Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(data).Replace("\t", " ")); // Simply removing an embedded tab may change the meaning and break JSON. The safest option is to replace one kind of whitespace with another.
}
}
}
}