From d5c91e54823c087a9f06be461c51a7cab5c76ee2 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Thu, 12 Jun 2014 17:40:26 +0000 Subject: [PATCH] starting messing the actually making a requests call --- klugman/base.py | 25 +++++++++++++++++++++++ klugman/klugman.py | 3 ++- klugman/v1.py | 15 +++++++++++--- klugman/v2.py | 50 +++++++++++++++++++++++++++++++++++++++++++++- requirements.txt | 2 ++ 5 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 klugman/base.py diff --git a/klugman/base.py b/klugman/base.py new file mode 100644 index 0000000..1bf3aaf --- /dev/null +++ b/klugman/base.py @@ -0,0 +1,25 @@ +# Copyright (c) 2014 Dark Secret Software Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import requests + + +def _remove_empty(kv): + """Remove kv pairs from dictionary where value is empty.""" + return dict([(k, v) for k, v in kv.iteritems() if v]) + + +def get(url, cmd, params): + return requests.get("%s/%s" % (url, cmd), params=params) diff --git a/klugman/klugman.py b/klugman/klugman.py index 93b73cb..a3c200a 100644 --- a/klugman/klugman.py +++ b/klugman/klugman.py @@ -59,4 +59,5 @@ if __name__ == '__main__': print "base url:", url argv = [arguments['']] + arguments[''] - impl(url, arguments, argv) + api = impl(url, arguments) + api.dispatch(argv) diff --git a/klugman/v1.py b/klugman/v1.py index 8515f06..5fb0771 100644 --- a/klugman/v1.py +++ b/klugman/v1.py @@ -32,6 +32,15 @@ from docopt import docopt class V1(object): - def __init__(self, base_url, base_args, cmdline): - arguments = docopt(__doc__, argv=cmdline) - print arguments + def __init__(self, base_url, base_args): + self.base_url = base_url + self.base_args = base_args + + def dispatch(self, cmdline): + self.arguments = docopt(__doc__, argv=cmdline) + print self.arguments + + if self.arguments['events']: + response = self.do_events() + # handle cmdline output here. + diff --git a/klugman/v2.py b/klugman/v2.py index f3d7c62..47d086c 100644 --- a/klugman/v2.py +++ b/klugman/v2.py @@ -13,8 +13,56 @@ # See the License for the specific language governing permissions and # limitations under the License. + +"""usage: + klugman.py events [options] + klugman.py events (-h | --help) + + options: + -i, --id + filter by event ID + -r, --request_id + filter by Request ID + -s, --start + starting datetime range + -e, --end + ending datetime range + + notes: + -r isn't needed if -i is supplied. +""" + + +import base + from docopt import docopt class V2(object): - pass + def __init__(self, base_url, base_args): + self.base_url = base_url + self.base_args = base_args + + def dispatch(self, cmdline): + self.arguments = docopt(__doc__, argv=cmdline) + print self.arguments + + if self.arguments['events']: + response = self.do_events() + # handle cmdline output here. + + def do_events(self): + eid = self.arguments.get('--id') + rid = self.arguments.get('--request_id') + start = self.arguments.get('--start') + end = self.arguments.get('--end') + + cmd = "/events" + if eid: + cmd = "/events/%d" % eid + params = base._remove_empty({'request_id': rid, + 'start_ts': start, + 'end_ts': end}) + + print "Params:", params + return base.get(self.base_url, cmd, params) diff --git a/requirements.txt b/requirements.txt index e5ed2a0..c9f1720 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ docopt +prettytable +requests