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
# 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]"
      return -1
    end

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

#closeObject



168
169
170
171
172
173
174
175
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 168

def close

  if @event_source != nil

    @event_source.close
    @event_source = nil
  end
end

#get_flag(identifier) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 90

def get_flag(identifier)

  @api.get_feature_config_by_identifier(

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

#get_flagsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 57

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



100
101
102
103
104
105
106
107
108
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 100

def get_segment(identifier)

  @api.get_segment_by_identifier(

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

#get_segmentsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 74

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 110

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



137
138
139
140
141
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
# File 'lib/ff/ruby/server/sdk/connector/harness_connector.rb', line 137

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