import ldclient

def clientFactory(context):
	server_base = context.getGlobalProperties().get("Live Design Server").getValue().getString()
	username = context.getGlobalProperties().get("Live Design Username").getValue().getString()
	password = context.getGlobalProperties().get("Live Design Password").getValue().getString()	

	verifySSL = True
	try:
		verifySSLConf = context.getGlobalProperties().get('/discngine/ldconn/VerifySSL').getValue().getString()
		if verifySSLConf is not None and verifySSLConf != 'True':
			verifySSL = False
	except:
		pass
	
	jsessionid = '';
	
	try:
		jsessionid = context.getGlobalProperties().get('ldsession').getValue().getString()
	except:
		pass
	
	client = None
	if jsessionid != '' and jsessionid is not None and jsessionid != 'none':
		client = ldclient.LDClient(server_base + '/livedesign/api', session_id=jsessionid, verify_ssl=verifySSL)
	elif username is not None and username != '' and password is not None and password != '':
		client = ldclient.LDClient(server_base + '/livedesign/api', username, password, verify_ssl=verifySSL)
	else:
		raise Exception('Specify either Live Design Username/Password or ldsession')
	
	client.ping()
    
	return client