-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ceramicstudio/olas/wrapper
Olas/wrapper
- Loading branch information
Showing
21 changed files
with
344 additions
and
255 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
venv | ||
__pycache__ | ||
.DS_Store | ||
dist | ||
build |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .orbis_python.orbis_db import OrbisDB | ||
from .ceramic_python.ceramic_client import CeramicClient |
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,3 @@ | ||
from .ceramic_client import CeramicClient | ||
from .did import DID | ||
from .model_instance_document import ModelInstanceDocument, ModelInstanceDocumentMetadata, ModelInstanceDocumentMetadataArgs |
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
6 changes: 2 additions & 4 deletions
6
ceramic_client/ceramic_python/helper.py → ceramicsdk/ceramic_python/helper.py
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
File renamed without changes.
File renamed without changes.
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,29 @@ | ||
from orbis_python.orbis_db import OrbisDB | ||
import os | ||
|
||
CONTEXT_ID = os.getenv("CONTEXT_ID") | ||
|
||
# Setup a table stream and a private key for the DID | ||
table_stream = "kjzl6hvfrbw6c6adsnzvbyr6itmf0igfy25xu0mqzei2pe2xw1hlusqyuknb9ky" | ||
did_pkey = os.urandom(32).hex() | ||
|
||
# Instantiate a read-only db | ||
db = OrbisDB.from_stream(table_stream) | ||
|
||
# Read the whole db | ||
print(db.read()) | ||
|
||
# Instantiate a read and write db | ||
db = OrbisDB(context_stream=CONTEXT_ID, table_stream=table_stream, controller_private_key=did_pkey) | ||
|
||
# Add a new row | ||
db.add_row({"user_id": 2, "user_name": "test_user_3", "user_points": 1000}) | ||
|
||
# Select some rows | ||
print(db.filter({"user_points": 1000})) | ||
|
||
# Update a row batch | ||
db.update_rows(filters={"user_name": "test_user_3"}, new_content={"user_points": 2000}) | ||
|
||
# Dump the db to a local json file | ||
db.dump() |
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 @@ | ||
from .orbis_db import OrbisDB |
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,148 @@ | ||
from ceramicsdk.ceramic_python.did import DID | ||
from ceramicsdk.ceramic_python.ceramic_client import CeramicClient | ||
from ceramicsdk.ceramic_python.model_instance_document import ModelInstanceDocument, ModelInstanceDocumentMetadataArgs | ||
import requests | ||
from typing import Optional | ||
from pathlib import Path | ||
import json | ||
from ceramicsdk.ceramic_python.model_instance_document import ModelInstanceDocument, ModelInstanceDocumentMetadataArgs | ||
|
||
class OrbisDB: | ||
"""A relational database stored on OrbisDB/Ceramic""" | ||
|
||
def __init__( | ||
self, | ||
c_endpoint: str, | ||
o_endpoint: str, | ||
context_stream: Optional[str] = None, | ||
table_stream: Optional[str] = None, | ||
controller_private_key: Optional[str] = None | ||
) -> None: | ||
|
||
if not table_stream and not controller_private_key: | ||
raise ValueError("Either the table stream or the controller needs to be specified when instantiating an OrbisDB class") | ||
self.o_endpoint = o_endpoint | ||
self.context_stream = context_stream | ||
self.table_stream = table_stream | ||
self.controller = DID(private_key=controller_private_key) | ||
self.ceramic_client = CeramicClient(c_endpoint, self.controller if self.controller else "") | ||
|
||
|
||
@classmethod | ||
def from_stream(cls, table_stream: Optional[str] = None): | ||
"""Load a read-only db from a stream""" | ||
return cls( | ||
context_stream=None, | ||
table_stream=table_stream, | ||
controller_private_key=None | ||
) | ||
|
||
|
||
def read(self, env_id: str): | ||
"""Read the db from Ceramic""" | ||
if not self.table_stream: | ||
raise ValueError("OrbisDB table stream has not being specified. Cannot read the database.") | ||
return self.query(env_id, f"SELECT * FROM {self.table_stream}") | ||
|
||
|
||
def dump(self, file_path: Path = Path("orbis_db.json")): | ||
"""Dump to json""" | ||
table = self.read() | ||
with open(file_path, "w", encoding="utf-8") as file: | ||
json.dump(table, file, indent=4) | ||
|
||
|
||
def add_row(self, entry_data): | ||
"""Add a new row to the table""" | ||
|
||
if not self.controller: | ||
raise ValueError("Read-only database. OrbisDB controller has not being specified. Cannot write to the database.") | ||
|
||
metadata_args = ModelInstanceDocumentMetadataArgs( | ||
controller=self.controller.public_key, | ||
model=self.table_stream, | ||
context=self.context_stream | ||
) | ||
|
||
doc = ModelInstanceDocument.create(self.ceramic_client, entry_data, metadata_args) | ||
return doc.stream_id | ||
|
||
|
||
def update_rows(self, filters, new_content: dict): | ||
"""Update rows""" | ||
|
||
if not self.controller: | ||
raise ValueError("Read-only database. OrbisDB controller has not being specified. Cannot write to the database.") | ||
|
||
document_ids = [row["stream_id"] for row in self.filter(filters)] | ||
|
||
metadata_args = ModelInstanceDocumentMetadataArgs( | ||
controller=self.controller.public_key, | ||
model=self.table_stream, | ||
context=self.context_stream, | ||
) | ||
|
||
for document_id in document_ids: | ||
patch = [] | ||
|
||
modelInstance = ModelInstanceDocument.load(self.ceramic_client, stream_id=document_id) | ||
new_doc = modelInstance.content.copy() | ||
|
||
for key, value in new_content.items(): | ||
new_doc[key] = value | ||
patch.append({ | ||
"op": "replace", | ||
"path": f"/{key}", | ||
"value": value | ||
}) | ||
|
||
modelInstance.patch(json_patch=patch, metadata_args=metadata_args, opts={'anchor': True, 'publish': True, 'sync': 0}) | ||
|
||
return len(document_ids) | ||
|
||
|
||
def query(self, env_id: str, query: str): | ||
"""Query the database | ||
Example: SELECT * FROM {TABLE_ID} | ||
""" | ||
|
||
body = { | ||
"jsonQuery": { | ||
"$raw": { | ||
"query": query, | ||
"params": [] | ||
} | ||
}, | ||
"env": env_id | ||
} | ||
headers = { | ||
"Content-Type": "application/json" | ||
} | ||
response = requests.post(url=self.o_endpoint, headers=headers, json=body) | ||
return response.json()["data"] | ||
|
||
|
||
def filter(self, env_id: str, filters): | ||
"""Filter""" | ||
|
||
filter_list = [f'{key} = {f"'{value}'" if isinstance(value, str) else value}' for key, value in filters.items()] # strings need to be wrapped around single quotes | ||
joined_filters = " AND ".join(filter_list) | ||
query = f"SELECT * FROM {self.table_stream} WHERE {joined_filters}" | ||
|
||
body = { | ||
"jsonQuery": { | ||
"$raw": { | ||
"query": query, | ||
"params": [] | ||
} | ||
}, | ||
"env": env_id | ||
} | ||
headers = { | ||
"Content-Type": "application/json" | ||
} | ||
response = requests.post(url=self.o_endpoint, headers=headers, json=body) | ||
return response.json().get("data", []) | ||
|
||
|
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 |
---|---|---|
|
@@ -4,15 +4,14 @@ | |
long_description = fh.read() | ||
|
||
setup( | ||
name="ceramic_python", | ||
version="0.1.7", | ||
author='Index', | ||
author_email='[email protected]', | ||
description="This Ceramic client implements the payload building, encoding, and signing needed to interact with the Ceramic Network. It currently supports ModelInstanceDocument.", | ||
name="ceramicsdk", | ||
version="0.1.0", | ||
author='Ceramic Ecosystem Developers', | ||
description="This Ceramic client implements the payload building, encoding, and signing needed to interact with the Ceramic Network. It currently supports ModelInstanceDocument and OrbisDB.", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/indexnetwork/ceramic-python/tree/main/ceramic-client", | ||
packages=find_packages(), | ||
url="https://github.com/ceramicstudio/orbis-python-starter/tree/main/py_lib", | ||
packages=find_packages(include=['ceramicsdk', 'ceramicsdk.*']), | ||
classifiers=[ | ||
"Development Status :: 3 - Alpha", | ||
"Intended Audience :: Developers", | ||
|
@@ -24,7 +23,7 @@ | |
], | ||
python_requires=">=3.7", | ||
install_requires=[ | ||
"requests==2.32.2", | ||
"requests==2.31.0", | ||
"python-dateutil==2.8.2", | ||
"pytz==2023.3", | ||
"jsonpatch==1.33", | ||
|
@@ -33,5 +32,9 @@ | |
"multiformats==0.3.1", | ||
"dag-cbor==0.3.2", | ||
"base58==2.1.1", | ||
"web3==7.2.0", | ||
"cbor2==5.6.4", | ||
"bip44==0.1.4", | ||
"varint", | ||
], | ||
) |
Oops, something went wrong.