# 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
    # Pass the search string to the database.
    results = pgp.search_through_keys(search_str, page=page, count=count)
    # Serialize the data into a JSON list of key ids.
    # Yes, this can hammer the server on shitty clients when they search.
    # But redis can pick up a lot of that slack.
    return json.dumps(
        {"count": results.total,
         "ids": [item.key_fp_id for item in results.items]}
    ), 200 if results.total else 404, {"Content-Type": "application/json"}


@pgpapi.route("/keyinfo/<keyid>", methods=["GET"])
def get_pgpapi_keyinfo(keyid):
    # 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.

# 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

    # Pass the search string to the database.
    results = pgp.search_through_keys(search_str, page=page, count=count)
    # Serialize the data into a JSON list of key ids.
    # Yes, this can hammer the server on shitty clients when they search.
    # But redis can pick up a lot of that slack.
    return json.dumps(
        {"count": results.total,
         "ids": [item.key_fp_id for item in results.items]}
    ), 200 if results.total else 404, {"Content-Type": "application/json"}


@pgpapi.route("/keyinfo/<keyid>", methods=["GET"])
def get_pgpapi_keyinfo(keyid):
    # 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)