33import logging
44import json
55
6- from splitio .api import APIException
6+ from splitio .api import APIException , headers_from_metadata
77from splitio .api .commons import headers_from_metadata , record_telemetry
88from splitio .spec import SPEC_VERSION
99from splitio .util .time import get_current_epoch_time_ms
@@ -32,6 +32,7 @@ def __init__(self, client, sdk_key, sdk_metadata, telemetry_runtime_producer):
3232 self ._sdk_key = sdk_key
3333 self ._metadata = headers_from_metadata (sdk_metadata )
3434 self ._telemetry_runtime_producer = telemetry_runtime_producer
35+ self ._client .set_telemetry_data (HTTPExceptionsAndLatencies .TOKEN , self ._telemetry_runtime_producer )
3536
3637 def authenticate (self ):
3738 """
@@ -40,18 +41,17 @@ def authenticate(self):
4041 :return: Json representation of an authentication.
4142 :rtype: splitio.models.token.Token
4243 """
43- start = get_current_epoch_time_ms ()
4444 try :
4545 response = self ._client .get (
4646 'auth' ,
47- '/ v2/auth?s=' + SPEC_VERSION ,
47+ 'v2/auth?s=' + SPEC_VERSION ,
4848 self ._sdk_key ,
4949 extra_headers = self ._metadata ,
5050 )
51- record_telemetry (response .status_code , get_current_epoch_time_ms () - start , HTTPExceptionsAndLatencies .TOKEN , self ._telemetry_runtime_producer )
5251 if 200 <= response .status_code < 300 :
5352 payload = json .loads (response .body )
5453 return from_raw (payload )
54+
5555 else :
5656 if (response .status_code >= 400 and response .status_code < 500 ):
5757 self ._telemetry_runtime_producer .record_auth_rejections ()
@@ -60,3 +60,50 @@ def authenticate(self):
6060 _LOGGER .error ('Exception raised while authenticating' )
6161 _LOGGER .debug ('Exception information: ' , exc_info = True )
6262 raise APIException ('Could not perform authentication.' ) from exc
63+
64+ class AuthAPIAsync (object ): # pylint: disable=too-few-public-methods
65+ """Async Class that uses an httpClient to communicate with the SDK Auth Service API."""
66+
67+ def __init__ (self , client , sdk_key , sdk_metadata , telemetry_runtime_producer ):
68+ """
69+ Class constructor.
70+
71+ :param client: HTTP Client responsble for issuing calls to the backend.
72+ :type client: HttpClient
73+ :param sdk_key: User sdk key.
74+ :type sdk_key: string
75+ :param sdk_metadata: SDK version & machine name & IP.
76+ :type sdk_metadata: splitio.client.util.SdkMetadata
77+ """
78+ self ._client = client
79+ self ._sdk_key = sdk_key
80+ self ._metadata = headers_from_metadata (sdk_metadata )
81+ self ._telemetry_runtime_producer = telemetry_runtime_producer
82+ self ._client .set_telemetry_data (HTTPExceptionsAndLatencies .TOKEN , self ._telemetry_runtime_producer )
83+
84+ async def authenticate (self ):
85+ """
86+ Perform authentication.
87+
88+ :return: Json representation of an authentication.
89+ :rtype: splitio.models.token.Token
90+ """
91+ try :
92+ response = await self ._client .get (
93+ 'auth' ,
94+ 'v2/auth?s=' + SPEC_VERSION ,
95+ self ._sdk_key ,
96+ extra_headers = self ._metadata ,
97+ )
98+ if 200 <= response .status_code < 300 :
99+ payload = json .loads (response .body )
100+ return from_raw (payload )
101+
102+ else :
103+ if (response .status_code >= 400 and response .status_code < 500 ):
104+ await self ._telemetry_runtime_producer .record_auth_rejections ()
105+ raise APIException (response .body , response .status_code )
106+ except HttpClientException as exc :
107+ _LOGGER .error ('Exception raised while authenticating' )
108+ _LOGGER .debug ('Exception information: ' , exc_info = True )
109+ raise APIException ('Could not perform authentication.' ) from exc
0 commit comments