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

Moves to HTTPX

Marcello Rocha4e166642ce01
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb
index 18b61b05f75..c047161eacc 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb
@@ -44,12 +44,8 @@ module Storages
# NOTE: This is currently creating a folder only on the root folder
def call(folder_path:)
- Util.using_admin_token(@storage) do |token|
- response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
- http.post("/v1.0/drives/#{@storage.drive_id}/root/children",
- payload(folder_path),
- { 'Authorization' => "Bearer #{token.access_token}", 'Content-Type' => 'application/json' })
- end
+ Util.using_admin_token(@storage) do |http|
+ response = http.post("/v1.0/drives/#{@storage.drive_id}/root/children", body: payload(folder_path))
handle_response(response)
end
@@ -60,17 +56,17 @@ module Storages
def handle_response(response)
data = ::Storages::StorageErrorData.new(source: self, payload: response.body)
- case response
- when Net::HTTPSuccess
+ case response.status
+ when 200..299
ServiceResult.success(result: file_info_for(MultiJson.load(response.body, symbolize_keys: true)),
message: 'Folder was successfully created.')
- when Net::HTTPNotFound
+ when 404
ServiceResult.failure(result: :not_found,
errors: ::Storages::StorageError.new(code: :not_found, data:))
- when Net::HTTPUnauthorized
+ when 401
ServiceResult.failure(result: :unauthorized,
errors: ::Storages::StorageError.new(code: :unauthorized, data:))
- when Net::HTTPConflict
+ when 409
ServiceResult.failure(result: :already_exists,
errors: ::Storages::StorageError.new(code: :conflict, data:))
else
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/delete_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/delete_folder_command.rb
index b5ad8333fe7..b422a57aaaf 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/delete_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/delete_folder_command.rb
@@ -43,13 +43,8 @@ module Storages
end
def call(location:)
- Util.using_admin_token(@storage) do |token|
- response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
- http.delete(
- "/v1.0/drives/#{@storage.drive_id}/items/#{location}",
- Authorization: "Bearer #{token.access_token}"
- )
- end
+ Util.using_admin_token(@storage) do |http|
+ response = http.delete("/v1.0/drives/#{@storage.drive_id}/items/#{location}")
ServiceResult.success(result: response.body)
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb
index d524cb5eb0c..725aa1a8038 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb
@@ -43,13 +43,8 @@ module Storages
end
def call(source:, target:)
- Util.using_admin_token(@storage) do |token|
- response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
- http.patch(uri_path(source),
- { name: target }.to_json,
- Authorization: "Bearer #{token.access_token}",
- 'Content-Type' => 'application/json')
- end
+ Util.using_admin_token(@storage) do |http|
+ response = http.patch(uri_path(source), body: { name: target }.to_json)
handle_response(response)
end
@@ -60,18 +55,16 @@ module Storages
def handle_response(response)
data = ::Storages::StorageErrorData.new(source: self.class, payload: response)
- case response
- when Net::HTTPSuccess
- parsed_response = MultiJson.load(response.body, symbolize_keys: true)
-
- ServiceResult.success(result: storage_file(parsed_response))
- when Net::HTTPUnauthorized
+ case response.status
+ when 200..299
+ ServiceResult.success(result: storage_file(response.json(symbolize_keys: true)))
+ when 401
ServiceResult.failure(result: :unauthorized,
errors: ::Storages::StorageError.new(code: :unauthorized, data:))
- when Net::HTTPNotFound
+ when 404
ServiceResult.failure(result: :not_found,
errors: ::Storages::StorageError.new(code: :not_found, data:))
- when Net::HTTPConflict
+ when 409
ServiceResult.failure(result: :conflict,
errors: ::Storages::StorageError.new(code: :conflict, data:))
else
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb
index 973265b9d94..9b51c67d185 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb
@@ -73,7 +73,12 @@ module Storages::Peripherals::StorageInteraction::OneDrive::Util
oauth_client.access_token!(scope: 'https://graph.microsoft.com/.default')
end
- yield token
+ yield HTTPX.with(
+ origin: storage.uri,
+ headers: {
+ authorization: "Bearer #{token.access_token}", accept: "application/json", 'content-type': 'application/json'
+ }
+ )
end
def extract_location(parent_reference, file_name = '')
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/rename_file_command_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/rename_file_command_spec.rb
index f4c6b8d2fb2..6f579777eea 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/rename_file_command_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/rename_file_command_spec.rb
@@ -31,7 +31,7 @@
require 'spec_helper'
require_module_spec_helper
-RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::RenameFileCommand, :vcr, :webmock do
+RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::RenameFileCommand, :webmock do
let(:storage) { create(:sharepoint_dev_drive_storage) }
let(:folder) do
Storages::Peripherals::Registry