@@ -119,10 +119,15 @@ def initialize( # rubocop:disable Metrics/ParameterLists
119
119
# @!macro api_errors
120
120
# @raise [Mailtrap::MailSizeError] If the message is too large.
121
121
def send_batch ( base , requests )
122
- perform_request ( :post , api_host , batch_request_path , {
123
- base :,
124
- requests :
125
- } )
122
+ perform_request (
123
+ method : :post ,
124
+ host : api_host ,
125
+ path : batch_request_path ,
126
+ body : {
127
+ base :,
128
+ requests :
129
+ }
130
+ )
126
131
end
127
132
128
133
# Sends an email
@@ -152,15 +157,26 @@ def send_batch(base, requests)
152
157
# @!macro api_errors
153
158
# @raise [Mailtrap::MailSizeError] If the message is too large
154
159
def send ( mail )
155
- perform_request ( :post , api_host , send_path , mail )
160
+ perform_request (
161
+ method : :post ,
162
+ host : api_host ,
163
+ path : send_path ,
164
+ body : mail
165
+ )
156
166
end
157
167
158
168
# Performs a GET request to the specified path
159
169
# @param path [String] The request path
170
+ # @param query_params [Hash] Query parameters to append to the URL (optional)
160
171
# @return [Hash, nil] The JSON response
161
172
# @!macro api_errors
162
- def get ( path )
163
- perform_request ( :get , general_api_host , path )
173
+ def get ( path , query_params = { } )
174
+ perform_request (
175
+ method : :get ,
176
+ host : general_api_host ,
177
+ path :,
178
+ query_params :
179
+ )
164
180
end
165
181
166
182
# Performs a POST request to the specified path
@@ -169,7 +185,12 @@ def get(path)
169
185
# @return [Hash, nil] The JSON response
170
186
# @!macro api_errors
171
187
def post ( path , body = nil )
172
- perform_request ( :post , general_api_host , path , body )
188
+ perform_request (
189
+ method : :post ,
190
+ host : general_api_host ,
191
+ path :,
192
+ body :
193
+ )
173
194
end
174
195
175
196
# Performs a PATCH request to the specified path
@@ -178,15 +199,24 @@ def post(path, body = nil)
178
199
# @return [Hash, nil] The JSON response
179
200
# @!macro api_errors
180
201
def patch ( path , body = nil )
181
- perform_request ( :patch , general_api_host , path , body )
202
+ perform_request (
203
+ method : :patch ,
204
+ host : general_api_host ,
205
+ path :,
206
+ body :
207
+ )
182
208
end
183
209
184
210
# Performs a DELETE request to the specified path
185
211
# @param path [String] The request path
186
212
# @return [Hash, nil] The JSON response
187
213
# @!macro api_errors
188
214
def delete ( path )
189
- perform_request ( :delete , general_api_host , path )
215
+ perform_request (
216
+ method : :delete ,
217
+ host : general_api_host ,
218
+ path :
219
+ )
190
220
end
191
221
192
222
private
@@ -213,23 +243,27 @@ def batch_request_path
213
243
"/api/batch#{ "/#{ inbox_id } " if sandbox } "
214
244
end
215
245
216
- def perform_request ( method , host , path , body = nil )
246
+ def perform_request ( method : , host : , path : , query_params : { } , body : nil )
217
247
http_client = http_client_for ( host )
218
- request = setup_request ( method , path , body )
248
+
249
+ uri = URI ::HTTPS . build ( host :, path :)
250
+ uri . query = URI . encode_www_form ( query_params ) if query_params . any?
251
+
252
+ request = setup_request ( method , uri , body )
219
253
response = http_client . request ( request )
220
254
handle_response ( response )
221
255
end
222
256
223
- def setup_request ( method , path , body = nil )
257
+ def setup_request ( method , uri_or_path , body = nil )
224
258
request = case method
225
259
when :get
226
- Net ::HTTP ::Get . new ( path )
260
+ Net ::HTTP ::Get . new ( uri_or_path )
227
261
when :post
228
- Net ::HTTP ::Post . new ( path )
262
+ Net ::HTTP ::Post . new ( uri_or_path )
229
263
when :patch
230
- Net ::HTTP ::Patch . new ( path )
264
+ Net ::HTTP ::Patch . new ( uri_or_path )
231
265
when :delete
232
- Net ::HTTP ::Delete . new ( path )
266
+ Net ::HTTP ::Delete . new ( uri_or_path )
233
267
else
234
268
raise ArgumentError , "Unsupported HTTP method: #{ method } "
235
269
end
0 commit comments