Created
November 19, 2015 16:56
-
-
Save meshulam/f3dbebb5d3fe5e024806 to your computer and use it in GitHub Desktop.
Replay JSON events from a file into TempoIQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import argparse | |
import httplib | |
import os | |
import sys | |
import time | |
# run 'python event-replay.py -h' to see usage. | |
# set default environment info here if you don't want to put it on the command | |
# line each time | |
default_options = { | |
"key": "your-key", | |
"secret": "your-secret", | |
"host": "your-tempo-host" | |
} | |
class Writer(object): | |
def __init__(self, options): | |
auth = base64.encodestring('{}:{}'.format(options.key, options.secret)) | |
auth = auth.replace('\n', '') | |
self.headers = { | |
"Content-type": "application/json", | |
"Authorization": "Basic {}".format(auth) | |
} | |
self.conn = httplib.HTTPSConnection(options.host) | |
def write_event(self, payload): | |
self.conn.request("POST", "/api/channels/0/event", payload, self.headers) | |
response = self.conn.getresponse() | |
response.read() | |
if (response.status >= 200 and response.status < 300): | |
return (True, response) | |
else: | |
return (False, response) | |
def apply_options(): | |
parser = argparse.ArgumentParser(description='Replay a data file into TempoIQ') | |
parser.add_argument("-n", "--host", default=default_options['host']) | |
parser.add_argument("-k", "--key", default=default_options['key']) | |
parser.add_argument("-s", "--secret", default=default_options['secret']) | |
parser.add_argument("filename") | |
return parser.parse_args() | |
def main(argv): | |
options = apply_options() | |
writer = Writer(options) | |
with open(options.filename) as events: | |
for event in events: | |
event = event.strip() | |
(success, response) = writer.write_event(event) | |
if (success): | |
print("Wrote event: " + event) | |
else: | |
print("Error writing data: " + response.reason) | |
time.sleep(1) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment