--- title: Pod Client keywords: fastai sidebar: home_sidebar nb_path: "nbs/pod.client.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}

class PodClient[source]

PodClient(url='http://localhost:3030', version='v3', database_key=None, owner_key=None)

{% endraw %} {% raw %}
{% endraw %}

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.

{% raw %}
client = PodClient()
success = client.test_connection()
assert success
Succesfully connected to pod
{% endraw %}

Creating Items and Edges

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.

{% raw %}
email_item = EmailMessage.from_data(content="example content field")
email_item
EmailMessage (#None)
{% endraw %} {% raw %}
succes = client.add_to_schema(email_item)
assert succes
{% endraw %} {% raw %}
email_item = EmailMessage.from_data(content="example content field")
client.create(email_item)
True
{% endraw %}

We can easily define our own types, and use them in the pod.

{% raw %}
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)
{% endraw %} {% raw %}
dog = Dog("max", 2)
client.add_to_schema(dog);
dog2 = Dog("bob", 3)
client.create(dog2);
{% endraw %} {% raw %}
dog_from_db = client.get(dog2.id, expanded=False)
{% endraw %}

We can connect items using edges. Let's create another item, a person, and connect the email and the person.

{% raw %}
person_item = Person.from_data(firstName="Alice", lastName="X")
succes = client.add_to_schema(person_item)
assert succes
{% endraw %} {% raw %}
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
{% endraw %} {% raw %}
client.get_edges(email_item.id)
[{'item': Person (#a63b11ebe6996b2efb00b75c42ccd930), 'name': 'sender'}]
{% endraw %}

If we use the normal client.get (without expanded=False), we also get items directly connected to the Item.

{% raw %}
email_from_db = client.get(email_item.id) 
{% endraw %} {% raw %}
assert isinstance(email_from_db.sender[0], Person)
{% endraw %}

Fetching and updating Items

Normal Items

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).

{% raw %}
person_item = Person.from_data(firstName="Alice")
assert client.create(person_item)
{% endraw %} {% raw %}
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
{% endraw %}

Appart from creating, we might want to update existing items:

{% raw %}
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"
{% endraw %}

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:

{% raw %}
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 (#a63b11ebe6996b2efb00b75c42ccd930),
 Person (#16560d3dd6550a6aa58d88405fab177a),
 Person (#fa5e4f210ec5b928e7eb263fcabf252f)]
{% endraw %}

Search last added items

{% raw %}
person_item2 = Person.from_data(firstName="Last Person")
client.create(person_item2);
{% endraw %} {% raw %}
assert client.search_last_added(type="Person").firstName == "Last Person"
{% endraw %}

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")

Uploading & downloading files

File API

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.

{% raw %}
from pymemri.data.photo import *
{% endraw %} {% raw %}
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
{% endraw %} {% raw %}
data = client.get_file(file.sha256)
arr = np.frombuffer(data, dtype=np.uint8)
assert (arr.reshape(640,640) == x).all()
{% endraw %}

Photo API

For photos we do this automatically using PodClient.create on a Photo and PodClient.get_photo:

{% raw %}
x = np.random.randint(0, 255+1, size=(640, 640), dtype=np.uint8)
photo = IPhoto.from_np(x)
{% endraw %} {% raw %}
succes = client.add_to_schema(IPhoto.from_np(x))
{% endraw %} {% raw %}
assert client.create(photo)
creating
creating photo file
creating
Uploaded file
{% endraw %} {% raw %}
res = client.get_photo(photo.id, size=640)
{% endraw %} {% raw %}
res
IPhoto (#e49db579f6427af57651fc3ca5efafcf)
{% endraw %} {% raw %}
assert (res.data == x).all()
{% endraw %}

Check if an item exists

Not supported yet by the new PodAPI

Resetting the db

{% raw %}
 
{% endraw %}