Skip to content

Phontan/EventStore.PythonClientAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 

Repository files navigation

EventStore.PythonClientAPI

Simple client for EventStore(https://github.com/EventStore/EventStore).

Short description

СlientAPI is a python http client for Event Store. It contains the necessary Event Store options, allows you to feel flexibility of EventStore. You can easy create, delete streams, write, read events forward and backward, from special stream or from all streams. Also ClientAPI supports projections. Just use property projections in ClientAPI and you can easily post, get, enable, disable projection etc.

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.

Installation

Now you can use ClientAPI. You can check if it works by typing from ClientAPI.EventStoreClient import *. If there are no errors everything is fine.

Implementation

To implement ClientAPI we chose http protocol. We use http tornado client as one of the fastest python http libs. We have sync and async modes for almost all methods. Async methods' callbacks will not be triggered unless you wait for them explicitly by calling method wait() from ClientAPI. This method will block main thread, to release it call resume() in one of your callbacks.

Functionality description

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.

Hello World

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: 

Demo application - Сhat

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages