--- title: Pod Client keywords: fastai sidebar: home_sidebar nb_path: "nbs/pod.client.ipynb" ---
Pymemri communicates with the pod via the PodClient
. The PodClient requires you to provide a database key and an owner key. During development, you don't have to worry about these keys, you can just omit the keys when initializing the PodClient
, which creates a new user by defining random keys. Note that this will create a new database for your every time you create a PodClient, if you want to access the same database with multiple PodClients, you have to set the same keys When you are using the app, setting the keys in the pod, and passing them when calling an integrator is handled for you by the app itself.
client = PodClient()
success = client.test_connection()
assert success
Now that we have access to the pod, we can create items here and upload them to the pod. All items are defined in the schema of the pod. When Initializing an Item, always make sure to use the from_data classmethod to initialize.
email_item = EmailMessage.from_data(content="example content field")
email_item
succes = client.add_to_schema(email_item)
assert succes
email_item = EmailMessage.from_data(content="example content field")
client.create(email_item)
We can easily define our own types, and use them in the pod.
class Dog(Item):
def __init__(self, name, age, id=None, deleted=None):
super().__init__(id=id, deleted=deleted)
self.name = name
self.age = age
@classmethod
def from_json(cls, json):
id = json.get("id", None)
name = json.get("name", None)
age = json.get("age", None)
return cls(id=id,name=name,age=age)
dog = Dog("max", 2)
client.add_to_schema(dog);
dog2 = Dog("bob", 3)
client.create(dog2);
dog_from_db = client.get(dog2.id, expanded=False)
We can connect items using edges. Let's create another item, a person, and connect the email and the person.
person_item = Person.from_data(firstName="Alice", lastName="X")
succes = client.add_to_schema(person_item)
assert succes
person_item = Person.from_data(firstName="Alice", lastName="X")
item_succes = client.create(person_item)
edge = Edge(email_item, person_item, "sender")
edge_succes = client.create_edge(edge)
assert item_succes and edge_succes
client.get_edges(email_item.id)
If we use the normal client.get
(without expanded=False
), we also get items directly connected to the Item.
email_from_db = client.get(email_item.id)
assert isinstance(email_from_db.sender[0], Person)
We can use the client to fetch data from the database. This is in particular useful for indexers, which often use data in the database as input for their models. The simplest form of querying the database is by querying items in the pod by their id (unique identifier).
person_item = Person.from_data(firstName="Alice")
assert client.create(person_item)
person_from_db = client.get(person_item.id, expanded=False)
assert person_from_db is not None
assert person_from_db == person_item
assert person_from_db.id is not None
Appart from creating, we might want to update existing items:
person_item.lastName = "Awesome"
client.update_item(person_item)
person_from_db = client.get(person_item.id, expanded=False)
assert person_from_db.lastName == "Awesome"
When we don't know the ids of the items we want to fetch, we can also search by property. We can use this for instance when we want to query all items from a particular type to perform some indexing on. We can get all Person
Items from the db by:
person_item2 = Person.from_data(firstName="Bob")
client.create(person_item2);
all_people = client.search({"type": "Person"})
assert all([isinstance(p, Person) for p in all_people]) and len(all_people) > 0
all_people[:3]
person_item2 = Person.from_data(firstName="Last Person")
client.create(person_item2);
assert client.search_last_added(type="Person").firstName == "Last Person"
In the near future, Pod will support searching by user defined properties as well. This will allow for the following. warning, this is currently not supported
client.search_last_added(type="Person", with_prop="ImportedBy", with_val="EmailImporter")
To work with files, the PodClient
has a file api. The file api works by posting a blob to the upload_file
endpoint, and creating an Item with a property with the same sha256 as the sha used in the endpoint.
from pymemri.data.photo import *
x = np.random.randint(0, 255+1, size=(640, 640), dtype=np.uint8)
photo = IPhoto.from_np(x)
file = photo.file[0]
succes = client.create(file)
succes2 = client._upload_image(x)
assert succes
assert succes2
data = client.get_file(file.sha256)
arr = np.frombuffer(data, dtype=np.uint8)
assert (arr.reshape(640,640) == x).all()
For photos we do this automatically using PodClient.create
on a Photo and PodClient.get_photo
:
x = np.random.randint(0, 255+1, size=(640, 640), dtype=np.uint8)
photo = IPhoto.from_np(x)
succes = client.add_to_schema(IPhoto.from_np(x))
assert client.create(photo)
res = client.get_photo(photo.id, size=640)
res
assert (res.data == x).all()
Not supported yet by the new PodAPI