Class: EventStore::HTTP::Write
- Inherits:
-
Object
- Object
- EventStore::HTTP::Write
- Includes:
- Request, Log::Dependency
- Defined in:
- lib/event_store/http/write.rb,
lib/event_store/http/write/log_text.rb,
lib/event_store/http/write/substitute.rb
Defined Under Namespace
Modules: LogText, Substitute
Constant Summary collapse
- Error =
Class.new StandardError
- ExpectedVersionError =
Class.new Error
Class Method Summary collapse
Instance Method Summary collapse
- #build_request(batch, stream, expected_version: nil) ⇒ Object
- #call(batch, stream, expected_version: nil) ⇒ Object
- #configure(session: nil) ⇒ Object
- #write_timeout?(response) ⇒ Boolean
- #wrong_expected_version?(response) ⇒ Boolean
Methods included from Request
Class Method Details
.stream_path(stream) ⇒ Object
70 71 72 |
# File 'lib/event_store/http/write.rb', line 70 def self.stream_path(stream) "/streams/#{stream}" end |
Instance Method Details
#build_request(batch, stream, expected_version: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/event_store/http/write.rb', line 57 def build_request(batch, stream, expected_version: nil) path = self.class.stream_path stream request = Net::HTTP::Post.new path request['Content-Type'] = MediaTypes::Events.mime request['ES-ExpectedVersion'] = expected_version.to_s if expected_version request.body = Transform::Write.(batch, :json) request end |
#call(batch, stream, expected_version: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/event_store/http/write.rb', line 19 def call(batch, stream, expected_version: nil) logger.trace { "Writing events (#{LogText.attributes batch, stream, expected_version})" } if batch.is_a? Array batch = MediaTypes::Events::Batch.build :events => batch end request = build_request batch, stream, expected_version: expected_version response = nil self.retry.() do |_retry| response = connection.request request if write_timeout? response logger.warn { "Write timeout (#{LogText.attributes batch, stream, expected_version, response: response})" } _retry.failed end end case response when Net::HTTPCreated then location = response['location'] logger.info { "Events written (#{LogText.attributes batch, stream, expected_version, response: response}, Location: #{location})" } URI.parse location when proc { wrong_expected_version? response } = "Wrong expected version (#{LogText.attributes batch, stream, expected_version, response: response})" logger.error { } raise ExpectedVersionError, else = "Request failed (#{LogText.attributes batch, stream, expected_version, response: response})" logger.error { } raise Error, end end |
#configure(session: nil) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/event_store/http/write.rb', line 11 def configure(session: nil) if session.nil? Retry.configure self else session.configure_retry self end end |
#write_timeout?(response) ⇒ Boolean
78 79 80 |
# File 'lib/event_store/http/write.rb', line 78 def write_timeout?(response) Net::HTTPBadRequest === response && response. == "Write timeout" end |
#wrong_expected_version?(response) ⇒ Boolean
74 75 76 |
# File 'lib/event_store/http/write.rb', line 74 def wrong_expected_version?(response) Net::HTTPBadRequest === response && response. == "Wrong expected EventNumber" end |