Class: ScalarRubyTest::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/amritk-scalar-test/runtime.rb

Direct Known Subclasses

Client

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, max_retries: 2, timeout: 60.0, initial_retry_delay: 0.5, max_retry_delay: 8.0, idempotency_header: "Idempotency-Key", logger: nil, default_headers: {}, default_query: {}) ⇒ APIClient

Returns a new instance of APIClient.



482
483
484
485
486
487
488
489
490
491
492
# File 'lib/amritk-scalar-test/runtime.rb', line 482

def initialize(base_url:, max_retries: 2, timeout: 60.0, initial_retry_delay: 0.5, max_retry_delay: 8.0, idempotency_header: "Idempotency-Key", logger: nil, default_headers: {}, default_query: {})
  @base_url = base_url.to_s.delete_suffix("/")
  @max_retries = max_retries
  @timeout = timeout
  @initial_retry_delay = initial_retry_delay
  @max_retry_delay = max_retry_delay
  @idempotency_header = idempotency_header
  @logger = logger
  @default_headers = default_headers
  @default_query = default_query
end

Instance Method Details

#auth_headersObject



600
601
602
# File 'lib/amritk-scalar-test/runtime.rb', line 600

def auth_headers
  {}
end

#auth_queryObject



604
605
606
# File 'lib/amritk-scalar-test/runtime.rb', line 604

def auth_query
  {}
end

#request(method:, path:, path_params: {}, path_settings: {}, query: {}, query_settings: {}, cookies: {}, cookie_settings: {}, body: nil, body_type: :json, content_type: nil, headers: nil, request_options: nil) ⇒ Object



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/amritk-scalar-test/runtime.rb', line 494

def request(
  method:, path:,
  path_params: {}, path_settings: {},
  query: {}, query_settings: {},
  cookies: {}, cookie_settings: {},
  body: nil, body_type: :json, content_type: nil,
  headers: nil, request_options: nil
)
  options = normalize_options(request_options)
  merged_query = @default_query.merge(auth_query).merge(option_hash(options, :query, :extra_query)).merge(query || {})
  # Link-header pagination passes the exact next-page URL via `url:`; use it
  # directly instead of rebuilding base_url + path + query.
  exact_url = option_value(options, :url)
  uri = exact_url ? URI.parse(exact_url.to_s) : build_uri(
    option_value(options, :base_url) || @base_url,
    path,
    path_params,
    path_settings,
    merged_query,
    query_settings
  )
  cookie_header = build_cookie_header(cookies || {}, cookie_settings || {})
  headers = (headers || {}).merge("Cookie" => [headers && headers["Cookie"], cookie_header].compact.reject(&:empty?).join("; ")) unless cookie_header.empty?
  request = build_request(method, uri, body, body_type, content_type, headers || {}, options)
  response = execute_with_retry(uri, request, option_value(options, :max_retries) || @max_retries, option_value(options, :timeout) || @timeout)
  parsed = parse_response_body(response)
  raise APIError.from_response(response, parsed) unless response.is_a?(Net::HTTPSuccess)
  parsed
end

#request_raw(method:, path:, path_params: {}, path_settings: {}, query: {}, query_settings: {}, cookies: {}, cookie_settings: {}, body: nil, body_type: :json, content_type: nil, headers: nil, request_options: nil) ⇒ Object



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/amritk-scalar-test/runtime.rb', line 524

def request_raw(
  method:, path:,
  path_params: {}, path_settings: {},
  query: {}, query_settings: {},
  cookies: {}, cookie_settings: {},
  body: nil, body_type: :json, content_type: nil,
  headers: nil, request_options: nil
)
  options = normalize_options(request_options)
  merged_query = @default_query.merge(auth_query).merge(option_hash(options, :query, :extra_query)).merge(query || {})
  exact_url = option_value(options, :url)
  uri = exact_url ? URI.parse(exact_url.to_s) : build_uri(
    option_value(options, :base_url) || @base_url,
    path,
    path_params,
    path_settings,
    merged_query,
    query_settings
  )
  cookie_header = build_cookie_header(cookies || {}, cookie_settings || {})
  headers = (headers || {}).merge("Cookie" => [headers && headers["Cookie"], cookie_header].compact.reject(&:empty?).join("; ")) unless cookie_header.empty?
  request = build_request(method, uri, body, body_type, content_type, headers || {}, options)
  response = execute_with_retry(uri, request, option_value(options, :max_retries) || @max_retries, option_value(options, :timeout) || @timeout)
  parsed = parse_response_body(response)
  raise APIError.from_response(response, parsed) unless response.is_a?(Net::HTTPSuccess)
  Runtime::RawResponse.new(data: parsed, status: response.code.to_i, headers: response.to_hash, body: response.body)
end

#request_stream(method:, path:, path_params: {}, path_settings: {}, query: {}, query_settings: {}, cookies: {}, cookie_settings: {}, body: nil, body_type: :json, content_type: nil, headers: nil, stream_format: :sse, descriptor: nil, handlers: [], request_options: nil) ⇒ Object



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/amritk-scalar-test/runtime.rb', line 552

def request_stream(
  method:, path:,
  path_params: {}, path_settings: {},
  query: {}, query_settings: {},
  cookies: {}, cookie_settings: {},
  body: nil, body_type: :json, content_type: nil,
  headers: nil, stream_format: :sse, descriptor: nil, handlers: [], request_options: nil
)
  options = normalize_options(request_options)
  merged_query = @default_query.merge(auth_query).merge(option_hash(options, :query, :extra_query)).merge(query || {})
  uri = build_uri(
    option_value(options, :base_url) || @base_url,
    path,
    path_params,
    path_settings,
    merged_query,
    query_settings
  )
  cookie_header = build_cookie_header(cookies || {}, cookie_settings || {})
  headers = (headers || {}).merge("Cookie" => [headers && headers["Cookie"], cookie_header].compact.reject(&:empty?).join("; ")) unless cookie_header.empty?
  request = build_request(method, uri, body, body_type, content_type, headers || {}, options)
  source = proc { |&chunk_handler| execute_stream_with_retry(uri, request, option_value(options, :max_retries) || @max_retries, option_value(options, :timeout) || @timeout, &chunk_handler) }
  Runtime::Stream.new(source: source, descriptor: descriptor, format: stream_format, handlers: handlers)
end

#websocket_request(method: "GET", path:, path_params: {}, path_settings: {}, query: {}, query_settings: {}, cookies: {}, cookie_settings: {}, body: nil, body_type: :json, content_type: nil, headers: nil, send_descriptor: nil, receive_descriptor: nil, event_name_separator: nil, request_options: nil) ⇒ Object



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/amritk-scalar-test/runtime.rb', line 577

def websocket_request(
  method: "GET", path:,
  path_params: {}, path_settings: {},
  query: {}, query_settings: {},
  cookies: {}, cookie_settings: {},
  body: nil, body_type: :json, content_type: nil,
  headers: nil, send_descriptor: nil, receive_descriptor: nil, event_name_separator: nil, request_options: nil
)
  options = normalize_options(request_options)
  merged_query = @default_query.merge(auth_query).merge(option_hash(options, :query, :extra_query)).merge(query || {})
  uri = build_uri(
    option_value(options, :base_url) || @base_url,
    path,
    path_params,
    path_settings,
    merged_query,
    query_settings
  )
  uri.scheme = uri.scheme == "https" ? "wss" : "ws"
  request_headers = sanitize_headers(@default_headers.merge(auth_headers_for(uri, options)).merge(headers || {}))
  Runtime::WebSocketRequest.new(url: uri.to_s, headers: request_headers, send_descriptor: send_descriptor, receive_descriptor: receive_descriptor, event_name_separator: event_name_separator)
end