from flask import Flask, send_from_directory, abort, request
import json
import requests
import os
app = Flask(__name__)

def pretty_print_POST(req):
    """
    At this point it is completely built and ready
    to be fired; it is "prepared".

    However pay attention at the formatting used in 
    this function because it is programmed to be pretty 
    printed and may differ from the actual request.
    """
    print('{}\n{}\r\n{}\r\n\r\n{}'.format(
        '-----------START-----------',
        req.method + ' ' + req.url,
        '\r\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
        req.body,
    ))

FILES_DIR = os.path.join(os.path.dirname(__file__), "files")
@app.route('/snipe_api')
def home():
    return "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiMmU3YmJhZTEwZjM1NThhNTQ2MzZmNTRiYjhiNzQ1NmRhNDRiZDAyNWJlZTNmZjFkZTAxZTI3NzZiNjEwODQ5MDRkNGRkYjE3Njg5ZDYyMjYiLCJpYXQiOjE3MzA1NTQxMzguMTAwMzAyLCJuYmYiOjE3MzA1NTQxMzguMTAwMzE0LCJleHAiOjIzNjE3MDYxMzcuOTkwMzQ0LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.rFVnJo5E-UHW-Z4fQ639xBXq9zroAGmPQSkm1Z6iVir-8NAm-AJy-_UZLH32IG9_aT9CGJZc6_hyCRisEhm2PIAvQ0ejUzZb8atNfD3bhMEp4DwjNZtsonO6xP8Mi4YXuscWuyxNAlg7svm53bAl3mop_S2jeOEQ5DM_RhjpYxy1VMvs3diiN7W6U-WDgznMl9FLQ7TFHQ8w8g3zCbIrI5PEeKn05525sQcvgonlo1hjEe5L2gcUCV5dvXnZHnyCaZcrCcuhmZM0Xy4_ZiK5EtFvKKm4BFprnis1pT9xEdv5QzRk-IJYrFkIcAZzha7sVGShln8HzyhTE27oq1wsUgBKY4J_VQhyc4343_n3eofAOrTSRbnsMNjZE0Vy7wUVw8snOVoz56T_AYa7IKtBuv0CBGS2XLVNERer-rkJtPLTVGPC9Ck0vKfO202klRlkPgfl_HeYMIDxBwDwY6dZSoNPPG1hPSTFFv74zkSz3AsxyKmD5QOp90tuclvpINTxghRhbpHSBl1TV4XRY9rbmQ9Wo40A7XRsSUGrh-SDErLp6J5Taa9rXrxaIVs5oFZA8ehaRcZlZRm-uiJXbgd7gHhWSNdqnZ1kbPGJwsXvGI8JTAL04LSd3moWyx3TCoP2yCLAdAqPoOe35RK76IWSpZ1Et-yKzYwPMHlsKiYLjCA"

@app.route('/download_late_command')
def download_file():
    try:
        return send_from_directory(FILES_DIR, "late_command.sh", as_attachment=True)
    except FileNotFoundError:
        abort(404, description="File not found")

@app.route('/preseed.cfg')
def download_pre():
    try:
        return send_from_directory(FILES_DIR, "preseed.cfg", as_attachment=True)
    except FileNotFoundError:
        abort(404, description="File not found")

@app.route('/print', methods=['POST'])
def print_label():
    url = "http://10.200.4.12:8000/label"
    data = request.get_json()
    label = requests.post(url,data=json.dumps(data)).content
    print(label)
    return "{Success}"

if __name__ == '__main__':
    app.run(debug=True)
