# Path: cfg.py
# API_VERSION = 1
# SKIER_VERSION = 1.5
# 
# Path: skier/pgp.py
# def add_pgp_key(armored: str) -> tuple:
# def get_pgp_armor_key(keyid: str) -> str:
# def get_pgp_keyinfo(keyid: str) -> keyinfo.KeyInfo:
# def search_through_keys(search_str: str, page: int=1, count: int=10) -> Pagination:
# 
# Path: skier/pgpactions.py
# def import_key(keyserver: str, keyid: str):
# def synch_keys():
# def _broadcast_add(key: db.Key):
# def broadcast_add(key: db.Key):


import json
import datetime
import db
from flask import Blueprint, request
from cfg import cfg
from skier import pgp, pgpactions
    # Load the key info.
    key = pgp.get_pgp_keyinfo(keyid)
    # Check if it's none before proceeding
    if not key:
        return "{}", 404, {"Content-Type": "application/json"}
    # Otherwise, return the key info
    return key.to_json(), 200, {"Content-Type": "application/json"}


@pgpapi.route("/addkey", methods=["POST", "PUT"])
def addkey():
    # Read in the key data from the url arguments. Goes like ?keydata=.
    try:
        keydata = request.args["keydata"]
    except KeyError:
        return json.dumps({"error": 1, "msg": "no-key"}), 401, {"Content-Type": "application/json"}
    # Attempt to add the key.
    key = pgp.add_pgp_key(keydata)
    if key[0]:
        pgpactions.broadcast_add(key[2])
        return json.dumps({"error": 0, "msg": key[1]}), 200, {"Content-Type": "application/json"}
    else:
        return json.dumps({"error": key[1], "msg": "invalid-key-data"}), 401, {"Content-Type": "application/json"}


@pgpapi.route("/import/<keyserver>/<keyid>", methods=["POST"])
def importkey(keyserver, keyid):
    if not keyserver in cfg.config.sks_imports:
        if not keyserver in cfg.config.skier_imports:
            return json.dumps({"code": 2, "err": "Invalid keyserver"}), 400, {"Content-Type": "application/json"}

# Path: cfg.py
# API_VERSION = 1
# SKIER_VERSION = 1.5
# 
# Path: skier/pgp.py
# def add_pgp_key(armored: str) -> tuple:
# def get_pgp_armor_key(keyid: str) -> str:
# def get_pgp_keyinfo(keyid: str) -> keyinfo.KeyInfo:
# def search_through_keys(search_str: str, page: int=1, count: int=10) -> Pagination:
# 
# Path: skier/pgpactions.py
# def import_key(keyserver: str, keyid: str):
# def synch_keys():
# def _broadcast_add(key: db.Key):
# def broadcast_add(key: db.Key):


# Path: skier/pgpapi.py
import json
import datetime
import db
from flask import Blueprint, request
from cfg import cfg
from skier import pgp, pgpactions

    # Load the key info.
    key = pgp.get_pgp_keyinfo(keyid)
    # Check if it's none before proceeding
    if not key:
        return "{}", 404, {"Content-Type": "application/json"}
    # Otherwise, return the key info
    return key.to_json(), 200, {"Content-Type": "application/json"}


@pgpapi.route("/addkey", methods=["POST", "PUT"])
def addkey():
    # Read in the key data from the url arguments. Goes like ?keydata=.
    try:
        keydata = request.args["keydata"]
    except KeyError:
        return json.dumps({"error": 1, "msg": "no-key"}), 401, {"Content-Type": "application/json"}
    # Attempt to add the key.
    key = pgp.add_pgp_key(keydata)
    if key[0]:
        pgpactions.broadcast_add(key[2])
        return json.dumps({"error": 0, "msg": key[1]}), 200, {"Content-Type": "application/json"}
    else:
        return json.dumps({"error": key[1], "msg": "invalid-key-data"}), 401, {"Content-Type": "application/json"}


@pgpapi.route("/import/<keyserver>/<keyid>", methods=["POST"])
def importkey(keyserver, keyid):
    if not keyserver in cfg.config.sks_imports:
        if not keyserver in cfg.config.skier_imports:
            return json.dumps({"code": 2, "err": "Invalid keyserver"}), 400, {"Content-Type": "application/json"}

    key = pgpactions.import_key(keyserver=keyserver, keyid=keyid)