From 7ca594b7ff41a8b00055df6997270963c67ce9a3 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Tue, 12 Jul 2022 18:08:53 +0800 Subject: [PATCH 1/9] feat: allow credential in client --- client/clip_client/client.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/client/clip_client/client.py b/client/clip_client/client.py index 126b5a167..ea982a9c8 100644 --- a/client/clip_client/client.py +++ b/client/clip_client/client.py @@ -30,26 +30,23 @@ def __init__(self, server: str): - port: the public port of the server :param server: the server URI """ - try: - r = urlparse(server) - _port = r.port - _scheme = r.scheme - if not _scheme: - raise - except: + r = urlparse(server) + _port = r.port + self.scheme = r.scheme + if not self.scheme: raise ValueError(f'{server} is not a valid scheme') _tls = False - if _scheme in ('grpcs', 'https', 'wss'): - _scheme = _scheme[:-1] + if self.scheme in ('grpcs', 'https', 'wss'): + self.scheme = self.scheme[:-1] _tls = True - if _scheme == 'ws': - _scheme = 'websocket' # temp fix for the core + if self.scheme == 'ws': + self.scheme = 'websocket' # temp fix for the core - if _scheme in ('grpc', 'http', 'websocket'): - _kwargs = dict(host=r.hostname, port=_port, protocol=_scheme, tls=_tls) + if self.scheme in ('grpc', 'http', 'websocket'): + _kwargs = dict(host=r.hostname, port=_port, protocol=self.scheme, tls=_tls) from jina import Client From fb713cab6237ebb547f00b057e9a284a2b82a0e4 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Wed, 13 Jul 2022 13:18:09 +0800 Subject: [PATCH 2/9] feat: add credential wrapper --- client/clip_client/client.py | 37 +++++++++++++++++++++++++----------- client/clip_client/helper.py | 8 ++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/client/clip_client/client.py b/client/clip_client/client.py index ea982a9c8..d6d8ff5d0 100644 --- a/client/clip_client/client.py +++ b/client/clip_client/client.py @@ -15,6 +15,7 @@ from urllib.parse import urlparse from functools import partial from docarray import DocumentArray +from helper import _grpc_credential_wrapper, _http_credential_wrapper if TYPE_CHECKING: import numpy as np @@ -22,7 +23,7 @@ class Client: - def __init__(self, server: str): + def __init__(self, server: str, credential: str = None, **kwargs): """Create a Clip client object that connects to the Clip server. Server scheme is in the format of `scheme://netloc:port`, where - scheme: one of grpc, websocket, http, grpcs, websockets, https @@ -30,23 +31,25 @@ def __init__(self, server: str): - port: the public port of the server :param server: the server URI """ - r = urlparse(server) - _port = r.port - self.scheme = r.scheme - if not self.scheme: + try: + r = urlparse(server) + _port = r.port + self._scheme = r.scheme + except: raise ValueError(f'{server} is not a valid scheme') + self.credential = credential _tls = False - if self.scheme in ('grpcs', 'https', 'wss'): - self.scheme = self.scheme[:-1] + if self._scheme in ('grpcs', 'https', 'wss'): + self._scheme = self._scheme[:-1] _tls = True - if self.scheme == 'ws': - self.scheme = 'websocket' # temp fix for the core + if self._scheme == 'ws': + self._scheme = 'websocket' # temp fix for the core - if self.scheme in ('grpc', 'http', 'websocket'): - _kwargs = dict(host=r.hostname, port=_port, protocol=self.scheme, tls=_tls) + if self._scheme in ('grpc', 'http', 'websocket'): + _kwargs = dict(host=r.hostname, port=_port, protocol=self._scheme, tls=_tls) from jina import Client @@ -183,6 +186,12 @@ def _get_post_payload(self, content, kwargs): inputs=self._iter_doc(content), request_size=kwargs.get('batch_size', 8), total_docs=len(content) if hasattr(content, '__len__') else None, + metadata=_grpc_credential_wrapper(self.credential) + if self._scheme == 'grpc' + else None, + headers=_http_credential_wrapper(self.credential) + if self._scheme == 'http' + else None, ) def profile(self, content: Optional[str] = '') -> Dict[str, float]: @@ -359,6 +368,12 @@ def _get_rank_payload(self, content, kwargs): ), request_size=kwargs.get('batch_size', 8), total_docs=len(content) if hasattr(content, '__len__') else None, + metadata=_grpc_credential_wrapper(self.credential) + if self._scheme == 'grpc' + else None, + headers=_http_credential_wrapper(self.credential) + if self._scheme == 'http' + else None, ) def rank(self, docs: Iterable['Document'], **kwargs) -> 'DocumentArray': diff --git a/client/clip_client/helper.py b/client/clip_client/helper.py index b47cbc32d..2bb73070a 100644 --- a/client/clip_client/helper.py +++ b/client/clip_client/helper.py @@ -52,3 +52,11 @@ def is_latest_version(package: str = None, github_repo: str = None) -> None: """ threading.Thread(target=_version_check, args=(package, github_repo)).start() + + +def _http_credential_wrapper(credential): + pass + + +def _grpc_credential_wrapper(credential): + return ('Authorization', f'{credential}') From c41c205fa0b84ff20ceb448f683d8f47379be2a7 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Wed, 13 Jul 2022 13:19:44 +0800 Subject: [PATCH 3/9] feat: add credential wrapper --- client/clip_client/helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/clip_client/helper.py b/client/clip_client/helper.py index 2bb73070a..6a953e789 100644 --- a/client/clip_client/helper.py +++ b/client/clip_client/helper.py @@ -55,7 +55,7 @@ def is_latest_version(package: str = None, github_repo: str = None) -> None: def _http_credential_wrapper(credential): - pass + return {'Authorization': f'{credential}'} def _grpc_credential_wrapper(credential): From cf71b40f88b1ef759d0711f3abbb956b9427cca7 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Wed, 13 Jul 2022 17:45:28 +0800 Subject: [PATCH 4/9] fix: credential is a dict --- client/clip_client/client.py | 30 ++++++++++++++---------------- client/clip_client/helper.py | 21 +++++++++++++++------ 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/client/clip_client/client.py b/client/clip_client/client.py index d6d8ff5d0..ccb9a4185 100644 --- a/client/clip_client/client.py +++ b/client/clip_client/client.py @@ -15,7 +15,7 @@ from urllib.parse import urlparse from functools import partial from docarray import DocumentArray -from helper import _grpc_credential_wrapper, _http_credential_wrapper +from clip_client.helper import get_authentication if TYPE_CHECKING: import numpy as np @@ -23,7 +23,7 @@ class Client: - def __init__(self, server: str, credential: str = None, **kwargs): + def __init__(self, server: str, credential: dict = None, **kwargs): """Create a Clip client object that connects to the Clip server. Server scheme is in the format of `scheme://netloc:port`, where - scheme: one of grpc, websocket, http, grpcs, websockets, https @@ -181,18 +181,17 @@ def _iter_doc(self, content) -> Generator['Document', None, None]: ) def _get_post_payload(self, content, kwargs): - return dict( + payload = dict( on='/', inputs=self._iter_doc(content), request_size=kwargs.get('batch_size', 8), total_docs=len(content) if hasattr(content, '__len__') else None, - metadata=_grpc_credential_wrapper(self.credential) - if self._scheme == 'grpc' - else None, - headers=_http_credential_wrapper(self.credential) - if self._scheme == 'http' - else None, ) + if self._scheme == 'grpc': + payload.update(metadata=get_authentication(self._scheme, self.credential)) + elif self._scheme == 'http': + payload.update(headers=get_authentication(self._scheme, self.credential)) + return payload def profile(self, content: Optional[str] = '') -> Dict[str, float]: """Profiling a single query's roundtrip including network and computation latency. Results is summarized in a table. @@ -361,20 +360,19 @@ def _iter_rank_docs( ) def _get_rank_payload(self, content, kwargs): - return dict( + payload = dict( on='/rank', inputs=self._iter_rank_docs( content, _source=kwargs.get('source', 'matches') ), request_size=kwargs.get('batch_size', 8), total_docs=len(content) if hasattr(content, '__len__') else None, - metadata=_grpc_credential_wrapper(self.credential) - if self._scheme == 'grpc' - else None, - headers=_http_credential_wrapper(self.credential) - if self._scheme == 'http' - else None, ) + if self._scheme == 'grpc': + payload.update(metadata=get_authentication(self._scheme, self.credential)) + elif self._scheme == 'http': + payload.update(headers=get_authentication(self._scheme, self.credential)) + return payload def rank(self, docs: Iterable['Document'], **kwargs) -> 'DocumentArray': """Rank image-text matches according to the server CLIP model. diff --git a/client/clip_client/helper.py b/client/clip_client/helper.py index 6a953e789..02562c50b 100644 --- a/client/clip_client/helper.py +++ b/client/clip_client/helper.py @@ -54,9 +54,18 @@ def is_latest_version(package: str = None, github_repo: str = None) -> None: threading.Thread(target=_version_check, args=(package, github_repo)).start() -def _http_credential_wrapper(credential): - return {'Authorization': f'{credential}'} - - -def _grpc_credential_wrapper(credential): - return ('Authorization', f'{credential}') +def get_authentication(scheme, credentials): + if scheme == 'grpc': + return ( + ('Authorization', credentials['Authorization']) + if 'Authorization' in credentials + else None + ) + elif scheme == 'http': + return ( + {'Authorization': credentials['Authorization']} + if 'Authorization' in credentials + else None + ) + else: + return None From 8ae53071f490e5ac46bb3aa6a3ddd0cd248545e6 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Wed, 13 Jul 2022 18:02:49 +0800 Subject: [PATCH 5/9] fix: default credential --- client/clip_client/client.py | 12 ++++++------ client/clip_client/helper.py | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/client/clip_client/client.py b/client/clip_client/client.py index ccb9a4185..42a12eab3 100644 --- a/client/clip_client/client.py +++ b/client/clip_client/client.py @@ -15,7 +15,7 @@ from urllib.parse import urlparse from functools import partial from docarray import DocumentArray -from clip_client.helper import get_authentication +from clip_client.helper import get_authorization if TYPE_CHECKING: import numpy as np @@ -23,7 +23,7 @@ class Client: - def __init__(self, server: str, credential: dict = None, **kwargs): + def __init__(self, server: str, credential: dict = {}, **kwargs): """Create a Clip client object that connects to the Clip server. Server scheme is in the format of `scheme://netloc:port`, where - scheme: one of grpc, websocket, http, grpcs, websockets, https @@ -188,9 +188,9 @@ def _get_post_payload(self, content, kwargs): total_docs=len(content) if hasattr(content, '__len__') else None, ) if self._scheme == 'grpc': - payload.update(metadata=get_authentication(self._scheme, self.credential)) + payload.update(metadata=get_authorization(self._scheme, self.credential)) elif self._scheme == 'http': - payload.update(headers=get_authentication(self._scheme, self.credential)) + payload.update(headers=get_authorization(self._scheme, self.credential)) return payload def profile(self, content: Optional[str] = '') -> Dict[str, float]: @@ -369,9 +369,9 @@ def _get_rank_payload(self, content, kwargs): total_docs=len(content) if hasattr(content, '__len__') else None, ) if self._scheme == 'grpc': - payload.update(metadata=get_authentication(self._scheme, self.credential)) + payload.update(metadata=get_authorization(self._scheme, self.credential)) elif self._scheme == 'http': - payload.update(headers=get_authentication(self._scheme, self.credential)) + payload.update(headers=get_authorization(self._scheme, self.credential)) return payload def rank(self, docs: Iterable['Document'], **kwargs) -> 'DocumentArray': diff --git a/client/clip_client/helper.py b/client/clip_client/helper.py index 02562c50b..a991b4db7 100644 --- a/client/clip_client/helper.py +++ b/client/clip_client/helper.py @@ -54,17 +54,17 @@ def is_latest_version(package: str = None, github_repo: str = None) -> None: threading.Thread(target=_version_check, args=(package, github_repo)).start() -def get_authentication(scheme, credentials): +def get_authorization(scheme, credential): if scheme == 'grpc': return ( - ('Authorization', credentials['Authorization']) - if 'Authorization' in credentials + ('Authorization', credential['Authorization']) + if 'Authorization' in credential else None ) elif scheme == 'http': return ( - {'Authorization': credentials['Authorization']} - if 'Authorization' in credentials + {'Authorization': credential['Authorization']} + if 'Authorization' in credential else None ) else: From 4393fc386b1f9e0c75562003856f4c80c42727b9 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Wed, 13 Jul 2022 18:26:25 +0800 Subject: [PATCH 6/9] fix: remove redundancy --- client/clip_client/client.py | 25 +++++++++++++++---------- client/clip_client/helper.py | 17 ----------------- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/client/clip_client/client.py b/client/clip_client/client.py index 42a12eab3..cfcd4c448 100644 --- a/client/clip_client/client.py +++ b/client/clip_client/client.py @@ -30,6 +30,7 @@ def __init__(self, server: str, credential: dict = {}, **kwargs): - netloc: the server ip address or hostname - port: the public port of the server :param server: the server URI + :param credential: the credential for authentication {'Authentication': ''} """ try: r = urlparse(server) @@ -38,15 +39,17 @@ def __init__(self, server: str, credential: dict = {}, **kwargs): except: raise ValueError(f'{server} is not a valid scheme') - self.credential = credential _tls = False - if self._scheme in ('grpcs', 'https', 'wss'): self._scheme = self._scheme[:-1] _tls = True if self._scheme == 'ws': self._scheme = 'websocket' # temp fix for the core + if credential: + raise ValueError( + 'credential is not supported for websocket, please use grpc or http' + ) if self._scheme in ('grpc', 'http', 'websocket'): _kwargs = dict(host=r.hostname, port=_port, protocol=self._scheme, tls=_tls) @@ -58,6 +61,8 @@ def __init__(self, server: str, credential: dict = {}, **kwargs): else: raise ValueError(f'{server} is not a valid scheme') + self.authorization = credential.get('Authorization', None) + @overload def encode( self, @@ -187,10 +192,10 @@ def _get_post_payload(self, content, kwargs): request_size=kwargs.get('batch_size', 8), total_docs=len(content) if hasattr(content, '__len__') else None, ) - if self._scheme == 'grpc': - payload.update(metadata=get_authorization(self._scheme, self.credential)) - elif self._scheme == 'http': - payload.update(headers=get_authorization(self._scheme, self.credential)) + if self._scheme == 'grpc' and self.authorization: + payload.update(metadata=('Authorization', self.authorization)) + elif self._scheme == 'http' and self.authorization: + payload.update(headers={'Authorization': self.authorization}) return payload def profile(self, content: Optional[str] = '') -> Dict[str, float]: @@ -368,10 +373,10 @@ def _get_rank_payload(self, content, kwargs): request_size=kwargs.get('batch_size', 8), total_docs=len(content) if hasattr(content, '__len__') else None, ) - if self._scheme == 'grpc': - payload.update(metadata=get_authorization(self._scheme, self.credential)) - elif self._scheme == 'http': - payload.update(headers=get_authorization(self._scheme, self.credential)) + if self._scheme == 'grpc' and self.authorization: + payload.update(metadata=('Authorization', self.authorization)) + elif self._scheme == 'http' and self.authorization: + payload.update(headers={'Authorization': self.authorization}) return payload def rank(self, docs: Iterable['Document'], **kwargs) -> 'DocumentArray': diff --git a/client/clip_client/helper.py b/client/clip_client/helper.py index a991b4db7..b47cbc32d 100644 --- a/client/clip_client/helper.py +++ b/client/clip_client/helper.py @@ -52,20 +52,3 @@ def is_latest_version(package: str = None, github_repo: str = None) -> None: """ threading.Thread(target=_version_check, args=(package, github_repo)).start() - - -def get_authorization(scheme, credential): - if scheme == 'grpc': - return ( - ('Authorization', credential['Authorization']) - if 'Authorization' in credential - else None - ) - elif scheme == 'http': - return ( - {'Authorization': credential['Authorization']} - if 'Authorization' in credential - else None - ) - else: - return None From ce83ba2c7b2d3391b1608db21c9b1197108731c3 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Wed, 13 Jul 2022 18:32:03 +0800 Subject: [PATCH 7/9] fix: remove unused import --- client/clip_client/client.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/client/clip_client/client.py b/client/clip_client/client.py index cfcd4c448..68fc3ee48 100644 --- a/client/clip_client/client.py +++ b/client/clip_client/client.py @@ -15,7 +15,6 @@ from urllib.parse import urlparse from functools import partial from docarray import DocumentArray -from clip_client.helper import get_authorization if TYPE_CHECKING: import numpy as np @@ -48,7 +47,7 @@ def __init__(self, server: str, credential: dict = {}, **kwargs): self._scheme = 'websocket' # temp fix for the core if credential: raise ValueError( - 'credential is not supported for websocket, please use grpc or http' + 'Credential is not supported for websocket, please use grpc or http' ) if self._scheme in ('grpc', 'http', 'websocket'): @@ -61,7 +60,7 @@ def __init__(self, server: str, credential: dict = {}, **kwargs): else: raise ValueError(f'{server} is not a valid scheme') - self.authorization = credential.get('Authorization', None) + self._authorization = credential.get('Authorization', None) @overload def encode( @@ -192,10 +191,10 @@ def _get_post_payload(self, content, kwargs): request_size=kwargs.get('batch_size', 8), total_docs=len(content) if hasattr(content, '__len__') else None, ) - if self._scheme == 'grpc' and self.authorization: - payload.update(metadata=('Authorization', self.authorization)) - elif self._scheme == 'http' and self.authorization: - payload.update(headers={'Authorization': self.authorization}) + if self._scheme == 'grpc' and self._authorization: + payload.update(metadata=('Authorization', self._authorization)) + elif self._scheme == 'http' and self._authorization: + payload.update(headers={'Authorization': self._authorization}) return payload def profile(self, content: Optional[str] = '') -> Dict[str, float]: @@ -373,10 +372,10 @@ def _get_rank_payload(self, content, kwargs): request_size=kwargs.get('batch_size', 8), total_docs=len(content) if hasattr(content, '__len__') else None, ) - if self._scheme == 'grpc' and self.authorization: - payload.update(metadata=('Authorization', self.authorization)) - elif self._scheme == 'http' and self.authorization: - payload.update(headers={'Authorization': self.authorization}) + if self._scheme == 'grpc' and self._authorization: + payload.update(metadata=('Authorization', self._authorization)) + elif self._scheme == 'http' and self._authorization: + payload.update(headers={'Authorization': self._authorization}) return payload def rank(self, docs: Iterable['Document'], **kwargs) -> 'DocumentArray': From f26acef2ffa3fb9193f41aeae649048fee898afc Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Wed, 13 Jul 2022 18:53:50 +0800 Subject: [PATCH 8/9] fix: warning at ws --- client/clip_client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/clip_client/client.py b/client/clip_client/client.py index 68fc3ee48..ee4f5f697 100644 --- a/client/clip_client/client.py +++ b/client/clip_client/client.py @@ -46,7 +46,7 @@ def __init__(self, server: str, credential: dict = {}, **kwargs): if self._scheme == 'ws': self._scheme = 'websocket' # temp fix for the core if credential: - raise ValueError( + warnings.warn( 'Credential is not supported for websocket, please use grpc or http' ) From 50e6901f9843198e1dcd9a13d3121e749602da45 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Wed, 20 Jul 2022 11:38:31 +0800 Subject: [PATCH 9/9] fix: typo --- client/clip_client/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/clip_client/client.py b/client/clip_client/client.py index ee4f5f697..7b1bf1e0e 100644 --- a/client/clip_client/client.py +++ b/client/clip_client/client.py @@ -192,7 +192,7 @@ def _get_post_payload(self, content, kwargs): total_docs=len(content) if hasattr(content, '__len__') else None, ) if self._scheme == 'grpc' and self._authorization: - payload.update(metadata=('Authorization', self._authorization)) + payload.update(metadata=('authorization', self._authorization)) elif self._scheme == 'http' and self._authorization: payload.update(headers={'Authorization': self._authorization}) return payload @@ -373,7 +373,7 @@ def _get_rank_payload(self, content, kwargs): total_docs=len(content) if hasattr(content, '__len__') else None, ) if self._scheme == 'grpc' and self._authorization: - payload.update(metadata=('Authorization', self._authorization)) + payload.update(metadata=('authorization', self._authorization)) elif self._scheme == 'http' and self._authorization: payload.update(headers={'Authorization': self._authorization}) return payload