Method: MatrixSdk::Api#initialize

Defined in:
lib/matrix_sdk/api.rb

#initialize(homeserver, **params) ⇒ Api

Note:

Using threadsafe :multithread currently doesn’t support connection re-use

Returns a new instance of Api.

Parameters:

  • homeserver (String, URI)

    The URL to the Matrix homeserver, without the /_matrix/ part

  • params (Hash)

    Additional parameters on creation

Options Hash (**params):

  • :protocols (Symbol[])

    The protocols to include (:AS, :CS, :IS, :SS), defaults to :CS

  • :address (String)

    The connection address to the homeserver, if different to the HS URL

  • :port (Integer)

    The connection port to the homeserver, if different to the HS URL

  • :access_token (String)

    The access token to use for the connection

  • :device_id (String)

    The ID of the logged in decide to use

  • :autoretry (Boolean) — default: true

    Should requests automatically be retried in case of rate limits

  • :validate_certificate (Boolean) — default: false

    Should the connection require valid SSL certificates

  • :transaction_id (Integer) — default: 0

    The starting ID for transactions

  • :backoff_time (Numeric) — default: 5000

    The request backoff time in milliseconds

  • :open_timeout (Numeric) — default: 60

    The timeout in seconds to wait for a TCP session to open

  • :read_timeout (Numeric) — default: 240

    The timeout in seconds for reading responses

  • :global_headers (Hash)

    Additional headers to set for all requests

  • :skip_login (Boolean)

    Should the API skip logging in if the HS URL contains user information

  • :synapse (Boolean) — default: true

    Is the API connecting to a Synapse instance

  • :threadsafe (Boolean, :multithread) — default: :multithread

    Should the connection be threadsafe/mutexed - or safe for simultaneous multi-thread usage. Will default to :multithread - a.k.a. per-thread HTTP connections and requests

Raises:

  • (ArgumentError)


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
80
# File 'lib/matrix_sdk/api.rb', line 46

def initialize(homeserver, **params)
  @homeserver = homeserver
  raise ArgumentError, 'Homeserver URL must be String or URI' unless @homeserver.is_a?(String) || @homeserver.is_a?(URI)

  @homeserver = URI.parse("#{'https://' unless @homeserver.start_with? 'http'}#{@homeserver}") unless @homeserver.is_a? URI
  @homeserver.path.gsub!(/\/?_matrix\/?/, '') if @homeserver.path =~ /_matrix\/?$/
  raise ArgumentError, 'Please use the base URL for your HS (without /_matrix/)' if @homeserver.path.include? '/_matrix/'

  @proxy_uri = params.fetch(:proxy_uri, nil)
  @connection_address = params.fetch(:address, nil)
  @connection_port = params.fetch(:port, nil)
  @access_token = params.fetch(:access_token, nil)
  @device_id = params.fetch(:device_id, nil)
  @autoretry = params.fetch(:autoretry, true)
  @validate_certificate = params.fetch(:validate_certificate, false)
  @transaction_id = params.fetch(:transaction_id, 0)
  @backoff_time = params.fetch(:backoff_time, 5000)
  @open_timeout = params.fetch(:open_timeout, nil)
  @read_timeout = params.fetch(:read_timeout, nil)
  @well_known = params.fetch(:well_known, {})
  @global_headers = DEFAULT_HEADERS.dup
  @global_headers.merge!(params.fetch(:global_headers)) if params.key? :global_headers
  @synapse = params.fetch(:synapse, true)
  @http = nil
  @inflight = []

  self.threadsafe = params.fetch(:threadsafe, :multithread)

  ([params.fetch(:protocols, [:CS])].flatten - protocols).each do |proto|
    self.class.include MatrixSdk::Protocols.const_get(proto)
  end

  (user: @homeserver.user, password: @homeserver.password) if @homeserver.user && @homeserver.password && !@access_token && !params[:skip_login] && protocol?(:CS)
  @homeserver.userinfo = '' unless params[:skip_login]
end