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.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/discorb/http.rb', line 174

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:



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/discorb/http.rb', line 149

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))
    data = get_response_data(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:



32
33
34
35
36
37
38
39
40
41
42
43
44
# 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)
    data = get_response_data(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



162
163
164
# File 'lib/discorb/http.rb', line 162

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:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/discorb/http.rb', line 89

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]

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:



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/discorb/http.rb', line 61

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)
    data = get_response_data(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:



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/discorb/http.rb', line 122

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)
    data = get_response_data(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