160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/code/object/http.rb', line 160
def self.code_fetch(*arguments)
verb = arguments.first.to_code.to_s.downcase
url = arguments.second.to_code.to_s
options = arguments.third.to_code
options = Dictionary.new if options.nothing?
username = options.code_get("username").to_s
password = options.code_get("password").to_s
body = options.code_get("body").to_s
= options.code_get("headers").raw || {}
data = options.code_get("data").raw || {}
query = options.code_get("query").raw || {}
query = query.to_a.flatten.map(&:to_s).each_slice(2).to_h.to_query
url = "#{url}?#{query}" if query.present?
uri = ::URI.parse(url)
http = ::Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == "https"
if username.present? || password.present?
[
"Authorization"
] = "Basic #{::Base64.strict_encode64("#{username}:#{password}")}"
end
request_class =
case verb
when "get"
::Net::HTTP::Get
when "head"
::Net::HTTP::Head
when "post"
::Net::HTTP::Post
when "put"
::Net::HTTP::Put
when "delete"
::Net::HTTP::Delete
when "connect"
::Net::HTTP::Get
when "options"
::Net::HTTP::Options
when "trace"
::Net::HTTP::Trace
when "patch"
::Net::HTTP::Patch
else
::Net::HTTP::Get
end
request = request_class.new(uri)
.each { |key, value| request[key.to_s] = value.to_s }
request.body = body if body.present?
request.set_form_data(**data.as_json) if data.present?
response = http.request(request)
code = response.code.to_i
status = STATUS_CODES.key(code) || :ok
Dictionary.new(code: code, status: status, body: response.body)
end
|