Class: Async::Matrix::Client
- Inherits:
-
Object
- Object
- Async::Matrix::Client
- Defined in:
- lib/async/matrix/client.rb
Overview
Async HTTP client for the Matrix Client-Server API.
Every outbound request is authenticated with the appservice as_token.
All methods are fiber-safe and run naturally inside Falcon's async reactor.
client = Async::Matrix::Client.new(config)
client.send_text("!room:example.com", "Hello world")
client.join_room("!room:example.com")
Direct Known Subclasses
Constant Summary collapse
- CLIENT_PREFIX =
"/_matrix/client/v3"- DEFAULT_MAX_RETRIES =
Retry defaults
3- DEFAULT_RETRY_BASE =
max retry attempts (0 disables)
0.5- DEFAULT_MAX_RETRY_DELAY =
initial backoff in seconds
30- RATE_LIMIT_STATUS =
Status codes eligible for retry
429- GATEWAY_ERROR_STATUSES =
[502, 503, 504].freeze
- DEFAULT_RESPONSE_SIZE_LIMIT =
Response size limits (bytes)
50 * 1024 * 1024
- DEFAULT_ERROR_RESPONSE_SIZE_LIMIT =
50 MiB for JSON API responses
512 * 1024
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
512 KiB for error bodies.
Instance Method Summary collapse
-
#api ⇒ Object
Returns a Gateway that provides method-chained access to every Matrix Client-Server API endpoint.
- #close ⇒ Object
- #get(path, max_retries: nil) ⇒ Object
-
#initialize(config, max_retries: DEFAULT_MAX_RETRIES, retry_base_delay: DEFAULT_RETRY_BASE, max_retry_delay: DEFAULT_MAX_RETRY_DELAY, ignore_rate_limit: false, response_size_limit: DEFAULT_RESPONSE_SIZE_LIMIT, error_response_size_limit: DEFAULT_ERROR_RESPONSE_SIZE_LIMIT) ⇒ Client
constructor
A new instance of Client.
-
#join_room(room_id) ⇒ Object
── Room actions ───────────────────────────────────────────.
- #leave_room(room_id) ⇒ Object
-
#media ⇒ Object
Returns a Gateway rooted at /_matrix/media/v3 for media operations.
-
#media_client ⇒ Object
Returns the binary media client used for upload/download operations.
- #post(path, body = {}, max_retries: nil) ⇒ Object
- #put(path, body = {}, max_retries: nil) ⇒ Object
- #send_html(room_id, html, plaintext = nil) ⇒ Object
-
#send_message_event(room_id, event_type, content) ⇒ Object
── Low-level HTTP ─────────────────────────────────────────.
- #send_notice(room_id, text) ⇒ Object
-
#send_text(room_id, text) ⇒ Object
── Messaging ──────────────────────────────────────────────.
-
#set_display_name(name, user_id = nil) ⇒ Object
── Profile ────────────────────────────────────────────────.
-
#whoami ⇒ Object
── Verification ───────────────────────────────────────────.
Constructor Details
#initialize(config, max_retries: DEFAULT_MAX_RETRIES, retry_base_delay: DEFAULT_RETRY_BASE, max_retry_delay: DEFAULT_MAX_RETRY_DELAY, ignore_rate_limit: false, response_size_limit: DEFAULT_RESPONSE_SIZE_LIMIT, error_response_size_limit: DEFAULT_ERROR_RESPONSE_SIZE_LIMIT) ⇒ Client
Returns a new instance of Client.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/async/matrix/client.rb', line 42 def initialize(config, max_retries: DEFAULT_MAX_RETRIES, retry_base_delay: DEFAULT_RETRY_BASE, max_retry_delay: DEFAULT_MAX_RETRY_DELAY, ignore_rate_limit: false, response_size_limit: DEFAULT_RESPONSE_SIZE_LIMIT, error_response_size_limit: DEFAULT_ERROR_RESPONSE_SIZE_LIMIT) @config = config @base = config.homeserver.address @max_retries = max_retries @retry_base_delay = retry_base_delay @max_retry_delay = max_retry_delay @ignore_rate_limit = ignore_rate_limit @response_size_limit = response_size_limit @error_response_size_limit = error_response_size_limit @headers = [ ["authorization", "Bearer #{config.appservice.as_token}"], ["content-type", "application/json"], ["user-agent", "AsyncMatrix/#{Async::Matrix::VERSION}"] ] end |
Instance Attribute Details
#config ⇒ Object (readonly)
512 KiB for error bodies
40 41 42 |
# File 'lib/async/matrix/client.rb', line 40 def config @config end |
Instance Method Details
#api ⇒ Object
Returns a Gateway that provides method-chained access to every Matrix Client-Server API endpoint. Chains are validated against the official OpenAPI path tree and terminated by .get(), .post(), .put(), or .delete().
client.api.account.whoami.get
client.api.createRoom.post(name: "Pub")
client.api.rooms("!room:ex.com").ban.post(user_id: "@bad:ex.com")
client.api.rooms("!room:ex.com")..get(dir: "b", limit: 10)
123 124 125 |
# File 'lib/async/matrix/client.rb', line 123 def api Api::Gateway.new(self) end |
#close ⇒ Object
165 166 167 168 169 170 |
# File 'lib/async/matrix/client.rb', line 165 def close @internet&.close @internet = nil @media_client&.close @media_client = nil end |
#get(path, max_retries: nil) ⇒ Object
153 154 155 |
# File 'lib/async/matrix/client.rb', line 153 def get(path, max_retries: nil) request("GET", path, nil, max_retries: max_retries) end |
#join_room(room_id) ⇒ Object
── Room actions ───────────────────────────────────────────
87 88 89 |
# File 'lib/async/matrix/client.rb', line 87 def join_room(room_id) post("#{CLIENT_PREFIX}/join/#{encode(room_id)}") end |
#leave_room(room_id) ⇒ Object
91 92 93 |
# File 'lib/async/matrix/client.rb', line 91 def leave_room(room_id) post("#{CLIENT_PREFIX}/rooms/#{encode(room_id)}/leave") end |
#media ⇒ Object
Returns a Gateway rooted at /_matrix/media/v3 for media operations. Binary routes (upload/download/thumbnail) are automatically detected by the Chain and dispatched to the MediaClient.
client.media.upload.post(bytes, content_type: "image/png")
client.media.download("example.com", "abc123").get
client.media.thumbnail("example.com", "abc123").get(width: 64, height: 64)
135 136 137 |
# File 'lib/async/matrix/client.rb', line 135 def media Api::Gateway.new(self, prefix: %w[_matrix media v3]) end |
#media_client ⇒ Object
Returns the binary media client used for upload/download operations. Lazily initialized, shares the same config as this client.
141 142 143 |
# File 'lib/async/matrix/client.rb', line 141 def media_client @media_client ||= MediaClient.new(@config) end |
#post(path, body = {}, max_retries: nil) ⇒ Object
161 162 163 |
# File 'lib/async/matrix/client.rb', line 161 def post(path, body = {}, max_retries: nil) request("POST", path, body, max_retries: max_retries) end |
#put(path, body = {}, max_retries: nil) ⇒ Object
157 158 159 |
# File 'lib/async/matrix/client.rb', line 157 def put(path, body = {}, max_retries: nil) request("PUT", path, body, max_retries: max_retries) end |
#send_html(room_id, html, plaintext = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/async/matrix/client.rb', line 70 def send_html(room_id, html, plaintext = nil) content = { msgtype: "m.text", body: plaintext || html.gsub(/<[^>]+>/, ""), format: "org.matrix.custom.html", formatted_body: html } (room_id, "m.room.message", content) end |
#send_message_event(room_id, event_type, content) ⇒ Object
── Low-level HTTP ─────────────────────────────────────────
147 148 149 150 151 |
# File 'lib/async/matrix/client.rb', line 147 def (room_id, event_type, content) txn_id = SecureRandom.uuid path = "#{CLIENT_PREFIX}/rooms/#{encode(room_id)}/send/#{encode(event_type)}/#{txn_id}" put(path, content) end |
#send_notice(room_id, text) ⇒ Object
80 81 82 83 |
# File 'lib/async/matrix/client.rb', line 80 def send_notice(room_id, text) content = {msgtype: "m.notice", body: text} (room_id, "m.room.message", content) end |
#send_text(room_id, text) ⇒ Object
── Messaging ──────────────────────────────────────────────
65 66 67 68 |
# File 'lib/async/matrix/client.rb', line 65 def send_text(room_id, text) content = {msgtype: "m.text", body: text} (room_id, "m.room.message", content) end |
#set_display_name(name, user_id = nil) ⇒ Object
── Profile ────────────────────────────────────────────────
97 98 99 100 101 102 103 |
# File 'lib/async/matrix/client.rb', line 97 def set_display_name(name, user_id = nil) uid = user_id || @config.bot_mxid put( "#{CLIENT_PREFIX}/profile/#{encode(uid)}/displayname", {displayname: name} ) end |
#whoami ⇒ Object
── Verification ───────────────────────────────────────────
107 108 109 |
# File 'lib/async/matrix/client.rb', line 107 def whoami get("#{CLIENT_PREFIX}/account/whoami") end |