From fa5d759ca01011983d2a86de2e4a8c10000c11a4 Mon Sep 17 00:00:00 2001 From: Artem Vavilov Date: Thu, 1 Feb 2024 18:33:00 +0200 Subject: [PATCH] feat: Add config to enable instant file download --- README.md | 1 + lib/crowdin-api/api_resources/glossaries.rb | 8 ++++---- lib/crowdin-api/api_resources/reports.rb | 14 ++++++++------ lib/crowdin-api/api_resources/source_files.rb | 6 ++++-- lib/crowdin-api/api_resources/tasks.rb | 6 ++++-- .../api_resources/translation_memory.rb | 4 ++-- lib/crowdin-api/api_resources/translations.rb | 18 ++++++++++++------ lib/crowdin-api/client/configuration.rb | 4 ++++ lib/crowdin-api/core/send_request.rb | 5 +++-- 9 files changed, 42 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index c518e90..d1fed0f 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ crowdin = Crowdin::Client.new do |config| config.api_token = 'YourApiToken' # [String] required config.organization_domain = 'YourOrganizationDomain' # [String] optional config.project_id = 'YourProjectId' # [Integer] nil by default + config.enable_download = false # [Boolean] true by default, immediately downloads the file and returns the dest path as the response config.enable_logger = true # [Boolean] false by default end # Note: Client will initialize default Logger instance if you have specify diff --git a/lib/crowdin-api/api_resources/glossaries.rb b/lib/crowdin-api/api_resources/glossaries.rb index 163954b..7d65742 100644 --- a/lib/crowdin-api/api_resources/glossaries.rb +++ b/lib/crowdin-api/api_resources/glossaries.rb @@ -58,7 +58,7 @@ def edit_glossary(glossary_id = nil, query = {}) Web::SendRequest.new(request).perform end - def export_glossary(query = {}, glossary_id = nil, destination = nil) + def export_glossary(query = {}, glossary_id = nil, destination = nil, download: config.download_enabled?) glossary_id || raise_parameter_is_required_error(:glossary_id) request = Web::Request.new( @@ -67,7 +67,7 @@ def export_glossary(query = {}, glossary_id = nil, destination = nil) "#{config.target_api_url}/glossaries/#{glossary_id}/exports", { params: query } ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end def check_glossary_export_status(glossary_id = nil, export_id = nil) @@ -82,7 +82,7 @@ def check_glossary_export_status(glossary_id = nil, export_id = nil) Web::SendRequest.new(request).perform end - def download_glossary(glossary_id = nil, export_id = nil, destination = nil) + def download_glossary(glossary_id = nil, export_id = nil, destination = nil, download: config.download_enabled?) glossary_id || raise_parameter_is_required_error(:glossary_id) export_id || raise_parameter_is_required_error(:export_id) @@ -91,7 +91,7 @@ def download_glossary(glossary_id = nil, export_id = nil, destination = nil) :get, "#{config.target_api_url}/glossaries/#{glossary_id}/exports/#{export_id}/download" ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end def import_glossary(glossary_id = nil, query = {}) diff --git a/lib/crowdin-api/api_resources/reports.rb b/lib/crowdin-api/api_resources/reports.rb index d3b698c..eb25787 100644 --- a/lib/crowdin-api/api_resources/reports.rb +++ b/lib/crowdin-api/api_resources/reports.rb @@ -27,7 +27,9 @@ def check_report_generation_status(report_id = nil, project_id = config.project_ Web::SendRequest.new(request).perform end - def download_report(report_id = nil, destination = nil, project_id = config.project_id) + def download_report( + report_id = nil, destination = nil, project_id = config.project_id, download: config.download_enabled? + ) report_id || raise_parameter_is_required_error(:report_id) project_id || raise_project_id_is_required_error @@ -36,7 +38,7 @@ def download_report(report_id = nil, destination = nil, project_id = config.proj :get, "#{config.target_api_url}/projects/#{project_id}/reports/#{report_id}/download" ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end # -- For Enterprise mode only -- @@ -67,7 +69,7 @@ def check_group_report_generation_status(group_id = nil, report_id = nil) Web::SendRequest.new(request).perform end - def download_group_report(group_id = nil, report_id = nil, destination = nil) + def download_group_report(group_id = nil, report_id = nil, destination = nil, download: config.download_enabled?) enterprise_mode? || raise_only_for_enterprise_mode_error group_id || raise_parameter_is_required_error(:group_id) report_id || raise_parameter_is_required_error(:report_id) @@ -77,7 +79,7 @@ def download_group_report(group_id = nil, report_id = nil, destination = nil) :get, "#{config.target_api_url}/groups/#{group_id}/reports/#{report_id}/download" ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end def generate_organization_report(query = {}) @@ -104,7 +106,7 @@ def check_organization_report_generation_status(report_id = nil) Web::SendRequest.new(request).perform end - def download_organization_report(report_id = nil, destination = nil) + def download_organization_report(report_id = nil, destination = nil, download: config.download_enabled?) enterprise_mode? || raise_only_for_enterprise_mode_error report_id || raise_parameter_is_required_error(:report_id) @@ -113,7 +115,7 @@ def download_organization_report(report_id = nil, destination = nil) :get, "#{config.target_api_url}/reports/#{report_id}/download" ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end def list_report_settings_templates(query = {}, project_id = config.project_id) diff --git a/lib/crowdin-api/api_resources/source_files.rb b/lib/crowdin-api/api_resources/source_files.rb index 87ac1c5..688df1d 100644 --- a/lib/crowdin-api/api_resources/source_files.rb +++ b/lib/crowdin-api/api_resources/source_files.rb @@ -255,7 +255,9 @@ def edit_file(file_id = nil, query = {}, project_id = config.project_id) # @param destination [String] Destination of File # * {https://developer.crowdin.com/api/v2/#operation/api.projects.files.download.get API Documentation} # * {https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.files.download.get Enterprise API Documentation} - def download_file(file_id = nil, destination = nil, project_id = config.project_id) + def download_file( + file_id = nil, destination = nil, project_id = config.project_id, download: config.download_enabled? + ) file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error @@ -264,7 +266,7 @@ def download_file(file_id = nil, destination = nil, project_id = config.project_ :get, "#{config.target_api_url}/projects/#{project_id}/files/#{file_id}/download" ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end # @param query [Hash] Request Body diff --git a/lib/crowdin-api/api_resources/tasks.rb b/lib/crowdin-api/api_resources/tasks.rb index 0e9c37d..b04e0d5 100644 --- a/lib/crowdin-api/api_resources/tasks.rb +++ b/lib/crowdin-api/api_resources/tasks.rb @@ -27,7 +27,9 @@ def add_task(query = {}, project_id = config.project_id) Web::SendRequest.new(request).perform end - def export_task_strings(task_id = nil, destination = nil, project_id = config.project_id) + def export_task_strings( + task_id = nil, destination = nil, project_id = config.project_id, download: config.download_enabled? + ) task_id || raise_parameter_is_required_error(:task_id) project_id || raise_project_id_is_required_error @@ -36,7 +38,7 @@ def export_task_strings(task_id = nil, destination = nil, project_id = config.pr :post, "#{config.target_api_url}/projects/#{project_id}/tasks/#{task_id}/exports" ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end def get_task(task_id = nil, project_id = config.project_id) diff --git a/lib/crowdin-api/api_resources/translation_memory.rb b/lib/crowdin-api/api_resources/translation_memory.rb index 831f887..b44730c 100644 --- a/lib/crowdin-api/api_resources/translation_memory.rb +++ b/lib/crowdin-api/api_resources/translation_memory.rb @@ -91,7 +91,7 @@ def check_tm_export_status(tm_id = nil, export_id = nil) Web::SendRequest.new(request).perform end - def download_tm(tm_id = nil, export_id = nil, destination = nil) + def download_tm(tm_id = nil, export_id = nil, destination = nil, download: config.download_enabled?) tm_id || raise_parameter_is_required_error(:tm_id) export_id || raise_parameter_is_required_error(:export_id) @@ -100,7 +100,7 @@ def download_tm(tm_id = nil, export_id = nil, destination = nil) :get, "#{config.target_api_url}/tms/#{tm_id}/exports/#{export_id}/download" ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end def import_tm(tm_id = nil, query = {}) diff --git a/lib/crowdin-api/api_resources/translations.rb b/lib/crowdin-api/api_resources/translations.rb index 0da678b..95d7408 100644 --- a/lib/crowdin-api/api_resources/translations.rb +++ b/lib/crowdin-api/api_resources/translations.rb @@ -40,7 +40,9 @@ def build_project_directory_translation(directory_id = nil, query = {}, project_ Web::SendRequest.new(request).perform end - def build_project_file_translation(file_id = nil, query = {}, destination = nil, project_id = config.project_id) + def build_project_file_translation( + file_id = nil, query = {}, destination = nil, project_id = config.project_id, download: config.download_enabled? + ) file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error @@ -52,7 +54,7 @@ def build_project_file_translation(file_id = nil, query = {}, destination = nil, "#{config.target_api_url}/projects/#{project_id}/translations/builds/files/#{file_id}", { params: query, headers: headers } ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end def list_project_builds(query = {}, project_id = config.project_id) @@ -92,7 +94,9 @@ def upload_translations(language_id = nil, query = {}, project_id = config.proje Web::SendRequest.new(request).perform end - def download_project_translations(build_id = nil, destination = nil, project_id = config.project_id) + def download_project_translations( + build_id = nil, destination = nil, project_id = config.project_id, download: config.download_enabled? + ) build_id || raise_parameter_is_required_error(:build_id) project_id || raise_project_id_is_required_error @@ -101,7 +105,7 @@ def download_project_translations(build_id = nil, destination = nil, project_id :get, "#{config.target_api_url}/projects/#{project_id}/translations/builds/#{build_id}/download" ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end def check_project_build_status(build_id = nil, project_id = config.project_id) @@ -128,7 +132,9 @@ def cancel_build(build_id = nil, project_id = config.project_id) Web::SendRequest.new(request).perform end - def export_project_translation(query = {}, destination = nil, project_id = config.project_id) + def export_project_translation( + query = {}, destination = nil, project_id = config.project_id, download: config.download_enabled? + ) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -137,7 +143,7 @@ def export_project_translation(query = {}, destination = nil, project_id = confi "#{config.target_api_url}/projects/#{project_id}/translations/exports", { params: query } ) - Web::SendRequest.new(request, destination).perform + Web::SendRequest.new(request, destination, download: download).perform end end end diff --git a/lib/crowdin-api/client/configuration.rb b/lib/crowdin-api/client/configuration.rb index eebf247..7755c67 100644 --- a/lib/crowdin-api/client/configuration.rb +++ b/lib/crowdin-api/client/configuration.rb @@ -6,12 +6,16 @@ class Configuration attr_accessor :project_id attr_accessor :organization_domain + attr_accessor :enable_download + alias download_enabled? enable_download + attr_accessor :enable_logger alias logger_enabled? enable_logger attr_reader :target_api_url def initialize + @enable_download = true @target_api_url = '/api/v2' end diff --git a/lib/crowdin-api/core/send_request.rb b/lib/crowdin-api/core/send_request.rb index 4a91e2b..88e504d 100644 --- a/lib/crowdin-api/core/send_request.rb +++ b/lib/crowdin-api/core/send_request.rb @@ -5,9 +5,10 @@ module Web class SendRequest attr_reader :request - def initialize(request, file_destination = nil) + def initialize(request, file_destination = nil, download: false) @request = request @file_destination = file_destination + @download = download @errors = [] end @@ -44,7 +45,7 @@ def parse_response(response) end def fetch_response_data(doc) - if doc['data'].is_a?(Hash) && doc['data']['url'] && doc['data']['url'].include?('response-content-disposition') + if @download && doc['data'].is_a?(Hash) && doc.dig('data', 'url')&.include?('response-content-disposition') download_file(doc['data']['url']) else doc