Add ability to generically call most Gerrit APIs.

This will make it easier to call new or uncommon Gerrit APIs, without
needing to add dedicated methods for each one. This also makes it easier
to debug API calls by exposing the raw response data, which is often
processed or filtered in the existing wrapper methods.

BUG=1052179
R=linxinan@chromium.org

Change-Id: I0b594a9fdaabae4b0ebb379dc5473f4d6de67a13
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3235714
Auto-Submit: Michael Moss <mmoss@chromium.org>
Reviewed-by: Dirk Pranke <dpranke@google.com>
Reviewed-by: Xinan Lin <linxinan@chromium.org>
Commit-Queue: Dirk Pranke <dpranke@google.com>
diff --git a/gerrit_client.py b/gerrit_client.py
index 07b6c0e..2a26eaa 100755
--- a/gerrit_client.py
+++ b/gerrit_client.py
@@ -81,6 +81,33 @@
 
 
 @subcommand.usage('[args ...]')
+def CMDrawapi(parser, args):
+  """Call an arbitrary Gerrit REST API endpoint."""
+  parser.add_option('--path', dest='path', help='HTTP path of the API endpoint')
+  parser.add_option('--method', dest='method',
+                    help='HTTP method for the API (default: GET)')
+  parser.add_option('--body', dest='body', help='API JSON body contents')
+  parser.add_option('--accept_status',
+                    dest='accept_status',
+                    help='Comma-delimited list of status codes for success.')
+
+  (opt, args) = parser.parse_args(args)
+  assert opt.path, "--path not defined"
+
+  host = urlparse.urlparse(opt.host).netloc
+  kwargs = {}
+  if opt.method:
+    kwargs['reqtype'] = opt.method.upper()
+  if opt.body:
+    kwargs['body'] = json.loads(opt.body)
+  if opt.accept_status:
+    kwargs['accept_statuses'] = [int(x) for x in opt.accept_status.split(',')]
+  result = gerrit_util.CallGerritApi(host, opt.path, **kwargs)
+  logging.info(result)
+  write_result(result, opt)
+
+
+@subcommand.usage('[args ...]')
 def CMDbranch(parser, args):
   """Create a branch in a gerrit project."""
   parser.add_option('--branch', dest='branch', help='branch name')