from typing import List
from typing import Dict
from typing import Any
import requests


def pinSearch(
    query: str, pinata_api_key: str, pinata_secret: str
) -> List[Dict[str, Any]]:
    """
    Purpose:
        Query pins for data
    Args:
        query - the query str
        pinata_api_key - pinata api key
        pinata_secret - pinata secret key
    Returns:
        data - array of pined objects
    """

    endpoint_uri = f"https://api.pinata.cloud/data/pinList?{query}"
    HEADERS = {
        "pinata_api_key": pinata_api_key,
        "pinata_secret_api_key": pinata_secret,
    }
    response = requests.get(endpoint_uri, headers=HEADERS).json()

    # now get the actual data from this
    data = []
    if "rows" in response:

        for item in response["rows"]:
            ipfs_pin_hash = item["ipfs_pin_hash"]
            hash_data = requests.get(
                f"https://gateway.pinata.cloud/ipfs/{ipfs_pin_hash}"
            ).json()
            data.append(hash_data)

    # print(response.json())
    return data