forked from 3scale-labs/3scale_ws_api_for_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport-test.py
78 lines (66 loc) · 1.93 KB
/
report-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ThreeScalePY
import getopt
import sys
import os
import time
def usage(errorMessage=None):
if errorMessage:
stream = sys.stderr
else:
stream = sys.stdout
stream.write("""
Usage: %s [--help|-h] --app-id|-i --provider-key|-p""" %
os.path.basename(sys.argv[0]))
if errorMessage:
stream.write("\nERROR: " + errorMessage + "\n")
exitCode = 1
else:
exitCode = 0
sys.exit(exitCode)
def main():
"""main method"""
app_id = None
provider_key = None
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:p:", ['help',
'app-id', 'provider-key'])
for opt, value in opts:
if (opt in ('-i', '--app-id')):
app_id = value
elif (opt in ('-p', '--provider-key')):
provider_key = value
elif (opt in ('-h', '--help')):
usage()
except getopt.GetoptError, err:
usage('Invalid option specified: %s' % err)
if not app_id:
usage('App Id not specified')
if not provider_key:
usage('Provider Key not specified')
report = ThreeScalePY.ThreeScaleReport(provider_key)
t1 = {}
trans_usage = {}
trans_usage['hits'] = 1
trans_usage['max_value'] = 5
t1['app_id'] = app_id
t1['usage'] = trans_usage
t2 = {}
trans_usage = {}
trans_usage['hits'] = 2
trans_usage['timestamp'] = time.gmtime(time.time())
trans_usage['max_value'] = 2
t2['app_id'] = app_id
t2['usage'] = trans_usage
transactions = [t1]
print "app id => %s" % app_id
print "provider key => %s" % provider_key
resp = report.report(transactions)
if resp:
print " SUCCESS: Report transaction posted"
else:
print " ERROR: Not posted, perhaps invalid credentials " \
"specified?"
if __name__ == '__main__':
main()