Skip to content

Commit 8cba10b

Browse files
Restructure code / Remove repetitive code ▶️ HttpClientExtensions
1 parent f8597ce commit 8cba10b

File tree

1 file changed

+24
-87
lines changed

1 file changed

+24
-87
lines changed

src/HttpClientToCurl/HttpClientExtensions.cs

Lines changed: 24 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,7 @@ public static class HttpClientExtensions
1414
public static string GenerateCurlInString(this HttpClient httpClient, HttpRequestMessage httpRequestMessage, Action<StringConfig> config = null)
1515
{
1616
var stringConfig = new StringConfig();
17-
config?.Invoke(stringConfig);
18-
19-
if (!stringConfig.TurnOn)
20-
{
21-
return string.Empty;
22-
}
23-
24-
string script = Generator.GenerateCurl(
25-
httpClient,
26-
httpRequestMessage,
27-
stringConfig);
17+
var script = GenerateCurlBaseOnConfig(httpClient, httpRequestMessage, stringConfig, config);
2818

2919
return script;
3020
}
@@ -37,17 +27,9 @@ public static string GenerateCurlInString(
3727
HttpContent httpContent = null,
3828
Action<StringConfig> config = null)
3929
{
40-
var stringConfig = new StringConfig();
41-
config?.Invoke(stringConfig);
42-
43-
if (!stringConfig.TurnOn)
44-
{
45-
return string.Empty;
46-
}
47-
4830
var httpRequestMessage = Helpers.FillHttpRequestMessage(httpMethod, httpRequestHeaders, httpContent, requestUri);
49-
50-
string script = Generator.GenerateCurl(httpClient, httpRequestMessage, stringConfig);
31+
var stringConfig = new StringConfig();
32+
var script = GenerateCurlBaseOnConfig(httpClient, httpRequestMessage, stringConfig, config);
5133

5234
return script;
5335
}
@@ -60,17 +42,9 @@ public static string GenerateCurlInString(
6042
HttpContent httpContent = null,
6143
Action<StringConfig> config = null)
6244
{
63-
var stringConfig = new StringConfig();
64-
config?.Invoke(stringConfig);
65-
66-
if (!stringConfig.TurnOn)
67-
{
68-
return string.Empty;
69-
}
70-
7145
var httpRequestMessage = Helpers.FillHttpRequestMessage(httpMethod, httpRequestHeaders, httpContent, requestUri);
72-
73-
string script = Generator.GenerateCurl(httpClient, httpRequestMessage, stringConfig);
46+
var stringConfig = new StringConfig();
47+
var script = GenerateCurlBaseOnConfig(httpClient, httpRequestMessage, stringConfig, config);
7448

7549
return script;
7650
}
@@ -82,14 +56,7 @@ public static string GenerateCurlInString(
8256
public static void GenerateCurlInConsole(this HttpClient httpClient, HttpRequestMessage httpRequestMessage, Action<ConsoleConfig> config = null)
8357
{
8458
var consoleConfig = new ConsoleConfig();
85-
config?.Invoke(consoleConfig);
86-
87-
if (!consoleConfig.TurnOn)
88-
{
89-
return;
90-
}
91-
92-
string script = Generator.GenerateCurl(httpClient, httpRequestMessage, consoleConfig);
59+
var script = GenerateCurlBaseOnConfig(httpClient, httpRequestMessage, consoleConfig, config);
9360

9461
Helpers.WriteInConsole(script, consoleConfig.EnableCodeBeautification, httpRequestMessage.Method);
9562
}
@@ -102,17 +69,9 @@ public static void GenerateCurlInConsole(
10269
HttpContent httpContent = null,
10370
Action<ConsoleConfig> config = null)
10471
{
105-
var consoleConfig = new ConsoleConfig();
106-
config?.Invoke(consoleConfig);
107-
108-
if (!consoleConfig.TurnOn)
109-
{
110-
return;
111-
}
112-
11372
var httpRequestMessage = Helpers.FillHttpRequestMessage(httpMethod, httpRequestHeaders, httpContent, requestUri);
114-
115-
string script = Generator.GenerateCurl(httpClient, httpRequestMessage, consoleConfig);
73+
var consoleConfig = new ConsoleConfig();
74+
var script = GenerateCurlBaseOnConfig(httpClient, httpRequestMessage, consoleConfig, config);
11675

11776
Helpers.WriteInConsole(script, consoleConfig.EnableCodeBeautification, httpRequestMessage.Method);
11877
}
@@ -125,17 +84,9 @@ public static void GenerateCurlInConsole(
12584
HttpContent httpContent = null,
12685
Action<ConsoleConfig> config = null)
12786
{
128-
var consoleConfig = new ConsoleConfig();
129-
config?.Invoke(consoleConfig);
130-
131-
if (!consoleConfig.TurnOn)
132-
{
133-
return;
134-
}
135-
13687
var httpRequestMessage = Helpers.FillHttpRequestMessage(httpMethod, httpRequestHeaders, httpContent, requestUri);
137-
138-
string script = Generator.GenerateCurl(httpClient, httpRequestMessage, consoleConfig);
88+
var consoleConfig = new ConsoleConfig();
89+
var script = GenerateCurlBaseOnConfig(httpClient, httpRequestMessage, consoleConfig, config);
13990

14091
Helpers.WriteInConsole(script, consoleConfig.EnableCodeBeautification, httpRequestMessage.Method);
14192
}
@@ -147,14 +98,7 @@ public static void GenerateCurlInConsole(
14798
public static void GenerateCurlInFile(this HttpClient httpClient, HttpRequestMessage httpRequestMessage, Action<FileConfig> config = null)
14899
{
149100
var fileConfig = new FileConfig();
150-
config?.Invoke(fileConfig);
151-
152-
if (!fileConfig.TurnOn)
153-
{
154-
return;
155-
}
156-
157-
string script = Generator.GenerateCurl(httpClient, httpRequestMessage, fileConfig);
101+
var script = GenerateCurlBaseOnConfig(httpClient, httpRequestMessage, fileConfig, config);
158102

159103
Helpers.WriteInFile(script, fileConfig.Filename, fileConfig.Path);
160104
}
@@ -167,17 +111,9 @@ public static void GenerateCurlInFile(
167111
HttpContent httpContent = null,
168112
Action<FileConfig> config = null)
169113
{
170-
var fileConfig = new FileConfig();
171-
config?.Invoke(fileConfig);
172-
173-
if (!fileConfig.TurnOn)
174-
{
175-
return;
176-
}
177-
178114
var httpRequestMessage = Helpers.FillHttpRequestMessage(httpMethod, httpRequestHeaders, httpContent, requestUri);
179-
180-
string script = Generator.GenerateCurl(httpClient, httpRequestMessage, fileConfig);
115+
var fileConfig = new FileConfig();
116+
var script = GenerateCurlBaseOnConfig(httpClient, httpRequestMessage, fileConfig, config);
181117

182118
Helpers.WriteInFile(script, fileConfig.Filename, fileConfig.Path);
183119
}
@@ -190,22 +126,23 @@ public static void GenerateCurlInFile(
190126
HttpContent httpContent = null,
191127
Action<FileConfig> config = null)
192128
{
129+
var httpRequestMessage = Helpers.FillHttpRequestMessage(httpMethod, httpRequestHeaders, httpContent, requestUri);
193130
var fileConfig = new FileConfig();
194-
config?.Invoke(fileConfig);
131+
var script = GenerateCurlBaseOnConfig(httpClient, httpRequestMessage, fileConfig, config);
195132

196-
if (!fileConfig.TurnOn)
197-
{
198-
return;
199-
}
200-
201-
var httpRequestMessage = Helpers.FillHttpRequestMessage(httpMethod, httpRequestHeaders, httpContent, requestUri);
133+
Helpers.WriteInFile(script, fileConfig.Filename, fileConfig.Path);
134+
}
202135

203-
string script = Generator.GenerateCurl(httpClient, httpRequestMessage, fileConfig);
136+
#endregion : Write in a file :\
204137

205-
Helpers.WriteInFile(script, fileConfig.Filename, fileConfig.Path);
138+
#region : Private methods :
139+
private static string GenerateCurlBaseOnConfig<TConfig>(HttpClient httpClient, HttpRequestMessage httpRequestMessage, TConfig config, Action<TConfig> configAction) where TConfig : BaseConfig
140+
{
141+
configAction?.Invoke(config);
142+
return config.TurnOn ? Generator.GenerateCurl(httpClient, httpRequestMessage, config) : string.Empty;
206143
}
207144

208-
#endregion : Write in a file :
145+
#endregion : Private methods :
209146

210147
#endregion :: EXTENSIONS ::
211148
}

0 commit comments

Comments
 (0)