Skip to content

Commit

Permalink
Adding in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mzkrasner committed Sep 24, 2024
1 parent 316fbc4 commit 557797a
Show file tree
Hide file tree
Showing 60 changed files with 1,434 additions and 7 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _make_raw_genesis(
header["unique"] = b64encode(random_bytes).decode('utf-8')

if metadata_args.context:
header["context"] = bytes(bytearray(list(base36_decode_with_prefix(metadata_args.context)))),
header["context"] = bytes(bytearray(list(base36_decode_with_prefix(metadata_args.context))))


return {"data": content, "header": header}
Expand Down
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "pageview",
"views": {
"issuer": {
"type": "documentAccount"
}
},
"schema": {
"type": "object",
"$defs": {
"DateTime": {
"type": "string",
"title": "DateTime",
"format": "date-time",
"maxLength": 100
}
},
"$schema": "https://json-schema.org/draft/2020-12/schema",
"required": ["page", "address", "timestamp", "customer_user_id"],
"properties": {
"page": {
"type": "string"
},
"address": {
"type": "string"
},
"timestamp": {
"$ref": "#/$defs/DateTime"
},
"customer_user_id": {
"type": "integer"
}
},
"additionalProperties": false
},
"version": "2.0",
"interface": false,
"implements": [],
"accountRelation": {
"type": "list"
},
"immutableFields": []
}
117 changes: 117 additions & 0 deletions examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
from dotenv import load_dotenv
import os
from pprint import pprint
from datetime import datetime, timezone
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ed25519
from ceramic_python.did import DID
from ceramic_python.ceramic_client import CeramicClient
from ceramic_client.ceramic_python.model_instance_document import ModelInstanceDocument, ModelInstanceDocumentMetadataArgs
import requests
from key_did_provider_ed25519.src.key_did_provider_ed25519.utils import encode_did

load_dotenv()

ENV_ID = os.getenv("ENV_ID")
TABLE_ID = os.getenv("TABLE_ID")
CONTEXT_ID = os.getenv("CONTEXT_ID")
AGENT_ONE_SEED = os.getenv("AGENT_ONE_SEED")
AGENT_TWO_SEED = os.getenv("AGENT_TWO_SEED")
AGENT_THREE_SEED = os.getenv("AGENT_THREE_SEED")
CERAMIC_ENDPOINT = os.getenv("CERAMIC_ENDPOINT")

orbis_url = "https://studio.useorbis.com/api/db/query/json"

switcher = {
"agent_one": AGENT_ONE_SEED,
"agent_two": AGENT_TWO_SEED,
"agent_three": AGENT_THREE_SEED
}

class CeramicActions:

def __init__(self, agent):
self.private_key = switcher.get(agent)
self.ed25519_private_key = ed25519.Ed25519PrivateKey.from_private_bytes(bytes.fromhex(self.private_key))
self.ed25519_public_key = self.ed25519_private_key.public_key()
self.did = encode_did(self.ed25519_public_key.public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw
))

def initialize_ceramic(self):
did = DID(
id=self.did,
private_key=self.private_key,
)
ceramic_client = CeramicClient(CERAMIC_ENDPOINT, did)
return ceramic_client

def create_document(self, content):
ceramic_client = self.initialize_ceramic()
pprint(f"Initialized Ceramic client with DID: {self.did}")

metadata_args = ModelInstanceDocumentMetadataArgs(
controller=self.did,
model=TABLE_ID,
context=CONTEXT_ID
)

# Get the current date and time in UTC
current_time = datetime.now(timezone.utc)

# Format it as an ISO 8601 string
formatted_time = current_time.isoformat()

content.update({
# get datetime
"timestamp": formatted_time,
})

doc = ModelInstanceDocument.create(ceramic_client, content, metadata_args)
pprint(f"Stream created with ID: {doc.stream_id}")
return doc

def get_all_documents(self):

body = {
"jsonQuery": {
"$raw": {
"query": f"SELECT * FROM {TABLE_ID}",
"params": []
}
},
"env": ENV_ID
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(url=orbis_url, headers=headers, json=body)
pprint(response.json())
return response.json()

def get_with_filter(self, filter):

filters = []
for (key, value) in filter.items():
filter = f"{key} = {value}"
filters.append(filter)

# Join the filters with 'AND' if there are multiple filters
joined_filters = " AND ".join(filters)

body = {
"jsonQuery": {
"$raw": {
"query": f"SELECT * FROM {TABLE_ID} WHERE {joined_filters}",
"params": []
}
},
"env": ENV_ID
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(url=orbis_url, headers=headers, json=body)
pprint(response.json())
return response.json()
Loading

0 comments on commit 557797a

Please sign in to comment.