Class: Pangea::Internal::Transport::BaseClient Abstract Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pangea/internal/transport/base_client.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class is abstract.

Direct Known Subclasses

Client

Constant Summary collapse

PLATFORM_HEADERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "user-agent" => "pangea-ruby/#{Pangea::VERSION}"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, headers: {}, initial_retry_delay: 0.0, max_retries: 0, timeout: 0.0) ⇒ BaseClient

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of BaseClient.

Parameters:

  • base_url (String)
  • headers (Hash{String=>String, Integer, Array<String, Integer, nil>, nil}) (defaults to: {})
  • initial_retry_delay (Float) (defaults to: 0.0)
  • max_retries (Integer) (defaults to: 0)
  • timeout (Float) (defaults to: 0.0)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pangea/internal/transport/base_client.rb', line 66

def initialize(
  base_url:,
  headers: {},
  initial_retry_delay: 0.0,
  max_retries: 0,
  timeout: 0.0
)
  @requester = Pangea::Internal::Transport::PooledNetRequester.new

  @base_url = Pangea::Internal::Util.parse_uri(base_url)
  @headers = Pangea::Internal::Util.normalized_headers(
    self.class::PLATFORM_HEADERS,
    {
      "accept" => "application/json",
      "content-type" => "application/json"
    },
    headers
  )
  @initial_retry_delay = initial_retry_delay
  @max_retries = max_retries
  @timeout = timeout
end

Instance Attribute Details

#requesterPangea::Internal::Transport::PooledNetRequester

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



57
58
59
# File 'lib/pangea/internal/transport/base_client.rb', line 57

def requester
  @requester
end

Class Method Details

.reap_connection!(status, stream:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:



25
26
27
28
29
30
31
32
33
# File 'lib/pangea/internal/transport/base_client.rb', line 25

def reap_connection!(status, stream:)
  case status
  in (..199) | (300..499)
    stream&.each { next }
  in Pangea::Errors::APIConnectionError | (500..)
    Pangea::Internal::Util.close_fused!(stream)
  else
  end
end

.validate!(req) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • req (Hash{Symbol=>Object})

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pangea/internal/transport/base_client.rb', line 40

def validate!(req)
  keys = [:method, :path, :query, :headers, :body, :unwrap, :page, :structure, :model, :options]
  case req
  in Hash
    req.each_key do |k|
      unless keys.include?(k)
        raise ArgumentError.new("Request `req` keys must be one of #{keys}, got #{k.inspect}")
      end
    end
  else
    raise ArgumentError.new("Request `req` must be a Hash or RequestOptions, got #{req.inspect}")
  end
end

Instance Method Details

#request(method, path, query: {}, headers: {}, body: nil, unwrap: nil, page: nil, stream: nil, model: Pangea::Internal::Type::Unknown, options: {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute the request specified by ‘req`. This is the method that all resource methods call into.

Parameters:

  • method (Symbol)
  • path (String, Array<String>)
  • query (Hash{String=>Array<String>, String, nil}, nil)
  • headers (Hash{String=>String, Integer, Array<String, Integer, nil>, nil}, nil)
  • body (Object, nil)
  • unwrap (Symbol, nil)
  • page (Class, nil)
  • model (Pangea::Internal::Type::Converter, Class, nil)
  • options (Pangea::RequestOptions, Hash{Symbol=>Object}, nil)

    .

    @option options [HashString, nil, nil] :extra_query

    @option options [Hashnil, nil] :extra_headers

    @option options [Object, nil] :extra_body

    @option options [Integer, nil] :max_retries

    @option options [Float, nil] :timeout

Returns:

  • (Object)

Raises:



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/pangea/internal/transport/base_client.rb', line 317

def request(req)
  self.class.validate!(req)
  model = req.fetch(:model) { Pangea::Internal::Type::Unknown }
  opts = req[:options].to_h
  Pangea::RequestOptions.validate!(opts)
  request = build_request(req.except(:options), opts)
  _url = request.fetch(:url)

  _status, response, stream = send_request(
    request,
    redirect_count: 0,
    retry_count: 0,
    send_retry_header: false
  )

  decoded = Pangea::Internal::Util.decode_content(response, stream: stream)
  case req
  in {structure: Class => pr}
    pr.new(response_data: decoded, model: model)
  in {page: Class => page}
    page.new(client: self, req: req, headers: response, page_data: decoded)
  else
    unwrapped = Pangea::Internal::Util.dig(decoded, req[:unwrap])
    Pangea::Internal::Type::Converter.coerce(model, unwrapped)
  end
end