1
1
using System ;
2
+ using System . Collections ;
2
3
using System . Collections . Generic ;
3
4
using System . Collections . Specialized ;
4
5
using System . Net . Http ;
@@ -115,6 +116,9 @@ private void BuildQueryParameters()
115
116
// add query parameters
116
117
NameValueCollection query = HttpUtility . ParseQueryString ( string . Empty ) ;
117
118
119
+ // Separately store the list parameters to add them last
120
+ List < string > listParameters = new ( ) ;
121
+
118
122
// build query string from parameters
119
123
foreach ( KeyValuePair < string , object > param in _parameters )
120
124
{
@@ -124,26 +128,52 @@ private void BuildQueryParameters()
124
128
continue ;
125
129
}
126
130
127
- query [ param . Key ] = param . Value switch
131
+ var @switch = new SwitchCase
128
132
{
129
- // TODO: Handle special conversions for other types
130
- // DateTime dateTime => dateTime.ToString("o", CultureInfo.InvariantCulture),
131
- var _ => param . Value . ToString ( ) ,
133
+ { param . Value is IList , ( ) => listParameters = AddListQueryParameter ( listParameters , param . Key , ( IList ) param . Value ) } ,
134
+ { SwitchCaseScenario . Default , ( ) => query [ param . Key ] = param . Value . ToString ( ) } ,
132
135
} ;
136
+ @switch . MatchFirstTrue ( ) ;
133
137
}
134
138
135
- // short circuit if no query parameters
136
- if ( query . Count == 0 )
139
+ // Finalize the query string
140
+ string queryString = query . ToString ( ) ?? string . Empty ;
141
+
142
+ // Add list parameters to the query string
143
+ string parameterCharacter = queryString . Length == 0 ? "?" : "&" ;
144
+ foreach ( string pair in listParameters )
137
145
{
138
- return ;
146
+ queryString += $ "{ parameterCharacter } { pair } ";
147
+ parameterCharacter = "&" ;
139
148
}
140
149
141
150
// rebuild the request URL with the query string appended
142
151
var uriBuilder = new UriBuilder ( _requestMessage . RequestUri ! )
143
152
{
144
- Query = query . ToString ( ) ,
153
+ Query = queryString ,
145
154
} ;
146
- _requestMessage . RequestUri = new Uri ( uriBuilder . ToString ( ) ) ;
155
+
156
+ // _requestMessage.RequestUri = new Uri(uriBuilder.ToString());
157
+ _requestMessage . RequestUri = uriBuilder . Uri ;
158
+ }
159
+
160
+ private static List < string > AddListQueryParameter ( List < string > pairs , string key , IList value )
161
+ {
162
+ string keyPrefix = $ "{ HttpUtility . UrlEncode ( key ) } []";
163
+ // ReSharper disable once LoopCanBeConvertedToQuery
164
+ foreach ( object ? item in value )
165
+ {
166
+ string ? itemString = item ? . ToString ( ) ;
167
+ if ( itemString == null )
168
+ {
169
+ continue ;
170
+ }
171
+
172
+ string pair = $ "{ keyPrefix } ={ HttpUtility . UrlEncode ( itemString ) } ";
173
+ pairs . Add ( pair ) ;
174
+ }
175
+
176
+ return pairs ;
147
177
}
148
178
149
179
/// <inheritdoc cref="EasyPostClient._isDisposed"/>
0 commit comments