Skip to content

Commit 2b2e81c

Browse files
committed
add sendjson example
1 parent 043f444 commit 2b2e81c

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

examples/example.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name": "bob", "count": 10}

examples/sendjson.py

+59-7
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,74 @@
22
file as an event"""
33
from __future__ import print_function
44

5+
import os
56
import sys
67
import json
8+
import argparse
9+
10+
SAMPLE_DIR = os.path.dirname(__file__)
11+
BASE_DIR = os.path.join(SAMPLE_DIR, '..', 'src')
12+
sys.path.append(BASE_DIR)
713

814
import eventfabric as ef
15+
import requests
16+
17+
def parse_args():
18+
"""parse and return command line arguments"""
19+
parser = argparse.ArgumentParser(description='Process some integers.')
20+
21+
parser.add_argument('--username', '-u', metavar='USERNAME',
22+
required=True,
23+
help='Username to authenticate to Event Fabric')
24+
parser.add_argument('--password', '-p', metavar='PASSWORD',
25+
required=True,
26+
help='Password to authenticate to Event Fabric')
27+
parser.add_argument('--channel', '-c', metavar='CHANNEL',
28+
required=True,
29+
help='Channel used in the generated event')
30+
parser.add_argument('--path', '-P', metavar='PATH',
31+
required=True,
32+
help='Path to the JSON file you want to send')
33+
parser.add_argument('--url', '-U', metavar='URL',
34+
help='URL for Event Fabric API',
35+
default="https://event-fabric.com/ef/api/")
36+
37+
return parser.parse_args()
938

1039
def main():
11-
username, password, channel, path = sys.argv[1:]
40+
"""main program entry point"""
41+
args = parse_args()
42+
43+
print("Config:", args.username, args.channel, args.path, args.url)
44+
45+
client = ef.Client(args.username, args.password, args.url)
46+
47+
try:
48+
login_ok, login_response = client.login()
49+
print("Login:", login_ok)
50+
51+
if not login_ok:
52+
print("Error authenticating", login_response)
53+
return
54+
55+
value = None
56+
try:
57+
value = json.load(open(args.path))
58+
except IOError as error:
59+
print("Error opening file", args.path, str(error))
60+
return
1261

13-
print("Config:", username, channel, path, api_root)
62+
event = ef.Event(value, args.channel)
63+
send_ok, send_response = client.send_event(event)
64+
print("Send Event:", send_ok)
1465

15-
c = ef.Client(username, password, "http://localhost:8080/ef/api/")
66+
if not send_ok:
67+
print("Error sending event", send_response)
68+
return
1669

17-
print("Login:", c.login())
18-
value = json.load(open(path))
19-
event = ef.Event(value, channel, username)
20-
print("Send Event:", c.send_event(event))
70+
except requests.exceptions.ConnectionError as conn_error:
71+
print("Error connection to server", args.url, str(conn_error))
72+
return
2173

2274
if __name__ == "__main__":
2375
main()

0 commit comments

Comments
 (0)