Skip to content

Commit

Permalink
[#50684] Migrate to httpx http ruby client
Browse files Browse the repository at this point in the history
  • Loading branch information
ba1ash committed Oct 24, 2023
1 parent c7ec5d3 commit 62328f8
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 188 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ gem 'factory_bot_rails', '~> 6.2.0', require: false

gem 'turbo-rails', "~> 1.1"

gem 'httpx'

group :test do
gem 'launchy', '~> 2.5.0'
gem 'rack-test', '~> 2.1.0'
Expand Down
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,11 @@ GEM
htmlbeautifier (1.4.2)
htmldiff (0.0.1)
htmlentities (4.3.4)
http-2-next (1.0.1)
http_parser.rb (0.6.0)
httpclient (2.8.3)
httpx (1.0.2)
http-2-next (>= 1.0.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
i18n-js (4.2.3)
Expand Down Expand Up @@ -1078,6 +1081,7 @@ DEPENDENCIES
grids!
html-pipeline (~> 2.14.0)
htmldiff
httpx
i18n-js (~> 4.2.3)
json_schemer (~> 2.0.0)
json_spec (~> 1.1.4)
Expand Down Expand Up @@ -1207,4 +1211,4 @@ RUBY VERSION
ruby 3.2.1p31

BUNDLED WITH
2.4.7
2.4.6
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,23 @@ def self.call(storage:, user:, group: storage.group)
end

def call(user:, group: @group)
response = Util.http(@uri).post(
Util.join_uri_path(@uri.path, 'ocs/v1.php/cloud/users', CGI.escapeURIComponent(user), 'groups'),
"groupid=#{CGI.escapeURIComponent(group)}",
Util
.basic_auth_header(@username, @password)
.merge(
'OCS-APIRequest' => 'true'
)
)
response = Util
.httpx
.basic_auth(@username, @password)
.with(headers: { 'OCS-APIRequest' => 'true' })
.post(
Util.join_uri_path(
@uri,
'ocs/v1.php/cloud/users',
CGI.escapeURIComponent(user),
'groups'
),
form: { "groupid" => CGI.escapeURIComponent(group) },
)

case response
when Net::HTTPSuccess
statuscode = Nokogiri::XML(response.body).xpath('/ocs/meta/statuscode').text
case response.status
when 200
statuscode = Nokogiri::XML(response.body.to_s).xpath('/ocs/meta/statuscode').text
case statuscode
when "100"
ServiceResult.success(message: "User has been added successfully")
Expand All @@ -68,13 +72,13 @@ def call(user:, group: @group)
when "105"
Util.error(:error, "Failed to add user to group")
end
when Net::HTTPMethodNotAllowed
when 405
Util.error(:not_allowed)
when Net::HTTPUnauthorized
when 401
Util.error(:unauthorized)
when Net::HTTPNotFound
when 404
Util.error(:not_found)
when Net::HTTPConflict
when 409
Util.error(:conflict)
else
Util.error(:error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ def build_origin_paths
escaped_username = CGI.escapeURIComponent(@username)

source = Util.join_uri_path(
@uri.path,
@uri,
"remote.php/dav/files",
escaped_username,
Util.escape_path(input[:source_path])
)

destination = Util.join_uri_path(
@uri.path,
@uri,
"remote.php/dav/files",
escaped_username,
Util.escape_path(input[:destination_path])
Expand All @@ -82,14 +82,17 @@ def validate_destination
request = Net::HTTP::Head.new(urls[:destination_url])
request.initialize_http_header Util.basic_auth_header(@username, @password)

response = Util.http(@uri).request(request)
response = Util
.httpx
.basic_auth(@username, @password)
.head(urls[:destination_url])

case response
when Net::HTTPSuccess
case response.status
when 200
Util.error(:conflict, 'Destination folder already exists.')
when Net::HTTPUnauthorized
when 401
Util.error(:unauthorized, "unauthorized (validate_destination)")
when Net::HTTPNotFound
when 404
ServiceResult.success(result: urls)
else
Util.error(:unknown, "Unexpected response (validate_destination): #{response.code}", response)
Expand All @@ -100,24 +103,27 @@ def validate_destination
# rubocop:disable Metrics/AbcSize
def copy_folder
->(urls) do
headers = Util.basic_auth_header(@username, @password)
headers['Destination'] = urls[:destination_url]
headers['Depth'] = 'infinity'

request = Net::HTTP::Copy.new(urls[:source_url], headers)
response = Util.http(@uri).request(request)

case response
when Net::HTTPCreated
response = Util
.httpx
.basic_auth(@username, @password)
.request("COPY",
urls[:source_url],
headers: {
'Destination' => urls[:destination_url],
'Depth' => 'infinity',
})

case response.status
when 201
ServiceResult.success(message: 'Folder was successfully copied')
when Net::HTTPUnauthorized
when 401
Util.error(:unauthorized, "Unauthorized (copy_folder)")
when Net::HTTPNotFound
when 404
Util.error(:not_found, "Project folder not found (copy_folder)")
when Net::HTTPConflict
when 409
Util.error(:conflict, Util.error_text_from_response(response))
else
Util.error(:unknown, "Unexpected response (copy_folder): #{response.code}", response)
Util.error(:unknown, "Unexpected response (copy_folder): #{response.status}", response)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,32 @@ def self.call(storage:, folder_path:)

# rubocop:disable Metrics/AbcSize
def call(folder_path:)
response = Util.http(@uri).mkcol(
Util.join_uri_path(@uri.path, "remote.php/dav/files", CGI.escapeURIComponent(@username), Util.escape_path(folder_path)),
nil,
Util.basic_auth_header(@username, @password)
)
response = Util
.httpx
.basic_auth(@username, @password)
.mkcol(
Util.join_uri_path(
@uri,
"remote.php/dav/files",
CGI.escapeURIComponent(@username),
Util.escape_path(folder_path)
)
)

case response
when Net::HTTPSuccess
case response.status
when 201
ServiceResult.success(message: 'Folder was successfully created.')
when Net::HTTPMethodNotAllowed
when 405
if Util.error_text_from_response(response) == 'The resource you tried to create already exists'
ServiceResult.success(message: 'Folder already exists.')
else
Util.error(:not_allowed)
end
when Net::HTTPUnauthorized
when 401
Util.error(:unauthorized)
when Net::HTTPNotFound
when 404
Util.error(:not_found)
when Net::HTTPConflict
when 409
Util.error(:conflict, Util.error_text_from_response(response))
else
Util.error(:error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,24 @@ def self.call(storage:, user:, file_link:)
def call(user:, file_link:)
result = Util.token(user:, configuration: @configuration) do |token|
service_result = begin
response = Util.http(@uri).post(
Util.join_uri_path(@uri.path, '/ocs/v2.php/apps/dav/api/v1/direct'),
{ fileId: file_link.origin_id }.to_json,
{
'Authorization' => "Bearer #{token.access_token}",
'OCS-APIRequest' => 'true',
'Accept' => 'application/json',
'Content-Type' => 'application/json'
}
)
case response
when Net::HTTPSuccess
response = Util
.httpx
.post(
Util.join_uri_path(@uri, '/ocs/v2.php/apps/dav/api/v1/direct'),
json: { fileId: file_link.origin_id },
headers: {
'Authorization' => "Bearer #{token.access_token}",
'OCS-APIRequest' => 'true',
'Accept' => 'application/json',
'Content-Type' => 'application/json'
}
)
case response.status
when 200
ServiceResult.success(result: response)
when Net::HTTPNotFound
when 404
Util.error(:not_found, 'Outbound request destination not found!', response)
when Net::HTTPUnauthorized
when 401
Util.error(:unauthorized, 'Outbound request not authorized!', response)
else
Util.error(:error, 'Outbound request failed!')
Expand All @@ -72,7 +74,7 @@ def call(user:, file_link:)
if resp.body.blank?
Util.error(:unauthorized, 'Outbound request not authorized!')
else
ServiceResult.success(result: resp.body)
ServiceResult.success(result: resp.body.to_s)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,23 @@ def call(user:, file_id:)
private

def file_info(file_id, token)
response = Util.http(@uri).get(
Util.join_uri_path(@uri.path, FILE_INFO_PATH, file_id),
{
'Authorization' => "Bearer #{token.access_token}",
'Accept' => 'application/json',
'OCS-APIRequest' => 'true'
}
)

case response
when Net::HTTPSuccess
response = Util
.httpx
.get(
Util.join_uri_path(@uri, FILE_INFO_PATH, file_id),
{
'Authorization' => "Bearer #{token.access_token}",
'Accept' => 'application/json',
'OCS-APIRequest' => 'true'
}
)

case response.status
when 200
ServiceResult.success(result: response.body)
when Net::HTTPNotFound
when 404
Util.error(:not_found, 'Outbound request destination not found!', response)
when Net::HTTPUnauthorized
when 401
Util.error(:unauthorized, 'Outbound request not authorized!', response)
else
Util.error(:error, 'Outbound request failed!')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ def call(user:, file_ids: [])
private

def files_info(file_ids, token)
response = Util.http(@uri).post(
Util.join_uri_path(@uri.path, FILES_INFO_PATH),
{ fileIds: file_ids }.to_json,
{
'Authorization' => "Bearer #{token.access_token}",
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'OCS-APIRequest' => 'true'
}
)
case response
when Net::HTTPSuccess
ServiceResult.success(result: response.body)
when Net::HTTPNotFound
httpx = HTTPX.with(headers:
{
'Authorization' => "Bearer #{token.access_token}",
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'OCS-APIRequest' => 'true'
}
)
response = httpx.post(Util.join_uri_path(@uri.to_s, FILES_INFO_PATH), json: { fileIds: file_ids })

case response.status
when 200
ServiceResult.success(result: response.body.to_s)
when 404
Util.error(:not_found, 'Outbound request destination not found!', response)
when Net::HTTPUnauthorized
when 401
Util.error(:unauthorized, 'Outbound request not authorized!', response)
else
Util.error(:error, 'Outbound request failed!')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,26 @@ def self.call(storage:, user:, folder:)
# rubocop:disable Metrics/AbcSize
def call(user:, folder:)
result = Util.token(user:, configuration: @configuration) do |token|
base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files")
@location_prefix = Util.join_uri_path(base_path, token.origin_user_id.gsub(' ', '%20'))

response = Util.http(@uri).propfind(
Util.join_uri_path(base_path, CGI.escapeURIComponent(token.origin_user_id), requested_folder(folder)),
requested_properties,
{
'Depth' => '1',
'Authorization' => "Bearer #{token.access_token}"
}
)

case response
when Net::HTTPSuccess
@location_prefix = Util.join_uri_path(@uri.path, "remote.php/dav/files", token.origin_user_id.gsub(' ', '%20'))

response = Util
.httpx
.request(
"PROPFIND",
Util.join_uri_path(@uri, "remote.php/dav/files", CGI.escapeURIComponent(token.origin_user_id), requested_folder(folder)),
xml: requested_properties,
headers: {
'Depth' => '1',
'Authorization' => "Bearer #{token.access_token}"
}
)

case response.status
when 207
ServiceResult.success(result: response.body)
when Net::HTTPNotFound
when 404
Util.error(:not_found)
when Net::HTTPUnauthorized
when 401
Util.error(:unauthorized)
else
Util.error(:error)
Expand Down Expand Up @@ -105,9 +107,9 @@ def storage_files(response)
.to_a

parent, *files =
a.map do |file_element|
storage_file(file_element)
end
a.map do |file_element|
storage_file(file_element)
end

::Storages::StorageFiles.new(files, parent, ancestors(parent.location))
end
Expand Down
Loading

0 comments on commit 62328f8

Please sign in to comment.