Skip to content

Commit

Permalink
connect behind proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Yitzhak Andrade committed Sep 27, 2017
1 parent d07e292 commit 494f24b
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions python3/luis_sdk/luis_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from urllib.parse import quote
import http.client
from .luis_response import LUISResponse
import os
import re

class LUISClient:
'''
Expand Down Expand Up @@ -90,14 +92,27 @@ def predict(self, text, response_handlers=None, daemon=False):
else:
return self.predict_async(text, response_handlers, daemon)

def _get_proxy(self):
proxy = os.getenv('https_proxy') or os.getenv('http_proxy')
return re.sub(r'https?://', '', proxy).split(':')

def _create_connection(self):
proxy = self._get_proxy()
if proxy:
conn = http.client.HTTPSConnection(proxy[0], proxy[1])
conn.set_tunnel(self._LUISURL)
else:
conn = http.client.HTTPSConnection(self._LUISURL)
return conn

def predict_sync(self, text):
'''
Predicts synchronously and returns a LUISResponse.
:param text: The text to be analysed and predicted.
:return: A LUISResponse object containing the response data.
'''
try:
conn = http.client.HTTPSConnection(self._LUISURL)
conn = self._create_connection()
conn.request('GET', self._predict_url_gen(text))
res = conn.getresponse()
return LUISResponse(res.read().decode('UTF-8'))
Expand Down Expand Up @@ -181,7 +196,7 @@ def reply_sync(self, text, response, force_set_parameter_name):
:return: A LUISResponse object containg the response data.
'''
try:
conn = http.client.HTTPSConnection(self._LUISURL)
conn = self._create_connection()
conn.request('GET', self._reply_url_gen(text, response, force_set_parameter_name))
res = conn.getresponse()
return LUISResponse(res.read().decode('UTF-8'))
Expand Down

1 comment on commit 494f24b

@ZackStone
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.