forked from ceramicstudio/orbis-python-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.py
158 lines (129 loc) · 5.03 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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_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_ENDPOINT = os.getenv("ORBIS_ENDPOINT")
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_ENDPOINT, 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_ENDPOINT, headers=headers, json=body)
pprint(response.json())
return response.json()
def update_document(self, document_id, 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,
})
patch =[]
modelInstance = ModelInstanceDocument.load(ceramic_client, stream_id=document_id)
new_doc = modelInstance.content.copy()
for key, value in 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})
# get updated state
new_state = ModelInstanceDocument.load(ceramic_client, stream_id=document_id).content
pprint(f"Stream updated with ID: {document_id}")
return new_state