karaden-prg-ruby 1.0.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -1
  3. data/lib/karaden/config.rb +2 -2
  4. data/lib/karaden/exception/bulk_message_create_failed_exception.rb +9 -0
  5. data/lib/karaden/exception/bulk_message_list_message_retry_limit_exceed_exception.rb +9 -0
  6. data/lib/karaden/exception/bulk_message_show_retry_limit_exceed_exception.rb +9 -0
  7. data/lib/karaden/exception/file_download_failed_exception.rb +9 -0
  8. data/lib/karaden/exception/file_not_found_exception.rb +9 -0
  9. data/lib/karaden/exception/file_upload_failed_exception.rb +9 -0
  10. data/lib/karaden/model/bulk_file.rb +34 -0
  11. data/lib/karaden/model/bulk_message.rb +53 -0
  12. data/lib/karaden/model/message.rb +4 -0
  13. data/lib/karaden/model/requestable.rb +7 -0
  14. data/lib/karaden/net/no_contents_response.rb +80 -0
  15. data/lib/karaden/net/requestor.rb +2 -2
  16. data/lib/karaden/net/requestor_interface.rb +1 -1
  17. data/lib/karaden/net/response.rb +8 -0
  18. data/lib/karaden/net/response_interface.rb +8 -0
  19. data/lib/karaden/param/message/bulk/bulk_message_create_params.rb +79 -0
  20. data/lib/karaden/param/message/bulk/bulk_message_download_params.rb +139 -0
  21. data/lib/karaden/param/message/bulk/bulk_message_list_message_params.rb +72 -0
  22. data/lib/karaden/param/message/bulk/bulk_message_params.rb +15 -0
  23. data/lib/karaden/param/message/bulk/bulk_message_show_params.rb +72 -0
  24. data/lib/karaden/request_options.rb +2 -0
  25. data/lib/karaden/service/bulk_message_service.rb +90 -0
  26. data/lib/karaden/utility.rb +31 -1
  27. data/lib/karaden.rb +15 -0
  28. data/mock/2023-12-01.yaml +783 -0
  29. data/mock/2024-03-01.yaml +1061 -0
  30. data/mock/latest +1 -1
  31. metadata +19 -2
@@ -0,0 +1,72 @@
1
+ module Karaden
2
+ module Param
3
+ module Message
4
+ module Bulk
5
+ class BulkMessageShowParams < BulkMessageParams
6
+ attr_accessor :id
7
+
8
+ def initialize()
9
+ @id = nil
10
+ super
11
+ end
12
+
13
+ def to_path
14
+ "#{CONTEXT_PATH}/#{@id}"
15
+ end
16
+
17
+ def validate
18
+ errors = Karaden::Model::KaradenObject.new
19
+ has_error = false
20
+
21
+ messages = validate_id
22
+ unless messages.empty?
23
+ errors.set_property('id', messages)
24
+ has_error = true
25
+ end
26
+
27
+ if has_error
28
+ e = Karaden::Exception::InvalidParamsException.new
29
+ error = Karaden::Model::Error.new
30
+ error.set_property('object', Karaden::Model::Error::OBJECT_NAME)
31
+ error.set_property('errors', errors)
32
+ e.error = error
33
+ raise e
34
+ end
35
+
36
+ self
37
+ end
38
+
39
+ def self.new_builder
40
+ BulkMessageShowParamsBuilder.new
41
+ end
42
+
43
+ protected
44
+
45
+ def validate_id
46
+ messages = []
47
+ if @id.nil? || @id == ''
48
+ messages << 'idは必須です。'
49
+ messages << '文字列(UUID)を入力してください。'
50
+ end
51
+ messages
52
+ end
53
+ end
54
+
55
+ class BulkMessageShowParamsBuilder
56
+ def initialize
57
+ @params = BulkMessageShowParams.new
58
+ end
59
+
60
+ def with_id(id)
61
+ @params.id = id
62
+ self
63
+ end
64
+
65
+ def build
66
+ @params.clone
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -140,10 +140,12 @@ module Karaden
140
140
 
141
141
  def with_connection_timeout(connection_timeout)
142
142
  @request_options.connection_timeout = connection_timeout
143
+ self
143
144
  end
144
145
 
145
146
  def with_read_timeout(read_timeout)
146
147
  @request_options.read_timeout = read_timeout
148
+ self
147
149
  end
148
150
 
149
151
  def build
@@ -0,0 +1,90 @@
1
+ module Karaden
2
+ module Service
3
+ class BulkMessageService
4
+ REGEX_PATTERN = /filename="([^"]+)"/.freeze
5
+
6
+ def self.create(filename, request_options = nil)
7
+ raise Karaden::Exception::FileNotFoundException unless File.file?(filename)
8
+
9
+ bulk_file = Karaden::Model::BulkFile.create(request_options)
10
+
11
+ Karaden::Utility.put_signed_url(bulk_file.url, filename, 'text/csv', request_options)
12
+
13
+ params = Karaden::Param::Message::Bulk::BulkMessageCreateParams
14
+ .new_builder
15
+ .with_bulk_file_id(bulk_file.id)
16
+ .build
17
+ Karaden::Model::BulkMessage.create(params, request_options)
18
+ end
19
+
20
+ def self.download(params, request_options = nil)
21
+ params.validate
22
+ show_params = Karaden::Param::Message::Bulk::BulkMessageShowParams.new_builder
23
+ .with_id(params.id)
24
+ .build
25
+ unless Karaden::Service::BulkMessageService.check_bulk_message_status(params.max_retries, params.retry_interval, show_params, request_options)
26
+ raise Exception::BulkMessageShowRetryLimitExceedException
27
+ end
28
+
29
+ list_message_params = Param::Message::Bulk::BulkMessageListMessageParams.new_builder
30
+ .with_id(params.id)
31
+ .build
32
+ download_url = Karaden::Service::BulkMessageService.get_download_url(params.max_retries, params.retry_interval, list_message_params, request_options)
33
+ raise Exception::BulkMessageListMessageRetryLimitExceedException if download_url.nil?
34
+
35
+ begin
36
+ Karaden::Service::BulkMessageService.get_contents(download_url, params.directory_path, request_options)
37
+ rescue StandardError
38
+ raise Exception::FileDownloadFailedException
39
+ end
40
+ end
41
+
42
+ def self.get_contents(download_url, directory_path, request_options = nil)
43
+ uri = URI.parse(download_url)
44
+ http = ::Net::HTTP.new(uri.host, uri.port)
45
+ http.use_ssl = uri.scheme == 'https'
46
+ http.open_timeout, http.read_timeout = Karaden::Utility.get_timeout(request_options)
47
+ http.request_get(uri.request_uri) do |response|
48
+ match = response['content-disposition'].match(Karaden::Service::BulkMessageService::REGEX_PATTERN)
49
+ raise Exception::FileDownloadFailedException unless match
50
+
51
+ filename = File.join(File.realpath(directory_path), match[1])
52
+ File.open(filename, 'w') do |file|
53
+ response.read_body do |chunk|
54
+ file.write(chunk)
55
+ end
56
+ end
57
+ end
58
+ true
59
+ end
60
+
61
+ def self.check_bulk_message_status(retry_count, retry_interval, params, request_options)
62
+ result = false
63
+ (0..retry_count).each do |count|
64
+ # sleep retry_interval if count.positive?
65
+ bulk_message = Karaden::Model::BulkMessage.show(params, request_options)
66
+ raise Exception::BulkMessageCreateFailedException if bulk_message.status == Karaden::Model::BulkMessage::STATUS_ERROR
67
+
68
+ if bulk_message.status == Karaden::Model::BulkMessage::STATUS_DONE
69
+ result = true
70
+ break
71
+ end
72
+ end
73
+ result
74
+ end
75
+
76
+ def self.get_download_url(retry_count, retry_interval, params, request_options)
77
+ result = nil
78
+ (0..retry_count).each do |count|
79
+ # sleep retry_interval if count.positive?
80
+ output = Karaden::Model::BulkMessage.list_message(params, request_options)
81
+ unless output.nil?
82
+ result = output
83
+ break
84
+ end
85
+ end
86
+ result
87
+ end
88
+ end
89
+ end
90
+ end
@@ -3,8 +3,12 @@ module Karaden
3
3
  OBJECT_TYPES = {
4
4
  Karaden::Model::Error::OBJECT_NAME => Karaden::Model::Error,
5
5
  Karaden::Model::Collection::OBJECT_NAME => Karaden::Model::Collection,
6
- Karaden::Model::Message::OBJECT_NAME => Karaden::Model::Message
6
+ Karaden::Model::Message::OBJECT_NAME => Karaden::Model::Message,
7
+ Karaden::Model::BulkFile::OBJECT_NAME => Karaden::Model::BulkFile,
8
+ Karaden::Model::BulkMessage::OBJECT_NAME => Karaden::Model::BulkMessage
7
9
  }.freeze
10
+ DEFAULT_CONNECTION_TIMEOUT = 10
11
+ DEFAULT_READ_TIMEOUT = 30
8
12
 
9
13
  def self.convert_to_karaden_object(contents, request_options)
10
14
  clazz = OBJECT_TYPES[contents['object']] || Karaden::Model::KaradenObject
@@ -31,5 +35,31 @@ module Karaden
31
35
  v.is_a?(Hash) ? convert_to_karaden_object(v, request_options) : v
32
36
  end
33
37
  end
38
+
39
+ def self.put_signed_url(signed_url, filename, content_type = 'application/octet-stream', request_options = nil)
40
+ uri = URI.parse(signed_url)
41
+ http = ::Net::HTTP.new(uri.host, uri.port)
42
+ http.use_ssl = uri.scheme == 'https'
43
+ http.open_timeout, http.read_timeout = get_timeout(request_options)
44
+
45
+ request = ::Net::HTTP::Put.new(uri.request_uri)
46
+ response = File.open(filename, 'rb') do |f|
47
+ request.body_stream = f
48
+ request['Content-Length'] = f.size
49
+ request['Content-Type'] = content_type
50
+
51
+ http.request(request)
52
+ end
53
+
54
+ raise Karaden::Exception::FileUploadFailedException unless response.code == '200'
55
+ rescue Karaden::Exception::FileUploadFailedException
56
+ raise
57
+ rescue StandardError
58
+ raise Karaden::Exception::FileUploadFailedException
59
+ end
60
+
61
+ def self.get_timeout(request_options = nil)
62
+ [request_options&.connection_timeout || DEFAULT_CONNECTION_TIMEOUT, request_options&.read_timeout || DEFAULT_READ_TIMEOUT]
63
+ end
34
64
  end
35
65
  end
data/lib/karaden.rb CHANGED
@@ -10,7 +10,13 @@ require_relative 'karaden/config'
10
10
  require_relative 'karaden/request_options'
11
11
  require_relative 'karaden/exception/karaden_exception'
12
12
  require_relative 'karaden/exception/bad_request_exception'
13
+ require_relative 'karaden/exception/bulk_message_create_failed_exception'
14
+ require_relative 'karaden/exception/bulk_message_list_message_retry_limit_exceed_exception'
15
+ require_relative 'karaden/exception/bulk_message_show_retry_limit_exceed_exception'
13
16
  require_relative 'karaden/exception/forbidden_exception'
17
+ require_relative 'karaden/exception/file_download_failed_exception'
18
+ require_relative 'karaden/exception/file_not_found_exception'
19
+ require_relative 'karaden/exception/file_upload_failed_exception'
14
20
  require_relative 'karaden/exception/not_found_exception'
15
21
  require_relative 'karaden/exception/invalid_request_options_exception'
16
22
  require_relative 'karaden/exception/invalid_params_exception'
@@ -23,14 +29,23 @@ require_relative 'karaden/net/requestor_interface'
23
29
  require_relative 'karaden/net/requestor'
24
30
  require_relative 'karaden/net/response_interface'
25
31
  require_relative 'karaden/net/response'
32
+ require_relative 'karaden/net/no_contents_response'
26
33
  require_relative 'karaden/model/karaden_object'
27
34
  require_relative 'karaden/model/requestable'
28
35
  require_relative 'karaden/model/message'
29
36
  require_relative 'karaden/model/error'
30
37
  require_relative 'karaden/model/collection'
38
+ require_relative 'karaden/model/bulk_file'
39
+ require_relative 'karaden/model/bulk_message'
31
40
  require_relative 'karaden/param/message/message_params'
32
41
  require_relative 'karaden/param/message/message_create_params'
33
42
  require_relative 'karaden/param/message/message_cancel_params'
34
43
  require_relative 'karaden/param/message/message_detail_params'
35
44
  require_relative 'karaden/param/message/message_list_params'
45
+ require_relative 'karaden/param/message/bulk/bulk_message_params'
46
+ require_relative 'karaden/param/message/bulk/bulk_message_create_params'
47
+ require_relative 'karaden/param/message/bulk/bulk_message_download_params'
48
+ require_relative 'karaden/param/message/bulk/bulk_message_list_message_params'
49
+ require_relative 'karaden/param/message/bulk/bulk_message_show_params'
50
+ require_relative 'karaden/service/bulk_message_service'
36
51
  require_relative 'karaden/utility'