Skip to content

Commit 2f172f6

Browse files
Merge pull request #44 from MaryamTaheri/refactor/HCG-43
refactor/HCG-43
2 parents 8f8210a + 30aa8d3 commit 2f172f6

File tree

12 files changed

+66
-98
lines changed

12 files changed

+66
-98
lines changed

examples/HttpClientToCurl.Sample.InString/ApiCaller.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,25 @@ namespace HttpClientToCurl.Sample.InString;
44

55
public static class ApiCaller
66
{
7+
// Create an instance of HttpClient
8+
private static readonly HttpClient Client = new();
9+
private const string ApiUrl = "https://jsonplaceholder.typicode.com/posts";
10+
private const string AccessToken = "YourAccessToken";
11+
712
public static async Task MakeApiCall()
813
{
9-
string apiUrl = "https://jsonplaceholder.typicode.com/posts";
10-
11-
// Create an instance of HttpClient
12-
HttpClient client = new();
13-
1414
try
1515
{
1616
// Create a sample JSON payload
17-
string jsonPayload =
18-
"{\"title\":\"New Post\",\"body\":\"This is the body of the new post\",\"userId\":1}";
19-
20-
// Create HttpRequestMessage with the POST verb
21-
HttpRequestMessage request = new(HttpMethod.Post, apiUrl);
22-
23-
// Set up the request headers
24-
request.Headers.Add("Authorization", "Bearer YourAccessToken"); // Add any necessary headers
25-
26-
// Set the request content with the JSON payload
27-
request.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
17+
var jsonPayload = "{\"title\":\"New Post\",\"body\":\"This is the body of the new post\",\"userId\":1}";
2818

2919
// Generate a curl command as a string for debugging or testing.
30-
// This string can be directly imported into Postman for checking and comparing against all the requirements.
31-
string curlCommandString = client.GenerateCurlInString(request);
32-
33-
Console.WriteLine(curlCommandString);
34-
35-
// Send the request
36-
HttpResponseMessage response = await client.SendAsync(request);
20+
var response = await SendHttpRequest(HttpMethod.Post, ApiUrl, jsonPayload);
3721

3822
// Check if the request was successful (status code 200-299)
3923
if (response.IsSuccessStatusCode)
4024
{
41-
// Read and print the response content as a string
42-
string responseBody = await response.Content.ReadAsStringAsync();
25+
var responseBody = await response.Content.ReadAsStringAsync();
4326
Console.WriteLine("Response from the API:\n" + responseBody);
4427
}
4528
else
@@ -52,4 +35,18 @@ public static async Task MakeApiCall()
5235
Console.WriteLine($"Exception: {ex.Message}");
5336
}
5437
}
38+
39+
private static async Task<HttpResponseMessage> SendHttpRequest(HttpMethod method, string url, string payload = null)
40+
{
41+
var request = new HttpRequestMessage(method, url);
42+
43+
request.Headers.Add("Authorization", $"Bearer {AccessToken}");
44+
request.Content = new StringContent(payload, Encoding.UTF8, "application/json");
45+
46+
Console.WriteLine(GenerateCurl(request));
47+
48+
return await Client.SendAsync(request);
49+
}
50+
51+
private static string GenerateCurl(HttpRequestMessage request) => Client.GenerateCurlInString(request);
5552
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using System.Text;
2+
using HttpClientToCurl.Builder.Interface;
3+
using HttpClientToCurl.Config;
24

35
namespace HttpClientToCurl.Builder.Concrete.Common;
46

5-
public abstract class BaseBuilder
7+
public abstract class BaseBuilder : IBuilder
68
{
79
protected readonly StringBuilder _stringBuilder = new();
10+
11+
public abstract string CreateCurl(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config);
812
}

src/HttpClientToCurl/Builder/Concrete/HttpDeleteBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using HttpClientToCurl.Builder.Concrete.Common;
22
using HttpClientToCurl.Config;
3-
using HttpClientToCurl.Builder.Interface;
43

54
namespace HttpClientToCurl.Builder.Concrete;
65

7-
public sealed class HttpDeleteBuilder : BaseBuilder, IBuilder
6+
public sealed class HttpDeleteBuilder : BaseBuilder
87
{
9-
public string Create(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
8+
public override string CreateCurl(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
109
{
1110
return _stringBuilder
1211
.Initialize(httpRequestMessage.Method)

src/HttpClientToCurl/Builder/Concrete/HttpGetBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using HttpClientToCurl.Builder.Concrete.Common;
22
using HttpClientToCurl.Config;
3-
using HttpClientToCurl.Builder.Interface;
43

54
namespace HttpClientToCurl.Builder.Concrete;
65

7-
public sealed class HttpGetBuilder : BaseBuilder, IBuilder
6+
public sealed class HttpGetBuilder : BaseBuilder
87
{
9-
public string Create(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
8+
public override string CreateCurl(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
109
{
1110
return _stringBuilder
1211
.Initialize(httpRequestMessage.Method)

src/HttpClientToCurl/Builder/Concrete/HttpPatchBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using HttpClientToCurl.Builder.Concrete.Common;
22
using HttpClientToCurl.Config;
3-
using HttpClientToCurl.Builder.Interface;
43

54
namespace HttpClientToCurl.Builder.Concrete;
65

7-
public sealed class HttpPatchBuilder : BaseBuilder, IBuilder
6+
public sealed class HttpPatchBuilder : BaseBuilder
87
{
9-
public string Create(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
8+
public override string CreateCurl(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
109
{
1110
return _stringBuilder
1211
.Initialize(httpRequestMessage.Method)

src/HttpClientToCurl/Builder/Concrete/HttpPostBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using HttpClientToCurl.Builder.Concrete.Common;
22
using HttpClientToCurl.Config;
3-
using HttpClientToCurl.Builder.Interface;
43

54
namespace HttpClientToCurl.Builder.Concrete;
65

7-
public sealed class HttpPostBuilder : BaseBuilder, IBuilder
6+
public sealed class HttpPostBuilder : BaseBuilder
87
{
9-
public string Create(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
8+
public override string CreateCurl(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
109
{
1110
return _stringBuilder
1211
.Initialize(httpRequestMessage.Method)

src/HttpClientToCurl/Builder/Concrete/HttpPutBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using HttpClientToCurl.Builder.Concrete.Common;
22
using HttpClientToCurl.Config;
3-
using HttpClientToCurl.Builder.Interface;
43

54
namespace HttpClientToCurl.Builder.Concrete;
65

7-
public sealed class HttpPutBuilder : BaseBuilder, IBuilder
6+
public sealed class HttpPutBuilder : BaseBuilder
87
{
9-
public string Create(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
8+
public override string CreateCurl(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
109
{
1110
return _stringBuilder
1211
.Initialize(httpRequestMessage.Method)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
using System.Text;
12
using HttpClientToCurl.Config;
23
using HttpClientToCurl.Builder.Interface;
34

45
namespace HttpClientToCurl.Builder.Director;
56

6-
public class Creator(IBuilder builder)
7+
public abstract class BaseBuilder : IBuilder
78
{
8-
public string CreateCurl(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
9-
{
10-
return builder.Create(httpClient, httpRequestMessage, config);
11-
}
9+
protected readonly StringBuilder _stringBuilder = new();
10+
11+
public abstract string CreateCurl(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config);
1212
}
Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using System.Data;
21
using HttpClientToCurl.Builder.Concrete;
3-
using HttpClientToCurl.Builder.Director;
2+
using HttpClientToCurl.Builder.Interface;
43
using HttpClientToCurl.Config;
54

65
namespace HttpClientToCurl.Builder;
@@ -9,45 +8,21 @@ public static class Generator
98
{
109
public static string GenerateCurl(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config)
1110
{
12-
string script;
11+
var builder = GetBuilder(httpRequestMessage.Method);
12+
return builder.CreateCurl(httpClient, httpRequestMessage, config);
13+
}
1314

14-
try
15-
{
16-
if (httpRequestMessage.Method == HttpMethod.Get)
17-
{
18-
var instance = new Creator(new HttpGetBuilder());
19-
script = instance.CreateCurl(httpClient, httpRequestMessage, config);
20-
}
21-
else if (httpRequestMessage.Method == HttpMethod.Post)
22-
{
23-
var instance = new Creator(new HttpPostBuilder());
24-
script = instance.CreateCurl(httpClient, httpRequestMessage, config);
25-
}
26-
else if (httpRequestMessage.Method == HttpMethod.Put)
27-
{
28-
var instance = new Creator(new HttpPutBuilder());
29-
script = instance.CreateCurl(httpClient, httpRequestMessage, config);
30-
}
31-
else if (httpRequestMessage.Method == HttpMethod.Patch)
32-
{
33-
var instance = new Creator(new HttpPatchBuilder());
34-
script = instance.CreateCurl(httpClient, httpRequestMessage, config);
35-
}
36-
else if (httpRequestMessage.Method == HttpMethod.Delete)
37-
{
38-
var instance = new Creator(new HttpDeleteBuilder());
39-
script = instance.CreateCurl(httpClient, httpRequestMessage, config);
40-
}
41-
else
42-
{
43-
throw new DataException($"not supported {httpRequestMessage.Method.Method} by HttpClientToCurl!");
44-
}
45-
}
46-
catch (Exception exception)
15+
private static IBuilder GetBuilder(HttpMethod method)
16+
{
17+
string methodName = method.Method;
18+
return methodName switch
4719
{
48-
script = $"GenerateCurlError => {exception.Message} {exception.InnerException}";
49-
}
50-
51-
return script;
20+
"GET" => new HttpGetBuilder(),
21+
"POST" => new HttpPostBuilder(),
22+
"PUT" => new HttpPutBuilder(),
23+
"PATCH" => new HttpPatchBuilder(),
24+
"DELETE" => new HttpDeleteBuilder(),
25+
_ => throw new NotSupportedException($"HTTP method {method} is not supported."),
26+
};
5227
}
5328
}

src/HttpClientToCurl/Builder/Interface/IBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ namespace HttpClientToCurl.Builder.Interface;
44

55
public interface IBuilder
66
{
7-
string Create(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config);
7+
string CreateCurl(HttpClient httpClient, HttpRequestMessage httpRequestMessage, BaseConfig config);
88
}

0 commit comments

Comments
 (0)