|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# |
| 4 | +# Copyright 2022, Optimizely and contributors |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +require 'json' |
| 20 | + |
| 21 | +module Optimizely |
| 22 | + class ZaiusGraphQlApiManager |
| 23 | + # Interface that handles fetching audience segments. |
| 24 | + |
| 25 | + def initialize(logger: nil, proxy_config: nil) |
| 26 | + @logger = logger || NoOpLogger.new |
| 27 | + @proxy_config = proxy_config |
| 28 | + end |
| 29 | + |
| 30 | + # Fetch segments from the ODP GraphQL API. |
| 31 | + # |
| 32 | + # @param api_key - public api key |
| 33 | + # @param api_host - domain url of the host |
| 34 | + # @param user_key - vuid or fs_user_id (client device id or fullstack id) |
| 35 | + # @param user_value - value of user_key |
| 36 | + # @param segments_to_check - array of segments to check |
| 37 | + |
| 38 | + def fetch_segments(api_key, api_host, user_key, user_value, segments_to_check) |
| 39 | + url = "#{api_host}/v3/graphql" |
| 40 | + |
| 41 | + headers = {'Content-Type' => 'application/json', 'x-api-key' => api_key.to_s} |
| 42 | + |
| 43 | + payload = { |
| 44 | + 'query' => %'query {customer(#{user_key}: "#{user_value}")' \ |
| 45 | + "{audiences(subset:#{segments_to_check || '[]'}) {edges {node {name state}}}}}" |
| 46 | + }.to_json |
| 47 | + |
| 48 | + begin |
| 49 | + response = Helpers::HttpUtils.make_request( |
| 50 | + url, :post, payload, headers, Optimizely::Helpers::Constants::ODP_GRAPHQL_API_CONFIG[:REQUEST_TIMEOUT], @proxy_config |
| 51 | + ) |
| 52 | + rescue SocketError, Timeout::Error, Net::ProtocolError, Errno::ECONNRESET => e |
| 53 | + @logger.log(Logger::DEBUG, "GraphQL download failed: #{e}") |
| 54 | + log_failure('network error') |
| 55 | + return [] |
| 56 | + rescue Errno::EINVAL, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError => e |
| 57 | + log_failure(e) |
| 58 | + return [] |
| 59 | + end |
| 60 | + |
| 61 | + status = response.code.to_i |
| 62 | + if status >= 400 |
| 63 | + log_failure(status) |
| 64 | + return [] |
| 65 | + end |
| 66 | + |
| 67 | + begin |
| 68 | + response = JSON.parse(response.body) |
| 69 | + rescue JSON::ParserError |
| 70 | + log_failure('JSON decode error') |
| 71 | + return [] |
| 72 | + end |
| 73 | + |
| 74 | + if response.include?('errors') |
| 75 | + error_class = response['errors']&.first&.dig('extensions', 'classification') || 'decode error' |
| 76 | + if error_class == 'InvalidIdentifierException' |
| 77 | + log_failure('invalid identifier', Logger::WARN) |
| 78 | + else |
| 79 | + log_failure(error_class) |
| 80 | + end |
| 81 | + return [] |
| 82 | + end |
| 83 | + |
| 84 | + audiences = response.dig('data', 'customer', 'audiences', 'edges') |
| 85 | + unless audiences |
| 86 | + log_failure('decode error') |
| 87 | + return [] |
| 88 | + end |
| 89 | + |
| 90 | + audiences.filter_map do |edge| |
| 91 | + name = edge.dig('node', 'name') |
| 92 | + state = edge.dig('node', 'state') |
| 93 | + unless name && state |
| 94 | + log_failure('decode error') |
| 95 | + return [] |
| 96 | + end |
| 97 | + state == 'qualified' ? name : nil |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + private |
| 102 | + |
| 103 | + def log_failure(message, level = Logger::ERROR) |
| 104 | + @logger.log(level, format(Optimizely::Helpers::Constants::ODP_LOGS[:FETCH_SEGMENTS_FAILED], message)) |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments