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.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/discorb/http.rb', line 194 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.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/discorb/http.rb', line 164 def delete(path, headers: nil, audit_log_reason: nil, **kwargs) Async do |task| resp = http.delete(get_path(path), get_headers(headers, "", audit_log_reason)) rd = resp.body data = if rd.nil? || rd.empty? nil else JSON.parse(rd, symbolize_names: true) end 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/discorb/http.rb', line 32 def get(path, headers: nil, audit_log_reason: nil, **kwargs) Async do |task| resp = http.get(get_path(path), get_headers(headers, "", audit_log_reason), **kwargs) rd = resp.body data = if rd.nil? || rd.empty? nil else JSON.parse(rd, symbolize_names: true) end 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
182 183 184 |
# File 'lib/discorb/http.rb', line 182 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.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/discorb/http.rb', line 99 def patch(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) Async do |task| resp = http.patch(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs) rd = resp.body data = if rd.nil? || rd.empty? nil else JSON.parse(rd, symbolize_names: true) end 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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/discorb/http.rb', line 66 def post(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) Async do |task| resp = http.post(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs) rd = resp.body data = if rd.nil? || rd.empty? nil else JSON.parse(rd, symbolize_names: true) end 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.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/discorb/http.rb', line 132 def put(path, body = "", headers: nil, audit_log_reason: nil, **kwargs) Async do |task| resp = http.put(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs) rd = resp.body data = if rd.nil? || rd.empty? nil else JSON.parse(rd, symbolize_names: true) end 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 |