|
12 | 12 | _LOGGER = logging.getLogger(__name__) |
13 | 13 |
|
14 | 14 |
|
15 | | -class ImpressionsAPI(object): # pylint: disable=too-few-public-methods |
16 | | - """Class that uses an httpClient to communicate with the impressions API.""" |
17 | | - |
18 | | - def __init__(self, client, sdk_key, sdk_metadata, telemetry_runtime_producer, mode=ImpressionsMode.OPTIMIZED): |
19 | | - """ |
20 | | - Class constructor. |
21 | | -
|
22 | | - :param client: HTTP Client responsble for issuing calls to the backend. |
23 | | - :type client: HttpClient |
24 | | - :param sdk_key: sdk key. |
25 | | - :type sdk_key: string |
26 | | - """ |
27 | | - self._client = client |
28 | | - self._sdk_key = sdk_key |
29 | | - self._metadata = headers_from_metadata(sdk_metadata) |
30 | | - self._metadata['SplitSDKImpressionsMode'] = mode.name |
31 | | - self._telemetry_runtime_producer = telemetry_runtime_producer |
| 15 | +class ImpressionsAPIBase(object): # pylint: disable=too-few-public-methods |
| 16 | + """Base Class that uses an httpClient to communicate with the impressions API.""" |
32 | 17 |
|
33 | 18 | @staticmethod |
34 | 19 | def _build_bulk(impressions): |
@@ -84,6 +69,25 @@ def _build_counters(counters): |
84 | 69 | ] |
85 | 70 | } |
86 | 71 |
|
| 72 | + |
| 73 | +class ImpressionsAPI(ImpressionsAPIBase): # pylint: disable=too-few-public-methods |
| 74 | + """Class that uses an httpClient to communicate with the impressions API.""" |
| 75 | + |
| 76 | + def __init__(self, client, sdk_key, sdk_metadata, telemetry_runtime_producer, mode=ImpressionsMode.OPTIMIZED): |
| 77 | + """ |
| 78 | + Class constructor. |
| 79 | +
|
| 80 | + :param client: HTTP Client responsble for issuing calls to the backend. |
| 81 | + :type client: HttpClient |
| 82 | + :param sdk_key: sdk key. |
| 83 | + :type sdk_key: string |
| 84 | + """ |
| 85 | + self._client = client |
| 86 | + self._sdk_key = sdk_key |
| 87 | + self._metadata = headers_from_metadata(sdk_metadata) |
| 88 | + self._metadata['SplitSDKImpressionsMode'] = mode.name |
| 89 | + self._telemetry_runtime_producer = telemetry_runtime_producer |
| 90 | + |
87 | 91 | def flush_impressions(self, impressions): |
88 | 92 | """ |
89 | 93 | Send impressions to the backend. |
@@ -136,3 +140,75 @@ def flush_counters(self, counters): |
136 | 140 | ) |
137 | 141 | _LOGGER.debug('Error: ', exc_info=True) |
138 | 142 | raise APIException('Impressions not flushed properly.') from exc |
| 143 | + |
| 144 | + |
| 145 | +class ImpressionsAPIAsync(ImpressionsAPIBase): # pylint: disable=too-few-public-methods |
| 146 | + """Async Class that uses an httpClient to communicate with the impressions API.""" |
| 147 | + |
| 148 | + def __init__(self, client, sdk_key, sdk_metadata, telemetry_runtime_producer, mode=ImpressionsMode.OPTIMIZED): |
| 149 | + """ |
| 150 | + Class constructor. |
| 151 | +
|
| 152 | + :param client: HTTP Client responsble for issuing calls to the backend. |
| 153 | + :type client: HttpClient |
| 154 | + :param sdk_key: sdk key. |
| 155 | + :type sdk_key: string |
| 156 | + """ |
| 157 | + self._client = client |
| 158 | + self._sdk_key = sdk_key |
| 159 | + self._metadata = headers_from_metadata(sdk_metadata) |
| 160 | + self._metadata['SplitSDKImpressionsMode'] = mode.name |
| 161 | + self._telemetry_runtime_producer = telemetry_runtime_producer |
| 162 | + |
| 163 | + async def flush_impressions(self, impressions): |
| 164 | + """ |
| 165 | + Send impressions to the backend. |
| 166 | +
|
| 167 | + :param impressions: Impressions bulk |
| 168 | + :type impressions: list |
| 169 | + """ |
| 170 | + bulk = self._build_bulk(impressions) |
| 171 | + self._client.set_telemetry_data(HTTPExceptionsAndLatencies.IMPRESSION, self._telemetry_runtime_producer) |
| 172 | + try: |
| 173 | + response = await self._client.post( |
| 174 | + 'events', |
| 175 | + 'testImpressions/bulk', |
| 176 | + self._sdk_key, |
| 177 | + body=bulk, |
| 178 | + extra_headers=self._metadata, |
| 179 | + ) |
| 180 | + if not 200 <= response.status_code < 300: |
| 181 | + raise APIException(response.body, response.status_code) |
| 182 | + except HttpClientException as exc: |
| 183 | + _LOGGER.error( |
| 184 | + 'Error posting impressions because an exception was raised by the HTTPClient' |
| 185 | + ) |
| 186 | + _LOGGER.debug('Error: ', exc_info=True) |
| 187 | + raise APIException('Impressions not flushed properly.') from exc |
| 188 | + |
| 189 | + async def flush_counters(self, counters): |
| 190 | + """ |
| 191 | + Send impressions to the backend. |
| 192 | +
|
| 193 | + :param impressions: Impressions bulk |
| 194 | + :type impressions: list |
| 195 | + """ |
| 196 | + bulk = self._build_counters(counters) |
| 197 | + self._client.set_telemetry_data(HTTPExceptionsAndLatencies.IMPRESSION_COUNT, self._telemetry_runtime_producer) |
| 198 | + try: |
| 199 | + response = await self._client.post( |
| 200 | + 'events', |
| 201 | + 'testImpressions/count', |
| 202 | + self._sdk_key, |
| 203 | + body=bulk, |
| 204 | + extra_headers=self._metadata, |
| 205 | + ) |
| 206 | + if not 200 <= response.status_code < 300: |
| 207 | + raise APIException(response.body, response.status_code) |
| 208 | + except HttpClientException as exc: |
| 209 | + _LOGGER.error( |
| 210 | + 'Error posting impressions counters because an exception was raised by the ' |
| 211 | + 'HTTPClient' |
| 212 | + ) |
| 213 | + _LOGGER.debug('Error: ', exc_info=True) |
| 214 | + raise APIException('Impressions not flushed properly.') from exc |
0 commit comments