Class: HttpMimic::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/http_mimic/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url, options = {}, config = nil) ⇒ Request

Returns a new instance of Request.



9
10
11
12
13
14
# File 'lib/http_mimic/request.rb', line 9

def initialize(method, url, options = {}, config = nil)
  @method  = method
  @url     = url
  @options = options.dup
  @config  = config || HttpMimic.configuration
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/http_mimic/request.rb', line 7

def config
  @config
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/http_mimic/request.rb', line 7

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/http_mimic/request.rb', line 7

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/http_mimic/request.rb', line 7

def url
  @url
end

Instance Method Details

#performObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/http_mimic/request.rb', line 16

def perform
  mode = (options[:mode] || config.mode || :auto).to_sym
  auto_fallback = options.fetch(:auto_fallback, config.auto_fallback)
  retry_statuses = options[:retry_statuses] || config.retry_statuses || [403, 429, 503]

  attempts = []
  profiles_to_try = determine_profiles(mode, auto_fallback)

  response = nil
  final_status = nil
  final_stderr = nil
  final_command = nil

  profiles_to_try.each_with_index do |profile, index|
    current_opts = options.merge(profile: profile)

    builder = CommandBuilder.new(method, url, current_opts, config)
    binary, args, stdin_data, final_url = builder.build
    full_command = [binary] + args

    log_debug("Executing HttpMimic command (attempt #{index + 1}, profile: #{profile}): #{full_command.join(' ')}")
    log_debug("Stdin data: #{stdin_data}") if stdin_data

    stdout, stderr, status = execute_open3(full_command, stdin_data)

    log_debug("Curl exit status: #{status.exitstatus}")
    log_debug("Curl stderr: #{stderr}") unless stderr.empty?

    response = ResponseParser.new(
      stdout,
      exit_status: status,
      stderr: stderr,
      command: full_command,
      request_url: final_url
    ).parse

    response.mode_used = profile
    response.fallback_triggered = (index > 0)

    attempts << {
      attempt: index + 1,
      profile: profile,
      command: full_command,
      code: response.code,
      exit_code: status.exitstatus,
      success: response.success?
    }
    response.attempts = attempts

    final_status = status
    final_stderr = stderr
    final_command = full_command

    is_blocked = (status.exitstatus != 0) || retry_statuses.include?(response.code)
    if !is_blocked || (index == profiles_to_try.size - 1)
      break
    end

    log_debug("[HttpMimic] Attempt #{index + 1} with #{profile} resulted in status #{response.code}. Triggering smart fallback to next profile...")
  end

  handle_errors(final_status, final_stderr, final_command, response)
  response
end