--- 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='v2', database_key=None, owner_key=None)

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

Pyintegrators communicate 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. 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 memri schema. When the schema is changed it automatically generates all the class definitions for the different languages used in memri, the python schema file lives in schema.py in the integrators package. 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 %}
success = client.create(email_item)
assert success
email_item
EmailMessage (#1)
{% 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")
item_succes = client.create(person_item)

edge = Edge(person_item, email_item, "author")
edge_succes = client.create_edge(edge)

assert item_succes and edge_succes
edge
Person (#2) --author-> EmailMessage (#1)
{% endraw %}

Fetching and updating Items

We can use the client to fetch data from the database. This is in particular usefull 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 uid (unique identifier).

{% raw %}
person_item = Person.from_data(firstName="Alice")
client.create(person_item)
person_from_db = client.get(person_item.uid)
assert person_from_db is not None
assert person_from_db == person_item
person_from_db
Person (#3)
{% 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.uid)
assert person_from_db.lastName == "Awesome"
person_from_db
Person (#3)
{% endraw %}

Sometimes, we might not know the uids of the items we want to fetch. We can also search by a certain property. We can use this for instance when we want to query all items from a particular type to perform some indexing on.

{% raw %}
person_item2 = Person.from_data(firstName="Bob")
client.create(person_item2);
all_people = client.search_by_fields({"_type": "Person"})

assert all([isinstance(p, Person) for p in all_people]) and len(all_people) > 0

all_people[:3]
[Person (#2), Person (#3), Person (#4)]
{% endraw %}

Uploading & downloading files

The file API is currently only tested for images.

{% raw %}
from pyintegrators.indexers.facerecognition.photo import *
{% endraw %} {% raw %}
x = np.random.randint(0, 255+1, size=(640, 640), dtype=np.uint8)
photo = IPhoto.from_np(x)
{% endraw %} {% raw %}
assert client.create(photo)
{% endraw %} {% raw %}
res = client.get_photo(photo.uid, size=640)
{% endraw %} {% raw %}
assert (res.data == x).all()
{% endraw %}

Check if an item exists

{% raw %}
person_item = Person.from_data(firstName="Eve",   externalId="gmail_1")
person_item2 = Person.from_data(firstName="Eve2", externalId="gmail_1")

client.create_if_external_id_not_exists(person_item)
client.create_if_external_id_not_exists(person_item2)

existing = client.search_by_fields({"externalId": "gmail_1"})
assert len(existing) == 1
client.delete_all()
{% endraw %}

Resetting the db

{% raw %}
client.delete_all()
{% endraw %}