Class: Html2mdMcpClient::Transport::Http
- Inherits:
-
Object
- Object
- Html2mdMcpClient::Transport::Http
- Defined in:
- lib/html2md_mcp_client/transport/http.rb
Instance Attribute Summary collapse
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(url, headers: {}) ⇒ Http
constructor
A new instance of Http.
- #send_notification(payload) ⇒ Object
- #send_request(payload) ⇒ Object
Constructor Details
#initialize(url, headers: {}) ⇒ Http
Returns a new instance of Http.
9 10 11 12 13 14 15 16 |
# File 'lib/html2md_mcp_client/transport/http.rb', line 9 def initialize(url, headers: {}) @uri = URI(url) @headers = headers.merge( 'Content-Type' => 'application/json', 'Accept' => 'application/json, text/event-stream' ) @session_id = nil end |
Instance Attribute Details
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
7 8 9 |
# File 'lib/html2md_mcp_client/transport/http.rb', line 7 def session_id @session_id end |
Instance Method Details
#close ⇒ Object
54 55 56 |
# File 'lib/html2md_mcp_client/transport/http.rb', line 54 def close @session_id = nil end |
#send_notification(payload) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/html2md_mcp_client/transport/http.rb', line 45 def send_notification(payload) http = build_http post = Net::HTTP::Post.new(@uri.request_uri, request_headers) post.body = payload.to_json http.request(post) rescue StandardError nil # fire-and-forget end |
#send_request(payload) ⇒ Object
18 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 |
# File 'lib/html2md_mcp_client/transport/http.rb', line 18 def send_request(payload) http = build_http post = Net::HTTP::Post.new(@uri.request_uri, request_headers) post.body = payload.to_json response = http.request(post) capture_session_id(response) unless response.is_a?(Net::HTTPSuccess) raise ConnectionError, "HTTP #{response.code}: #{response.body}" end body = response.body.to_s.strip if response['content-type']&.include?('text/event-stream') body = parse_sse(body) end JSON.parse(body) rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EPIPE, SocketError, Net::OpenTimeout, Net::ReadTimeout => e raise ConnectionError, "Cannot connect to #{@uri}: #{e.message}" rescue JSON::ParserError => e raise ProtocolError, "Invalid JSON response: #{e.message}" end |