-
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
3 changed files
with
38 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
from .gw3 import * | ||
from .multi import * | ||
from .pinata import * | ||
from .public 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,34 @@ | ||
import logging | ||
import requests | ||
|
||
from .types import FetchError, IPFSProvider, PinError, UploadError | ||
from .cid import CIDv0, CIDv1 | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class PublicIPFS(IPFSProvider): | ||
"""Public IPFS gateway (fetch-only provider)""" | ||
|
||
GATEWAY = "https://ipfs.io" | ||
|
||
def __init__(self, *, timeout: int) -> None: | ||
super().__init__() | ||
self.timeout = timeout | ||
|
||
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: | ||
logger.error({"msg": "Request has been failed", "error": str(ex)}) | ||
raise FetchError(cid) from ex | ||
return resp.content | ||
|
||
def upload(self, content: bytes, name: str | None = None) -> CIDv0 | CIDv1: | ||
raise UploadError | ||
|
||
def pin(self, cid: CIDv0 | CIDv1) -> None: | ||
raise PinError(cid) |