Module: Growatt::Connection

Included in:
API
Defined in:
lib/growatt/connection.rb

Overview

Handles HTTP connection setup and authentication management.

This module establishes a Faraday connection to interact with the Growatt API, ensuring proper authentication via cookies and setting up required headers.

Instance Method Summary collapse

Instance Method Details

#connectionFaraday::Connection

Establishes a Faraday connection with appropriate middleware and settings.

Returns:

  • (Faraday::Connection)

    The configured Faraday connection instance.

Raises:



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
# File 'lib/growatt/connection.rb', line 16

def connection
  raise ConfigurationError, "Option for endpoint is not defined" unless endpoint

  options = setup_options
  @connection ||= Faraday::Connection.new(options) do |connection|
    # Enable cookie-based authentication
    connection.use :cookie_jar

    # Handle HTTP response errors
    connection.use Faraday::Response::RaiseError

    # Set up default Faraday adapter
    connection.adapter Faraday.default_adapter

    # Configure authentication and request headers
    setup_authorization(connection)
    setup_headers(connection)

    # Parse JSON responses automatically
    connection.response :json, content_type: /\bjson$/

    # Ensure requests are URL-encoded
    connection.use Faraday::Request::UrlEncoded

    # Configure logging if a logger is present
    setup_logger_filtering(connection, logger) if logger
  end
end