Simple client for EventStore(https://github.com/EventStore/EventStore).
- Short description
- Installation
- Implementation
- Functionality description
- Hello World
- Demo application - Сhat
Current API is wrtten for Python 2.7 and tested on Windows 7 x64 and Ubuntu Linux x64, but should work fine everywhere where Python 2.7 is supported.
- Make sure you have Python 2.7 installed on your machine (or install it from here http://www.python.org/getit/ )
- Install tornado web server (http://pypi.python.org/pypi/tornado)
- Download PythonClientAPI zip from github (http://github.com/Phontan/EventStore.PythonClientAPI)
- Unpackage it and open console in this folder as admin(sudo).
- Run this code in python intepreter setup.py install
As mentioned above, we have sync and async modes. If your operation is successful, sync mode returns to you some answer, and async mode calls your on_success callback. If your operation failed, sync mode throws an exception, and async calls your on_failed callback. If you pass some not expected arguments both modes throw error.
All methods discribed below have also asyncronous mode: method name ends with _async, and have two additional arguments(on_success and on_failed). These two arguments should be functions with one argument, or lambdas.
In all methods stream_id is a string, expected_version, event_number, start_position and count are integers, prepare_position and commit_position are long integers
To create stream in Event Store use create_stream(stream_id, metadata="")
metadata can be of any type
To delete stream from Event Store use delete_stream(stream_id, expected_version=-2)
To append events to some stream use append_to_stream(stream_id, events, expected_version=-2)
events is either an instanse of class Event.WriteEvent, or list of such instances.
To create an instance of class WriteEvent use
WriteEvent(data, metadata="", event_id = None, event_type=None, is_json = False)
event_id should a GUID. By default event_id is a random GUID and event_type is python's type of data argument.
When reading events from Event Store you receive instace(s) of class ReadEvent class which has fields data, metadata, event_type and event_number.
To read one event use read_event(stream_id, event_number).
If operation succeeded, this method returns instance of ReadEvent class.
To read stream events use one of methods:
read_stream_events_backward(stream_id, start_position, count)
read_stream_events_forward(stream_id, start_position, count)
These methods return you list of ReadEvent objects.
To read from all streams use
read_all_events_backward(prepare_position, commit_position, count)
read_all_events_forward(prepare_position, commit_position, count)
These methods return you object, with fields prepare_position, commit_position and events,
where events is a list of ReadEvent objects.
To subscribe on some stream use following:
subscribe(stream_id, callback, start_from_begining = False)
unsubscribe(stream_id)
First method subscribes on some stream, and second unsubscribes. Also you can subscribe on many streams.
To subscribe on all Event Store changes use:
subscribe_all(self, callback, start_from_begining = False)
subscribe_all()
You can only one time subscribe on all.
Make sure you are running compatible version of Event Store on default address(127.0.0.1:2113) and run following code:
from ClientAPI.EventStoreClient import * from ClientAPI.Event import * client = Client("127.0.0.1", 2113) stream_id = "some_stream" client.create_stream(stream_id) client.append_to_stream(stream_id, WriteEvent("hello, Event Store!")) events = client.read_stream_events_backward(stream_id, 5, 100) for event in events: print "Type: {0}, Data: {1}".format(event.event_type, event.data) client.delete_stream(stream_id)
After running this code you should receive output similar to
Type: <type 'str' >, Data: hello, Event Store! Type: $stream-created, Data:For wider demonstration of this API you can look at small chat application. Take a look at EventStorePythonClientAPI/Demo/chat-with-pojections.py for implementation with projections. First run file start-chat-with-pojections.py(it creates projection), and than you can use chat-with-pojections.py as a chat based on Event Store.