forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestServiceBase.cs
More file actions
210 lines (188 loc) · 8.28 KB
/
RestServiceBase.cs
File metadata and controls
210 lines (188 loc) · 8.28 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
using System;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
namespace ServiceStack.ServiceInterface
{
/// <summary>
/// Base class for services that support HTTP verbs.
/// </summary>
/// <typeparam name="TRequest">The request class that the descendent class
/// is responsible for processing.</typeparam>
public abstract class RestServiceBase<TRequest>
: ServiceBase<TRequest>,
IRestGetService<TRequest>,
IRestPutService<TRequest>,
IRestPostService<TRequest>,
IRestDeleteService<TRequest>,
IRestPatchService<TRequest>
{
/// <summary>
/// What gets run when the request is sent to AsyncOneWay endpoint.
/// For a REST service the OnPost() method is called.
/// </summary>
protected override object Run(TRequest request)
{
return OnPost(request);
}
/// <summary>
/// The method may overriden by the descendent class to provide support for the
/// GET verb on <see cref="TRequest"/> objects.
/// </summary>
/// <param name="request">The request object containing parameters for the GET
/// operation.</param>
/// <returns>
/// A response object that the client expects, <see langword="null"/> if a response
/// object is not applicable, or an <see cref="HttpResult"/> object, to indicate an
/// error condition to the client. The <see cref="HttpResult"/> object allows the
/// implementation to control the exact HTTP status code returned to the client.
/// Any return value other than <see cref="HttpResult"/> causes the HTTP status code
/// of 200 to be returned to the client.
/// </returns>
public virtual object OnGet(TRequest request)
{
throw new NotImplementedException("This base method should be overridden but not called");
}
public object Get(TRequest request)
{
try
{
BeforeEachRequest(request);
return AfterEachRequest(request, OnGet(request));
}
catch (Exception ex)
{
var result = HandleException(request, ex);
if (result == null) throw;
return result;
}
}
/// <summary>
/// The method may overriden by the descendent class to provide support for the
/// PUT verb on <see cref="TRequest"/> objects.
/// </summary>
/// <param name="request">The request object containing parameters for the PUT
/// operation.</param>
/// <returns>
/// A response object that the client expects, <see langword="null"/> if a response
/// object is not applicable, or an <see cref="HttpResult"/> object, to indicate an
/// error condition to the client. The <see cref="HttpResult"/> object allows the
/// implementation to control the exact HTTP status code returned to the client.
/// Any return value other than <see cref="HttpResult"/> causes the HTTP status code
/// of 200 to be returned to the client.
/// </returns>
public virtual object OnPut(TRequest request)
{
throw new NotImplementedException("This base method should be overridden but not called");
}
public object Put(TRequest request)
{
try
{
BeforeEachRequest(request);
return AfterEachRequest(request, OnPut(request));
}
catch (Exception ex)
{
var result = HandleException(request, ex);
if (result == null) throw;
return result;
}
}
/// <summary>
/// The method may overriden by the descendent class to provide support for the
/// POST verb on <see cref="TRequest"/> objects.
/// </summary>
/// <param name="request">The request object containing parameters for the POST
/// operation.</param>
/// <returns>
/// A response object that the client expects, <see langword="null"/> if a response
/// object is not applicable, or an <see cref="HttpResult"/> object, to indicate an
/// error condition to the client. The <see cref="HttpResult"/> object allows the
/// implementation to control the exact HTTP status code returned to the client.
/// Any return value other than <see cref="HttpResult"/> causes the HTTP status code
/// of 200 to be returned to the client.
/// </returns>
public virtual object OnPost(TRequest request)
{
throw new NotImplementedException("This base method should be overridden but not called");
}
public object Post(TRequest request)
{
try
{
BeforeEachRequest(request);
return AfterEachRequest(request, OnPost(request));
}
catch (Exception ex)
{
var result = HandleException(request, ex);
if (result == null) throw;
return result;
}
}
/// <summary>
/// The method may overriden by the descendent class to provide support for the
/// DELETE verb on <see cref="TRequest"/> objects.
/// </summary>
/// <param name="request">The request object containing parameters for the DELETE
/// operation.</param>
/// <returns>
/// A response object that the client expects, <see langword="null"/> if a response
/// object is not applicable, or an <see cref="HttpResult"/> object, to indicate an
/// error condition to the client. The <see cref="HttpResult"/> object allows the
/// implementation to control the exact HTTP status code returned to the client.
/// Any return value other than <see cref="HttpResult"/> causes the HTTP status code
/// of 200 to be returned to the client.
/// </returns>
public virtual object OnDelete(TRequest request)
{
throw new NotImplementedException("This base method should be overridden but not called");
}
public object Delete(TRequest request)
{
try
{
BeforeEachRequest(request);
return AfterEachRequest(request, OnDelete(request));
}
catch (Exception ex)
{
var result = HandleException(request, ex);
if (result == null) throw;
return result;
}
}
/// <summary>
/// The method may overriden by the descendent class to provide support for the
/// PATCH verb on <see cref="TRequest"/> objects.
/// </summary>
/// <param name="request">The request object containing parameters for the PATCH
/// operation.</param>
/// <returns>
/// A response object that the client expects, <see langword="null"/> if a response
/// object is not applicable, or an <see cref="HttpResult"/> object, to indicate an
/// error condition to the client. The <see cref="HttpResult"/> object allows the
/// implementation to control the exact HTTP status code returned to the client.
/// Any return value other than <see cref="HttpResult"/> causes the HTTP status code
/// of 200 to be returned to the client.
/// </returns>
public virtual object OnPatch(TRequest request)
{
throw new NotImplementedException("This base method should be overridden but not called");
}
public object Patch(TRequest request)
{
try
{
BeforeEachRequest(request);
return AfterEachRequest(request, OnPatch(request));
}
catch (Exception ex)
{
var result = HandleException(request, ex);
if (result == null) throw;
return result;
}
}
}
}