Class: Discorb::Internet

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

Overview

A class to handle internet 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.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/discorb/internet.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]

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:



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/discorb/internet.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]

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
45
46
47
48
49
# File 'lib/discorb/internet.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/internet.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]

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:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/discorb/internet.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]

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:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/discorb/internet.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]

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:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/discorb/internet.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