Merge lp:~cprov/tanuki-agent/api-example-contrib into lp:tanuki-agent

Proposed by Celso Providelo
Status: Merged
Approved by: Roberto Alsina
Approved revision: 89
Merged at revision: 89
Proposed branch: lp:~cprov/tanuki-agent/api-example-contrib
Merge into: lp:tanuki-agent
Diff against target: 37 lines (+11/-5)
1 file modified
scripts/api_example.py (+11/-5)
To merge this branch: bzr merge lp:~cprov/tanuki-agent/api-example-contrib
Reviewer Review Type Date Requested Status
Roberto Alsina (community) Approve
Paul Larson (community) Approve
Review via email: mp+272827@code.launchpad.net

Commit message

Contributed changes to allow passing JSON contents from a file.

Description of the change

Contributed changes to allow passing JSON contents from a file.

To post a comment you must log in.
Revision history for this message
Paul Larson (pwlars) wrote :

+1 from me if that matters :)

review: Approve
Revision history for this message
Roberto Alsina (ralsina) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'scripts/api_example.py'
2--- scripts/api_example.py 2015-08-27 21:05:54 +0000
3+++ scripts/api_example.py 2015-09-29 17:44:18 +0000
4@@ -6,14 +6,15 @@
5 api_example.py [options] <configfile> <url>
6
7 Options:
8- -h --help Show this screen
9- -d, --data data POST Data
10- -X method HTTP Method
11- -v Verbose debug output
12+ -h --help Show this screen
13+ -d, --data data|@<file> POST Data
14+ -X method HTTP Method
15+ -v Verbose debug output
16 """
17
18 import json
19 import logging
20+import os
21 import sys
22
23 from configparser import ConfigParser
24@@ -39,7 +40,12 @@
25 self.method = arguments.get('-X')
26 self.url = arguments.get('<url>')
27 pd = arguments.get('--data')
28- self.post_data = json.loads(pd) if pd else None
29+ if pd and pd.startswith('@'):
30+ # Filename specified, replace pd with data from the file
31+ with open(os.path.expanduser(pd[1:])) as datafile:
32+ self.post_data = json.load(datafile)
33+ else:
34+ self.post_data = json.loads(pd) if pd else None
35
36 if arguments.get('-v'):
37 logger.setLevel(logging.DEBUG)

Subscribers

People subscribed via source and target branches

to all changes: