-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
from .dummy import * | ||
from .gw3 import * | ||
from .multi import * | ||
from .pinata import * | ||
from .types import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import requests | ||
|
||
from .cid import CIDv0, CIDv1, is_cid_v0 | ||
from .types import FetchError, IPFSProvider, PinError, UploadError | ||
|
||
|
||
class Pinata(IPFSProvider): | ||
"""pinata.cloud IPFS provider""" | ||
|
||
API_ENDPOINT = "https://api.pinata.cloud" | ||
GATEWAY = "https://gateway.pinata.cloud" | ||
|
||
def __init__(self, jwt_token: str, *, timeout: int) -> None: | ||
super().__init__() | ||
self.timeout = timeout | ||
self.session = requests.Session() | ||
self.session.headers["Authorization"] = f"Bearer {jwt_token}" | ||
|
||
def fetch(self, cid: CIDv0 | CIDv1) -> bytes: | ||
url = f"{self.GATEWAY}/ipfs/{cid}" | ||
try: | ||
resp = requests.get(url, timeout=self.timeout) | ||
resp.raise_for_status() | ||
except requests.RequestException as ex: | ||
raise FetchError(cid) from ex | ||
return resp.content | ||
|
||
def upload(self, content: bytes, name: str | None = None) -> CIDv0 | CIDv1: | ||
url = f"{self.API_ENDPOINT}/pinning/pinFileToIPFS" | ||
try: | ||
with self.session as s: | ||
resp = s.post(url, files={"file": content}) | ||
resp.raise_for_status() | ||
except requests.RequestException as ex: | ||
raise UploadError from ex | ||
cid = resp.json()["IpfsHash"] | ||
return CIDv0(cid) if is_cid_v0(cid) else CIDv1(cid) | ||
|
||
def pin(self, cid: CIDv0 | CIDv1) -> None: | ||
url = f"{self.API_ENDPOINT}/pinning/pinByHash" | ||
try: | ||
with self.session as s: | ||
resp = s.post(url, json={"hashToPin": str(cid)}) | ||
resp.raise_for_status() | ||
except requests.RequestException as ex: | ||
raise PinError(cid) from ex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters