Skip to content

Commit 8a3b8b0

Browse files
committed
[R] Request parsing to separate class
[#4] [add] implementation
1 parent 76b19d6 commit 8a3b8b0

File tree

7 files changed

+159
-22
lines changed

7 files changed

+159
-22
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#nullable disable
2+
3+
namespace Simplify.Web.Postman.Models
4+
{
5+
/// <summary>
6+
/// provides Body model
7+
/// </summary>
8+
public class Body
9+
{
10+
/// <summary>
11+
/// Gets or sets the mode.
12+
/// </summary>
13+
/// <value>
14+
/// The mode.
15+
/// </value>
16+
public string Mode { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets the raw body data.
20+
/// </summary>
21+
/// <value>
22+
/// The raw.
23+
/// </value>
24+
public string Raw { get; set; }
25+
26+
/// <summary>
27+
/// Gets or sets the options.
28+
/// </summary>
29+
/// <value>
30+
/// The options.
31+
/// </value>
32+
public BodyOptions Options { get; set; }
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#nullable disable
2+
3+
namespace Simplify.Web.Postman.Models
4+
{
5+
/// <summary>
6+
/// Provides body options model
7+
/// </summary>
8+
public class BodyOptions
9+
{
10+
/// <summary>
11+
/// Gets or sets the sub options.
12+
/// </summary>
13+
/// <value>
14+
/// The raw.
15+
/// </value>
16+
public BodyRawOptions Raw { get; set; }
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#nullable disable
2+
3+
namespace Simplify.Web.Postman.Models
4+
{
5+
/// <summary>
6+
/// Provides body options raw options
7+
/// </summary>
8+
public class BodyRawOptions
9+
{
10+
/// <summary>
11+
/// Gets or sets the language.
12+
/// </summary>
13+
/// <value>
14+
/// The language.
15+
/// </value>
16+
public string Language { get; set; }
17+
}
18+
}

src/Simplify.Web.Postman/Models/Request.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,13 @@ public class Request
2222
/// The URL.
2323
/// </value>
2424
public Url Url { get; set; }
25+
26+
/// <summary>
27+
/// Gets or sets the body.
28+
/// </summary>
29+
/// <value>
30+
/// The body.
31+
/// </value>
32+
public Body Body { get; set; }
2533
}
2634
}

src/Simplify.Web.Postman/PartBuilders/CollectionHeaderBuilder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ public class CollectionHeaderBuilder : ICollectionPartBuilder
1515
/// Initializes a new instance of the <see cref="CollectionHeaderBuilder"/> class.
1616
/// </summary>
1717
/// <param name="settings">The settings.</param>
18-
public CollectionHeaderBuilder(IPostmanGenerationSettings settings)
19-
{
20-
_settings = settings;
21-
}
18+
public CollectionHeaderBuilder(IPostmanGenerationSettings settings) => _settings = settings;
2219

2320
/// <summary>
2421
/// Builds the specified model part.

src/Simplify.Web.Postman/PartBuilders/CollectionItemsBuilder.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Collections;
2-
using System;
1+
using System;
32
using System.Linq;
43
using System.Collections.Generic;
54
using Simplify.Web.Meta;
@@ -13,11 +12,6 @@ namespace Simplify.Web.Postman.PartBuilders
1312
/// <seealso cref="ICollectionPartBuilder" />
1413
public class CollectionItemsBuilder : ICollectionPartBuilder
1514
{
16-
/// <summary>
17-
/// The base URL path.
18-
/// </summary>
19-
public const string BaseUrlPath = "{{BaseUrl}}";
20-
2115
/// <summary>
2216
/// Builds the specified model part.
2317
/// </summary>
@@ -38,18 +32,10 @@ public void Build(CollectionModel model)
3832
private static CollectionItem BuildCollectionItem(IControllerMetaData metaData, KeyValuePair<HttpMethod, string> route) =>
3933
new()
4034
{
41-
Name = metaData.ControllerType.Name,
42-
Request = new Request
43-
{
44-
Url = new Url
45-
{
46-
Host = BaseUrlPath,
47-
Path = BuildPathItems(route.Value)
48-
},
49-
Method = route.Key.ToString().ToUpper()
50-
}
35+
Name = BuildName(metaData),
36+
Request = RequestBuilder.Build(metaData, route)
5137
};
5238

53-
private static IList<string> BuildPathItems(string controllerRoute) => controllerRoute.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToList();
39+
private static string BuildName(IControllerMetaData metaData) => metaData.ControllerType.Name;
5440
}
5541
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text.Encodings.Web;
5+
using System.Text.Json;
6+
using Simplify.Web.Meta;
7+
using Simplify.Web.Postman.Models;
8+
9+
namespace Simplify.Web.Postman.PartBuilders
10+
{
11+
/// <summary>
12+
/// Provides request builder
13+
/// </summary>
14+
public static class RequestBuilder
15+
{
16+
/// <summary>
17+
/// The base URL path.
18+
/// </summary>
19+
public const string DefaultBaseUrlPath = "{{BaseUrl}}";
20+
21+
private static string? _baseUrlPath;
22+
23+
/// <summary>
24+
/// Gets or sets the base URL path.
25+
/// </summary>
26+
public static string BaseUrlPath
27+
{
28+
get => _baseUrlPath ?? DefaultBaseUrlPath;
29+
set => _baseUrlPath = value ?? throw new NotImplementedException(nameof(value));
30+
}
31+
32+
/// <summary>
33+
/// Builds the request
34+
/// </summary>
35+
/// <param name="metaData">The meta data.</param>
36+
/// <param name="route">The route.</param>
37+
public static Request Build(IControllerMetaData metaData, KeyValuePair<HttpMethod, string> route) =>
38+
new()
39+
{
40+
Url = new Url
41+
{
42+
Host = BaseUrlPath,
43+
Path = BuildPath(route.Value)
44+
},
45+
Method = route.Key.ToString().ToUpper(),
46+
Body = TryBuildBody(metaData)
47+
};
48+
49+
private static IList<string> BuildPath(string controllerRoute) => controllerRoute.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToList();
50+
51+
private static Body? TryBuildBody(IControllerMetaData metaData)
52+
{
53+
if (metaData.ControllerType.BaseType.GenericTypeArguments.Length == 0)
54+
return null;
55+
56+
var body = new Body()
57+
{
58+
Mode = "raw",
59+
Raw = BuildRequestJsonData(metaData.ControllerType.BaseType.GenericTypeArguments[0]),
60+
Options = new()
61+
{
62+
Raw = new()
63+
{
64+
Language = "json"
65+
}
66+
}
67+
};
68+
69+
return body;
70+
}
71+
private static string BuildRequestJsonData(Type modelType) => JsonSerializer.Serialize(Activator.CreateInstance(modelType), new()
72+
{
73+
WriteIndented = true
74+
});
75+
}
76+
}

0 commit comments

Comments
 (0)