Skip to content

Commit 59fe0c6

Browse files
author
Matt Melton
committed
Update template - interfaces + non async
Implement templates. Revert removal of non async methods
1 parent 1a8c1e8 commit 59fe0c6

File tree

1 file changed

+96
-6
lines changed

1 file changed

+96
-6
lines changed

WebApiProxy.Tasks/Templates/CSharpProxyTemplate.tt

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ namespace <#= Configuration.Namespace#>
3939
#region Models
4040
namespace <#= Configuration.Namespace#>.Models
4141
{
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+
4254
<# foreach(var model in Configuration.Metadata.Models.Where(m => m.Type.Equals("class"))) { #>
4355
/// <summary>
4456
/// <#= model.Description.ToSummary() #>
@@ -84,6 +96,57 @@ namespace <#= Configuration.Namespace#>.Models
8496
}
8597
#endregion
8698

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+
87150
#region Clients
88151
namespace <#= Configuration.Namespace#>.Clients
89152
{
@@ -95,7 +158,7 @@ namespace <#= Configuration.Namespace#>.Clients
95158
/// <summary>
96159
/// Gests the HttpClient.
97160
/// </summary>
98-
public HttpClient HttpClient { get; private set; }
161+
public HttpClient HttpClient { get; protected set; }
99162

100163
/// <summary>
101164
/// Initializes a new instance of the <see cref="ClientBase"/> class.
@@ -108,6 +171,15 @@ namespace <#= Configuration.Namespace#>.Clients
108171
};
109172
}
110173

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+
111183
/// <summary>
112184
/// Initializes a new instance of the <see cref="ClientBase"/> class.
113185
/// </summary>
@@ -133,7 +205,7 @@ namespace <#= Configuration.Namespace#>.Clients
133205
/// <summary>
134206
/// <#= definition.Description.ToSummary() #>
135207
/// </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#>
137209
{
138210

139211
/// <summary>
@@ -156,7 +228,7 @@ namespace <#= Configuration.Namespace#>.Clients
156228

157229
var queryParameterString = "\"";
158230
var bodyParameterString = ", default(HttpResponseMessage)";
159-
231+
var parameterNameList = "";
160232

161233
if (method.BodyParameter != null) {
162234
allParameters = allParameters.Concat(new [] { method.BodyParameter });
@@ -166,10 +238,8 @@ namespace <#= Configuration.Namespace#>.Clients
166238

167239
if (allParameters.Any()) {
168240
queryParameterString = "?";
169-
170241
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));
173243
}
174244

175245
var parameterList = "";
@@ -197,6 +267,26 @@ namespace <#= Configuration.Namespace#>.Clients
197267
return await HttpClient.<#=method.Type.ToTitle()#><#= postOrPut ? "AsJson" : "" #>Async<#= postOrPut && method.BodyParameter != null ? "<" + method.BodyParameter.Type + ">" : "" #>(<#=url#><#= postOrPut ? bodyParameterString:""#>);
198268
}
199269

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+
200290
<#}#>
201291
#endregion
202292
}

0 commit comments

Comments
 (0)