Class: DiscordRDA::RestProxy
- Inherits:
-
Object
- Object
- DiscordRDA::RestProxy
- Defined in:
- lib/discord_rda/connection/rest_proxy.rb
Overview
REST Proxy for horizontal scaling. Allows multiple bot processes to share a single REST connection pool.
Defined Under Namespace
Classes: APIError, RateLimitedError
Instance Attribute Summary collapse
-
#authorization ⇒ String
readonly
Proxy authorization token.
-
#base_url ⇒ String
readonly
Proxy base URL.
-
#connections ⇒ Hash
readonly
Active connections.
-
#logger ⇒ Logger
readonly
Logger instance.
Instance Method Summary collapse
-
#forward(method, route, options = {}) ⇒ Hash
Forward a request to the proxy.
-
#health_check ⇒ Hash
Get proxy health/status.
-
#initialize(base_url:, authorization:, logger: nil) ⇒ RestProxy
constructor
Initialize REST proxy client.
-
#register_bot(bot_id, token) ⇒ Hash
Register a bot instance with the proxy.
-
#start ⇒ void
Start the proxy client.
-
#stop ⇒ void
Stop the proxy client.
-
#update_token(old_token, new_token) ⇒ void
Update bearer token (for OAuth2 bots).
Constructor Details
#initialize(base_url:, authorization:, logger: nil) ⇒ RestProxy
Initialize REST proxy client
24 25 26 27 28 29 30 31 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 24 def initialize(base_url:, authorization:, logger: nil) @base_url = base_url = @logger = logger @connections = {} @mutex = Mutex.new @internet = nil end |
Instance Attribute Details
#authorization ⇒ String (readonly)
Returns Proxy authorization token.
12 13 14 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 12 def end |
#base_url ⇒ String (readonly)
Returns Proxy base URL.
9 10 11 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 9 def base_url @base_url end |
#connections ⇒ Hash (readonly)
Returns Active connections.
18 19 20 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 18 def connections @connections end |
#logger ⇒ Logger (readonly)
Returns Logger instance.
15 16 17 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 15 def logger @logger end |
Instance Method Details
#forward(method, route, options = {}) ⇒ Hash
Forward a request to the proxy
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 51 def forward(method, route, = {}) url = "#{@base_url}#{route}" headers = { 'Authorization' => , 'Content-Type' => 'application/json', 'X-Proxy-Method' => method.to_s.upcase }.merge([:headers] || {}) body = [:body] ? Oj.dump([:body]) : nil response = case method when :get @internet.get(url, headers) when :post @internet.post(url, headers, body) when :put @internet.put(url, headers, body) when :patch @internet.patch(url, headers, body) when :delete @internet.delete(url, headers) end handle_response(response) end |
#health_check ⇒ Hash
Get proxy health/status
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 80 def health_check begin response = @internet.get("#{@base_url}/health", {}) { healthy: response.status == 200, status: response.status, timestamp: Time.now.utc } rescue => e { healthy: false, error: e., timestamp: Time.now.utc } end end |
#register_bot(bot_id, token) ⇒ Hash
Register a bot instance with the proxy
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 101 def register_bot(bot_id, token) response = @internet.post( "#{@base_url}/register", { 'Authorization' => , 'Content-Type' => 'application/json' }, Oj.dump({ bot_id: bot_id, token: token }) ) handle_response(response) end |
#start ⇒ void
This method returns an undefined value.
Start the proxy client
35 36 37 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 35 def start @internet = Async::HTTP::Internet.new end |
#stop ⇒ void
This method returns an undefined value.
Stop the proxy client
41 42 43 44 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 41 def stop @internet&.close @internet = nil end |
#update_token(old_token, new_token) ⇒ void
This method returns an undefined value.
Update bearer token (for OAuth2 bots)
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/discord_rda/connection/rest_proxy.rb', line 118 def update_token(old_token, new_token) @internet.post( "#{@base_url}/update-token", { 'Authorization' => , 'Content-Type' => 'application/json' }, Oj.dump({ old_token: old_token, new_token: new_token }) ) end |