Skip to content

Commit c6ccf76

Browse files
authored
point to new stroage service (#3)
1 parent aa7c34e commit c6ccf76

File tree

4 files changed

+174
-118
lines changed

4 files changed

+174
-118
lines changed

refiner/config.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,14 @@ class Settings(BaseSettings):
4040
description="Dialect of the schema"
4141
)
4242

43-
# Optional, required if using https://pinata.cloud (IPFS pinning service)
44-
# PINATA_API_KEY: Optional[str] = Field(
45-
# default=None,
46-
# description="Pinata API key"
47-
# )
48-
49-
# PINATA_API_SECRET: Optional[str] = Field(
50-
# default=None,
51-
# description="Pinata API secret"
52-
# )
53-
54-
PINATA_API_JWT: Optional[str] = Field(
43+
# Relay upload URL for offchain storage (provided by the relay service)
44+
RELAY_UPLOAD_URL: str = Field(
5545
default="",
56-
description="Pinata API JWT"
57-
)
58-
59-
IPFS_GATEWAY_URL: str = Field(
60-
default="https://dfusion-social-lens.mypinata.cloud/ipfs",
61-
description="IPFS gateway URL for accessing uploaded files"
46+
description="URL to upload refined data through the relay service"
6247
)
6348

6449
class Config:
6550
env_file = ".env"
6651
case_sensitive = True
6752

68-
settings = Settings()
53+
settings = Settings()

refiner/refine.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from refiner.transformer.webapp_transformer import WebappTransformer
99
from refiner.config import settings
1010
from refiner.utils.encrypt import encrypt_file
11-
from refiner.utils.ipfs import upload_file_to_ipfs, upload_json_to_ipfs
11+
from refiner.utils.storage import upload_file_to_storage, upload_json_to_storage
1212

1313
class Refiner:
1414
def __init__(self):
@@ -56,18 +56,19 @@ def transform(self) -> Output:
5656
)
5757
output.schema = schema
5858

59-
# Upload the schema to IPFS
59+
# Upload the schema to storage
6060
schema_file = os.path.join(settings.OUTPUT_DIR, 'schema.json')
6161
with open(schema_file, 'w') as f:
6262
json.dump(schema.model_dump(), f, indent=4)
63-
schema_ipfs_hash = upload_json_to_ipfs(schema.model_dump())
64-
logging.info(f"Schema uploaded to IPFS with hash: {schema_ipfs_hash}")
63+
schema_result = upload_json_to_storage(schema.model_dump())
64+
logging.info(f"Schema uploaded to storage - hash: {schema_result['hash'][:16]}...")
6565

66-
# Encrypt and upload the database to IPFS
66+
# Encrypt and upload the database to storage
6767
encrypted_path = encrypt_file(settings.REFINEMENT_ENCRYPTION_KEY, self.db_path)
68-
ipfs_hash = upload_file_to_ipfs(encrypted_path)
69-
output.refinement_url = f"{settings.IPFS_GATEWAY_URL}/{ipfs_hash}"
68+
upload_result = upload_file_to_storage(encrypted_path)
69+
output.refinement_url = upload_result['url']
70+
logging.info(f"Encrypted database uploaded - URL: {output.refinement_url}")
7071
continue
7172

7273
logging.info("Data transformation completed successfully")
73-
return output
74+
return output

refiner/utils/ipfs.py

Lines changed: 21 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,21 @@
1-
import json
2-
import logging
3-
import os
4-
import requests
5-
from refiner.config import settings
6-
7-
PINATA_FILE_API_ENDPOINT = "https://api.pinata.cloud/pinning/pinFileToIPFS"
8-
PINATA_JSON_API_ENDPOINT = "https://api.pinata.cloud/pinning/pinJSONToIPFS"
9-
10-
def upload_json_to_ipfs(data):
11-
"""
12-
Uploads JSON data to IPFS using Pinata API.
13-
Reference: https://pinata-cloud.readme.io/reference/post_pinning-pinjsontoipfs
14-
:param data: JSON data to upload (dictionary or list)
15-
:return: IPFS hash
16-
"""
17-
if not settings.PINATA_API_JWT:
18-
raise Exception("Error: Pinata IPFS API credentials not found, please check your environment variables")
19-
20-
headers = {
21-
"accept": "application/json",
22-
"content-type": "application/json",
23-
"authorization": f"Bearer {settings.PINATA_API_JWT}"
24-
}
25-
26-
try:
27-
response = requests.post(
28-
PINATA_JSON_API_ENDPOINT,
29-
data=json.dumps(data),
30-
headers=headers
31-
)
32-
response.raise_for_status()
33-
34-
result = response.json()
35-
logging.info(f"Successfully uploaded JSON to IPFS with hash: {result['IpfsHash']}")
36-
return result['IpfsHash']
37-
38-
except requests.exceptions.RequestException as e:
39-
logging.error(f"An error occurred while uploading JSON to IPFS: {e}")
40-
raise e
41-
42-
def upload_file_to_ipfs(file_path=None):
43-
"""
44-
Uploads a file to IPFS using Pinata API (https://pinata.cloud/).
45-
Reference: https://pinata-cloud.readme.io/reference/post_pinning-pinfiletoipfs
46-
:param file_path: Path to the file to upload (defaults to encrypted database)
47-
:return: IPFS hash
48-
"""
49-
if file_path is None:
50-
# Default to the encrypted database file
51-
file_path = os.path.join(settings.OUTPUT_DIR, "db.libsql.pgp")
52-
53-
if not os.path.exists(file_path):
54-
raise FileNotFoundError(f"File not found: {file_path}")
55-
56-
if not settings.PINATA_API_JWT:
57-
raise Exception("Error: Pinata IPFS API credentials not found, please check your environment variables")
58-
59-
headers = {
60-
"authorization": f"Bearer {settings.PINATA_API_JWT}"
61-
}
62-
63-
try:
64-
with open(file_path, 'rb') as file:
65-
files = {
66-
'file': file
67-
}
68-
response = requests.post(
69-
PINATA_FILE_API_ENDPOINT,
70-
files=files,
71-
headers=headers
72-
)
73-
74-
response.raise_for_status()
75-
result = response.json()
76-
logging.info(f"Successfully uploaded file to IPFS with hash: {result['IpfsHash']}")
77-
return result['IpfsHash']
78-
79-
except requests.exceptions.RequestException as e:
80-
logging.error(f"An error occurred while uploading file to IPFS: {e}")
81-
raise e
82-
83-
# Test with: python -m refiner.utils.ipfs
84-
if __name__ == "__main__":
85-
ipfs_hash = upload_file_to_ipfs()
86-
print(f"File uploaded to IPFS with hash: {ipfs_hash}")
87-
print(f"Access at: {settings.IPFS_GATEWAY_URL}/{ipfs_hash}")
88-
89-
ipfs_hash = upload_json_to_ipfs()
90-
print(f"JSON uploaded to IPFS with hash: {ipfs_hash}")
91-
print(f"Access at: {settings.IPFS_GATEWAY_URL}/{ipfs_hash}")
1+
"""
2+
IPFS utilities - now uses relay-based offchain storage.
3+
4+
This module is kept for backward compatibility.
5+
All functions now use the relay upload URL instead of direct Pinata access.
6+
"""
7+
8+
# Re-export from storage module for backward compatibility
9+
from refiner.utils.storage import (
10+
upload_file_to_ipfs,
11+
upload_json_to_ipfs,
12+
upload_file_to_storage,
13+
upload_json_to_storage,
14+
)
15+
16+
__all__ = [
17+
'upload_file_to_ipfs',
18+
'upload_json_to_ipfs',
19+
'upload_file_to_storage',
20+
'upload_json_to_storage',
21+
]

refiner/utils/storage.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
"""
2+
Offchain storage utilities for uploading refined data.
3+
4+
Uploads go through the relay service which handles authentication
5+
and forwards to the offchain storage service.
6+
"""
7+
import json
8+
import logging
9+
import os
10+
import requests
11+
from refiner.config import settings
12+
13+
14+
def upload_file_to_storage(file_path=None):
15+
"""
16+
Uploads a file to offchain storage via the relay service.
17+
18+
:param file_path: Path to the file to upload (defaults to encrypted database)
19+
:return: Dict with hash and url from storage service
20+
"""
21+
if file_path is None:
22+
# Default to the encrypted database file
23+
file_path = os.path.join(settings.OUTPUT_DIR, "db.libsql.pgp")
24+
25+
if not os.path.exists(file_path):
26+
raise FileNotFoundError(f"File not found: {file_path}")
27+
28+
if not settings.RELAY_UPLOAD_URL:
29+
raise Exception(
30+
"Error: RELAY_UPLOAD_URL not configured. "
31+
"This should be provided by the relay service via env_vars."
32+
)
33+
34+
try:
35+
with open(file_path, 'rb') as file:
36+
files = {
37+
'file': (os.path.basename(file_path), file, 'application/octet-stream')
38+
}
39+
response = requests.post(
40+
settings.RELAY_UPLOAD_URL,
41+
files=files
42+
)
43+
44+
response.raise_for_status()
45+
result = response.json()
46+
47+
# Response format: { url, ipfsHash (which is now SHA-256 hash), size }
48+
content_hash = result.get('ipfsHash') or result.get('hash')
49+
url = result.get('url')
50+
51+
logging.info(f"Successfully uploaded file to storage - hash: {content_hash[:16]}...")
52+
53+
return {
54+
'hash': content_hash,
55+
'url': url,
56+
'size': result.get('size', os.path.getsize(file_path))
57+
}
58+
59+
except requests.exceptions.RequestException as e:
60+
logging.error(f"An error occurred while uploading file to storage: {e}")
61+
raise e
62+
63+
64+
def upload_json_to_storage(data):
65+
"""
66+
Uploads JSON data to offchain storage via the relay service.
67+
68+
:param data: JSON data to upload (dictionary or list)
69+
:return: Dict with hash and url from storage service
70+
"""
71+
if not settings.RELAY_UPLOAD_URL:
72+
raise Exception(
73+
"Error: RELAY_UPLOAD_URL not configured. "
74+
"This should be provided by the relay service via env_vars."
75+
)
76+
77+
try:
78+
# Convert JSON to bytes
79+
json_bytes = json.dumps(data).encode('utf-8')
80+
81+
files = {
82+
'file': ('data.json', json_bytes, 'application/json')
83+
}
84+
response = requests.post(
85+
settings.RELAY_UPLOAD_URL,
86+
files=files
87+
)
88+
89+
response.raise_for_status()
90+
result = response.json()
91+
92+
content_hash = result.get('ipfsHash') or result.get('hash')
93+
url = result.get('url')
94+
95+
logging.info(f"Successfully uploaded JSON to storage - hash: {content_hash[:16]}...")
96+
97+
return {
98+
'hash': content_hash,
99+
'url': url,
100+
'size': result.get('size', len(json_bytes))
101+
}
102+
103+
except requests.exceptions.RequestException as e:
104+
logging.error(f"An error occurred while uploading JSON to storage: {e}")
105+
raise e
106+
107+
108+
# Backward compatibility aliases
109+
def upload_file_to_ipfs(file_path=None):
110+
"""
111+
Backward compatible alias for upload_file_to_storage.
112+
Returns just the hash string for compatibility with existing code.
113+
"""
114+
result = upload_file_to_storage(file_path)
115+
return result['hash']
116+
117+
118+
def upload_json_to_ipfs(data):
119+
"""
120+
Backward compatible alias for upload_json_to_storage.
121+
Returns just the hash string for compatibility with existing code.
122+
"""
123+
result = upload_json_to_storage(data)
124+
return result['hash']
125+
126+
127+
# Test with: python -m refiner.utils.storage
128+
if __name__ == "__main__":
129+
# Test file upload
130+
test_file = os.path.join(settings.OUTPUT_DIR, "db.libsql.pgp")
131+
if os.path.exists(test_file):
132+
result = upload_file_to_storage(test_file)
133+
print(f"File uploaded - hash: {result['hash']}")
134+
print(f"URL: {result['url']}")
135+
136+
# Test JSON upload
137+
test_json = {"test": "data", "timestamp": "2024-01-01"}
138+
result = upload_json_to_storage(test_json)
139+
print(f"JSON uploaded - hash: {result['hash']}")
140+
print(f"URL: {result['url']}")

0 commit comments

Comments
 (0)