<commit_before>#! /usr/bin/python3
# -*- coding: utf-8 -*-

#
#   Description : Server 
#
#
#from sqlalchemy import create_engine
#from sqlalchemy.orm import sessionmaker
from flask import Flask, request, jsonify, Response
#from flask_cors import CORS
#from db_schema import *
import json
import datetime
import time
import siphash
import binascii

__author__ = '<NAME>'
__author_email__ = '<EMAIL>'

__version__ = "1.0.1"
__copyright__ = ""
__licence__ = "GPL"
__status__ = ""



key = b'<KEY>'

key = b'<KEY>'

app = Flask(__name__)


@app.route("/drinks/api/sync", methods=['GET'])
def sync():
    """ return drinks catalog


    :return: JSON request tous les capteurs de la classe
    """
    sip = siphash.SipHash_2_4(key)

    now = round(time.time()/60)
    catalog = ['Coca 1.50' , 'Sprite 1.50'] 

    request = {'Header': "Hello World", 'Products': catalog, 'Time': now}

    hash_str = request['Header'] + "".join(catalog) + now.__str__()
    for c in hash_str:
        sip.update(binascii.a2b_qp(c))

    request['Hash'] = hex(sip.hash())[2:].upper()
    
    return json.dumps(request)


@app.route("/drinks/api/buy", methods=['POST'])
def buy():
    """ buy request
    

    :return: JSON request tous les capteurs de la classe
    """
    sip = siphash.SipHash_2_4(key)

    now = round(time.time()/60)
#    if request._headers['Content-Type'] == 'application/json':
    dict_req = request.get_json()

    badge = dict_req['Badge']
    product = dict_req['Product']
    time_req = dict_req['Time']
    hash_req = dict_req['Hash']

    print(badge + " " + product + " " + time_req + " " + hash_req)

# TODO : verify incoming hash

# TODO : compute proper outgoig hash
    hash_str =  now.__str__()
    for c in hash_str:
        sip.update(binascii.a2b_qp(c))

    ret = {'Melody': "Une melody", 'Message': ['message1', 'message2'], 'Time': now}
    
    ret['Hash'] = hex(sip.hash())[2:].upper()

    return json.dumps(ret)


if __name__ == "__main__":
    app.run(
        host='0.0.0.0',
        port=int('5000')
    )
<commit_msg>added database support for catalog and transaction. Money is not deduced from account yet<commit_after>#! /usr/bin/python3
# -*- coding: utf-8 -*-

#
#   Description : Server 
#
#
import json
import datetime
import time
import siphash
import binascii
import os
import sys
import datetime
from flask import Flask, request, jsonify, Response
from sqlalchemy import Column, ForeignKey, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from drinkingBuddyDB_declarative import Base, Category, Inventory, User, Transaction

__author__ = '<NAME>'
__author_email__ = '<EMAIL>'

__version__ = "1.0.1"
__copyright__ = ""
__licence__ = "GPL"
__status__ = ""

engine = create_engine('sqlite:///drinkingBuddy.db')

Base.metadata.bind = engine

DBSession = sessionmaker(bind=engine)

session = DBSession()


key = b'<KEY>'

app = Flask(__name__)


@app.route("/DrinkingBuddy/sync", methods=['GET'])
def sync():
    """ return drinks catalog


    :return: JSON request tous les capteurs de la classe
    """
    sip = siphash.SipHash_2_4(key)

    now = round(time.time()/60)
    elements = session.query(Inventory, Inventory.name, Inventory.price).all()
    catalog = [e.name + " " + "{:.2f}".format(e.price/100) for e in elements]

    request = {'Header': "DrinkingBuddy", 'Products': catalog, 'Time': now}

    hash_str = request['Header'] + "".join(catalog) + now.__str__()
    for c in hash_str:
        sip.update(binascii.a2b_qp(c))

    request['Hash'] = hex(sip.hash())[2:].upper()
    
    return json.dumps(request)


@app.route("/DrinkingBuddy/buy", methods=['POST'])
def buy():
    """ buy request
    

    :return: JSON request tous les capteurs de la classe
    """
    sipout = siphash.SipHash_2_4(key)
    sipin = siphash.SipHash_2_4(key)

    now = round(time.time()/60)

    dict_req = request.get_json()

    badge = dict_req['Badge']
    product = dict_req['Product']
    time_req = dict_req['Time']

    hash_verif = badge + str(product) + str(time_req)
    for c in hash_verif:
        sipin.update(binascii.a2b_qp(c))

    if dict_req['Hash'] == hex(sipin.hash())[2:].upper():
        print("Cool hash's OK")
    else:
        print("Pas Cool !!!!!!!!!!")
    print(badge + " " + product + " " + time_req + " " + dict_req['Hash'])

    new_transaction = Transaction(date = datetime.datetime.now(), value = 1, user = session.query(User).filter(User.id == int(badge,16)).one(), element = session.query(Inventory).filter(Inventory.id == int(product)).one())
    session.add(new_transaction)
    session.commit()

    ret = {'Melody': "Une melody", 'Message': ['Successfull transaction', 'Have a nice day'], 'Time': now.__str__()}
    
    hash_str = ret['Melody'] + "".join(ret['Message']) + now.__str__()
    for c in hash_str:
        sipout.update(binascii.a2b_qp(c))

    ret['Hash'] = hex(sipout.hash())[2:].upper()

    return json.dumps(ret)

@app.route("/DrinkingBuddy/balance", methods=['POST'])
def getBalance():
    """ Get balance request
    

    :return: JSON request tous les capteurs de la classe
    """
    sipout = siphash.SipHash_2_4(key)
    sipin = siphash.SipHash_2_4(key)

    now = round(time.time()/60)

#    if request._headers['Content-Type'] == 'application/json':
    dict_req = request.get_json()

    badge = dict_req['Badge']
    time_req = dict_req['Time']

    hash_verif = badge + time_req
    for c in hash_verif:
        sipin.update(binascii.a2b_qp(c))

    if dict_req['Hash'] == hex(sipin.hash())[2:].upper():
        print("Cool hash's OK")
    else:
        print("Pas Cool !!!!!!!!!!")
   
    badgeId = int(badge, 16)
    element = session.query(User, User.name, User.balance).filter(User.id == badgeId)
    if element.count() == 0:
        messages = ['Unknown User', '0.00']
    else:
        messages = [element.one().name, "{:.2f}".format(element.one().balance/100)]

    ret = {'Melody': "Une melody", 'Message': messages, 'Time': now}
    
    hash_str = ret['Melody'] + "".join(messages) + now.__str__()
    for c in hash_str:
        sipout.update(binascii.a2b_qp(c))

    ret['Hash'] = hex(sipout.hash())[2:].upper()

    return json.dumps(ret)

if __name__ == "__main__":
    app.run(
        host='0.0.0.0',
        port=int('5000')
    )

{'fraction_non_alphanumeric': 0.16330991412958626, 'fraction_numerical': 0.013895394223263077, 'mean_word_length': 3.4301521438450897, 'pattern_counts': {'":': 2, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1626203', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2225, 'n_words': 502}