|
| 1 | +# Copyright 2022, Optimizely |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import json |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +import requests |
| 20 | +from requests.exceptions import RequestException, ConnectionError, Timeout, JSONDecodeError |
| 21 | + |
| 22 | +from optimizely import logger as optimizely_logger |
| 23 | +from optimizely.helpers.enums import Errors, OdpGraphQLApiConfig |
| 24 | + |
| 25 | +""" |
| 26 | + ODP GraphQL API |
| 27 | + - https://api.zaius.com/v3/graphql |
| 28 | + - test ODP public API key = "W4WzcEs-ABgXorzY7h1LCQ" |
| 29 | +
|
| 30 | +
|
| 31 | + [GraphQL Request] |
| 32 | +
|
| 33 | + # fetch info with fs_user_id for ["has_email", "has_email_opted_in", "push_on_sale"] segments |
| 34 | + curl -i -H 'Content-Type: application/json' -H 'x-api-key: W4WzcEs-ABgXorzY7h1LCQ' -X POST -d |
| 35 | + '{"query":"query {customer(fs_user_id: \"tester-101\") {audiences(subset:[\"has_email\", |
| 36 | + \"has_email_opted_in\", \"push_on_sale\"]) {edges {node {name state}}}}}"}' https://api.zaius.com/v3/graphql |
| 37 | + # fetch info with vuid for ["has_email", "has_email_opted_in", "push_on_sale"] segments |
| 38 | + curl -i -H 'Content-Type: application/json' -H 'x-api-key: W4WzcEs-ABgXorzY7h1LCQ' -X POST -d |
| 39 | + '{"query":"query {customer(vuid: \"d66a9d81923d4d2f99d8f64338976322\") {audiences(subset:[\"has_email\", |
| 40 | + \"has_email_opted_in\", \"push_on_sale\"]) {edges {node {name state}}}}}"}' https://api.zaius.com/v3/graphql |
| 41 | +
|
| 42 | + query MyQuery { |
| 43 | + customer(vuid: "d66a9d81923d4d2f99d8f64338976322") { |
| 44 | + audiences(subset:["has_email", "has_email_opted_in", "push_on_sale"]) { |
| 45 | + edges { |
| 46 | + node { |
| 47 | + name |
| 48 | + state |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +
|
| 55 | +
|
| 56 | + [GraphQL Response] |
| 57 | + { |
| 58 | + "data": { |
| 59 | + "customer": { |
| 60 | + "audiences": { |
| 61 | + "edges": [ |
| 62 | + { |
| 63 | + "node": { |
| 64 | + "name": "has_email", |
| 65 | + "state": "qualified", |
| 66 | + } |
| 67 | + }, |
| 68 | + { |
| 69 | + "node": { |
| 70 | + "name": "has_email_opted_in", |
| 71 | + "state": "qualified", |
| 72 | + } |
| 73 | + }, |
| 74 | + ... |
| 75 | + ] |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +
|
| 81 | + [GraphQL Error Response] |
| 82 | + { |
| 83 | + "errors": [ |
| 84 | + { |
| 85 | + "message": "Exception while fetching data (/customer) : java.lang.RuntimeException: |
| 86 | + could not resolve _fs_user_id = asdsdaddddd", |
| 87 | + "locations": [ |
| 88 | + { |
| 89 | + "line": 2, |
| 90 | + "column": 3 |
| 91 | + } |
| 92 | + ], |
| 93 | + "path": [ |
| 94 | + "customer" |
| 95 | + ], |
| 96 | + "extensions": { |
| 97 | + "classification": "InvalidIdentifierException" |
| 98 | + } |
| 99 | + } |
| 100 | + ], |
| 101 | + "data": { |
| 102 | + "customer": null |
| 103 | + } |
| 104 | + } |
| 105 | +""" |
| 106 | + |
| 107 | + |
| 108 | +class ZaiusGraphQLApiManager: |
| 109 | + """Interface for manging the fetching of audience segments.""" |
| 110 | + |
| 111 | + def __init__(self, logger: Optional[optimizely_logger.Logger] = None): |
| 112 | + self.logger = logger or optimizely_logger.NoOpLogger() |
| 113 | + |
| 114 | + def fetch_segments(self, api_key: str, api_host: str, user_key: str, |
| 115 | + user_value: str, segments_to_check: list[str]) -> Optional[list[str]]: |
| 116 | + """ |
| 117 | + Fetch segments from ODP GraphQL API. |
| 118 | +
|
| 119 | + Args: |
| 120 | + api_key: public api key |
| 121 | + api_host: domain url of the host |
| 122 | + user_key: vuid or fs_user_id (client device id or fullstack id) |
| 123 | + user_value: vaue of user_key |
| 124 | + segments_to_check: lit of segments to check |
| 125 | +
|
| 126 | + Returns: |
| 127 | + Audience segments from GraphQL. |
| 128 | + """ |
| 129 | + url = f'{api_host}/v3/graphql' |
| 130 | + request_headers = {'content-type': 'application/json', |
| 131 | + 'x-api-key': str(api_key)} |
| 132 | + |
| 133 | + segments_filter = self.make_subset_filter(segments_to_check) |
| 134 | + payload_dict = { |
| 135 | + 'query': 'query {customer(' + str(user_key) + ': "' + str(user_value) + '") ' |
| 136 | + '{audiences' + segments_filter + ' {edges {node {name state}}}}}' |
| 137 | + } |
| 138 | + |
| 139 | + try: |
| 140 | + response = requests.post(url=url, |
| 141 | + headers=request_headers, |
| 142 | + data=json.dumps(payload_dict), |
| 143 | + timeout=OdpGraphQLApiConfig.REQUEST_TIMEOUT) |
| 144 | + |
| 145 | + response.raise_for_status() |
| 146 | + response_dict = response.json() |
| 147 | + |
| 148 | + # There is no status code with network issues such as ConnectionError or Timeouts |
| 149 | + # (i.e. no internet, server can't be reached). |
| 150 | + except (ConnectionError, Timeout) as err: |
| 151 | + self.logger.debug(f'GraphQL download failed: {err}') |
| 152 | + self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format('network error')) |
| 153 | + return None |
| 154 | + except JSONDecodeError: |
| 155 | + self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format('JSON decode error')) |
| 156 | + return None |
| 157 | + except RequestException as err: |
| 158 | + self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format(err)) |
| 159 | + return None |
| 160 | + |
| 161 | + if response_dict and 'errors' in response_dict: |
| 162 | + try: |
| 163 | + error_class = response_dict['errors'][0]['extensions']['classification'] |
| 164 | + except (KeyError, IndexError): |
| 165 | + self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format('decode error')) |
| 166 | + return None |
| 167 | + |
| 168 | + if error_class == 'InvalidIdentifierException': |
| 169 | + self.logger.error(Errors.INVALID_SEGMENT_IDENTIFIER) |
| 170 | + return None |
| 171 | + else: |
| 172 | + self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format(error_class)) |
| 173 | + return None |
| 174 | + else: |
| 175 | + try: |
| 176 | + audiences = response_dict['data']['customer']['audiences']['edges'] |
| 177 | + segments = [edge['node']['name'] for edge in audiences if edge['node']['state'] == 'qualified'] |
| 178 | + return segments |
| 179 | + except KeyError: |
| 180 | + self.logger.error(Errors.FETCH_SEGMENTS_FAILED.format('decode error')) |
| 181 | + return None |
| 182 | + |
| 183 | + @staticmethod |
| 184 | + def make_subset_filter(segments: list[str]) -> str: |
| 185 | + """ |
| 186 | + segments = []: (fetch none) |
| 187 | + --> subsetFilter = "(subset:[])" |
| 188 | + segments = ["a"]: (fetch one segment) |
| 189 | + --> subsetFilter = '(subset:["a"])' |
| 190 | +
|
| 191 | + Purposely using .join() method to deal with special cases of |
| 192 | + any words with apostrophes (i.e. don't). .join() method enquotes |
| 193 | + correctly without conflicting with the apostrophe. |
| 194 | + """ |
| 195 | + if segments == []: |
| 196 | + return '(subset:[])' |
| 197 | + return '(subset:["' + '", "'.join(segments) + '"]' + ')' |
0 commit comments