1+ //HintName: G.PathBuilder.g.cs
2+ #nullable enable
3+
4+ namespace G
5+ {
6+ /// <summary>
7+ /// A helper class to build URL paths with optional and required parameters.
8+ /// </summary>
9+ public class PathBuilder
10+ {
11+ private readonly global ::System . Text . StringBuilder _stringBuilder =
12+ new global ::System . Text . StringBuilder ( capacity : 256 ) ;
13+ private bool _firstParameter = true ;
14+
15+ /// <summary>
16+ /// Initializes a new instance of the <see cref="PathBuilder"/> class.
17+ /// </summary>
18+ /// <param name="path">The base path for the URL.</param>
19+ /// <param name="baseUri">The base URI to prepend to the path, if any.</param>
20+ public PathBuilder (
21+ string path ,
22+ global ::System . Uri ? baseUri = null )
23+ {
24+ if ( baseUri is not null )
25+ {
26+ _stringBuilder . Append ( baseUri . AbsoluteUri . TrimEnd ( '/' ) ) ;
27+ }
28+
29+ _stringBuilder . Append ( path ) ;
30+ }
31+
32+ /// <summary>
33+ /// Adds a required parameter to the URL.
34+ /// </summary>
35+ /// <param name="name">The name of the parameter.</param>
36+ /// <param name="value">The value of the parameter.</param>
37+ /// <returns>The current <see cref="PathBuilder"/> instance.</returns>
38+ public PathBuilder AddRequiredParameter (
39+ string name ,
40+ string value )
41+ {
42+ if ( _firstParameter )
43+ {
44+ _stringBuilder . Append ( '?' ) ;
45+ _firstParameter = false ;
46+ }
47+ else
48+ {
49+ _stringBuilder . Append ( '&' ) ;
50+ }
51+
52+ _stringBuilder . Append ( global ::System . Uri . EscapeDataString ( name ) ) ;
53+ _stringBuilder . Append ( '=' ) ;
54+ _stringBuilder . Append ( global ::System . Uri . EscapeDataString ( value ) ) ;
55+
56+ return this ;
57+ }
58+
59+ /// <summary>
60+ /// Adds a required parameter with multiple values to the URL.
61+ /// </summary>
62+ /// <param name="name">The name of the parameter.</param>
63+ /// <param name="value">The values of the parameter.</param>
64+ /// <param name="delimiter">The delimiter to use between values.</param>
65+ /// <param name="explode">Whether to explode the values into separate parameters.</param>
66+ /// <returns>The current <see cref="PathBuilder"/> instance.</returns>
67+ public PathBuilder AddRequiredParameter (
68+ string name ,
69+ global ::System . Collections . Generic . IEnumerable < string > value ,
70+ string delimiter = "," ,
71+ bool explode = false )
72+ {
73+ if ( explode )
74+ {
75+ foreach ( var item in value )
76+ {
77+ AddRequiredParameter ( $ "{ name } ", item ) ;
78+ }
79+
80+ return this ;
81+ }
82+
83+ AddRequiredParameter ( name , string . Join ( delimiter , value ) ) ;
84+
85+ return this ;
86+ }
87+
88+ /// <summary>
89+ /// Adds a required parameter with multiple values to the URL, using a selector function.
90+ /// </summary>
91+ /// <typeparam name="T">The type of the values.</typeparam>
92+ /// <param name="name">The name of the parameter.</param>
93+ /// <param name="value">The values of the parameter.</param>
94+ /// <param name="selector">The function to select the string representation of each value.</param>
95+ /// <param name="delimiter">The delimiter to use between values.</param>
96+ /// <param name="explode">Whether to explode the values into separate parameters.</param>
97+ /// <returns>The current <see cref="PathBuilder"/> instance.</returns>
98+ public PathBuilder AddRequiredParameter < T > (
99+ string name ,
100+ global ::System . Collections . Generic . IEnumerable < T > value ,
101+ global ::System . Func < T , string > selector ,
102+ string delimiter = "," ,
103+ bool explode = false )
104+ {
105+ AddRequiredParameter (
106+ name ,
107+ global ::System . Linq . Enumerable . ToArray ( global ::System . Linq . Enumerable . Select ( value , selector ) ) ,
108+ delimiter ,
109+ explode ) ;
110+
111+ return this ;
112+ }
113+
114+ /// <summary>
115+ /// Adds an optional parameter to the URL.
116+ /// </summary>
117+ /// <param name="name">The name of the parameter.</param>
118+ /// <param name="value">The value of the parameter, or null if not present.</param>
119+ /// <returns>The current <see cref="PathBuilder"/> instance.</returns>
120+ public PathBuilder AddOptionalParameter (
121+ string name ,
122+ string ? value )
123+ {
124+ if ( value is not null )
125+ {
126+ AddRequiredParameter ( name , value ) ;
127+ }
128+
129+ return this ;
130+ }
131+
132+ /// <summary>
133+ /// Adds an optional parameter with multiple values to the URL.
134+ /// </summary>
135+ /// <param name="name">The name of the parameter.</param>
136+ /// <param name="value">The values of the parameter, or null if not present.</param>
137+ /// <param name="delimiter">The delimiter to use between values.</param>
138+ /// <param name="explode">Whether to explode the values into separate parameters.</param>
139+ /// <returns>The current <see cref="PathBuilder"/> instance.</returns>
140+ public PathBuilder AddOptionalParameter (
141+ string name ,
142+ global ::System . Collections . Generic . IEnumerable < string > ? value ,
143+ string delimiter = "," ,
144+ bool explode = false )
145+ {
146+ if ( value is not null )
147+ {
148+ AddRequiredParameter ( name , value , delimiter , explode ) ;
149+ }
150+
151+ return this ;
152+ }
153+
154+ /// <summary>
155+ /// Adds an optional parameter with multiple values to the URL, using a selector function.
156+ /// </summary>
157+ /// <typeparam name="T">The type of the values.</typeparam>
158+ /// <param name="name">The name of the parameter.</param>
159+ /// <param name="value">The values of the parameter, or null if not present.</param>
160+ /// <param name="selector">The function to select the string representation of each value.</param>
161+ /// <param name="delimiter">The delimiter to use between values.</param>
162+ /// <param name="explode">Whether to explode the values into separate parameters.</param>
163+ /// <returns>The current <see cref="PathBuilder"/> instance.</returns>
164+ public PathBuilder AddOptionalParameter < T > (
165+ string name ,
166+ global ::System . Collections . Generic . IEnumerable < T > ? value ,
167+ global ::System . Func < T , string > selector ,
168+ string delimiter = "," ,
169+ bool explode = false )
170+ {
171+ if ( value is not null )
172+ {
173+ AddRequiredParameter (
174+ name ,
175+ global ::System . Linq . Enumerable . ToArray ( global ::System . Linq . Enumerable . Select ( value , selector ) ) ,
176+ delimiter ,
177+ explode ) ;
178+ }
179+
180+ return this ;
181+ }
182+
183+ /// <summary>
184+ /// Adds a required parameter to the URL, using a formattable value.
185+ /// </summary>
186+ /// <typeparam name="T">The type of the value.</typeparam>
187+ /// <param name="name">The name of the parameter.</param>
188+ /// <param name="value">The value of the parameter.</param>
189+ /// <param name="format">The format string.</param>
190+ /// <param name="formatProvider">The format provider.</param>
191+ /// <returns>The current <see cref="PathBuilder"/> instance.</returns>
192+ public PathBuilder AddRequiredParameter < T > (
193+ string name ,
194+ T value ,
195+ string ? format = null ,
196+ global ::System . IFormatProvider ? formatProvider = null )
197+ where T : global ::System . IFormattable
198+ {
199+ AddRequiredParameter ( name , value . ToString ( format , formatProvider ) ) ;
200+
201+ return this ;
202+ }
203+
204+ /// <summary>
205+ /// Adds an optional parameter to the URL, using a formattable value.
206+ /// </summary>
207+ /// <typeparam name="T">The type of the value.</typeparam>
208+ /// <param name="name">The name of the parameter.</param>
209+ /// <param name="value">The value of the parameter, or null if not present.</param>
210+ /// <param name="format">The format string.</param>
211+ /// <param name="formatProvider">The format provider.</param>
212+ /// <returns>The current <see cref="PathBuilder"/> instance.</returns>
213+ public PathBuilder AddOptionalParameter < T > (
214+ string name ,
215+ T ? value ,
216+ string ? format = null ,
217+ global ::System . IFormatProvider ? formatProvider = null )
218+ where T : global ::System . IFormattable
219+ {
220+ if ( value is not null )
221+ {
222+ AddOptionalParameter ( name , value . ToString ( format , formatProvider ) ) ;
223+ }
224+
225+ return this ;
226+ }
227+
228+ /// <summary>
229+ /// Returns the constructed URL as a string.
230+ /// </summary>
231+ /// <returns>The constructed URL.</returns>
232+ public override string ToString ( ) => _stringBuilder . ToString ( ) ;
233+ }
234+
235+ /// <summary>
236+ ///
237+ /// </summary>
238+ public class EndPointAuthorization
239+ {
240+ /// <summary>
241+ ///
242+ /// </summary>
243+ public string Type { get ; set ; } = string . Empty ;
244+
245+ /// <summary>
246+ ///
247+ /// </summary>
248+ public string Location { get ; set ; } = string . Empty ;
249+
250+ /// <summary>
251+ ///
252+ /// </summary>
253+ public string Name { get ; set ; } = string . Empty ;
254+
255+ /// <summary>
256+ ///
257+ /// </summary>
258+ public string Value { get ; set ; } = string . Empty ;
259+ }
260+ }
0 commit comments