Documentation for python-hydra-agent

A GSOC-2018 story

Sandeep Chauhan
Hydra Ecosystem Developers
6 min readJun 9, 2018

--

Hello there! First phase of coding of GSoC 2018 is almost completed. I am working on project “Implement Redis as a datastore” under the Python Hydra organisation. For the first phase of coding my major task was to build a graph structure representing content in a data API and store it in Redis with the help of redisgraph python client. Here is the source code for the graph implementation that I have written in the first phase.

Explaining the code

There are three modules or files (`hydra_graph`, `collections_endpoint`, `classes_objects`) which are responsible for graph structure and its storage in Redis. The file `hydra_graph` is the main file and other two work as modules. First of all we have setup the connection to Redis as `redis_con` and set graph name in Redis “apidoc” (the entrypoint documentation in Hydra is called “ApiDoc”) and use it in file as redis_graph.

Next we pass a url for and Hydra server and we find the API documentation for the given server, then we load its data into the apidoc graph. Internally we get the entrypoint with help of `hydrus.hydaraspec.doc_maker`; this generates a node for the entrypoint in our graph and set its properties like “id”, “url”, and “supportedOperation” as entrypoint_properties in the node.

Now we move to the part get_apistructure. Here we’ll find the two types of endpoints `collection_endpoints` and `class_endpoints` with the help of `hydrus.hydraspec.doc_writer.EntryPointCollection` and `hydrus.hydraspec.doc_writer.EntryPointClass` respectively.

In the above code, we are splitting the endpoints and here we are only storing endpoint’s name and its id. After that we call the functions in their respective modules like for `class_endpoint` where we have to call and pass it in the `classes_objects` module, the same for `collection_endpoint` in `collection_endpoints` module but first we call `class_endpoints` or `classes_objects.ClassEndpoints.endpointclasses()`.

In the code we are creating a node for every endpoint stored in `class_endpoint` and the properties for node are id, type, properties and `property_value`. For every endpoint we are creating a url as new_url which help us in fetching the data from the server endpoint and here we are storing the endpoint data in new_file.
For every endpoint, the `node_properties[“operations”]` part contain all the operation which can be done on that endpoint and for this to work, we have an function `get_operation` which returns all the operations. The `node_properties[“properties”]` part contains a list of all the `supportedProperty` associated with the endpoint as `supported_properties_list and node_properties[“property_value”]` part contains the value also for all those properties which are not an object or endpoint itself. Yes, it can be happen any endpoint can contain another endpoint or non-endpoint object as its `supportedProperty`.

So, for that type of endpoints that are referenced by a property we should have an edge between the endpoints (being careful at the to check that the target endpoint already exist as a node). For this we have a Python dictionary `endpoint_property_list` which keeps track of which endpoint holds which property and after creating all the endpoint nodes we can set an edge between them you can see the code for this here.

If the object of the relation is not an endpoint, we have to create a node for every entity type with its property and `property_value` and connect it with the endpoint. For whole this functionality we have defined a function called `objects_property` .

In this function, we are creating a node for every non-endpoint object property and connect it to its parent endpoint. It is actually a recursive function because if object have again object as property in itself. So, these node can called as terminal node or leaf node for any endpoint.

And now, we have call `collection_endpoints` or `collection_endpoint.CollectionEndpoints.endpointCollection()`.

As you can see in above code, for every endpoint stored in `collection_endpoint` we are creating a node with properties like id, operations, and members. Here you see that members are those endpoints which are contained by the endpoint. In `class_endpoint` there was only an endpoint but here in `collection_endpoint` we have collections of members for every endpoint. We can get the collection of endpoints or members for every endpoint by fetch the data from the server.

Now we have to create a node for every endpoint’s member, for this we have defined an function `collectionobjects`.

For every endpoint in collection, we have to fetch the data from the server endpoint and load it.
At this point we have data for the endpoint and we are using it in the properties of node. Every endpoint node have property for its operations, id, type, properties and `property_value`. It seems like same as class endpoint node’s properties because class endpoint is also only an endpoint object same as endpoint in collection endpoint.
Now we can do a similar operation in class’ endpoint for storing its properties, with the benefit that we have already created nodes for all classes’ endpoints already. If there exists any property endpoint then we can connect the endpoint to property directly without any consideration. That was the reason we call `class_endpoint` before `collection_endpoint`. Code for this is shown below:

Again if endpoint contains a non-endpoint object property then it will pass through the above function objects_property().

This way we can store whole data from the server in Redis in graphical manner. If you want to better understanding you should go thorough source code.

Recap

We store in Redis different types of node:

1- For `entrypoint_node`:

2- For `collection_endpoint_node`:

3- For endpoint part as `collection_endpoint`:

4- For `class_endpoint`: same as for 3

5- For `object_node` :

Example demo for graph structure :-

Let server is http://35.224.198.158:8081/api and the graph generated for this with the help of graphviz is:

graph structure

Here are some other graphs that’s also generated by code for different data.

Thanks! Have a nice day.

--

--