##// END OF EJS Templates
Added optional --format=json into api cli. That will return pure JSON data from server
marcink -
r2381:e487d2a6 beta
parent child Browse files
Show More
@@ -41,6 +41,8 b' except ImportError:'
41 41
42 42
43 43 CONFIG_NAME = '.rhodecode'
44 FORMAT_PRETTY = 'pretty'
45 FORMAT_JSON = 'json'
44 46
45 47
46 48 class RcConf(object):
@@ -114,12 +116,14 b' class RcConf(object):'
114 116 pass
115 117
116 118
117 def api_call(apikey, apihost, method=None, **kw):
119 def api_call(apikey, apihost, format, method=None, **kw):
118 120 """
119 121 Api_call wrapper for RhodeCode
120 122
121 123 :param apikey:
122 124 :param apihost:
125 :param format: formatting, pretty means prints and pprint of json
126 json returns unparsed json
123 127 :param method:
124 128 """
125 129 def _build_data(random_id):
@@ -142,13 +146,19 b' def api_call(apikey, apihost, method=Non'
142 146 req = urllib2.Request('%s/_admin/api' % apihost,
143 147 data=json.dumps(_build_data(id_)),
144 148 headers={'content-type': 'text/plain'})
145 print 'calling %s to %s' % (req.get_data(), apihost)
149 if format == FORMAT_PRETTY:
150 sys.stdout.write('calling %s to %s \n' % (req.get_data(), apihost))
146 151 ret = urllib2.urlopen(req)
147 json_data = json.loads(ret.read())
152 raw_json = ret.read()
153 json_data = json.loads(raw_json)
148 154 id_ret = json_data['id']
149 155 _formatted_json = pprint.pformat(json_data)
150 156 if id_ret == id_:
151 print 'rhodecode said:\n%s' % (_formatted_json)
157 if format == FORMAT_JSON:
158 sys.stdout.write(str(raw_json))
159 else:
160 sys.stdout.write('rhodecode returned:\n%s\n' % (_formatted_json))
161
152 162 else:
153 163 raise Exception('something went wrong. '
154 164 'ID mismatch got %s, expected %s | %s' % (
@@ -156,7 +166,7 b' def api_call(apikey, apihost, method=Non'
156 166
157 167
158 168 def argparser(argv):
159 usage = ("rhodecode_api [-h] [--apikey APIKEY] [--apihost APIHOST] "
169 usage = ("rhodecode_api [-h] [--format=FORMAT] [--apikey=APIKEY] [--apihost=APIHOST] "
160 170 "_create_config or METHOD <key:val> <key2:val> ...")
161 171
162 172 parser = argparse.ArgumentParser(description='RhodeCode API cli',
@@ -171,7 +181,11 b' def argparser(argv):'
171 181 group.add_argument('method', metavar='METHOD', type=str,
172 182 help='API method name to call followed by key:value attributes',
173 183 )
174
184 group.add_argument('--format', dest='format', type=str,
185 help='output format default: `pretty` can '
186 'be also `%s`' % FORMAT_JSON,
187 default=FORMAT_PRETTY
188 )
175 189 args, other = parser.parse_known_args()
176 190 return parser, args, other
177 191
@@ -209,7 +223,7 b' def main(argv=None):'
209 223 method = args.method
210 224 margs = dict(map(lambda s: s.split(':', 1), other))
211 225
212 api_call(apikey, host, method, **margs)
226 api_call(apikey, host, args.format, method, **margs)
213 227 return 0
214 228
215 229 if __name__ == '__main__':
General Comments 0
You need to be logged in to leave comments. Login now