Class: Vox::HTTP::Client
- Inherits:
-
Object
- Object
- Vox::HTTP::Client
- Includes:
- Routes
- Defined in:
- lib/vox/http/client.rb
Overview
HTTP Client for interacting with the REST portion of the Discord API
Constant Summary collapse
- API_VERSION =
Discord REST API version.
'8'
- API_BASE =
Base URL to base endpoints off of.
"https://discord.com/api/v#{API_VERSION}/"
- DEFAULT_HEADERS =
Headers that form the base for every request.
{ 'User-Agent': "DiscordBot (https://github.com/swarley/vox, #{Vox::VERSION})" }.freeze
Instance Method Summary collapse
-
#initialize(token) {|@conn| ... } ⇒ Client
constructor
A new instance of Client.
-
#request(route, query: nil, data: nil, json: nil, raw: nil, reason: nil) ⇒ Hash?
Run a request.
Methods included from Routes
Constructor Details
#initialize(token) {|@conn| ... } ⇒ Client
Returns a new instance of Client.
46 47 48 49 50 |
# File 'lib/vox/http/client.rb', line 46 def initialize(token) @conn = default_connection @conn.('Bot', token.delete_prefix('Bot ')) yield(@conn) if block_given? end |
Instance Method Details
#request(route, query: nil, data: nil, json: nil, raw: nil, reason: nil) ⇒ Hash?
Run a request
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/vox/http/client.rb', line 60 def request(route, query: nil, data: nil, json: nil, raw: nil, reason: nil) req = @conn.build_request(route.verb) do |r| setup_basic_request(r, route, query: query, reason: reason) if json r.body = MultiJson.dump(json) r.headers['Content-Type'] = 'application/json' elsif data r.body = data end end begin resp = @conn.builder.build_response(@conn, req) handle_response(resp, raw, req..context[:trace]) rescue Error::TooManyRequests retry end end |