Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add config to enable instant file download #68

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/crowdin-api/api_resources/glossaries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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 = {})
Expand Down
14 changes: 8 additions & 6 deletions lib/crowdin-api/api_resources/reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 --
Expand Down Expand Up @@ -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)
Expand All @@ -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 = {})
Expand All @@ -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)

Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions lib/crowdin-api/api_resources/source_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/crowdin-api/api_resources/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/crowdin-api/api_resources/translation_memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 = {})
Expand Down
18 changes: 12 additions & 6 deletions lib/crowdin-api/api_resources/translations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/crowdin-api/client/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions lib/crowdin-api/core/send_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading