Skip to content

Instantly share code, notes, and snippets.

@predic8
Created March 2, 2020 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save predic8/55c137ebc2407941e2ae7e0943542bca to your computer and use it in GitHub Desktop.
Save predic8/55c137ebc2407941e2ae7e0943542bca to your computer and use it in GitHub Desktop.
Event Store Demo Dateien
import requests
from requests.auth import HTTPBasicAuth
import logging
import json
from uuid import uuid4
from time import sleep
headers= { "Accept": "application/vnd.eventstore.events+json" }
auth=HTTPBasicAuth('changeit', 'changeit');
def link(data, rel):
try:
return [ l["uri"] for l in data["links"] if l["relation"] == rel][0]
except:
return None
uri = "http://127.0.0.1:2113/streams/verkaeufe?embed=body"
etag = ""
while(True):
headers["If-None-Match"] = etag
res = requests.get( uri, auth=auth, headers=headers)
if res.status_code == 304:
print("Nichts!")
sleep(2)
continue
etag = res.headers.get("ETag")
data = json.loads(res.text)
print("Empfangen: ", len(data["entries"]))
for e in data["entries"][::-1]:
v = json.loads(e["data"])
print('{}: {} {} zu je {} €'.format(e["title"],v['menge'],v['name'], v['preis']))
previous = link(data, 'previous')
if previous:
uri = previous + "?embed=body"

###

GET http://127.0.0.1:2113/streams/verkaeufe/0 Accept: application/json

###

POST http://127.0.0.1:2113/streams/verkaeufe ES-EventType: Verkauf ES-EventId: {{$guid}} Content-Type: application/json

{

"name": "Fett Bemme", "preis": 2.59, "menge": 2

}

###

GET http://127.0.0.1:2113/streams/verkaeufe Accept: application/vnd.eventstore.events+json If-None-Match: "82;-1733724312"

### Subscription erzeugen

POST http://127.0.0.1:2113/subscriptions/verkaeufe/lager Content-Type: application/json

{

"messageTimeoutMilliseconds": 60000

}

### Subscription abfragen

GET http://127.0.0.1:2113/subscriptions/verkaeufe/lager Accept: application/vnd.eventstore.competingatom+json

### ACK

POST http://127.0.0.1:2113/subscriptions/verkaeufe/lager/ack/695cf8c3-6fb6-4c52-ba55-7d60ba16f1b8 Accept: application/json

###

POST http://127.0.0.1:2113/streams/verkaeufe Content-Type: application/vnd.eventstore.events+json

[
{

"eventId": "{{$guid}}", "eventType": "Verkauf", "data": { "name": "Dauerlutscher", "preis": 0.79, "menge": 3 }

}

]

### Batch

POST http://127.0.0.1:2113/streams/verkaeufe Content-Type: application/vnd.eventstore.events+json

[
{

"eventId": "{{$guid}}", "eventType": "Verkauf", "data": { "name": "Müsli", "preis": 3.79, "menge": 1 }

},
{

"eventId": "{{$guid}}", "eventType": "Verkauf", "data": { "name": "Würstchen", "preis": 5.30, "menge": 3 }

},
{

"eventId": "{{$guid}}", "eventType": "Verkauf", "data": { "name": "Taschentücher", "preis": 1.79, "menge": 10 }

}

]

import requests
from requests.auth import HTTPBasicAuth
import logging
import json
from uuid import uuid4
from random import random, randint, choice
waren = [ 'Milch','Bananen','Äpfel','Birnen','Brot','Tomaten','Käse','Gurken','Zitrone']
auth=HTTPBasicAuth('changeit', 'changeit');
headers= { "Content-Type": "application/vnd.eventstore.events+json" }
for i in range(1,1000):
events = []
for j in range(1,1000):
events.append({
"eventId" : str(uuid4()),
"eventType" : "Verkauf",
"data" : {
"name": choice(waren),
"preis": round(random() * 10,2),
"menge": randint(1,101),
}
})
requests.post( 'http://127.0.0.1:2113/streams/verkaeufe', data=json.dumps(events), auth=auth, headers=headers)
import requests
from requests.auth import HTTPBasicAuth
import logging
import json
from uuid import uuid4
from random import random, randint, choice
waren = [ 'Milch','Bananen','Äpfel','Birnen','Brot','Tomaten','Käse','Gurken','Zitrone']
auth=HTTPBasicAuth('changeit', 'changeit');
headers= { "Content-Type": "application/vnd.eventstore.events+json" }
for i in range(1,81):
event = [{
"eventId" : str(uuid4()),
"eventType" : "Verkauf",
"data" : {
"name": choice(waren),
"preis": round(random() * 10,2),
"menge": randint(1,101),
}
}]
requests.post( 'http://127.0.0.1:2113/streams/verkaeufe', data=json.dumps(event), auth=auth, headers=headers)
import requests
from requests.auth import HTTPBasicAuth
import json
from time import sleep
def link(data, rel):
try:
return [ l["uri"] for l in data["links"] if l["relation"] == rel][0]
except:
return None
headers= { "Accept": "application/vnd.eventstore.competingatom+json" }
auth=HTTPBasicAuth('changeit', 'changeit')
while(True):
res = requests.get( "http://127.0.0.1:2113/subscriptions/verkaeufe/lager?embed=body", auth=auth, headers=headers)
entries = json.loads(res.text)["entries"]
for e in entries:
v = json.loads(e["data"])
print('{}: {} {} zu je {} €'.format(e["title"],v['menge'],v['name'], v['preis']))
requests.post( link(e,'ack'), auth=auth, headers={ "Accept": "application/json" })
sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment