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

class PodClient[source]

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

{% endraw %}

We communicate with the pod with the PodClient. The PodClient requires us to provide a database key and an owner key. You don't have to worry about these keys: when you run an Integrator from a memri client, this goes via the pod, which provides these keys for you. For testing purposes, we can make our own keys.

{% raw %}
client = PodClient(database_key="0" * 64, owner_key="1" * 64)
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 (#917040)
{% 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 (#917041) --author-> EmailMessage (#917040)
{% 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 (#917042)
{% 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 (#917042)
{% 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 (#32002), Person (#32004), Person (#32005)]
{% endraw %}