forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectServiceClient.cs
More file actions
226 lines (187 loc) · 6.21 KB
/
DirectServiceClient.cs
File metadata and controls
226 lines (187 loc) · 6.21 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
using System;
using System.IO;
using ServiceStack.Service;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceHost;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints.Support.Mocks;
using ServiceStack.WebHost.Endpoints.Tests.Mocks;
namespace ServiceStack.WebHost.Endpoints.Tests.Support
{
public class DirectServiceClient : IServiceClient, IRestClient
{
ServiceManager ServiceManager { get; set; }
readonly HttpRequestMock httpReq = new HttpRequestMock();
readonly HttpResponseMock httpRes = new HttpResponseMock();
public DirectServiceClient(ServiceManager serviceManager)
{
this.ServiceManager = serviceManager;
}
public void SendOneWay(object request)
{
ServiceManager.Execute(request);
}
public void SendOneWay(string relativeOrAbsoluteUrl, object request)
{
ServiceManager.Execute(request);
}
private bool ApplyRequestFilters<TResponse>(object request)
{
if (EndpointHost.ApplyRequestFilters(httpReq, httpRes, request))
{
ThrowIfError<TResponse>(httpRes);
return true;
}
return false;
}
private void ThrowIfError<TResponse>(HttpResponseMock httpRes)
{
if (httpRes.StatusCode >= 400)
{
var webEx = new WebServiceException("WebServiceException, StatusCode: " + httpRes.StatusCode) {
StatusCode = httpRes.StatusCode,
StatusDescription = httpRes.StatusDescription,
};
try
{
var deserializer = EndpointHost.AppHost.ContentTypeFilters.GetStreamDeserializer(httpReq.ResponseContentType);
webEx.ResponseDto = deserializer(typeof(TResponse), httpRes.OutputStream);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
throw webEx;
}
}
private bool ApplyResponseFilters<TResponse>(object response)
{
if (EndpointHost.ApplyResponseFilters(httpReq, httpRes, response))
{
ThrowIfError<TResponse>(httpRes);
return true;
}
return false;
}
public TResponse Send<TResponse>(object request)
{
httpReq.HttpMethod = HttpMethod.Post;
if (ApplyRequestFilters<TResponse>(request)) return default(TResponse);
var response = ServiceManager.ServiceController.Execute(request,
new HttpRequestContext(httpReq, httpRes, request, EndpointAttributes.HttpPost));
if (ApplyResponseFilters<TResponse>(response)) return (TResponse)response;
return (TResponse)response;
}
public TResponse Get<TResponse>(string relativeOrAbsoluteUrl)
{
httpReq.HttpMethod = HttpMethod.Get;
var requestTypeName = typeof(TResponse).Namespace + "." + relativeOrAbsoluteUrl;
var requestType = typeof (TResponse).Assembly.GetType(requestTypeName);
if (requestType == null)
throw new ArgumentException("Type not found: " + requestTypeName);
var request = requestType.CreateInstance();
if (ApplyRequestFilters<TResponse>(request)) return default(TResponse);
var response = ServiceManager.ServiceController.Execute(request,
new HttpRequestContext(httpReq, httpRes, request, EndpointAttributes.HttpGet));
if (ApplyResponseFilters<TResponse>(response)) return (TResponse)response;
return (TResponse)response;
}
public TResponse Delete<TResponse>(string relativeOrAbsoluteUrl)
{
throw new NotImplementedException();
}
public TResponse Post<TResponse>(string relativeOrAbsoluteUrl, object request)
{
throw new NotImplementedException();
}
public TResponse Put<TResponse>(string relativeOrAbsoluteUrl, object request)
{
throw new NotImplementedException();
}
public TResponse Patch<TResponse>(string relativeOrAbsoluteUrl, object request)
{
throw new NotImplementedException();
}
public TResponse PostFile<TResponse>(string relativeOrAbsoluteUrl, FileInfo fileToUpload, string mimeType)
{
throw new NotImplementedException();
}
public TResponse PostFile<TResponse>(string relativeOrAbsoluteUrl, Stream fileToUpload, string fileInfo, string mimeType)
{
throw new NotImplementedException();
}
public void SendAsync<TResponse>(object request, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
{
var response = default(TResponse);
try
{
try
{
if (ApplyRequestFilters<TResponse>(request))
{
onSuccess(default(TResponse));
return;
}
}
catch (Exception ex)
{
onError(default(TResponse), ex);
return;
}
response = this.Send<TResponse>(request);
try
{
if (ApplyResponseFilters<TResponse>(request))
{
onSuccess(response);
return;
}
}
catch (Exception ex)
{
onError(response, ex);
return;
}
onSuccess(response);
}
catch (Exception ex)
{
if (onError != null)
{
onError(response, ex);
return;
}
Console.WriteLine("Error: " + ex.Message);
}
}
public void SetCredentials(string userName, string password)
{
throw new NotImplementedException();
}
public void GetAsync<TResponse>(string relativeOrAbsoluteUrl, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
{
throw new NotImplementedException();
}
public void DeleteAsync<TResponse>(string relativeOrAbsoluteUrl, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
{
throw new NotImplementedException();
}
public void PostAsync<TResponse>(string relativeOrAbsoluteUrl, object request, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
{
throw new NotImplementedException();
}
public void PutAsync<TResponse>(string relativeOrAbsoluteUrl, object request, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
{
throw new NotImplementedException();
}
public void Dispose() { }
public TResponse PostFileWithRequest<TResponse>(string relativeOrAbsoluteUrl, FileInfo fileToUpload, object request)
{
throw new NotImplementedException();
}
public TResponse PostFileWithRequest<TResponse>(string relativeOrAbsoluteUrl, Stream fileToUpload, string fileName, object request)
{
throw new NotImplementedException();
}
}
}