import settings
from models import ProtoCredential, Credential, QueryResp
from dataclasses import asdict as to_dict
import requests


class VaultInterface:
    base = f"http://{settings.API_IP}:{settings.API_PORT}"

    def add_credential(self, credentials: ProtoCredential) -> int:
        resp = requests.post(
            self.base + "/add_cred/",
            json=to_dict(credentials)
        )

        return resp.status_code

    def query_credentials(self) -> QueryResp:
        resp = requests.get(
            self.base
        )

        data = [Credential(**i) for i in resp.json()]

        for i in data:
            i.id = str(i.id)

        result = QueryResp(
            data=data,
            status_code=resp.status_code
        )

        return result

    def delete_credentials(self, index: int) -> int:
        resp = requests.post(
            self.base + "/delete_cred/" + "?id=" + index
        )

        return resp.status_code

    def delete_all(self) -> int:
        resp = requests.post(
            self.base + "/delete_all"
        )

        return resp.status_code

import settings
from models import ProtoCredential, Credential, QueryResp
from dataclasses import asdict as to_dict
import requests


class VaultInterface:
    base = f"http://{settings.API_IP}:{settings.API_PORT}"

    def add_credential(self, credentials: ProtoCredential) -> int:
        resp = requests.post(
            self.base + "/add_cred/",
            json=to_dict(credentials)
        )

        return resp.status_code

    def query_credentials(self) -> QueryResp:
        resp = requests.get(
            self.base
        )

        data = [Credential(**i) for i in resp.json()]

        for i in data:
            i.id = str(i.id)

        result = QueryResp(
            data=data,
            status_code=resp.status_code
        )

        return result

    def delete_credentials(self, index: int) -> int:
        resp = requests.post(
            self.base + "/delete_cred/" + "?id=" + index
        )

        return resp.status_code

    def delete_all(self) -> int:
        resp = requests.post(
            self.base + "/delete_all"
        )

        return resp.status_code
