Typhoeus

repository·master·Indexed 24 days ago

https://github.com/typhoeus/typhoeus

A high-performance Ruby HTTP client library capable of running multiple requests in parallel using the Typhoeus::Hydra feature. It provides a core interface consisting of Request, Response, and Hydra classes, supporting features such as request memoization, built-in caching (with Dalli, Rails, and Redis adapters), request stubbing for testing, and streaming for large response bodies. It also includes a Faraday adapter and Rack middleware for parameter decoding.

Tokens
5.7K
Snippets
27
Records
58
Agent score
87%

What's inside Typhoeus

  1. Core Typhoeus Interface: Request, Response, and Hydra

    master

    Typhoeus is built around three primary classes:

    • Typhoeus::Request: Represents an HTTP request object.
    • Typhoeus::Response: Represents an HTTP response object.
    • Typhoeus::Hydra: Manages making parallel HTTP connections.

    You can instantiate a request with a URL and an optional hash of settings.

    request = Typhoeus::Request.new(
      "www.example.com",
      method: :post,
      body: "this is a request body",
      params: { field1: "a field" },
      headers: { Accept: "text/html" }
    )
  2. Execute Parallel Requests with Hydra

    master

    Use Typhoeus::Hydra to manage multiple requests in parallel. Queue requests using hydra.queue(request) and then call hydra.run (which is a blocking call). You can also queue new requests inside an on_complete callback to create dependency chains.

    hydra = Typhoeus::Hydra.hydra
    
    first_request = Typhoeus::Request.new("http://example.com/posts/1")
    first_request.on_complete do |response|
      third_url = response.body
      third_request = Typhoeus::Request.new(third_url)
      hydra.queue third_request
    end
    
    second_request = Typhoeus::Request.new("http://example.com/posts/2")
    
    hydra.queue first_request
    hydra.queue second_request
    hydra.run
  3. Stream Large Response Bodies

    master

    For large responses, use the on_body callback to process data in chunks. When on_body is used, Typhoeus will not store the complete response in memory. To stop the stream early and prevent memory leaks, return the :abort symbol from the on_body block.

    downloaded_file = File.open 'huge.iso', 'wb'
    request = Typhoeus::Request.new("www.example.com/huge.iso")
    
    request.on_headers do |response|
      raise "Request failed" if response.code != 200
    end
    
    request.on_body do |chunk|
      downloaded_file.write(chunk)
      # To interrupt the stream halfway:
      # :abort if buffer.size > 1024 * 1024
    end
    
    request.on_complete do |response|
      downloaded_file.close
      # Note: response.body will be empty when streaming
    end
    
    request.run
  4. Handle HTTP Errors and Callbacks

    master

    Use on_complete to define a callback that executes after a request finishes. This is the recommended way to handle success, timeouts, and connection errors. Note that callbacks must be defined before calling .run.

    request = Typhoeus::Request.new("www.example.com", followlocation: true)
    
    request.on_complete do |response|
      if response.success?
        # Success logic
      elsif response.timed_out?
        # Timeout logic
      elsif response.code == 0
        # Connection/Network error logic
        log(response.return_message)
      else
        # Non-successful HTTP response (e.g. 404, 500)
        log("HTTP request failed: " + response.code.to_s)
      end
    end
    
    request.run
  5. Distinguish between :params and :body for requests

    master
    When making POST or PUT requests in version 0.5+, ensure you are using the correct key for your data. The :params key is used for URL parameters, while the :body key is used for the request body. Previously, :params for POST requests was merged into the body.
  6. Migrate option names from 0.5

    master
    In version 0.5, several option names were renamed. If you encounter an Ethon::Errors::InvalidOption error, check the error message for the suggested replacement. For example, follow_location was renamed to followlocation.
  7. Enable Request Memoization

    master

    When enabled, Hydra will memoize requests within a single run call. If the same request is queued multiple times, only one actual network request is issued, but on_complete handlers for all queued instances will still be executed.

    Typhoeus::Config.memoize = true
    
    hydra = Typhoeus::Hydra.new(max_concurrency: 1)
    2.times do
      hydra.queue Typhoeus::Request.new("www.example.com")
    end
    hydra.run
  8. Configure Caching

    master
    Typhoeus supports built-in caching. You can set a global cache via Typhoeus::Config.cache. Supported adapters include Dalli, Rails, and Redis. You can also specify a per-request cache or disable caching for a specific request by setting cache: false.