-
Notifications
You must be signed in to change notification settings - Fork 51
/
StrategyRunnerClient.cs
84 lines (72 loc) · 2.93 KB
/
StrategyRunnerClient.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DevelopmentInProgress.TradeView.Core.TradeStrategy
{
public static class StrategyRunnerClient
{
public static async Task<HttpResponseMessage> PostAsync(HttpClient httpClient, Uri uri, string jsonSerializedStrategy, IEnumerable<string> libraries, CancellationToken cancellationToken)
{
if (httpClient == null)
{
throw new ArgumentNullException(nameof(httpClient));
}
if (libraries == null)
{
throw new ArgumentNullException(nameof(libraries));
}
var stringContent = new StringContent(jsonSerializedStrategy, Encoding.UTF8, "application/json");
var byteArrayContents = new List<ByteArrayContent>();
try
{
using var client = new HttpClient();
using var multipartFormDataContent = new MultipartFormDataContent
{
{ stringContent, "strategy" }
};
foreach (var file in libraries)
{
var fileInfo = new FileInfo(file);
using var fileStream = File.OpenRead(file);
using var br = new BinaryReader(fileStream);
var byteArrayContent = new ByteArrayContent(br.ReadBytes((int)fileStream.Length));
multipartFormDataContent.Add(byteArrayContent, fileInfo.Name, fileInfo.FullName);
byteArrayContents.Add(byteArrayContent);
}
return await httpClient.PostAsync(uri, multipartFormDataContent, cancellationToken).ConfigureAwait(false);
}
finally
{
foreach (var byteArrayContent in byteArrayContents)
{
byteArrayContent.Dispose();
}
stringContent.Dispose();
}
}
public static async Task<HttpResponseMessage> PostAsync(HttpClient httpClient, Uri uri, string jsonSerializedStrategyParameters, CancellationToken cancellationToken)
{
if (httpClient == null)
{
throw new ArgumentNullException(nameof(httpClient));
}
var stringContent = new StringContent(jsonSerializedStrategyParameters, Encoding.UTF8, "application/json");
try
{
using var multipartFormDataContent = new MultipartFormDataContent
{
{ stringContent, "strategyparameters" }
};
return await httpClient.PostAsync(uri, multipartFormDataContent, cancellationToken).ConfigureAwait(false);
}
finally
{
stringContent.Dispose();
}
}
}
}