Class: Rager::Http::Adapters::AsyncHttp

Inherits:
Abstract
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rager/http/adapters/async_http.rb

Instance Method Summary collapse

Constructor Details

#initializeAsyncHttp

Returns a new instance of AsyncHttp.



13
14
15
16
17
# File 'lib/rager/http/adapters/async_http.rb', line 13

def initialize
  require "async/http"

  @internet = T.let(Async::HTTP::Internet.new, Async::HTTP::Internet)
end

Instance Method Details

#make_request(request) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rager/http/adapters/async_http.rb', line 24

def make_request(request)
  response = @internet.call(
    request.verb.serialize,
    request.url,
    request.headers.to_a,
    request.body
  )

  body = if response.body.nil?
    nil
  elsif (response.headers["Transfer-Encoding"]&.downcase == "chunked") ||
      response.headers["content-type"]&.downcase&.include?("text/event-stream")
    body_enum(response)
  else
    response.body.read
  end

  Response.new(
    status: response.status,
    headers: response.headers.to_h,
    body: body
  )
end