@@ -39,6 +39,18 @@ namespace <#= Configuration.Namespace#>
39
39
#region Models
40
40
namespace <#= Configuration.Namespace#>.Models
41
41
{
42
+ public class WebApiProxyResponseException : Exception
43
+ {
44
+ public HttpStatusCode StatusCode { get; private set; }
45
+ public string Content { get; private set; }
46
+
47
+ public WebApiProxyResponseException(HttpStatusCode statusCode, string content) : base("A " + statusCode + " (" + (int)statusCode + ") http exception occured. See Content for response body.")
48
+ {
49
+ StatusCode = statusCode;
50
+ Content = content;
51
+ }
52
+ }
53
+
42
54
<# foreach(var model in Configuration.Metadata.Models.Where(m => m.Type.Equals("class"))) { #>
43
55
/// <summary>
44
56
/// <#= model.Description.ToSummary() #>
@@ -84,6 +96,57 @@ namespace <#= Configuration.Namespace#>.Models
84
96
}
85
97
#endregion
86
98
99
+ #region Interfaces
100
+ namespace <#= Configuration.Namespace#>.Interfaces
101
+ {
102
+ public interface IClientBase : IDisposable
103
+ {
104
+ HttpClient HttpClient { get; }
105
+ }
106
+
107
+ <# foreach(var definition in Configuration.Metadata.Definitions) { #>
108
+ public partial interface I<#=definition.Name#><#=Configuration.ClientSuffix#> : IClientBase
109
+ {
110
+ <# foreach(var method in definition.ActionMethods) {
111
+ var allParameters = method.UrlParameters.AsEnumerable().Where(m => m != null);
112
+
113
+ //var queryParameterString = "\"";
114
+ var bodyParameterString = "";
115
+
116
+
117
+ if (method.BodyParameter != null) {
118
+ allParameters = allParameters.Concat(new [] { method.BodyParameter });
119
+ bodyParameterString = ", " + method.BodyParameter.Name;
120
+ }
121
+
122
+ var parameterList = "";
123
+
124
+ if (allParameters.Any())
125
+ {
126
+ var q = allParameters.Select(m => m.Type + " " + m.Name);
127
+ if (q != null)
128
+ parameterList = string.Join(",", q.ToArray());
129
+ }
130
+ #>
131
+
132
+ <# foreach(var p in method.UrlParameters) { #>
133
+ /// <param name="<#= p.Name #>"><#= p.Description #></param>
134
+ <# } #>
135
+ /// <returns></returns>
136
+ Task<HttpResponseMessage> <#= method.Name #>Async(<#= parameterList#>);
137
+
138
+ <# foreach(var p in method.UrlParameters) {#>
139
+ /// <param name="<#= p.Name #>"><#= p.Description #></param>
140
+ <# } #>
141
+ /// <returns></returns>
142
+ <#= String.IsNullOrEmpty(method.ReturnType) ? "void" : method.ReturnType #> <#= method.Name #>(<#= parameterList#>);
143
+ <#}#>
144
+ }
145
+ <#}#>
146
+
147
+ }
148
+ #endregion
149
+
87
150
#region Clients
88
151
namespace <#= Configuration.Namespace#>.Clients
89
152
{
@@ -95,7 +158,7 @@ namespace <#= Configuration.Namespace#>.Clients
95
158
/// <summary>
96
159
/// Gests the HttpClient.
97
160
/// </summary>
98
- public HttpClient HttpClient { get; private set; }
161
+ public HttpClient HttpClient { get; protected set; }
99
162
100
163
/// <summary>
101
164
/// Initializes a new instance of the <see cref="ClientBase"/> class.
@@ -108,6 +171,15 @@ namespace <#= Configuration.Namespace#>.Clients
108
171
};
109
172
}
110
173
174
+ public virtual void EnsureSuccess(HttpResponseMessage response)
175
+ {
176
+ if (response.IsSuccessStatusCode)
177
+ return;
178
+
179
+ var content = response.Content.ReadAsStringAsync().Result;
180
+ throw new WebApiProxyResponseException(response.StatusCode, content);
181
+ }
182
+
111
183
/// <summary>
112
184
/// Initializes a new instance of the <see cref="ClientBase"/> class.
113
185
/// </summary>
@@ -133,7 +205,7 @@ namespace <#= Configuration.Namespace#>.Clients
133
205
/// <summary>
134
206
/// <#= definition.Description.ToSummary() #>
135
207
/// </summary>
136
- public partial class <#=definition.Name#><#= Configuration.ClientSuffix#> : ClientBase
208
+ public partial class <#=definition.Name#><#= Configuration.ClientSuffix#> : ClientBase, Interfaces.I<#=definition.Name#><#=Configuration.ClientSuffix#>
137
209
{
138
210
139
211
/// <summary>
@@ -156,7 +228,7 @@ namespace <#= Configuration.Namespace#>.Clients
156
228
157
229
var queryParameterString = "\"";
158
230
var bodyParameterString = ", default(HttpResponseMessage)";
159
-
231
+ var parameterNameList = "";
160
232
161
233
if (method.BodyParameter != null) {
162
234
allParameters = allParameters.Concat(new [] { method.BodyParameter });
@@ -166,10 +238,8 @@ namespace <#= Configuration.Namespace#>.Clients
166
238
167
239
if (allParameters.Any()) {
168
240
queryParameterString = "?";
169
-
170
241
queryParameterString += string.Join(" + \"&", allParameters.Where(m => m != null).Select(m => m.Name + "=\" + " + m.Name).ToArray());
171
-
172
-
242
+ parameterNameList = string.Join(", ", allParameters.Select(m => m.Name));
173
243
}
174
244
175
245
var parameterList = "";
@@ -197,6 +267,26 @@ namespace <#= Configuration.Namespace#>.Clients
197
267
return await HttpClient.<#=method.Type.ToTitle()#><#= postOrPut ? "AsJson" : "" #>Async<#= postOrPut && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPut ? bodyParameterString:""#>);
198
268
}
199
269
270
+ /// <summary>
271
+ /// <#= method.Description #>
272
+ /// </summary>
273
+ <# foreach(var p in method.UrlParameters) { #>
274
+ /// <param name="<#= p.Name #>"><#= p.Description #></param>
275
+ <# } #>
276
+ public virtual <#= String.IsNullOrEmpty(method.ReturnType) ? "void" : method.ReturnType #> <#= method.Name #>(<#= parameterList#>)
277
+ {
278
+ <#
279
+ var parametersValues = String.IsNullOrEmpty(bodyParameterString) ? (url.IndexOf("+") > -1 ? url.Substring(url.IndexOf("+") + 1) : "") : bodyParameterString.Substring(2);
280
+ #>
281
+ var result = Task.Run(() => <#= method.Name #>Async(<#=parameterNameList#>)).Result;
282
+
283
+ EnsureSuccess(result);
284
+
285
+ <# if(!String.IsNullOrEmpty(method.ReturnType)) { #>
286
+ return result.Content.ReadAsAsync<<#= method.ReturnType #>>().Result;
287
+ <# } #>
288
+ }
289
+
200
290
<#}#>
201
291
#endregion
202
292
}
0 commit comments