Class: HarnessConnector

Inherits:
Connector show all
Defined in:
lib/ff/ruby/server/sdk/connector/harness_connector.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, config, on_unauthorized) ⇒ HarnessConnector

Returns a new instance of HarnessConnector.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 10

def initialize(api_key, config, on_unauthorized)

  @api_key = api_key
  @config = config
  @on_unauthorized = on_unauthorized
  @user_agent = "RubySDK " + Ff::Ruby::Server::Sdk::VERSION
  @sdk_info = "Ruby #{Ff::Ruby::Server::Sdk::VERSION} Server"

  @api = OpenapiClient::ClientApi.new(make_api_client)
  @metrics_api = OpenapiClient::MetricsApi.new(make_metrics_api_client)

  @config.logger.debug "Api: " + @api.to_s
  @config.logger.debug "Metrics api: " + @metrics_api.to_s
end

Instance Method Details

#authenticateObject



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
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 25

def authenticate

  begin

    options = {
      :'authentication_request' => {
        :'apiKey' => @api_key
      }
    }

    response = @api.authenticate(opts = options)
    @token = response.auth_token

    @config.logger.debug "Token has been obtained"
    process_token
    return 200

  rescue OpenapiClient::ApiError => e

    if e.message.include? "the server returns an error"
      # NOTE openapi-generator 5.2.1 has a bug where exceptions don't contain any useful information and we can't
      # determine if a timeout has occurred. This is fixed in 6.3.0 but requires Ruby version to be increased to 2.7
      # https://github.com/OpenAPITools/openapi-generator/releases/tag/v6.3.0
      @config.logger.warn "OpenapiClient::ApiError [\n\n#{e}\n]"

      if e.code
        return e.code
      end

      return -1
    end

    log_error("auth", e)
    return e.code
  end
end

#closeObject



173
174
175
176
177
178
179
180
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 173

def close

  if @event_source != nil

    @event_source.close
    @event_source = nil
  end
end

#get_flag(identifier) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 95

def get_flag(identifier)

  @api.get_feature_config_by_identifier(

    identifier = identifier,
    environment_uuid = @environment,
    opts = get_query_params
  )
end

#get_flagsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 62

def get_flags

  begin

    return @api.get_feature_config(

      environment_uuid = @environment,
      opts = get_query_params
    )

  rescue OpenapiClient::ApiError => e

    log_error("get_feature_config", e)
    return nil
  end
end

#get_segment(identifier) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 105

def get_segment(identifier)

  @api.get_segment_by_identifier(

    identifier = identifier,
    environment_uuid = @environment,
    opts = get_segment_query_params
  )
end

#get_segmentsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 79

def get_segments

  begin
    return @api.get_all_segments(

      environment_uuid = @environment,
      opts = get_segment_query_params
    )

  rescue OpenapiClient::ApiError => e

    log_error("get_all_segments", e)
    return nil
  end
end

#post_metrics(metrics) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 115

def post_metrics(metrics)

  begin

    options = {

      :'metrics' => metrics,
      :'query_params' => {

        :'cluster' => @cluster
      }
    }

    @metrics_api.post_metrics(

      environment = @environment,
      opts = options
    )

    @config.logger.debug "Successfully sent analytics data to the server"

  rescue OpenapiClient::ApiError => e
    log_error("post_metrics", e)
    SdkCodes.warn_post_metrics_failed @config.logger, e.message
  end
end

#stream(updater) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 142

def stream(updater)

  if @event_source != nil

    @event_source.close
    @event_source = nil
  end

  url = @config.config_url + "/stream?cluster=" + @cluster.to_s

  headers = {

    "Authorization" => "Bearer " + @token,
    "API-Key" => @api_key,
    "User-Agent" => @user_agent,
    "Harness-SDK-Info" =>  @sdk_info,
    "Harness-AccountID" => @account_id,
    "Harness-EnvironmentID" => @environment_id
  }.compact

  @event_source = Events.new(

    url,
    headers,
    updater,
    @config
  )

  @event_source
end