Class: Discorb::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/discorb/http.rb

Overview

A class to handle http requests. This class is internal use only.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.multipart(payload, files) -> Array[String, String]

A helper method to send multipart/form-data requests.

Parameters:

  • payload (Hash)

    The payload to send.

  • files (Array<Discorb::File>)

    The files to send.

Returns:

  • (Array[String, String])

    The boundary and body.



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]

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Execute a DELETE request.

Parameters:

  • path (String)

    The path to the resource.

  • headers (Hash) (defaults to: nil)

    The headers to send with the request.

  • audit_log_reason (String) (defaults to: nil)

    The audit log reason to send with the request.

  • kwargs (Hash)

    The keyword arguments.

Returns:

  • (Array[Net::HTTPResponse, Hash])

    The response and as JSON.

  • (Array[Net::HTTPResponse, nil])

    The response was 204.

Raises:



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]

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Execute a GET request.

Parameters:

  • path (String)

    The path to the resource.

  • headers (Hash) (defaults to: nil)

    The headers to send with the request.

  • audit_log_reason (String) (defaults to: nil)

    The audit log reason to send with the request.

  • kwargs (Hash)

    The keyword arguments.

Returns:

  • (Array[Net::HTTPResponse, Hash])

    The response and as JSON.

  • (Array[Net::HTTPResponse, nil])

    The response was 204.

Raises:



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]

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Execute a PATCH request.

Parameters:

  • path (String)

    The path to the resource.

  • body (String, Hash) (defaults to: "")

    The body of the request.

  • headers (Hash) (defaults to: nil)

    The headers to send with the request.

  • audit_log_reason (String) (defaults to: nil)

    The audit log reason to send with the request.

  • kwargs (Hash)

    The keyword arguments.

Returns:

  • (Array[Net::HTTPResponse, Hash])

    The response and as JSON.

  • (Array[Net::HTTPResponse, nil])

    The response was 204.

Raises:



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]

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Execute a POST request.

Parameters:

  • path (String)

    The path to the resource.

  • body (String, Hash) (defaults to: "")

    The body of the request.

  • headers (Hash) (defaults to: nil)

    The headers to send with the request.

  • audit_log_reason (String) (defaults to: nil)

    The audit log reason to send with the request.

  • kwargs (Hash)

    The keyword arguments.

Returns:

  • (Array[Net::HTTPResponse, Hash])

    The response and as JSON.

  • (Array[Net::HTTPResponse, nil])

    The response was 204.

Raises:



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]

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Execute a PUT request.

Parameters:

  • path (String)

    The path to the resource.

  • body (String, Hash) (defaults to: "")

    The body of the request.

  • headers (Hash) (defaults to: nil)

    The headers to send with the request.

  • audit_log_reason (String) (defaults to: nil)

    The audit log reason to send with the request.

  • kwargs (Hash)

    The keyword arguments.

Returns:

  • (Array[Net::HTTPResponse, Hash])

    The response and as JSON.

  • (Array[Net::HTTPResponse, nil])

    The response was 204.

Raises:



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