Class: Discorb::HTTP
- Inherits:
-
Object
- Object
- Discorb::HTTP
- Defined in:
- lib/discorb/http.rb
Overview
A class to handle http requests. This class is internal use only.
Class Method Summary collapse
-
.multipart(payload, files) -> Array[String, String]
A helper method to send multipart/form-data requests.
Instance Method Summary collapse
-
#delete(path, headers: nil, audit_log_reason: nil, **kwargs) -> Array[Net::HTTPResponse, Hash], Array[Net::HTTPResponse, nil]
Execute a DELETE request.
-
#get(path, headers: nil, audit_log_reason: nil, **kwargs) -> Array[Net::HTTPResponse, Hash], Array[Net::HTTPResponse, nil]
Execute a GET request.
- #inspect -> Object
-
#patch(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) -> Array[Net::HTTPResponse, Hash], Array[Net::HTTPResponse, nil]
Execute a PATCH request.
-
#post(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) -> Array[Net::HTTPResponse, Hash], Array[Net::HTTPResponse, nil]
Execute a POST request.
-
#put(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) -> Array[Net::HTTPResponse, Hash], Array[Net::HTTPResponse, nil]
Execute a PUT request.
Class Method Details
.multipart(payload, files) -> Array[String, String]
A helper method to send multipart/form-data requests.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/discorb/http.rb', line 180 def self.multipart(payload, files) boundary = "DiscorbBySevenC7CMultipartFormData#{Time.now.to_f}" str_payloads = [<<~HTTP] Content-Disposition: form-data; name="payload_json" Content-Type: application/json #{payload.to_json} HTTP files.each do |single_file| str_payloads << <<~HTTP Content-Disposition: form-data; name="file"; filename="#{single_file.filename}" Content-Type: #{single_file.content_type} #{single_file.io.read} HTTP end [boundary, "--#{boundary}\n#{str_payloads.join("\n--#{boundary}\n")}\n--#{boundary}--"] end |
Instance Method Details
#delete(path, headers: nil, audit_log_reason: nil, **kwargs) -> Array[Net::HTTPResponse, Hash], Array[Net::HTTPResponse, nil]
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
Execute a DELETE request.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/discorb/http.rb', line 153 def delete(path, headers: nil, audit_log_reason: nil, **kwargs) Async do |task| @ratelimit_handler.wait("DELETE", path) resp = http.delete(get_path(path), get_headers(headers, "", audit_log_reason)) data = get_response_data(resp) @ratelimit_handler.save("DELETE", path, resp) test_error(if resp.code == "429" task.sleep(data[:retry_after]) delete(path, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait else [resp, data] end) end end |
#get(path, headers: nil, audit_log_reason: nil, **kwargs) -> Array[Net::HTTPResponse, Hash], Array[Net::HTTPResponse, nil]
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
Execute a GET request.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/discorb/http.rb', line 33 def get(path, headers: nil, audit_log_reason: nil, **kwargs) Async do |task| @ratelimit_handler.wait("GET", path) resp = http.get(get_path(path), get_headers(headers, "", audit_log_reason), **kwargs) data = get_response_data(resp) @ratelimit_handler.save("GET", path, resp) test_error(if resp.code == "429" @client.log.warn "Ratelimit exceeded for #{path}, trying again in #{data[:retry_after]} seconds." task.sleep(data[:retry_after]) get(path, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait else [resp, data] end) end end |
#inspect -> Object
168 169 170 |
# File 'lib/discorb/http.rb', line 168 def inspect "#<#{self.class} client=#{@client}>" end |
#patch(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) -> Array[Net::HTTPResponse, Hash], Array[Net::HTTPResponse, nil]
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
Execute a PATCH request.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/discorb/http.rb', line 94 def patch(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) Async do |task| @ratelimit_handler.wait("PATCH", path) resp = http.patch(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs) data = get_response_data(resp) @ratelimit_handler.save("PATCH", path, resp) test_error(if resp.code == "429" task.sleep(data[:retry_after]) patch(path, body, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait else [resp, data] end) end end |
#post(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) -> Array[Net::HTTPResponse, Hash], Array[Net::HTTPResponse, nil]
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
Execute a POST request.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/discorb/http.rb', line 64 def post(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) Async do |task| @ratelimit_handler.wait("POST", path) resp = http.post(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs) data = get_response_data(resp) @ratelimit_handler.save("POST", path, resp) test_error(if resp.code == "429" task.sleep(data[:retry_after]) post(path, body, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait else [resp, data] end) end end |
#put(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) -> Array[Net::HTTPResponse, Hash], Array[Net::HTTPResponse, nil]
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
Execute a PUT request.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/discorb/http.rb', line 124 def put(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) Async do |task| @ratelimit_handler.wait("PUT", path) resp = http.put(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs) data = get_response_data(resp) @ratelimit_handler.save("PUT", path, resp) test_error(if resp.code == "429" task.sleep(data[:retry_after]) put(path, body, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait else [resp, data] end) end end |