<commit_before>#! /usr/bin/python
'''
    Restful API for Multi-thread Cache system
    LICENSE MIT @2017 <NAME> <<EMAIL>>
'''

__author__ = "ilausuch"
__date__ = "$16-jun-2017 22:31:10$"

import sys
import json
import falcon
import logging

from falcon_cors import CORS

from Addons.Client import Client

from APICore.Entity import Entity
from APICore.Bank import Bank
from APICore.Cache import Cache

logging.basicConfig(level=logging.DEBUG)

# By default values
SERVER_IP = "127.0.0.1"
SEVER_PORT = 10001

'''
Load configuration
'''
try:
    f = open('config.json')
    data = f.read()

    try:
        cfg = json.loads(data)
        try:
            SERVER_IP = cfg["CACHE_SERVER_IP"]
            SEVER_PORT = cfg["CACHE_SERVER_PORT"]
            API_PATH = cfg["API_PATH"]

        except:
            print ("Required CACHE_SERVER_IP, CACHE_SERVER_PORT API_PATH in config.json")
            sys.exit(1)
    except:
        print ("config.json must be a json")
        sys.exit(1)
except:
    print ("Requies the config.json file")
    sys.exit(1)

'''
Create a client
'''
client = Client(SERVER_IP, SEVER_PORT)

'''
API
'''


def sendJson(resp, data):
    # resp.headers['Content-Type'] = "application/json"
    resp.body = json.dumps(data)


class index:
    def on_get(self, req, resp):
        resp.body = "Cache system API"


class entry:
    def on_get(self, req, resp, **params):
        try:
            bank = params["bank"]
            key = params["key"]
        except Exception:
            raise falcon.HTTPBadRequest('Invalid request',
                                        "bank and key are required parameters")

        try:
            result = Entity(client, bank, key).get()
            js = json.loads(result)
        except Exception:
            raise falcon.HTTPServiceUnavailable('Service Outage',
                                                'Internal error', 30)
        else:
            if js["status"] == 'error':
                raise falcon.HTTPBadRequest('Cache Server error message',
                                            js["message"])

            sendJson(resp, js)

    def on_post(self, req, resp, **params):
        try:
            bank = params["bank"]
            key = params["key"]
        except Exception:
            raise falcon.HTTPBadRequest('Invalid request',
                                        "bank and key are required parameters")

        try:
            result = Entity(client, bank, key).post(req.stream.read())
            js = json.loads(result)
        except Exception:
            raise falcon.HTTPServiceUnavailable('Service Outage',
                                                'Internal error', 30)
        else:
            if js["status"] == 'error':
                raise falcon.HTTPBadRequest('Cache Server error message',
                                            js["message"])

            sendJson(resp, js)


class entries:
    def on_get(self, req, resp, **params):
        try:
            bank = params["bank"]
        except Exception:
            raise falcon.HTTPBadRequest('Invalid request',
                                        "bank is a required parameters")

        try:
            result = Bank(client, bank).getKeys()
            js = json.loads(result)
        except Exception:
            raise falcon.HTTPServiceUnavailable('Service Outage',
                                                'Internal error', 30)<commit_msg>Force conversion from UTF8 to ascii.<commit_after>#! /usr/bin/python
'''
    Restful API for Multi-thread Cache system
    LICENSE MIT @2017 <NAME> <<EMAIL>>
'''

__author__ = "ilausuch"
__date__ = "$16-jun-2017 22:31:10$"

import sys
import json
import falcon
import logging

from falcon_cors import CORS

from Addons.Client import Client

from APICore.Entity import Entity
from APICore.Bank import Bank
from APICore.Cache import Cache

logging.basicConfig(level=logging.DEBUG)

# By default values
SERVER_IP = "127.0.0.1"
SEVER_PORT = 10001

'''
Load configuration
'''
try:
    f = open('config.json')
    data = f.read()

    try:
        cfg = json.loads(data)
        try:
            SERVER_IP = cfg["CACHE_SERVER_IP"]
            SEVER_PORT = cfg["CACHE_SERVER_PORT"]
            API_PATH = cfg["API_PATH"]

        except:
            print ("Required CACHE_SERVER_IP, CACHE_SERVER_PORT API_PATH in config.json")
            sys.exit(1)
    except:
        print ("config.json must be a json")
        sys.exit(1)
except:
    print ("Requies the config.json file")
    sys.exit(1)

'''
Create a client
'''
client = Client(SERVER_IP, SEVER_PORT)

'''
API
'''


def sendJson(resp, data):
    # resp.headers['Content-Type'] = "application/json"
    resp.body = json.dumps(data)


class index:
    def on_get(self, req, resp):
        resp.body = "Cache system API"


class entry:
    def on_get(self, req, resp, **params):
        try:
            bank = params["bank"]
            key = params["key"]
        except Exception:
            raise falcon.HTTPBadRequest('Invalid request',
                                        "bank and key are required parameters")

        try:
            result = Entity(client, bank, key).get()
            js = json.loads(result)
        except Exception:
            raise falcon.HTTPServiceUnavailable('Service Outage',
                                                'Internal error', 30)
        else:
            if js["status"] == 'error':
                raise falcon.HTTPBadRequest('Cache Server error message',
                                            js["message"])

            sendJson(resp, js)

    def on_post(self, req, resp, **params):
        try:
            bank = params["bank"]
            key = params["key"]
        except Exception:
            raise falcon.HTTPBadRequest('Invalid request',
                                        "bank and key are required parameters")

        try:
            result = Entity(client, bank, key).post(req.stream.read().decode('UTF-8'))
            js = json.loads(result)
        except Exception:
            raise falcon.HTTPServiceUnavailable('Service Outage',
                                                'Internal error', 30)
        else:
            if js["status"] == 'error':
                raise falcon.HTTPBadRequest('Cache Server error message',
                                            js["message"])

            sendJson(resp, js)


class entries:
    def on_get(self, req, resp, **params):
        try:
            bank = params["bank"]
        except Exception:
            raise falcon.HTTPBadRequest('Invalid request',
                                        "bank is a required parameters")

        try:
            result = Bank(client, bank).getKeys()
            js = json.loads(result)
        except Exception:
            raise falcon.HTTPServiceUnavailable('Service Outage',
                                                'Internal error', 30)
{'fraction_non_alphanumeric': 0.10534659172791469, 'fraction_numerical': 0.010664360858913388, 'mean_word_length': 1.720501764014112, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5844878', 'n_tokens_mistral': 1886, 'n_tokens_neox': 1820, 'n_words': 506}