-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenSRSClient.cs
76 lines (67 loc) · 2.58 KB
/
OpenSRSClient.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
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using OpenSRS.NET.Actions;
#if NETCORE
using Microsoft.Extensions.Options;
#endif
namespace OpenSRS.NET
{
public partial class OpenSRSClient
{
#if !NETCORE
private static readonly Uri ProductionEndpoint = new Uri("https://rr-n1-tor.opensrs.net:55443");
private static readonly Uri TestingEndpoint = new Uri("https://horizon.opensrs.net:55443");
private readonly string Key;
#else
private readonly string Key = "";
#endif
private readonly HttpClient httpClient;
#if !NETCORE
public OpenSRSClient(string key, string userName, bool test = false)
{
Key = key;
httpClient = new HttpClient
{
BaseAddress = test ? TestingEndpoint : ProductionEndpoint
};
httpClient.DefaultRequestHeaders.Add("X-Username", userName);
httpClient.DefaultRequestHeaders.Add("Keep-Alive", "false");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
}
#else
public OpenSRSClient(HttpClient _httpClient, IOptions<OpenSRSClientOptions> options)
{
httpClient = _httpClient;
httpClient.DefaultRequestHeaders.Add("Keep-Alive", "false");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
Key = options.Value.Key;
}
#endif
private async Task<string> SendAsync(OpenSRSRequest request)
{
var sb = new StringBuilder();
sb.AppendLine(@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?>");
sb.AppendLine(@"<!DOCTYPE OPS_envelope SYSTEM ""ops.dtd"">");
sb.Append(request.ToXml().ToString());
var msg = sb.ToString();
var content = new StringContent(msg, Encoding.UTF8, "text/xml");
content.Headers.Add("X-Signature", ComputeSignature(msg));
using (var resp = await httpClient.PostAsync("/", content).ConfigureAwait(false))
{
return await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
private string ComputeSignature(string message)
{
// md5_hex(md5_hex($xml, $private_key),$private_key)
return Extensions.CalculateMD5Hash(Extensions.CalculateMD5Hash(message + Key) + Key);
}
}
public class OpenSRSClientOptions
{
public string Key { get; set; } = "";
}
}