Skip to content

Commit 2e17450

Browse files
Refactor ClientBuilder
1 parent 7de9dbf commit 2e17450

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

Simple.API/ClientBuilder.cs

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,39 @@ namespace Simple.API;
1212
public class ClientBuilder : DispatchProxy
1313
{
1414
static readonly Type TypeOfTask = typeof(Task);
15-
static readonly Type TypeOfClientInfo = typeof(ClientInfo);
16-
static readonly Type TypeOfResponseExtensions = typeof(ResponseExtensions);
1715

18-
static readonly MethodInfo[] MethodsOfClientInfo = TypeOfClientInfo.GetMethods();
16+
static readonly MethodInfo[] MethodsOfClientInfo = typeof(ClientInfo).GetMethods();
1917
static readonly MethodInfo MethodGetAsync = MethodsOfClientInfo.Where(o => o.Name == nameof(ClientInfo.GetAsync)).First();
2018
static readonly MethodInfo MethodPostAsync = MethodsOfClientInfo.Where(o => o.Name == nameof(ClientInfo.PostAsync)).First();
2119
static readonly MethodInfo MethodPutAsync = MethodsOfClientInfo.Where(o => o.Name == nameof(ClientInfo.PutAsync)).First();
2220

21+
static readonly Type TypeOfResponseExtensions = typeof(ResponseExtensions);
2322
static readonly MethodInfo MethodGetSuccessfulDataTask = TypeOfResponseExtensions
2423
.GetMethods()
2524
.Where(o => o.Name == nameof(ResponseExtensions.GetSuccessfulData)
2625
&& o.ReturnType.BaseType.Name == "Task")
2726
.First(); // Exception if not found
2827

29-
30-
31-
3228
internal ClientInfo client;
33-
public ClientBuilder()
34-
{
35-
}
3629

3730
public static T Create<T>(string uri, HttpMessageHandler clientHandler = null)
3831
where T : class
3932
{
40-
var proxy = Create<T, ClientBuilder>();
33+
// Base ClientInfo
4134
var client = new ClientInfo(uri, clientHandler);
42-
(proxy as ClientBuilder).client = client;
4335

36+
// Process Interface Attributes
4437
var typeT = typeof(T);
4538
var timeoutAttr = typeT.GetCustomAttribute<TimeoutAttribute>();
4639
if (timeoutAttr != null)
4740
{
4841
client.Timeout = timeoutAttr.Timeout;
4942
}
5043

44+
// Create proxy
45+
var proxy = DispatchProxy.Create<T, ClientBuilder>();
46+
(proxy as ClientBuilder).client = client;
47+
5148
return proxy;
5249
}
5350

@@ -68,8 +65,8 @@ protected override object Invoke(MethodInfo targetMethod, object[] args)
6865

6966
// Check InRoute
7067
var methodParams = targetMethod.GetParameters();
71-
var inRoutes = methodParams.Select(o => o.GetCustomAttribute<InRouteAttribute>()).ToArray();
72-
var hasAnyInRoutes = inRoutes.Any(o => o != null);
68+
var inRoutesParams = methodParams.Select(o => o.GetCustomAttribute<InRouteAttribute>()).ToArray();
69+
var hasAnyInRoutes = inRoutesParams.Any(o => o != null);
7370
var hasRouteParam = httpMethod.Route.Contains('{');
7471
if (hasAnyInRoutes && !hasRouteParam) throw new InvalidOperationException($"Attribute {nameof(InRouteAttribute)} must be used in a route with parameters");
7572
if (!hasAnyInRoutes && hasRouteParam) throw new InvalidOperationException($"Route parameters must be used with {nameof(InRouteAttribute)}");
@@ -94,37 +91,11 @@ protected override object Invoke(MethodInfo targetMethod, object[] args)
9491
usesResponseReturn = true;
9592
}
9693

97-
/* Extract Route information */
94+
/* Extract Route parameters */
9895
string route = httpMethod.Route;
9996
if (hasRouteParam)
10097
{
101-
Dictionary<string, string> dicParams = [];
102-
// Parse route params
103-
for (int i = 0; i < inRoutes.Length; i++)
104-
{
105-
if (inRoutes[i] == null) continue;
106-
107-
// Get 'Key' in [inRoutes] and 'value' in [args]
108-
var name = methodParams[i].Name;
109-
var value = args[i];
110-
111-
dicParams[name] = value?.ToString() ?? "";
112-
}
113-
114-
// Replace route
115-
foreach (var pair in dicParams)
116-
{
117-
string key = $"{{{pair.Key}}}";
118-
route = route.Replace(key, pair.Value);
119-
}
120-
121-
// rebuild args
122-
List<object> lstParams = new List<object>(args);
123-
for (int i = lstParams.Count - 1; i >= 0; i--)
124-
{
125-
if (inRoutes[i] != null) lstParams.RemoveAt(i);
126-
}
127-
args = lstParams.ToArray();
98+
processInRouteArguments(methodParams, inRoutesParams, ref route, ref args);
12899
}
129100

130101
/* Method Selection */
@@ -142,6 +113,37 @@ protected override object Invoke(MethodInfo targetMethod, object[] args)
142113
return task;
143114
}
144115

116+
private static void processInRouteArguments(ParameterInfo[] methodParams, InRouteAttribute[] inRoutes, ref string route, ref object[] args)
117+
{
118+
Dictionary<string, string> dicParams = [];
119+
// Parse route params
120+
for (int i = 0; i < inRoutes.Length; i++)
121+
{
122+
if (inRoutes[i] == null) continue;
123+
124+
// Get 'Key' in [inRoutes] and 'value' in [args]
125+
var name = methodParams[i].Name;
126+
var value = args[i];
127+
128+
dicParams[name] = value?.ToString() ?? "";
129+
}
130+
131+
// Replace route
132+
foreach (var pair in dicParams)
133+
{
134+
string key = $"{{{pair.Key}}}";
135+
route = route.Replace(key, pair.Value);
136+
}
137+
138+
// rebuild args
139+
List<object> lstParams = new List<object>(args);
140+
for (int i = lstParams.Count - 1; i >= 0; i--)
141+
{
142+
if (inRoutes[i] != null) lstParams.RemoveAt(i);
143+
}
144+
args = lstParams.ToArray();
145+
}
146+
145147
private static void selectMethodToExecute(string route, object[] args, MethodAttribute httpMethod, Type innerType, out MethodInfo methodToCall, out object[] methodArgs)
146148
{
147149
if (httpMethod is GetAttribute)

Simple.Test/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
Console.WriteLine("Choose an Example");
44

55
//await Simple.Test.Example_ClientInfo.Run();
6-
//await Simple.Test.Example_ClientBuilder.Run();
6+
await Simple.Test.Example_ClientBuilder.Run();
77
//await Simple.Test.Example_WebSocket.Run_String();
88
//await Simple.Test.Example_WebSocket.Run_Json();

0 commit comments

Comments
 (0)