Skip to content

Commit

Permalink
Switches IPFS get to Pinata
Browse files Browse the repository at this point in the history
  • Loading branch information
kgrofelnik committed Apr 3, 2024
1 parent 529aa87 commit b94df77
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions oracles/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SERVE_METRICS = os.getenv("SERVE_METRICS", "False").lower() == "true"
NFT_STORAGE_API_KEY = os.getenv("NFT_STORAGE_API_KEY")
BEARLY_API_KEY = os.getenv("BEARLY_API_KEY")
PINATA_GATEWAY_TOKEN = os.getenv("PINATA_GATEWAY_TOKEN")

KNOWLEDGE_BASE_MAX_SIZE_BYTES = int(
os.getenv("KNOWLEDGE_BASE_MAX_SIZE_BYTES", 10485760)
Expand Down
12 changes: 9 additions & 3 deletions oracles/src/repositories/ipfs_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import settings
from typing import Union

NFT_STORAGE_LINK_BASE = "https://ipfs.io/ipfs/{}"
PINATA_LINK_BASE = "https://galadriel.mypinata.cloud/ipfs/{}"


class IpfsRepository:
async def read_file(self, cid: str, max_bytes: int = 0) -> bytes:
async with aiohttp.ClientSession() as session:
async with session.get(NFT_STORAGE_LINK_BASE.format(cid)) as response:
headers = {
"x-pinata-gateway-token": settings.PINATA_GATEWAY_TOKEN
}
async with session.get(PINATA_LINK_BASE.format(cid)) as response:
response.raise_for_status()
data = bytearray()
while True:
Expand Down Expand Up @@ -45,6 +48,9 @@ async def write_file(self, data: Union[str, bytes]) -> str:

async def main():
ipfs = IpfsRepository()
print(await ipfs.write_file(bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])))
cid = await ipfs.write_file(bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
assert cid is not None
data = await ipfs.read_file(cid)
assert data == bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

asyncio.run(main())

0 comments on commit b94df77

Please sign in to comment.