Mercurial > kallithea
annotate kallithea/controllers/api/__init__.py @ 8192:b7caa806cf6e
py3: use inspect.getfullargspec instead of deprecated inspect.getargspec
Avoid some pytest warnings.
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Wed, 25 Dec 2019 17:07:35 +0100 |
parents | abb00ff224b6 |
children | e51ad2cd400e |
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 |
7811
0a277465fddf
scripts: initial run of import cleanup using isort
Mads Kiilerich <mads@kiilerich.com>
parents:
7697
diff
changeset
|
29 import itertools |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 import logging |
2656
c785ad556d44
added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
31 import time |
7811
0a277465fddf
scripts: initial run of import cleanup using isort
Mads Kiilerich <mads@kiilerich.com>
parents:
7697
diff
changeset
|
32 import traceback |
0a277465fddf
scripts: initial run of import cleanup using isort
Mads Kiilerich <mads@kiilerich.com>
parents:
7697
diff
changeset
|
33 import types |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
1508
diff
changeset
|
34 |
7811
0a277465fddf
scripts: initial run of import cleanup using isort
Mads Kiilerich <mads@kiilerich.com>
parents:
7697
diff
changeset
|
35 from tg import Response, TGController, request, response |
7812
fe4086096758
flake8: fix some F401 '...' imported but unused
Mads Kiilerich <mads@kiilerich.com>
parents:
7811
diff
changeset
|
36 from webob.exc import HTTPError, HTTPException |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 |
8120
58b6e4cd6fe9
lib: clean up ext_json and how it is used - avoid monkey patching
Mads Kiilerich <mads@kiilerich.com>
parents:
8074
diff
changeset
|
38 from kallithea.lib import ext_json |
7811
0a277465fddf
scripts: initial run of import cleanup using isort
Mads Kiilerich <mads@kiilerich.com>
parents:
7697
diff
changeset
|
39 from kallithea.lib.auth import AuthUser |
0a277465fddf
scripts: initial run of import cleanup using isort
Mads Kiilerich <mads@kiilerich.com>
parents:
7697
diff
changeset
|
40 from kallithea.lib.base import _get_ip_addr as _get_ip |
8068
c82ef5ec8dcd
lib: refactor _get_access_path as get_path_info
Mads Kiilerich <mads@kiilerich.com>
parents:
8032
diff
changeset
|
41 from kallithea.lib.base import get_path_info |
8120
58b6e4cd6fe9
lib: clean up ext_json and how it is used - avoid monkey patching
Mads Kiilerich <mads@kiilerich.com>
parents:
8074
diff
changeset
|
42 from kallithea.lib.utils2 import ascii_bytes, safe_str |
7811
0a277465fddf
scripts: initial run of import cleanup using isort
Mads Kiilerich <mads@kiilerich.com>
parents:
7697
diff
changeset
|
43 from kallithea.model.db import User |
0a277465fddf
scripts: initial run of import cleanup using isort
Mads Kiilerich <mads@kiilerich.com>
parents:
7697
diff
changeset
|
44 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 log = logging.getLogger('JSONRPC') |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
48 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 class JSONRPCError(BaseException): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 def __init__(self, message): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 self.message = message |
1708
fee9895fa46e
changed API to match fully JSON-RPC specs
Marcin Kuzminski <marcin@python-works.com>
parents:
1693
diff
changeset
|
53 super(JSONRPCError, self).__init__() |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 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
|
56 return safe_str(self.message) |
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 |
6569
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
59 class JSONRPCErrorResponse(Response, HTTPException): |
1661
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
60 """ |
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
61 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
|
62 """ |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
63 |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
64 def __init__(self, message=None, retid=None, code=None): |
6569
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
65 HTTPException.__init__(self, message, self) |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
66 Response.__init__(self, |
6569
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
67 json_body=dict(id=retid, result=None, error=message), |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
68 status=code, |
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
69 content_type='application/json') |
1661
fe5575f95850
API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents:
1514
diff
changeset
|
70 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 |
6569
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
72 class JSONRPCController(TGController): |
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 A WSGI-speaking JSON-RPC controller class |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
75 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 See the specification: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 <http://json-rpc.org/wiki/specification>`. |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
78 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 Valid controller return values should be json-serializable objects. |
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 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
|
82 if they want to pass meaningful errors to the client. |
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 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
85 |
3126
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
86 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
|
87 return _get_ip(environ) |
3126
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
88 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
89 def _get_method_args(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
90 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 Return `self._rpc_args` to dispatched controller method |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 chosen by __call__ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
94 return self._rpc_args |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
95 |
6569
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
96 def _dispatch(self, state, remainder=None): |
1445
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 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
|
99 controller and if it exists, dispatch to it. |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 """ |
6569
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
101 # Since we are here we should respond as JSON |
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
102 response.content_type = 'application/json' |
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 |
6569
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
104 environ = state.request.environ |
2656
c785ad556d44
added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
105 start = time.time() |
7697
226893a56a81
auth: move IP check to AuthUser.make - it is more about accepting authentication than about permissions after authentication
Mads Kiilerich <mads@kiilerich.com>
parents:
7696
diff
changeset
|
106 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
|
107 self._req_id = None |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 if 'CONTENT_LENGTH' not in environ: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 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
|
110 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
|
111 message="No Content-Length in request") |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
112 else: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 length = environ['CONTENT_LENGTH'] or 0 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 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
|
115 log.debug('Content-Length: %s', length) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 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
|
118 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
|
119 message="Content-Length is 0") |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 raw_body = environ['wsgi.input'].read(length) |
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 try: |
8120
58b6e4cd6fe9
lib: clean up ext_json and how it is used - avoid monkey patching
Mads Kiilerich <mads@kiilerich.com>
parents:
8074
diff
changeset
|
124 json_body = ext_json.loads(raw_body) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
125 except ValueError as e: |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
126 # 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
|
127 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
|
128 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
|
129 % (e, raw_body)) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 |
5124
e3aab61a9411
spelling: more consistent casing of 'API key'
Mads Kiilerich <madski@unity3d.com>
parents:
4422
diff
changeset
|
131 # check AUTH based on API key |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
132 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
133 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
|
134 self._req_id = json_body['id'] |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
135 self._req_method = json_body['method'] |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
140 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
|
141 self._req_method, self._request_params) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
142 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
|
143 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
|
144 message='Incorrect JSON query missing %s' % e) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
145 |
1693
60249224be04
fix for api key lookup, reuse same function in user model
Marcin Kuzminski <marcin@python-works.com>
parents:
1661
diff
changeset
|
146 # 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
|
147 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
148 u = User.get_by_api_key(self._req_api_key) |
7697
226893a56a81
auth: move IP check to AuthUser.make - it is more about accepting authentication than about permissions after authentication
Mads Kiilerich <mads@kiilerich.com>
parents:
7696
diff
changeset
|
149 auth_user = AuthUser.make(dbuser=u, ip_addr=ip_addr) |
7696
077ba994ee03
auth: introduce AuthUser.make factory which can return None if user can't be authenticated
Mads Kiilerich <mads@kiilerich.com>
parents:
7485
diff
changeset
|
150 if auth_user is None: |
6376
dc94e662ee74
tg: refactor API JSON RPC error handling to prepare for TG
Mads Kiilerich <mads@kiilerich.com>
parents:
6212
diff
changeset
|
151 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
|
152 message='Invalid API key') |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
153 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
|
154 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
|
155 message='Invalid API key') |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
156 |
7696
077ba994ee03
auth: introduce AuthUser.make factory which can return None if user can't be authenticated
Mads Kiilerich <mads@kiilerich.com>
parents:
7485
diff
changeset
|
157 request.authuser = auth_user |
7697
226893a56a81
auth: move IP check to AuthUser.make - it is more about accepting authentication than about permissions after authentication
Mads Kiilerich <mads@kiilerich.com>
parents:
7696
diff
changeset
|
158 request.ip_addr = ip_addr |
7696
077ba994ee03
auth: introduce AuthUser.make factory which can return None if user can't be authenticated
Mads Kiilerich <mads@kiilerich.com>
parents:
7485
diff
changeset
|
159 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
160 self._error = None |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
161 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
162 self._func = self._find_method() |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
163 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
|
164 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
|
165 message=str(e)) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
166 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
167 # 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
|
168 # self.kargs and dispatch control to WGIController |
8192
b7caa806cf6e
py3: use inspect.getfullargspec instead of deprecated inspect.getargspec
Mads Kiilerich <mads@kiilerich.com>
parents:
8182
diff
changeset
|
169 argspec = inspect.getfullargspec(self._func) |
b7caa806cf6e
py3: use inspect.getfullargspec instead of deprecated inspect.getargspec
Mads Kiilerich <mads@kiilerich.com>
parents:
8182
diff
changeset
|
170 arglist = argspec.args[1:] |
b7caa806cf6e
py3: use inspect.getfullargspec instead of deprecated inspect.getargspec
Mads Kiilerich <mads@kiilerich.com>
parents:
8182
diff
changeset
|
171 argtypes = [type(arg) for arg in argspec.defaults or []] |
8011
4fcf63512b77
py3: prepare for types.NotImplementedType going away
Mads Kiilerich <mads@kiilerich.com>
parents:
7824
diff
changeset
|
172 default_empty = type(NotImplemented) |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
1508
diff
changeset
|
173 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
174 # kw arguments required by this method |
8192
b7caa806cf6e
py3: use inspect.getfullargspec instead of deprecated inspect.getargspec
Mads Kiilerich <mads@kiilerich.com>
parents:
8182
diff
changeset
|
175 func_kwargs = dict(itertools.zip_longest(reversed(arglist), reversed(argtypes), |
6208
f4d128af1a01
compat: drop unnecessary wrappers for old Python versions
Mads Kiilerich <madski@unity3d.com>
parents:
5790
diff
changeset
|
176 fillvalue=default_empty)) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
177 |
1500 | 178 # This attribute will need to be first param of a method that uses |
179 # api_key, which is translated to instance of user at that name | |
180 USER_SESSION_ATTR = 'apiuser' | |
181 | |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
182 # get our arglist and check if we provided them as args |
8179
756e46bd926b
py3: trivial renaming of .iteritems() to .items()
Mads Kiilerich <mads@kiilerich.com>
parents:
8120
diff
changeset
|
183 for arg, default in func_kwargs.items(): |
1500 | 184 if arg == USER_SESSION_ATTR: |
5124
e3aab61a9411
spelling: more consistent casing of 'API key'
Mads Kiilerich <madski@unity3d.com>
parents:
4422
diff
changeset
|
185 # USER_SESSION_ATTR is something translated from API key and |
1500 | 186 # 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
|
187 continue |
1514
87ec80c280bb
fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents:
1508
diff
changeset
|
188 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
189 # 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
|
190 # 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
|
191 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
|
192 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
|
193 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
|
194 message='Missing non optional `%s` arg in JSON DATA' % arg, |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
195 ) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
196 |
6212
8be0633ff852
api: fail when given unknown arguments
Mads Kiilerich <madski@unity3d.com>
parents:
6211
diff
changeset
|
197 extra = set(self._request_params).difference(func_kwargs) |
8be0633ff852
api: fail when given unknown arguments
Mads Kiilerich <madski@unity3d.com>
parents:
6211
diff
changeset
|
198 if extra: |
7824
548357c4301d
flake8: fix E117 over-indented
Mads Kiilerich <mads@kiilerich.com>
parents:
7812
diff
changeset
|
199 raise JSONRPCErrorResponse( |
548357c4301d
flake8: fix E117 over-indented
Mads Kiilerich <mads@kiilerich.com>
parents:
7812
diff
changeset
|
200 retid=self._req_id, |
548357c4301d
flake8: fix E117 over-indented
Mads Kiilerich <mads@kiilerich.com>
parents:
7812
diff
changeset
|
201 message='Unknown %s arg in JSON DATA' % |
548357c4301d
flake8: fix E117 over-indented
Mads Kiilerich <mads@kiilerich.com>
parents:
7812
diff
changeset
|
202 ', '.join('`%s`' % arg for arg in extra), |
548357c4301d
flake8: fix E117 over-indented
Mads Kiilerich <mads@kiilerich.com>
parents:
7812
diff
changeset
|
203 ) |
6212
8be0633ff852
api: fail when given unknown arguments
Mads Kiilerich <madski@unity3d.com>
parents:
6211
diff
changeset
|
204 |
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
|
205 self._rpc_args = {} |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1708
diff
changeset
|
206 self._rpc_args.update(self._request_params) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
207 self._rpc_args['action'] = self._req_method |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
208 self._rpc_args['environ'] = environ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
209 |
2656
c785ad556d44
added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
210 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
|
211 self._get_ip_addr(environ), |
8068
c82ef5ec8dcd
lib: refactor _get_access_path as get_path_info
Mads Kiilerich <mads@kiilerich.com>
parents:
8032
diff
changeset
|
212 get_path_info(environ), time.time() - start) |
2656
c785ad556d44
added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
213 ) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
214 |
6569
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
215 state.set_action(self._rpc_call, []) |
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
216 state.set_params(self._rpc_args) |
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
217 return state |
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
218 |
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
219 def _rpc_call(self, action, environ, **rpc_args): |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
220 """ |
6569
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
221 Call the specified RPC Method |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
222 """ |
4116
ffd45b185016
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents:
3179
diff
changeset
|
223 raw_response = '' |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
224 try: |
6569
e1ab82613133
backend: replace Pylons with TurboGears2
Alessandro Molina <alessandro.molina@axant.it>
parents:
6553
diff
changeset
|
225 raw_response = getattr(self, action)(**rpc_args) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
226 if isinstance(raw_response, HTTPError): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
227 self._error = str(raw_response) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
228 except JSONRPCError as e: |
8032
287b5f1cb40a
py3: only use safe_str for string conversion - not for arbitrary __str__ invocation
Mads Kiilerich <mads@kiilerich.com>
parents:
8012
diff
changeset
|
229 self._error = unicode(e) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
230 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
|
231 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
|
232 traceback.format_exc(),) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
233 json_exc = JSONRPCError('Internal server error') |
8032
287b5f1cb40a
py3: only use safe_str for string conversion - not for arbitrary __str__ invocation
Mads Kiilerich <mads@kiilerich.com>
parents:
8012
diff
changeset
|
234 self._error = unicode(json_exc) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
235 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
236 if self._error is not None: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
237 raw_response = None |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
238 |
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 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
|
240 try: |
8120
58b6e4cd6fe9
lib: clean up ext_json and how it is used - avoid monkey patching
Mads Kiilerich <mads@kiilerich.com>
parents:
8074
diff
changeset
|
241 return ascii_bytes(ext_json.dumps(response)) |
5374
d69aa464f373
cleanup: consistently use 'except ... as ...:'
Mads Kiilerich <madski@unity3d.com>
parents:
5326
diff
changeset
|
242 except TypeError as e: |
8074
4780af2c6ff2
api: better error logging for API usage errors - say which request failed to encode response
Mads Kiilerich <mads@kiilerich.com>
parents:
8068
diff
changeset
|
243 log.error('API FAILED. Error encoding response for %s %s: %s\n%s', action, rpc_args, e, traceback.format_exc()) |
8120
58b6e4cd6fe9
lib: clean up ext_json and how it is used - avoid monkey patching
Mads Kiilerich <mads@kiilerich.com>
parents:
8074
diff
changeset
|
244 return ascii_bytes(ext_json.dumps( |
1796
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
245 dict( |
2151
12ceeda33339
#404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
246 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
|
247 result=None, |
8120
58b6e4cd6fe9
lib: clean up ext_json and how it is used - avoid monkey patching
Mads Kiilerich <mads@kiilerich.com>
parents:
8074
diff
changeset
|
248 error="Error encoding response", |
1796
6274adc06988
fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
249 ) |
8120
58b6e4cd6fe9
lib: clean up ext_json and how it is used - avoid monkey patching
Mads Kiilerich <mads@kiilerich.com>
parents:
8074
diff
changeset
|
250 )) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
251 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
252 def _find_method(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
253 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
254 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
|
255 """ |
5375
0210d0b769d4
cleanup: pass log strings unformatted - avoid unnecessary % formatting when not logging
Mads Kiilerich <madski@unity3d.com>
parents:
5374
diff
changeset
|
256 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
|
257 if self._req_method.startswith('_'): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
258 raise AttributeError("Method not allowed") |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
259 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
260 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
261 func = getattr(self, self._req_method, None) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
262 except UnicodeEncodeError: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
263 raise AttributeError("Problem decoding unicode in requested " |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
264 "method name.") |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
265 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
266 if isinstance(func, types.MethodType): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
267 return func |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
268 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
|
269 raise AttributeError("No such method: %s" % (self._req_method,)) |