# coding=utf-8
from embeddings.embedding import Embedding
import requests
import json


class Liandongembedding(Embedding):
    def __init__(self, **param):
        self.url = param['api_url']
        self.cache = {}

    def getem(self, question):
        if question in self.cache.keys():
            return self.cache['question']
        headers = {
            'Content-Type': 'application/json',
            'Content-Length': '<calculated when request is sent>'
            }
        data = {
            "text": question
            }
        result = requests.post(self.url,
                               headers=headers,
                               data=json.dumps(data))
        em = json.loads(result.content)['data']
        self.cache['question'] = em
        return em
