Skip to content

Commit

Permalink
Proxy support for embeddings
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Aug 24, 2020
1 parent a813a04 commit 7bf1848
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Orange/misc/server_embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

from Orange.misc.utils.embedder_utils import (EmbedderCache,
EmbeddingCancelledException,
EmbeddingConnectionError)
EmbeddingConnectionError,
get_proxies)

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -150,7 +151,7 @@ async def embedd_batch(
"""
requests = []
async with AsyncClient(
timeout=self.timeout, base_url=self.server_url
timeout=self.timeout, base_url=self.server_url, proxies=get_proxies()
) as client:
for p in data:
if self._cancelled:
Expand Down
25 changes: 25 additions & 0 deletions Orange/misc/utils/embedder_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import logging
import hashlib
import pickle
from os import environ
from os.path import join, isfile
from typing import Optional, Dict

from Orange.canvas.config import cache_dir

Expand Down Expand Up @@ -72,3 +74,26 @@ def get_cached_result_or_none(self, cache_key):

def add(self, cache_key, value):
self._cache_dict[cache_key] = value


def get_proxies() -> Optional[Dict[str, str]]:
"""
Return dict with proxy addresses if they exists.
Returns
-------
proxy_dict
Dictionary with format {proxy type: proxy address} or None if
they not set.
"""
def add_protocol(url: Optional[str], prot: str) -> Optional[str]:
if url and not url.startswith(prot):
return f"{prot}://{url}"
return url
http_proxy = add_protocol(environ.get("http_proxy"), "http")
https_proxy = add_protocol(environ.get("https_proxy"), "https")
if http_proxy and https_proxy: # both proxy addresses defined
return {"http://": https_proxy, "https://": https_proxy}
elif any([https_proxy, http_proxy]): # one of the proxies defined
return {"all://": http_proxy or https_proxy}
return None # proxies not defined

0 comments on commit 7bf1848

Please sign in to comment.