Mercurial > kallithea
annotate kallithea/controllers/api/__init__.py @ 6211:2990b0587e3f
api: stop passing apiuser as parameter to handler functions - if they really need it, use self.authuser
author | Mads Kiilerich <madski@unity3d.com> |
---|---|
date | Mon, 12 Sep 2016 17:41:20 +0200 |
parents | 41e70d120a5e |
children | 8be0633ff852 |
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 |
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
|
37 from pylons import request |
1661
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
38 |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
39 from webob.exc import HTTPError |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 |
4186
7e5f8c12a3fc
First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
4116
diff
changeset
|
41 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
|
42 from kallithea.model import meta |
6208
f4d128af1a01
compat: drop unnecessary wrappers for old Python versions
Mads Kiilerich <madski@unity3d.com>
parents:
5790
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 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
|
47 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 log = logging.getLogger('JSONRPC') |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
50 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 class JSONRPCError(BaseException): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 def __init__(self, message): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 self.message = message |
1708
fee9895fa46e
changed API to match fully JSON-RPC specs
Marcin Kuzminski <marcin@python-works.com>
parents:
1693
diff
changeset
|
55 super(JSONRPCError, self).__init__() |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 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
|
58 return safe_str(self.message) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
61 def jsonrpc_error(message, retid=None, code=None): |
1661
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
62 """ |
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
63 Generate a Response object with a JSON-RPC error body |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
64 |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
65 :param code: |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
66 :param retid: |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
67 :param message: |
1661
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
68 """ |
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
69 from pylons.controllers.util import Response |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
70 return Response( |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
71 body=json.dumps(dict(id=retid, result=None, error=message)), |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
72 status=code, |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
73 content_type='application/json' |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
74 ) |
1661
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
75 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 class JSONRPCController(WSGIController): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 A WSGI-speaking JSON-RPC controller class |
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 See the specification: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
82 <http://json-rpc.org/wiki/specification>`. |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
83 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 Valid controller return values should be json-serializable objects. |
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 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
|
87 if they want to pass meaningful errors to the client. |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
88 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
89 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
90 |
3126
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
91 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
|
92 return _get_ip(environ) |
3126
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
93 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
94 def _get_method_args(self): |
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` to dispatched controller method |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 chosen by __call__ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
98 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 return self._rpc_args |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 def __call__(self, environ, start_response): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
103 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
|
104 controller and if it exists, dispatch to it. |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
105 """ |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
106 try: |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
107 return self._handle_request(environ, start_response) |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
108 finally: |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
109 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
|
110 |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
111 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
|
112 start = time.time() |
3126
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
113 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
|
114 self._req_id = None |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 if 'CONTENT_LENGTH' not in environ: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 log.debug("No Content-Length") |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
117 return jsonrpc_error(retid=self._req_id, |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
118 message="No Content-Length in request") |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 else: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 length = environ['CONTENT_LENGTH'] or 0 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 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
|
122 log.debug('Content-Length: %s', length) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 if length == 0: |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
125 return jsonrpc_error(retid=self._req_id, |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
126 message="Content-Length is 0") |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
127 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 raw_body = environ['wsgi.input'].read(length) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
129 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 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
|
131 json_body = json.loads(raw_body) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
132 except ValueError as e: |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
133 # catch JSON errors Here |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
134 return jsonrpc_error(retid=self._req_id, |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
135 message="JSON parse error ERR:%s RAW:%r" |
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
136 % (e, raw_body)) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
137 |
5124
e3aab61a9411
spelling: more consistent casing of 'API key'
Mads Kiilerich <madski@unity3d.com>
parents:
4422
diff
changeset
|
138 # check AUTH based on API key |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
139 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
140 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
|
141 self._req_id = json_body['id'] |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
142 self._req_method = json_body['method'] |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
143 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
|
144 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
|
145 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
|
146 |
1976 | 147 log.debug( |
5375
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
148 'method: %s, params: %s', self._req_method, |
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
149 self._request_params |
1976 | 150 ) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
151 except KeyError as e: |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
152 return jsonrpc_error(retid=self._req_id, |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
153 message='Incorrect JSON query missing %s' % e) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
154 |
1693
60249224be04
fix for api key lookup, reuse same function in user model
Marcin Kuzminski <marcin@python-works.com>
parents:
1661
diff
changeset
|
155 # 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
|
156 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
157 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
|
158 if u is None: |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
159 return jsonrpc_error(retid=self._req_id, |
5124
e3aab61a9411
spelling: more consistent casing of 'API key'
Mads Kiilerich <madski@unity3d.com>
parents:
4422
diff
changeset
|
160 message='Invalid API key') |
3146
c5169e445fb8
Full IP restrictions enabled
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
161 |
5326
7557da2252a3
auth: construct AuthUser from either user_id or db.User object
Søren Løvborg <kwi@kwi.dk>
parents:
5321
diff
changeset
|
162 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
|
163 if not AuthUser.check_ip_allowed(auth_u, 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
|
164 return jsonrpc_error(retid=self._req_id, |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
165 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
|
166 else: |
5375
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
167 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
|
168 |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
169 except Exception as e: |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
170 return jsonrpc_error(retid=self._req_id, |
5124
e3aab61a9411
spelling: more consistent casing of 'API key'
Mads Kiilerich <madski@unity3d.com>
parents:
4422
diff
changeset
|
171 message='Invalid API key') |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
172 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
173 self._error = None |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
174 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
175 self._func = self._find_method() |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
176 except AttributeError as e: |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
177 return jsonrpc_error(retid=self._req_id, |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
178 message=str(e)) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
179 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
180 # 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
|
181 # self.kargs and dispatch control to WGIController |
1504
ed3254ac279b
Added optional arguments into API
Marcin Kuzminski <marcin@python-works.com>
parents:
1500
diff
changeset
|
182 argspec = inspect.getargspec(self._func) |
ed3254ac279b
Added optional arguments into API
Marcin Kuzminski <marcin@python-works.com>
parents:
1500
diff
changeset
|
183 arglist = argspec[0][1:] |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
184 defaults = map(type, argspec[3] or []) |
1504
ed3254ac279b
Added optional arguments into API
Marcin Kuzminski <marcin@python-works.com>
parents:
1500
diff
changeset
|
185 default_empty = types.NotImplementedType |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
1508
diff
changeset
|
186 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
187 # 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
|
188 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
|
189 fillvalue=default_empty)) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
190 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
191 # 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
|
192 # 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
|
193 # 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
|
194 self.authuser = request.user = auth_u |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
195 |
1500 | 196 # This attribute will need to be first param of a method that uses |
197 # api_key, which is translated to instance of user at that name | |
198 USER_SESSION_ATTR = 'apiuser' | |
199 | |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
200 # 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
|
201 for arg, default in func_kwargs.iteritems(): |
1500 | 202 if arg == USER_SESSION_ATTR: |
5124
e3aab61a9411
spelling: more consistent casing of 'API key'
Mads Kiilerich <madski@unity3d.com>
parents:
4422
diff
changeset
|
203 # USER_SESSION_ATTR is something translated from API key and |
1500 | 204 # 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
|
205 continue |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
1508
diff
changeset
|
206 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
207 # 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
|
208 # 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
|
209 if default == default_empty and arg not in self._request_params: |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
210 return jsonrpc_error( |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2151
diff
changeset
|
211 retid=self._req_id, |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
212 message=( |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
213 'Missing non optional `%s` arg in JSON DATA' % arg |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
214 ) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
215 ) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
216 |
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
|
217 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
|
218 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
219 self._rpc_args.update(self._request_params) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
220 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
221 self._rpc_args['action'] = self._req_method |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
222 self._rpc_args['environ'] = environ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
223 self._rpc_args['start_response'] = start_response |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
224 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
225 status = [] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
226 headers = [] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
227 exc_info = [] |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
228 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
229 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
|
230 status.append(new_status) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
231 headers.extend(new_headers) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
232 exc_info.append(new_exc_info) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
233 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
234 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
|
235 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
|
236 replace_header(headers, 'Content-Type', 'application/json') |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
237 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
|
238 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
|
239 self._get_ip_addr(environ), |
2656
c785ad556d44
added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
240 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
|
241 ) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
242 return output |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
243 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
244 def _dispatch_call(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
245 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
246 Implement dispatch interface specified by WSGIController |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
247 """ |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
248 raw_response = '' |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
249 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
250 raw_response = self._inspect_call(self._func) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
251 if isinstance(raw_response, HTTPError): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
252 self._error = str(raw_response) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
253 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
|
254 self._error = safe_str(e) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
255 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
|
256 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
|
257 traceback.format_exc(),) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
258 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
|
259 self._error = safe_str(json_exc) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
260 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
261 if self._error is not None: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
262 raw_response = None |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
263 |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
264 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
|
265 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
266 return json.dumps(response) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
267 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
|
268 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
|
269 return json.dumps( |
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
270 dict( |
2151
12ceeda33339
#404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
271 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
|
272 result=None, |
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
273 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
|
274 ) |
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
275 ) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
276 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
277 def _find_method(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
278 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
279 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
|
280 """ |
5375
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
281 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
|
282 if self._req_method.startswith('_'): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
283 raise AttributeError("Method not allowed") |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
284 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
285 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
286 func = getattr(self, self._req_method, None) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
287 except UnicodeEncodeError: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
288 raise AttributeError("Problem decoding unicode in requested " |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
289 "method name.") |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
290 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
291 if isinstance(func, types.MethodType): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
292 return func |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
293 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
|
294 raise AttributeError("No such method: %s" % (self._req_method,)) |