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
|
using Google.Protobuf;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Tango.PMR.Synchronization;
namespace Tango.Transport.Web
{
public class WebTransportClient : IWebTransportClient
{
private HttpClient _httpClient;
private static JsonSerializerSettings _settings;
public string AuthenticationToken { get; set; }
public TimeSpan RequestTimeout
{
get { return _httpClient.Timeout; }
set { _httpClient.Timeout = value; }
}
static WebTransportClient()
{
_settings = new JsonSerializerSettings()
{
};
}
public WebTransportClient()
{
_httpClient = new HttpClient();
}
public WebTransportClient(String authenticationToken) : this()
{
AuthenticationToken = authenticationToken;
}
public Task<Response> PostProto<Request, Response>(String url, Request request) where Request : class, IMessage where Response : class, IMessage
{
return Task.Factory.StartNew<Response>(() =>
{
var req = new ByteArrayContent(request.ToByteArray());
req.Headers.Add("Content-Type", "application/x-protobuf");
if (AuthenticationToken != null)
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
}
else
{
_httpClient.DefaultRequestHeaders.Authorization = null;
}
var response = _httpClient.PostAsync(url, req).Result;
var data = response.Content.ReadAsByteArrayAsync().Result;
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
HttpProtoException exception = HttpProtoException.Parser.ParseFrom(data);
throw new HttpException(exception.StatusCode, exception.Message);
}
Response dummy = Activator.CreateInstance<Response>() as Response;
return dummy.GetParser().ParseFrom(data) as Response;
});
}
public Task<Response> PostJson<Request, Response>(string url, Request request) where Request : class, IWebRequestMessage where Response : class, IWebResponseMessage
{
return Task.Factory.StartNew<Response>(() =>
{
var req = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request, _settings)));
req.Headers.Add("Content-Type", "application/json");
if (AuthenticationToken != null)
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
}
else
{
_httpClient.DefaultRequestHeaders.Authorization = null;
}
var response = _httpClient.PostAsync(url, req).Result;
var data = response.Content.ReadAsStringAsync().Result;
try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
bool handled = false;
try
{
String message = JObject.Parse(data).GetValue("Message").ToString();
Exception exception = null;
try
{
String exceptionMessage = JObject.Parse(data).GetValue("ExceptionMessage").ToString();
String exceptionType = JObject.Parse(data).GetValue("ExceptionType").ToString();
String stackTrace = JObject.Parse(data).GetValue("StackTrace").ToString();
Type type = GetType(exceptionType);
if (type != null)
{
exception = Activator.CreateInstance(type, new object[] { exceptionMessage + "\n" + stackTrace }) as Exception;
}
else
{
exception = new HttpException(exceptionMessage + "\n" + stackTrace);
}
}
catch
{
if (message == null)
{
Logging.LogManager.Default.Log($"Error parsing response message!\n{data}");
}
throw new HttpRequestException(ex.Message + " " + message);
}
handled = true;
throw exception;
}
catch (Exception handledException)
{
if (handled)
{
throw handledException;
}
else
{
throw ex;
}
}
}
return JsonConvert.DeserializeObject<Response>(data);
});
}
private static Type GetType(string typeName)
{
var type = Type.GetType(typeName);
if (type != null) return type;
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
type = a.GetType(typeName);
if (type != null)
return type;
}
return null;
}
public void Dispose()
{
_httpClient.Dispose();
}
}
}
|