Show More
@@ -41,6 +41,8 b' except ImportError:' | |||||
41 |
|
41 | |||
42 |
|
42 | |||
43 | CONFIG_NAME = '.rhodecode' |
|
43 | CONFIG_NAME = '.rhodecode' | |
|
44 | FORMAT_PRETTY = 'pretty' | |||
|
45 | FORMAT_JSON = 'json' | |||
44 |
|
46 | |||
45 |
|
47 | |||
46 | class RcConf(object): |
|
48 | class RcConf(object): | |
@@ -114,12 +116,14 b' class RcConf(object):' | |||||
114 | pass |
|
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 | Api_call wrapper for RhodeCode |
|
121 | Api_call wrapper for RhodeCode | |
120 |
|
122 | |||
121 | :param apikey: |
|
123 | :param apikey: | |
122 | :param apihost: |
|
124 | :param apihost: | |
|
125 | :param format: formatting, pretty means prints and pprint of json | |||
|
126 | json returns unparsed json | |||
123 | :param method: |
|
127 | :param method: | |
124 | """ |
|
128 | """ | |
125 | def _build_data(random_id): |
|
129 | def _build_data(random_id): | |
@@ -142,13 +146,19 b' def api_call(apikey, apihost, method=Non' | |||||
142 | req = urllib2.Request('%s/_admin/api' % apihost, |
|
146 | req = urllib2.Request('%s/_admin/api' % apihost, | |
143 | data=json.dumps(_build_data(id_)), |
|
147 | data=json.dumps(_build_data(id_)), | |
144 | headers={'content-type': 'text/plain'}) |
|
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 | ret = urllib2.urlopen(req) |
|
151 | ret = urllib2.urlopen(req) | |
147 |
json |
|
152 | raw_json = ret.read() | |
|
153 | json_data = json.loads(raw_json) | |||
148 | id_ret = json_data['id'] |
|
154 | id_ret = json_data['id'] | |
149 | _formatted_json = pprint.pformat(json_data) |
|
155 | _formatted_json = pprint.pformat(json_data) | |
150 | if id_ret == id_: |
|
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 | else: |
|
162 | else: | |
153 | raise Exception('something went wrong. ' |
|
163 | raise Exception('something went wrong. ' | |
154 | 'ID mismatch got %s, expected %s | %s' % ( |
|
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 | def argparser(argv): |
|
168 | def argparser(argv): | |
159 |
usage = ("rhodecode_api [-h] [--apikey |
|
169 | usage = ("rhodecode_api [-h] [--format=FORMAT] [--apikey=APIKEY] [--apihost=APIHOST] " | |
160 | "_create_config or METHOD <key:val> <key2:val> ...") |
|
170 | "_create_config or METHOD <key:val> <key2:val> ...") | |
161 |
|
171 | |||
162 | parser = argparse.ArgumentParser(description='RhodeCode API cli', |
|
172 | parser = argparse.ArgumentParser(description='RhodeCode API cli', | |
@@ -171,7 +181,11 b' def argparser(argv):' | |||||
171 | group.add_argument('method', metavar='METHOD', type=str, |
|
181 | group.add_argument('method', metavar='METHOD', type=str, | |
172 | help='API method name to call followed by key:value attributes', |
|
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 | args, other = parser.parse_known_args() |
|
189 | args, other = parser.parse_known_args() | |
176 | return parser, args, other |
|
190 | return parser, args, other | |
177 |
|
191 | |||
@@ -209,7 +223,7 b' def main(argv=None):' | |||||
209 | method = args.method |
|
223 | method = args.method | |
210 | margs = dict(map(lambda s: s.split(':', 1), other)) |
|
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 | return 0 |
|
227 | return 0 | |
214 |
|
228 | |||
215 | if __name__ == '__main__': |
|
229 | if __name__ == '__main__': |
General Comments 0
You need to be logged in to leave comments.
Login now