from nlp_backend import NlpBackend
import apiai

class ApiaiBackend(NlpBackend):
	def __init__(self):
		CLIENT_ACCESS_TOKEN='31dc1846968d441b950e3a6b15797bff'
		self.ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN)
	def get_intent(self, query, session_id):
		pass
	def make_request(self, query, session_id):
		request = self.ai.text_request()
		request.lang = 'en'		# optional, default value equal 'en'
		request.session_id = session_id
		request.query = query
		response = request.getresponse()
		data = response.read()
		return data


@@ -40,16 +40,30 @@
 rt apiai
+%0Aimport socket
 %0A%0Aclass 
@@ -109,16 +109,115 @@
 (self):%0A
+%09%09# We should separate configurations like this in separate settings file to avoid later headaches%0A
 %09%09CLIENT
@@ -403,16 +403,136 @@
 on_id):%0A
+%09%09if not self._check_connection():%0A%09%09%09raise ConnectionError(%22No internet connection, please connect to the internet.%22)%0A%0A
 %09%09reques
@@ -736,16 +736,16 @@
 .read()%0A
-
 %09%09return
@@ -751,8 +751,459 @@
 n data%0A%0A
+%09def _check_connection(self,host=%228.8.8.8%22, port=53, timeout=3):%0A%09%09# Try to call host by ip address to prevent DNS lookup%0A%09%09sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)%0A%09%09sock.settimeout(timeout)%0A%09%09try:%0A%09%09%09sock.connect((host, port))%0A%09%09%09return True%0A%09%09except:%0A%09%09%09# Try to call host by domain name in case ip address is no longer valid%0A%09%09%09try:%0A%09%09%09%09host = %22google.com%22%0A%09%09%09%09port = 80%0A%09%09%09%09sock.connect((host, port))%0A%09%09%09except:%0A%09%09%09%09return False%0A%0A

add check internet connection method to ApiaiBackend module