This is a minimal Python API for the Graph. It works with Python 2 as well as Python 3.
Optionally, create and activate a virtual environment, so that installations are not system-wide:
$ python -m venv <path to venv>
$ source <path to venv>/bin/activate
The Python Graph API can be installed with pip
directly from our git
repository:
$ pip install git+git://git.graph-it.com/graphit/graph-client-py.git
The Graph API uses msgpack. So, the Python binding for it will be installed as a dependency.
In order to use TLS connections to graphs, a client certificate chain
including the private key has to be available in a file.
(Internally,
SSLContext.load_cert_chain()
is used to establish the TLS connection.)
In our current graph installations a chain file with a private key can be
found in /var/lib/graph/var/cert/srv/local.srv.pem
:
$ scp graph.example.com:/var/lib/graph/var/cert/srv/local.srv.pem graph.crt
A graph connection is initialised by giving a URL and (if it is a TLS URL) the filename of the certificate chain to the constructor:
>>> import graph
>>> gc = graph.Connection('tls://graph.example.com:4439', 'graph.crt')
Then all functions of the Graph API can be called on the connection object:
>>> graphmodul_guid = gc.attributsknoten('graphmodul_name', 'graph')
>>> knotens = gc.listeattribute(graphmodul_guid, 'knoten', 'typ, dokumentation, anzahlattribute', 'typ asc')
Observe that all string data returned by the API are bytestrings (even the
keys in *attribute
results) and have to be decoded to be used as Python
strings:
>>> for knoten in knotens.values():
... print("{}: {} ({})".format(knoten[b'typ'].decode(), knoten[b'dokumentation'].decode(), knoten[b'anzahlattribute'].decode()))
...
aktion: <p>Auszuführende Aktion</p>
(5)
aktionfunktion: <p>Auszuführende Funktion, die an einem Knoten hinterlegt werden und von Änderung an lokalen Attributen getriggert werden kann.</p>
(22)
attribut: <p>Allgemeines Merkmal</p>
(16)
[…]
The test.py
script reads the configuration of a main graph from
config.json
, queries this main graph for remote graphs configured in it,
prints a summary of all graph modules in the main graph and all remote
graphs and downloads stationeries in all the graphs if they are configured
there.
Up to now, only a raw connection is implemented, i.e., the Python implementation does not know anything about the actual API calls and just passes them to the graph server.
We should add implementation and documentation for these actual API calls. This would also allow to do at least some of the necessary conversions already in the library.