Mercurial > kallithea
annotate kallithea/controllers/api/__init__.py @ 6376:dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Fri, 23 Dec 2016 23:39:36 +0100 |
parents | 8be0633ff852 |
children | 3dcf1f82311a |
rev | line source |
---|---|
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
2 # This program is free software: you can redistribute it and/or modify |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
3 # it under the terms of the GNU General Public License as published by |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
4 # the Free Software Foundation, either version 3 of the License, or |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
5 # (at your option) any later version. |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1796
diff
changeset
|
6 # |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
7 # This program is distributed in the hope that it will be useful, |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 # GNU General Public License for more details. |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1796
diff
changeset
|
11 # |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 # You should have received a copy of the GNU General Public License |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
13 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
14 """ |
4186
7e5f8c12a3fc
First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4116
diff
changeset
|
15 kallithea.controllers.api |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
16 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
17 |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
18 JSON RPC controller |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
19 |
4211
1948ede028ef
RhodeCode GmbH is not the sole author of this work
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4208
diff
changeset
|
20 This file was forked by the Kallithea project in July 2014. |
1948ede028ef
RhodeCode GmbH is not the sole author of this work
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4208
diff
changeset
|
21 Original author and date, and relevant copyright and licensing information is below: |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
22 :created_on: Aug 20, 2011 |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
23 :author: marcink |
4211
1948ede028ef
RhodeCode GmbH is not the sole author of this work
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4208
diff
changeset
|
24 :copyright: (c) 2013 RhodeCode GmbH, and others. |
4208
ad38f9f93b3b
Correct licensing information in individual files.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4197
diff
changeset
|
25 :license: GPLv3, see LICENSE.md for more details. |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
26 """ |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 import inspect |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 import logging |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 import types |
1508
4aba7be311e8
API added checks for a valid repository on pull command
Marcin Kuzminski <marcin@python-works.com>
parents:
1504
diff
changeset
|
31 import traceback |
2656
c785ad556d44
added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
32 import time |
6208
f4d128af1a01
compat: drop unnecessary wrappers for old Python versions
Mads Kiilerich <madski@unity3d.com>
parents:
5790
diff
changeset
|
33 import itertools |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
1508
diff
changeset
|
34 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 from paste.response import replace_header |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 from pylons.controllers import WSGIController |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
37 from pylons.controllers.util import Response |
6209
41e70d120a5e
api: set authuser in the thread global request instace - and temporarily verify that it matches what is passed explicitly to auth methods
Mads Kiilerich <madski@unity3d.com>
parents:
6208
diff
changeset
|
38 from pylons import request |
1661
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
39 |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
40 from webob.exc import HTTPError |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 |
4186
7e5f8c12a3fc
First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4116
diff
changeset
|
42 from kallithea.model.db import User |
7e5f8c12a3fc
First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4116
diff
changeset
|
43 from kallithea.model import meta |
6208
f4d128af1a01
compat: drop unnecessary wrappers for old Python versions
Mads Kiilerich <madski@unity3d.com>
parents:
5790
diff
changeset
|
44 from kallithea.lib.compat import json |
4186
7e5f8c12a3fc
First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4116
diff
changeset
|
45 from kallithea.lib.auth import AuthUser |
7e5f8c12a3fc
First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4116
diff
changeset
|
46 from kallithea.lib.base import _get_ip_addr as _get_ip, _get_access_path |
7e5f8c12a3fc
First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4116
diff
changeset
|
47 from kallithea.lib.utils2 import safe_unicode, safe_str |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 log = logging.getLogger('JSONRPC') |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
51 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 class JSONRPCError(BaseException): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 def __init__(self, message): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 self.message = message |
1708
fee9895fa46e
changed API to match fully JSON-RPC specs
Marcin Kuzminski <marcin@python-works.com>
parents:
1693
diff
changeset
|
56 super(JSONRPCError, self).__init__() |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 def __str__(self): |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
59 return safe_str(self.message) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
62 class JSONRPCErrorResponse(Response, Exception): |
1661
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
63 """ |
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
64 Generate a Response object with a JSON-RPC error body |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
65 """ |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
66 |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
67 def __init__(self, message=None, retid=None, code=None): |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
68 Response.__init__(self, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
69 body=json.dumps(dict(id=retid, result=None, error=message)), |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
70 status=code, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
71 content_type='application/json') |
1661
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
72 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 class JSONRPCController(WSGIController): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
75 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 A WSGI-speaking JSON-RPC controller class |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
77 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 See the specification: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 <http://json-rpc.org/wiki/specification>`. |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
80 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
81 Valid controller return values should be json-serializable objects. |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
82 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
83 Sub-classes should catch their exceptions and raise JSONRPCError |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 if they want to pass meaningful errors to the client. |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
85 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
86 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 |
3126
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
88 def _get_ip_addr(self, environ): |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
89 return _get_ip(environ) |
3126
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
90 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 def _get_method_args(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 Return `self._rpc_args` to dispatched controller method |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
94 chosen by __call__ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
95 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
96 return self._rpc_args |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
98 def __call__(self, environ, start_response): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 Parse the request body as JSON, look up the method on the |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 controller and if it exists, dispatch to it. |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 """ |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
103 try: |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
104 return self._handle_request(environ, start_response) |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
105 except JSONRPCErrorResponse as e: |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
106 return e |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
107 finally: |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
108 meta.Session.remove() |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
109 |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
110 def _handle_request(self, environ, start_response): |
2656
c785ad556d44
added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
111 start = time.time() |
3126
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
112 ip_addr = self.ip_addr = self._get_ip_addr(environ) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
113 self._req_id = None |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 if 'CONTENT_LENGTH' not in environ: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 log.debug("No Content-Length") |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
116 raise JSONRPCErrorResponse(retid=self._req_id, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
117 message="No Content-Length in request") |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 else: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 length = environ['CONTENT_LENGTH'] or 0 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 length = int(environ['CONTENT_LENGTH']) |
5375
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
121 log.debug('Content-Length: %s', length) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
122 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 if length == 0: |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
124 raise JSONRPCErrorResponse(retid=self._req_id, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
125 message="Content-Length is 0") |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
126 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
127 raw_body = environ['wsgi.input'].read(length) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
129 try: |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
130 json_body = json.loads(raw_body) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
131 except ValueError as e: |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
132 # catch JSON errors Here |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
133 raise JSONRPCErrorResponse(retid=self._req_id, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
134 message="JSON parse error ERR:%s RAW:%r" |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
135 % (e, raw_body)) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
136 |
5124
e3aab61a9411
spelling: more consistent casing of 'API key'
Mads Kiilerich <madski@unity3d.com>
parents:
4422
diff
changeset
|
137 # check AUTH based on API key |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
138 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
139 self._req_api_key = json_body['api_key'] |
1708
fee9895fa46e
changed API to match fully JSON-RPC specs
Marcin Kuzminski <marcin@python-works.com>
parents:
1693
diff
changeset
|
140 self._req_id = json_body['id'] |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
141 self._req_method = json_body['method'] |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
142 self._request_params = json_body['args'] |
3165
e1baadec6217
fixes issue #702 API methods without arguments fail when "args":null
Marcin Kuzminski <marcin@python-works.com>
parents:
3163
diff
changeset
|
143 if not isinstance(self._request_params, dict): |
e1baadec6217
fixes issue #702 API methods without arguments fail when "args":null
Marcin Kuzminski <marcin@python-works.com>
parents:
3163
diff
changeset
|
144 self._request_params = {} |
e1baadec6217
fixes issue #702 API methods without arguments fail when "args":null
Marcin Kuzminski <marcin@python-works.com>
parents:
3163
diff
changeset
|
145 |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
146 log.debug('method: %s, params: %s', |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
147 self._req_method, self._request_params) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
148 except KeyError as e: |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
149 raise JSONRPCErrorResponse(retid=self._req_id, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
150 message='Incorrect JSON query missing %s' % e) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
151 |
1693
60249224be04
fix for api key lookup, reuse same function in user model
Marcin Kuzminski <marcin@python-works.com>
parents:
1661
diff
changeset
|
152 # check if we can find this session using api_key |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
153 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
154 u = User.get_by_api_key(self._req_api_key) |
1693
60249224be04
fix for api key lookup, reuse same function in user model
Marcin Kuzminski <marcin@python-works.com>
parents:
1661
diff
changeset
|
155 if u is None: |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
156 raise JSONRPCErrorResponse(retid=self._req_id, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
157 message='Invalid API key') |
3146
c5169e445fb8
Full IP restrictions enabled
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
158 |
5326
7557da2252a3
auth: construct AuthUser from either user_id or db.User object
Søren Løvborg <kwi@kwi.dk>
parents:
5321
diff
changeset
|
159 auth_u = AuthUser(dbuser=u) |
5252
815bf70a88ce
AuthUser: simplify check_ip_allowed and drop is_ip_allowed
Søren Løvborg <kwi@kwi.dk>
parents:
5211
diff
changeset
|
160 if not AuthUser.check_ip_allowed(auth_u, ip_addr): |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
161 raise JSONRPCErrorResponse(retid=self._req_id, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
162 message='request from IP:%s not allowed' % (ip_addr,)) |
3125
9b92cf5a0cca
Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents:
2656
diff
changeset
|
163 else: |
5375
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
164 log.info('Access for IP:%s allowed', ip_addr) |
3125
9b92cf5a0cca
Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents:
2656
diff
changeset
|
165 |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
166 except Exception as e: |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
167 raise JSONRPCErrorResponse(retid=self._req_id, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
168 message='Invalid API key') |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
169 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
170 self._error = None |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
171 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
172 self._func = self._find_method() |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
173 except AttributeError as e: |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
174 raise JSONRPCErrorResponse(retid=self._req_id, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
175 message=str(e)) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
176 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
177 # now that we have a method, add self._req_params to |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
178 # self.kargs and dispatch control to WGIController |
1504
ed3254ac279b
Added optional arguments into API
Marcin Kuzminski <marcin@python-works.com>
parents:
1500
diff
changeset
|
179 argspec = inspect.getargspec(self._func) |
ed3254ac279b
Added optional arguments into API
Marcin Kuzminski <marcin@python-works.com>
parents:
1500
diff
changeset
|
180 arglist = argspec[0][1:] |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
181 defaults = map(type, argspec[3] or []) |
1504
ed3254ac279b
Added optional arguments into API
Marcin Kuzminski <marcin@python-works.com>
parents:
1500
diff
changeset
|
182 default_empty = types.NotImplementedType |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
1508
diff
changeset
|
183 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
184 # kw arguments required by this method |
6208
f4d128af1a01
compat: drop unnecessary wrappers for old Python versions
Mads Kiilerich <madski@unity3d.com>
parents:
5790
diff
changeset
|
185 func_kwargs = dict(itertools.izip_longest(reversed(arglist), reversed(defaults), |
f4d128af1a01
compat: drop unnecessary wrappers for old Python versions
Mads Kiilerich <madski@unity3d.com>
parents:
5790
diff
changeset
|
186 fillvalue=default_empty)) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
187 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
188 # this is little trick to inject logged in user for |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
189 # perms decorators to work they expect the controller class to have |
4197
d208416c84c6
Rename rhodecode_user to authuser - it is an AuthUser instance
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4187
diff
changeset
|
190 # authuser attribute set |
6209
41e70d120a5e
api: set authuser in the thread global request instace - and temporarily verify that it matches what is passed explicitly to auth methods
Mads Kiilerich <madski@unity3d.com>
parents:
6208
diff
changeset
|
191 self.authuser = request.user = auth_u |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
192 |
1500 | 193 # This attribute will need to be first param of a method that uses |
194 # api_key, which is translated to instance of user at that name | |
195 USER_SESSION_ATTR = 'apiuser' | |
196 | |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
197 # get our arglist and check if we provided them as args |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
198 for arg, default in func_kwargs.iteritems(): |
1500 | 199 if arg == USER_SESSION_ATTR: |
5124
e3aab61a9411
spelling: more consistent casing of 'API key'
Mads Kiilerich <madski@unity3d.com>
parents:
4422
diff
changeset
|
200 # USER_SESSION_ATTR is something translated from API key and |
1500 | 201 # this is checked before so we don't need validate it |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
202 continue |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
1508
diff
changeset
|
203 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
204 # skip the required param check if it's default value is |
1504
ed3254ac279b
Added optional arguments into API
Marcin Kuzminski <marcin@python-works.com>
parents:
1500
diff
changeset
|
205 # NotImplementedType (default_empty) |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
206 if default == default_empty and arg not in self._request_params: |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
207 raise JSONRPCErrorResponse( |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
208 retid=self._req_id, |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
209 message='Missing non optional `%s` arg in JSON DATA' % arg, |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
210 ) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
211 |
6212
8be0633ff852
api: fail when given unknown arguments
Mads Kiilerich <madski@unity3d.com>
parents:
6211
diff
changeset
|
212 extra = set(self._request_params).difference(func_kwargs) |
8be0633ff852
api: fail when given unknown arguments
Mads Kiilerich <madski@unity3d.com>
parents:
6211
diff
changeset
|
213 if extra: |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
214 raise JSONRPCErrorResponse( |
6212
8be0633ff852
api: fail when given unknown arguments
Mads Kiilerich <madski@unity3d.com>
parents:
6211
diff
changeset
|
215 retid=self._req_id, |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
216 message='Unknown %s arg in JSON DATA' % |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
217 ', '.join('`%s`' % arg for arg in extra), |
6212
8be0633ff852
api: fail when given unknown arguments
Mads Kiilerich <madski@unity3d.com>
parents:
6211
diff
changeset
|
218 ) |
8be0633ff852
api: fail when given unknown arguments
Mads Kiilerich <madski@unity3d.com>
parents:
6211
diff
changeset
|
219 |
6211
2990b0587e3f
api: stop passing apiuser as parameter to handler functions - if they really need it, use self.authuser
Mads Kiilerich <madski@unity3d.com>
parents:
6209
diff
changeset
|
220 self._rpc_args = {} |
3165
e1baadec6217
fixes issue #702 API methods without arguments fail when "args":null
Marcin Kuzminski <marcin@python-works.com>
parents:
3163
diff
changeset
|
221 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
222 self._rpc_args.update(self._request_params) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
223 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
224 self._rpc_args['action'] = self._req_method |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
225 self._rpc_args['environ'] = environ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
226 self._rpc_args['start_response'] = start_response |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
227 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
228 status = [] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
229 headers = [] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
230 exc_info = [] |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
231 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
232 def change_content(new_status, new_headers, new_exc_info=None): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
233 status.append(new_status) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
234 headers.extend(new_headers) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
235 exc_info.append(new_exc_info) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
236 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
237 output = WSGIController.__call__(self, environ, change_content) |
5790
ac4daf775230
api: avoid sending double Content-Length header (Issue #201)
Mads Kiilerich <madski@unity3d.com>
parents:
5375
diff
changeset
|
238 output = list(output) # expand iterator - just to ensure exact timing |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
239 replace_header(headers, 'Content-Type', 'application/json') |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
240 start_response(status[0], headers, exc_info[0]) |
2656
c785ad556d44
added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
241 log.info('IP: %s Request to %s time: %.3fs' % ( |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
242 self._get_ip_addr(environ), |
2656
c785ad556d44
added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
243 safe_unicode(_get_access_path(environ)), time.time() - start) |
c785ad556d44
added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
244 ) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
245 return output |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
246 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
247 def _dispatch_call(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
248 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
249 Implement dispatch interface specified by WSGIController |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
250 """ |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
251 raw_response = '' |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
252 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
253 raw_response = self._inspect_call(self._func) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
254 if isinstance(raw_response, HTTPError): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
255 self._error = str(raw_response) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
256 except JSONRPCError as e: |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
257 self._error = safe_str(e) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
258 except Exception as e: |
5375
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
259 log.error('Encountered unhandled exception: %s', |
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
260 traceback.format_exc(),) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
261 json_exc = JSONRPCError('Internal server error') |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
262 self._error = safe_str(json_exc) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
263 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
264 if self._error is not None: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
265 raw_response = None |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
266 |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
267 response = dict(id=self._req_id, result=raw_response, error=self._error) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
268 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
269 return json.dumps(response) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
270 except TypeError as e: |
5375
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
271 log.error('API FAILED. Error encoding response: %s', e) |
1796
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
272 return json.dumps( |
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
273 dict( |
2151
12ceeda33339
#404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
274 id=self._req_id, |
1796
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
275 result=None, |
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
276 error="Error encoding response" |
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
277 ) |
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
278 ) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
279 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
280 def _find_method(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
281 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
282 Return method named by `self._req_method` in controller if able |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
283 """ |
5375
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
284 log.debug('Trying to find JSON-RPC method: %s', self._req_method) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
285 if self._req_method.startswith('_'): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
286 raise AttributeError("Method not allowed") |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
287 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
288 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
289 func = getattr(self, self._req_method, None) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
290 except UnicodeEncodeError: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
291 raise AttributeError("Problem decoding unicode in requested " |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
292 "method name.") |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
293 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
294 if isinstance(func, types.MethodType): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
295 return func |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
296 else: |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
297 raise AttributeError("No such method: %s" % (self._req_method,)) |