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

Use HTTPX for one_drive interaction.

Pavel Balashou281ca01fd558
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb
index 6723f7a76f0..bd5bea55e39 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb
@@ -44,12 +44,16 @@ module Storages
def call(user:, file_link:)
Util.using_user_token(@storage, user) do |token|
- response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
- http.get(uri_path_for(file_link.origin_id), { 'Authorization' => "Bearer #{token.access_token}" })
- end
+ response = HTTPX.get(
+ Util.join_uri_path(
+ @uri,
+ uri_path_for(file_link.origin_id)
+ ),
+ headers: { 'Authorization' => "Bearer #{token.access_token}" }
+ )
- if Net::HTTPRedirection === response
- ServiceResult.success(result: response.header['Location'])
+ if (300..399).include?(response.status)
+ ServiceResult.success(result: response.headers['Location'])
else
handle_errors(response)
end
@@ -60,13 +64,13 @@ module Storages
def handle_errors(response)
case response
- when Net::HTTPNotFound
+ when 404
ServiceResult.failure(result: :not_found,
errors: ::Storages::StorageError.new(code: :not_found, data: response.body))
- when Net::HTTPForbidden
+ when 403
ServiceResult.failure(result: :forbidden,
errors: ::Storages::StorageError.new(code: :forbidden, data: response.body))
- when Net::HTTPUnauthorized
+ when 401
ServiceResult.failure(result: :unauthorized,
errors: ::Storages::StorageError.new(code: :unauthorized, data: response.body))
else
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
index 35fc2c87b4f..84d1f1be121 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
@@ -48,10 +48,10 @@ module Storages
def call(user:, folder:)
result = Util.using_user_token(@storage, user) do |token|
- # Make the Get Request to the necessary endpoints
- response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
- http.get(children_uri_path_for(folder) + FIELDS, { 'Authorization' => "Bearer #{token.access_token}" })
- end
+ response = HTTPX.get(
+ Util.join_uri_path(@uri, children_uri_path_for(folder) + FIELDS),
+ headers: { 'Authorization' => "Bearer #{token.access_token}" }
+ )
handle_response(response, :value)
end
@@ -66,16 +66,16 @@ module Storages
private
def handle_response(response, map_value)
- json = MultiJson.load(response.body, symbolize_keys: true)
+ json = MultiJson.load(response.body.to_s, symbolize_keys: true)
error_data = ::Storages::StorageErrorData.new(source: self, payload: json)
- case response
- when Net::HTTPSuccess
- ServiceResult.success(result: MultiJson.load(response.body, symbolize_keys: true)[map_value])
- when Net::HTTPNotFound
+ case response.status
+ when 200..299
+ ServiceResult.success(result: json.fetch(map_value))
+ when 404
ServiceResult.failure(result: :not_found,
errors: ::Storages::StorageError.new(code: :not_found, data: error_data))
- when Net::HTTPUnauthorized
+ when 401
ServiceResult.failure(result: :unauthorized,
errors: ::Storages::StorageError.new(code: :unauthorized, data: error_data))
else
@@ -108,9 +108,10 @@ module Storages
def empty_response(user, folder)
result = Util.using_user_token(@storage, user) do |token|
- response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
- http.get(location_uri_path_for(folder) + FIELDS, { 'Authorization' => "Bearer #{token.access_token}" })
- end
+ response = HTTPX.get(
+ Util.join_uri_path(@uri, location_uri_path_for(folder) + FIELDS),
+ headers: { 'Authorization' => "Bearer #{token.access_token}" }
+ )
handle_response(response, :id)
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb
index d0125ed5ba0..150af97837d 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb
@@ -32,27 +32,30 @@ module Storages
private
def make_file_request(drive_item_id, token, select_url_query)
- response_data = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
- http.get(uri_path_for(drive_item_id) + select_url_query, { 'Authorization' => "Bearer #{token.access_token}" })
- end
-
- handle_responses(response_data)
+ # response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
+ # http.get(uri_path_for(drive_item_id) + select_url_query, { 'Authorization' => "Bearer #{token.access_token}" })
+ # end
+ response = HTTPX.get(
+ UTIL.join_uri_path(@uri, uri_path_for(drive_item_id) + select_url_query),
+ headers: { 'Authorization' => "Bearer #{token.access_token}" }
+ )
+ handle_responses(response)
end
def handle_responses(response)
- json = MultiJson.load(response.body, symbolize_keys: true)
+ json = MultiJson.load(response.body.to_s, symbolize_keys: true)
error_data = ::Storages::StorageErrorData.new(source: self, payload: json)
- case response
- when Net::HTTPSuccess
+ case response.status
+ when 200..299
ServiceResult.success(result: json)
- when Net::HTTPNotFound
+ when 404
ServiceResult.failure(result: :not_found,
errors: ::Storages::StorageError.new(code: :not_found, data: error_data))
- when Net::HTTPForbidden
+ when 403
ServiceResult.failure(result: :forbidden,
errors: ::Storages::StorageError.new(code: :forbidden, data: error_data))
- when Net::HTTPUnauthorized
+ when 401
ServiceResult.failure(result: :unauthorized,
errors: ::Storages::StorageError.new(code: :unauthorized, data: error_data))
else
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb
index 57b6be0c419..a9c7c43b095 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb
@@ -51,27 +51,27 @@ module Storages
private
def request_drive(token)
- response_data = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
- http.get(drive_uri_path, { 'Authorization' => "Bearer #{token.access_token}" })
- end
-
- handle_responses(response_data)
+ response = HTTPX.get(
+ Util.join_uri_path(@uri, drive_uri_path),
+ headers: { 'Authorization' => "Bearer #{token.access_token}" }
+ )
+ handle_responses(response)
end
def handle_responses(response)
- json = MultiJson.load(response.body, symbolize_keys: true)
+ json = MultiJson.load(response.body.to_s, symbolize_keys: true)
error_data = ::Storages::StorageErrorData.new(source: self, payload: json)
- case response
- when Net::HTTPSuccess
+ case response.status
+ when 200..299
ServiceResult.success(result: json)
- when Net::HTTPNotFound
+ when 404
ServiceResult.failure(result: :not_found,
errors: ::Storages::StorageError.new(code: :not_found, data: error_data))
- when Net::HTTPForbidden
+ when 403
ServiceResult.failure(result: :forbidden,
errors: ::Storages::StorageError.new(code: :forbidden, data: error_data))
- when Net::HTTPUnauthorized
+ when 401
ServiceResult.failure(result: :unauthorized,
errors: ::Storages::StorageError.new(code: :unauthorized, data: error_data))
else
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb
index 582a2a5bda1..babf8490ca9 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb
@@ -46,11 +46,13 @@ module Storages
folder, filename = data.slice('parent', 'file_name').values
Util.using_user_token(@storage, user) do |token|
- response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
- http.post(uri_path_for(folder, filename),
- payload(filename),
- { 'Authorization' => "Bearer #{token.access_token}", 'Content-Type' => 'application/json' })
- end
+ response = HTTPX
+ .with(headers: { 'Authorization' => "Bearer #{token.access_token}",
+ 'Content-Type' => 'application/json' })
+ .post(
+ Util.join_uri_path(@uri, uri_path_for(folder, filename)),
+ json: payload(filename)
+ )
handle_response(response)
end
@@ -59,17 +61,17 @@ module Storages
private
def payload(filename)
- { item: { "@microsoft.graph.conflictBehavior" => "rename", name: filename } }.to_json
+ { item: { "@microsoft.graph.conflictBehavior" => "rename", name: filename } }
end
def handle_response(response)
- case response
- when Net::HTTPSuccess
+ case response.status
+ when 200..299
upload_url = MultiJson.load(response.body, symbolize_keys: true)[:uploadUrl]
ServiceResult.success(result: ::Storages::UploadLink.new(URI(upload_url), :put))
- when Net::HTTPNotFound
+ when 404
ServiceResult.failure(result: :not_found, errors: ::Storages::StorageError.new(code: :not_found))
- when Net::HTTPUnauthorized
+ when 401
ServiceResult.failure(result: :unauthorized, errors: ::Storages::StorageError.new(code: :unauthorized))
else
ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error))
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 ffc6dc19eb4..b6294765245 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
@@ -59,6 +59,14 @@ module Storages::Peripherals::StorageInteraction::OneDrive::Util
)
end
+ def join_uri_path(uri, *)
+ # We use `File.join` to ensure single `/` in between every part. This API will break if executed on a
+ # Windows context, as it used `\` as file separators. But we anticipate that OpenProject
+ # Server is not run on a Windows context.
+ # URI::join cannot be used, as it behaves very different for the path parts depending on trailing slashes.
+ File.join(uri.to_s, *)
+ end
+
def extract_location(parent_reference, file_name = '')
location = parent_reference[:path].gsub(/.*root:/, '')