Class: Optimizely::OdpSegmentApiManager

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/odp/odp_segment_api_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, proxy_config: nil, timeout: nil) ⇒ OdpSegmentApiManager

Interface that handles fetching audience segments.



26
27
28
29
30
# File 'lib/optimizely/odp/odp_segment_api_manager.rb', line 26

def initialize(logger: nil, proxy_config: nil, timeout: nil)
  @logger = logger || NoOpLogger.new
  @proxy_config = proxy_config
  @timeout = timeout || Optimizely::Helpers::Constants::ODP_GRAPHQL_API_CONFIG[:REQUEST_TIMEOUT]
end

Instance Method Details

#fetch_segments(api_key, api_host, user_key, user_value, segments_to_check) ⇒ Object

Fetch segments from the ODP GraphQL API.

Parameters:

  • api_key
    • public api key

  • api_host
    • domain url of the host

  • user_key
    • vuid or fs_user_id (client device id or fullstack id)

  • user_value
    • value of user_key

  • segments_to_check
    • array of segments to check



40
41
42
43
44
45
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/optimizely/odp/odp_segment_api_manager.rb', line 40

def fetch_segments(api_key, api_host, user_key, user_value, segments_to_check)
  url = "#{api_host}/v3/graphql"

  headers = {'Content-Type' => 'application/json', 'x-api-key' => api_key.to_s}

  payload = {
    query: 'query($userId: String, $audiences: [String]) {' \
           "customer(#{user_key}: $userId) " \
           '{audiences(subset: $audiences) {edges {node {name state}}}}}',
    variables: {
      userId: user_value.to_s,
      audiences: segments_to_check || []
    }
  }.to_json

  begin
    response = Helpers::HttpUtils.make_request(
      url, :post, payload, headers, @timeout, @proxy_config
    )
  rescue SocketError, Timeout::Error, Net::ProtocolError, Errno::ECONNRESET => e
    @logger.log(Logger::DEBUG, "GraphQL download failed: #{e}")
    log_segments_failure('network error')
    return nil
  rescue Errno::EINVAL, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, HTTPUriError => e
    log_segments_failure(e)
    return nil
  end

  status = response.code.to_i
  if status >= 400
    log_segments_failure(status)
    return nil
  end

  begin
    response = JSON.parse(response.body)
  rescue JSON::ParserError
    log_segments_failure('JSON decode error')
    return nil
  end

  if response.include?('errors')
    error = response['errors'].first if response['errors'].is_a? Array
    error_code = extract_component(error, 'extensions', 'code')
    if error_code == 'INVALID_IDENTIFIER_EXCEPTION'
      log_segments_failure('invalid identifier', Logger::WARN)
    else
      error_class = extract_component(error, 'extensions', 'classification') || 'decode error'
      log_segments_failure(error_class)
    end
    return nil
  end

  audiences = extract_component(response, 'data', 'customer', 'audiences', 'edges')
  unless audiences
    log_segments_failure('decode error')
    return nil
  end

  audiences.filter_map do |edge|
    name = extract_component(edge, 'node', 'name')
    state = extract_component(edge, 'node', 'state')
    unless name && state
      log_segments_failure('decode error')
      return nil
    end
    state == 'qualified' ? name : nil
  end
end