Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#50684] Migrate to httpx http ruby client

Pavel Balashou822608741fbe
diff --git a/Gemfile b/Gemfile
index 0035a68a01c..69b817053e6 100644
--- a/Gemfile
+++ b/Gemfile
@@ -218,6 +218,8 @@ gem 'factory_bot_rails', '~> 6.4.0', require: false
gem 'turbo-rails', "~> 1.1"
+gem 'httpx'
+
group :test do
gem 'launchy', '~> 2.5.0'
gem 'rack-test', '~> 2.1.0'
diff --git a/Gemfile.lock b/Gemfile.lock
index de7d6f946b3..4a3f2e9a51b 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -574,8 +574,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)
@@ -1115,6 +1118,7 @@ DEPENDENCIES
grids!
html-pipeline (~> 2.14.0)
htmldiff
+ httpx
i18n-js (~> 4.2.3)
json_schemer (~> 2.1.0)
json_spec (~> 1.1.4)
@@ -1245,4 +1249,4 @@ RUBY VERSION
ruby 3.2.1p31
BUNDLED WITH
- 2.4.7
+ 2.4.6
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb
index 8e5896573e8..1e4f7ad154d 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb
@@ -43,21 +43,25 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
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) },
+ )
error_data = Storages::StorageErrorData.new(source: self.class, payload: response)
- 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"
@@ -73,13 +77,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
when "105"
Util.error(:error, "Failed to add user to group", error_data)
end
- when Net::HTTPMethodNotAllowed
+ when 405
Util.error(:not_allowed, 'Outbound request method not allowed', error_data)
- when Net::HTTPNotFound
+ when 401
Util.error(:not_found, 'Outbound request destination not found', error_data)
- when Net::HTTPUnauthorized
+ when 404
Util.error(:unauthorized, 'Outbound request not authorized', error_data)
- when Net::HTTPConflict
+ when 409
Util.error(:conflict, Util.error_text_from_response(response), error_data)
else
Util.error(:error, 'Outbound request failed', error_data)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
index b41c18b35b7..f4ea5b8916f 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
@@ -60,14 +60,14 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
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])
@@ -82,14 +82,17 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
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)
@@ -100,24 +103,27 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# 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
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
index c8fb5ef0382..619996c98c8 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
@@ -44,28 +44,34 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# 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)
+ )
+ )
error_data = Storages::StorageErrorData.new(source: self.class, payload: response)
- 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, 'Outbound request method not allowed', error_data)
end
- when Net::HTTPNotFound
- Util.error(:not_found, 'Outbound request destination not found', error_data)
- when Net::HTTPUnauthorized
+ when 401
Util.error(:unauthorized, 'Outbound request not authorized', error_data)
- when Net::HTTPConflict
+ when 404
+ Util.error(:not_found, 'Outbound request destination not found', error_data)
+ when 409
Util.error(:conflict, Util.error_text_from_response(response), error_data)
else
Util.error(:error, 'Outbound request failed', error_data)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
index e9a8c56ed69..186645748a9 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
@@ -45,22 +45,24 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
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!')
@@ -72,7 +74,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
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
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb
index 85d4d8384b8..3f1a544c640 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb
@@ -52,21 +52,23 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
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!')
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
index 1ad6192aeb4..3038fde0a9b 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
@@ -60,22 +60,22 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
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!', response)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
index cf33c90830d..0c0bbcac81a 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
@@ -39,17 +39,19 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
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}"
- }
- )
+ @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}"
+ }
+ )
handle_response(response)
end
@@ -62,12 +64,12 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def handle_response(response)
error_data = Storages::StorageErrorData.new(source: self, payload: response)
- case response
- when Net::HTTPSuccess
+ case response.status
+ when 207
ServiceResult.success(result: response.body)
- when Net::HTTPNotFound
+ when 404
Util.error(:not_found, 'Outbound request destination not found', error_data)
- when Net::HTTPUnauthorized
+ when 401
Util.error(:unauthorized, 'Outbound request not authorized', error_data)
else
Util.error(:error, 'Outbound request failed', error_data)
@@ -108,9 +110,9 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
.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
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb
index 92e63054a8f..de17d4c6021 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb
@@ -44,27 +44,28 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# rubocop:disable Metrics/AbcSize
def call(group:)
- response = Util.http(@uri).get(
- Util.join_uri_path(@uri.path, "ocs/v1.php/cloud/groups", 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'})
+ .get(Util.join_uri_path(@uri, "ocs/v1.php/cloud/groups", CGI.escapeURIComponent(group)))
error_data = Storages::StorageErrorData.new(source: self.class, payload: response)
- case response
- when Net::HTTPSuccess
- group_users = Nokogiri::XML(response.body)
+ case response.status
+ when 200
+ group_users = Nokogiri::XML(response.body.to_s)
.xpath('/ocs/data/users/element')
.map(&:text)
ServiceResult.success(result: group_users)
- when Net::HTTPMethodNotAllowed
+ when 405
Util.error(:not_allowed, 'Outbound request method not allowed', error_data)
- when Net::HTTPNotFound
- Util.error(:not_found, 'Outbound request destination not found', error_data)
- when Net::HTTPUnauthorized
+ when 401
Util.error(:unauthorized, 'Outbound request not authorized', error_data)
- when Net::HTTPConflict
- Util.error(:conflict, Util.error_text_from_response(response), error_data)
+ when 404
+ Util.error(:not_found, 'Outbound request destination not found', error_data)
+ when 409
+ Util.error(:conflict, error_text_from_response(response), error_data)
else
Util.error(:error, 'Outbound request failed', error_data)
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
index 04c7e8ab72e..b2a65fdd633 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
@@ -32,7 +32,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Internal
def initialize(storage)
@uri = storage.uri
- @base_path = UTIL.join_uri_path(@uri.path, "remote.php/dav/files", CGI.escapeURIComponent(storage.username))
+ @base_path =
@username = storage.username
@password = storage.password
end
@@ -42,17 +42,17 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Internal
end
def call(location:)
- response = UTIL.http(@uri).delete(
- UTIL.join_uri_path(@base_path, UTIL.escape_path(location)),
- UTIL.basic_auth_header(@username, @password)
- )
+ response = UTIL
+ .httpx
+ .basic_auth(@username, @password)
+ .delete(UTIL.join_uri_path(@uri, "remote.php/dav/files", CGI.escapeURIComponent(@username), UTIL.escape_path(location)))
- case response
- when Net::HTTPSuccess
+ case response.status
+ when 204
ServiceResult.success
- when Net::HTTPNotFound
+ when 404
UTIL.error(:not_found)
- when Net::HTTPUnauthorized
+ when 401
UTIL.error(:unauthorized)
else
UTIL.error(:error)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
index 866e11fca73..b70863a8ae4 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
@@ -90,22 +90,26 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Internal
end
end.to_xml
- response = UTIL.http(@uri).propfind(
- UTIL.join_uri_path(
- @uri.path,
- 'remote.php/dav/files',
- CGI.escapeURIComponent(@username),
- UTIL.escape_path(path)
- ),
- body,
- UTIL.basic_auth_header(@username, @password).merge('Depth' => depth)
- )
+ response = UTIL
+ .httpx
+ .basic_auth(@username, @password)
+ .with(headers: { "Depth" => depth})
+ .request(
+ "PROPFIND",
+ UTIL.join_uri_path(
+ @uri,
+ 'remote.php/dav/files',
+ CGI.escapeURIComponent(@username),
+ UTIL.escape_path(path)
+ ),
+ xml: body,
+ )
error_data = Storages::StorageErrorData.new(source: self.class, payload: response)
- case response
- when Net::HTTPSuccess
- doc = Nokogiri::XML response.body
+ case response.status
+ when 200..299
+ doc = Nokogiri::XML(response.body.to_s)
result = {}
doc.xpath('/d:multistatus/d:response').each do |resource_section|
resource = CGI.unescape(resource_section.xpath("d:href").text.strip)
@@ -121,12 +125,12 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Internal
end
ServiceResult.success(result:)
- when Net::HTTPMethodNotAllowed
+ when 405
UTIL.error(:not_allowed, 'Outbound request method not allowed', error_data)
- when Net::HTTPNotFound
- UTIL.error(:not_found, 'Outbound request destination not found', error_data)
- when Net::HTTPUnauthorized
+ when 401
UTIL.error(:unauthorized, 'Outbound request not authorized', error_data)
+ when 404
+ UTIL.error(:not_found, 'Outbound request destination not found', error_data)
else
UTIL.error(:error, 'Outbound request failed', error_data)
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb
index 06bb0daf0ff..5a2110fa0db 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb
@@ -43,21 +43,22 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# rubocop:disable Metrics/AbcSize
def call(user:, group: @group)
- response = Util.http(@uri).delete(
- 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' })
+ .delete(
+ Util.join_uri_path(@uri,
+ "ocs/v1.php/cloud/users",
+ CGI.escapeURIComponent(user),
+ "groups?groupid=#{CGI.escapeURIComponent(group)}")
+ )
error_data = Storages::StorageErrorData.new(source: self.class, payload: response)
- 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 removed from group")
@@ -73,13 +74,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
message = Nokogiri::XML(response.body).xpath('/ocs/meta/message').text
Util.error(:error, "Failed to remove user #{user} from group #{group}: #{message}", error_data)
end
- when Net::HTTPMethodNotAllowed
+ when 405
Util.error(:not_allowed, 'Outbound request method not allowed', error_data)
- when Net::HTTPNotFound
- Util.error(:not_found, 'Outbound request destination not found', error_data)
- when Net::HTTPUnauthorized
+ when 401
Util.error(:unauthorized, 'Outbound request not authorized', error_data)
- when Net::HTTPConflict
+ when 404
+ Util.error(:not_found, 'Outbound request destination not found', error_data)
+ when 409
Util.error(:conflict, Util.error_text_from_response(response), error_data)
else
Util.error(:error, 'Outbound request failed', error_data)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb
index f4e85a078b5..09973734337 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb
@@ -34,7 +34,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def initialize(storage)
@uri = storage.uri
- @base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files", CGI.escapeURIComponent(storage.username))
+ @base_path = Util.join_uri_path(@uri, "remote.php/dav/files", CGI.escapeURIComponent(storage.username))
@username = storage.username
@password = storage.password
end
@@ -44,21 +44,25 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
def call(source:, target:)
- response = Util.http(@uri).move(
- Util.join_uri_path(@base_path, Util.escape_path(source)),
- Util.basic_auth_header(@username, @password).merge(
- 'Destination' => Util.join_uri_path(@base_path, Util.escape_path(target))
- )
- )
+ response = Util
+ .httpx
+ .basic_auth(@username, @password)
+ .request(
+ "MOVE",
+ Util.join_uri_path(@base_path, Util.escape_path(source)),
+ headers: {
+ 'Destination' => Util.join_uri_path(@uri.path, "remote.php/dav/files", CGI.escapeURIComponent(@username), Util.escape_path(target))
+ }
+ )
error_data = Storages::StorageErrorData.new(source: self.class, payload: response)
- case response
- when Net::HTTPSuccess
+ case response.status
+ when 201
ServiceResult.success
- when Net::HTTPNotFound
+ when 404
Util.error(:not_found, 'Outbound request destination not found', error_data)
- when Net::HTTPUnauthorized
+ when 401
Util.error(:unauthorized, 'Outbound request not authorized', error_data)
else
Util.error(:error, 'Outbound request failed', error_data)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
index 5de94706e7d..18f90cde322 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
@@ -79,28 +79,32 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
end.to_xml
- response = Util.http(@uri).proppatch(
- Util.join_uri_path(@uri.path,
- "remote.php/dav/files",
- CGI.escapeURIComponent(@username),
- Util.escape_path(path)),
- body,
- Util.basic_auth_header(@username, @password)
- )
+ response = Util
+ .httpx
+ .with(debug_level: 1, debug: $stdout)
+ .basic_auth(@username, @password)
+ .request(
+ "PROPPATCH",
+ Util.join_uri_path(@uri,
+ "remote.php/dav/files",
+ CGI.escapeURIComponent(@username),
+ Util.escape_path(path)),
+ xml: body
+ )
error_data = Storages::StorageErrorData.new(source: self.class, payload: response)
- case response
- when Net::HTTPSuccess
- doc = Nokogiri::XML(response.body)
+ case response.status
+ when 207
+ doc = Nokogiri::XML(response.body.to_s)
if doc.xpath("/d:multistatus/d:response/d:propstat[d:status[text() = 'HTTP/1.1 200 OK']]/d:prop/nc:acl-list").present?
ServiceResult.success(result: :success)
else
Util.error(:error, "nc:acl properly has not been set for #{path}", error_data)
end
- when Net::HTTPNotFound
+ when 404
Util.error(:not_found, 'Outbound request destination not found', error_data)
- when Net::HTTPUnauthorized
+ when 401
Util.error(:unauthorized, 'Outbound request not authorized', error_data)
else
Util.error(:error, 'Outbound request failed', error_data)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
index 55b29a50606..19acdbcb7a7 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
@@ -79,6 +79,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Util
http
end
+ def httpx
+ HTTPX.plugin(:basic_auth).plugin(:webdav)
+ end
+
def error_text_from_response(response)
Nokogiri::XML(response.body).xpath("//s:message").text
end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb
index 2ab785810b1..431d79de298 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb
@@ -116,6 +116,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::CopyTemplat
expect(result).to be_failure
expect(result.errors.code).to eq(:conflict)
+ expect(result.errors.log_message).to eq("The destination node is not found")
end
end
end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_info_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_info_query_spec.rb
index d40f992e679..a18ff41bc57 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_info_query_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_info_query_spec.rb
@@ -31,7 +31,7 @@
require 'spec_helper'
require_module_spec_helper
-RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQuery, :vcr, :webmock do
+RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQuery, :webmock do
using Storages::Peripherals::ServiceResultRefinements
let(:user) { create(:user) }
diff --git a/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb b/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb
index 360cb841928..b0527091d91 100644
--- a/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb
+++ b/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb
@@ -27,7 +27,6 @@
#++
require 'spec_helper'
-require 'webmock/rspec'
RSpec.describe OAuthClients::RedirectUriFromStateService, type: :model do
let(:state) { 'asdf123425' }
diff --git a/spec/support/webmock.rb b/spec/support/webmock.rb
index ce192bf8139..3b6ccb9bb11 100644
--- a/spec/support/webmock.rb
+++ b/spec/support/webmock.rb
@@ -25,7 +25,9 @@
#
# See COPYRIGHT and LICENSE files for more details.
#++
+
require 'webmock/rspec'
+require "httpx/adapters/webmock"
RSpec.configure do |config|
config.before(:suite) do