Mercurial > kallithea
annotate rhodecode/controllers/api/__init__.py @ 1500:256e729a94cd beta
Extended API
- updated docs
- created two new methods for creating users and creating users groups
- changed user attribute generated from api_key to apiuser for better name compatibility with functoin parameters
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Fri, 30 Sep 2011 18:03:20 +0300 |
parents | b951731f2143 |
children | ed3254ac279b |
rev | line source |
---|---|
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
2 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
3 rhodecode.controllers.api |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
5 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
6 JSON RPC controller |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
7 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
8 :created_on: Aug 20, 2011 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 :author: marcink |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
11 :license: GPLv3, see COPYING for more details. |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
13 # This program is free software; you can redistribute it and/or |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 # modify it under the terms of the GNU General Public License |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 # as published by the Free Software Foundation; version 2 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 # of the License or (at your opinion) any later version of the license. |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 # |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 # 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
|
19 # 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
|
20 # 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
|
21 # GNU General Public License for more details. |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 # |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 # You should have received a copy of the GNU General Public License |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 # along with this program; if not, write to the Free Software |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 # MA 02110-1301, USA. |
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 json |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 import logging |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 import types |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 import urllib |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 from paste.response import replace_header |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 from pylons.controllers import WSGIController |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 from pylons.controllers.util import Response |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError, \ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 HTTPBadRequest, HTTPError |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 |
1489 | 42 from rhodecode.model.db import User |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 from rhodecode.lib.auth import AuthUser |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 log = logging.getLogger('JSONRPC') |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 class JSONRPCError(BaseException): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 def __init__(self, message): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 self.message = message |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 def __str__(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 return str(self.message) |
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 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 def jsonrpc_error(message, code=None): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 """Generate a Response object with a JSON-RPC error body""" |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 return Response(body=json.dumps(dict(result=None, |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 error=message))) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
62 class JSONRPCController(WSGIController): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
64 A WSGI-speaking JSON-RPC controller class |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
65 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 See the specification: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
67 <http://json-rpc.org/wiki/specification>`. |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
68 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
69 Valid controller return values should be json-serializable objects. |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
70 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 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
|
72 if they want to pass meaningful errors to the client. |
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 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
75 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 def _get_method_args(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 Return `self._rpc_args` to dispatched controller method |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 chosen by __call__ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
80 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
81 return self._rpc_args |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
82 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
83 def __call__(self, environ, start_response): |
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 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
|
86 controller and if it exists, dispatch to it. |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
88 if 'CONTENT_LENGTH' not in environ: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
89 log.debug("No Content-Length") |
1489 | 90 return jsonrpc_error(message="No Content-Length in request") |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 else: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 length = environ['CONTENT_LENGTH'] or 0 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 length = int(environ['CONTENT_LENGTH']) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
94 log.debug('Content-Length: %s', length) |
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 if length == 0: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 log.debug("Content-Length is 0") |
1489 | 98 return jsonrpc_error(message="Content-Length is 0") |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 raw_body = environ['wsgi.input'].read(length) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
103 json_body = json.loads(urllib.unquote_plus(raw_body)) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
104 except ValueError as e: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
105 #catch JSON errors Here |
1489 | 106 return jsonrpc_error(message="JSON parse error ERR:%s RAW:%r" \ |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
107 % (e, urllib.unquote_plus(raw_body))) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 #check AUTH based on API KEY |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
110 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
111 self._req_api_key = json_body['api_key'] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
112 self._req_method = json_body['method'] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 self._req_params = json_body['args'] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 log.debug('method: %s, params: %s', |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 self._req_method, |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 self._req_params) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 except KeyError as e: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 return jsonrpc_error(message='Incorrect JSON query missing %s' % e) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 #check if we can find this session using api_key |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
122 u = User.get_by_api_key(self._req_api_key) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 auth_u = AuthUser(u.user_id, self._req_api_key) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 except Exception as e: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
125 return jsonrpc_error(message='Invalid API KEY') |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
126 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
127 self._error = None |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
129 self._func = self._find_method() |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 except AttributeError, e: |
1489 | 131 return jsonrpc_error(message=str(e)) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
132 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
133 # 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
|
134 # self.kargs and dispatch control to WGIController |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
135 arglist = inspect.getargspec(self._func)[0][1:] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
136 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
137 # this is little trick to inject logged in user for |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
138 # perms decorators to work they expect the controller class to have |
1500 | 139 # rhodecode_user attribute set |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
140 self.rhodecode_user = auth_u |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
141 |
1500 | 142 # This attribute will need to be first param of a method that uses |
143 # api_key, which is translated to instance of user at that name | |
144 USER_SESSION_ATTR = 'apiuser' | |
145 | |
146 if USER_SESSION_ATTR not in arglist: | |
1489 | 147 return jsonrpc_error(message='This method [%s] does not support ' |
1500 | 148 'authentication (missing %s param)' % |
149 (self._func.__name__, USER_SESSION_ATTR)) | |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
150 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
151 # get our arglist and check if we provided them as args |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
152 for arg in arglist: |
1500 | 153 if arg == USER_SESSION_ATTR: |
154 # USER_SESSION_ATTR is something translated from api key and | |
155 # 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
|
156 continue |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
157 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
158 if not self._req_params or arg not in self._req_params: |
1489 | 159 return jsonrpc_error(message='Missing %s arg in JSON DATA' % arg) |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
160 |
1500 | 161 self._rpc_args = {USER_SESSION_ATTR:u} |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
162 self._rpc_args.update(self._req_params) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
163 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
164 self._rpc_args['action'] = self._req_method |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
165 self._rpc_args['environ'] = environ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
166 self._rpc_args['start_response'] = start_response |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
167 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
168 status = [] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
169 headers = [] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
170 exc_info = [] |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
171 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
|
172 status.append(new_status) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
173 headers.extend(new_headers) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
174 exc_info.append(new_exc_info) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
175 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
176 output = WSGIController.__call__(self, environ, change_content) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
177 output = list(output) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
178 headers.append(('Content-Length', str(len(output[0])))) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
179 replace_header(headers, 'Content-Type', 'application/json') |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
180 start_response(status[0], headers, exc_info[0]) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
181 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
182 return output |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
183 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
184 def _dispatch_call(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
185 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
186 Implement dispatch interface specified by WSGIController |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
187 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
188 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
189 raw_response = self._inspect_call(self._func) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
190 if isinstance(raw_response, HTTPError): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
191 self._error = str(raw_response) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
192 except JSONRPCError as e: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
193 self._error = str(e) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
194 except Exception as e: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
195 log.debug('Encountered unhandled exception: %s', repr(e)) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
196 json_exc = JSONRPCError('Internal server error') |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
197 self._error = str(json_exc) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
198 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
199 if self._error is not None: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
200 raw_response = None |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
201 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
202 response = dict(result=raw_response, error=self._error) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
203 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
204 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
205 return json.dumps(response) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
206 except TypeError, e: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
207 log.debug('Error encoding response: %s', e) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
208 return json.dumps(dict(result=None, |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
209 error="Error encoding response")) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
210 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
211 def _find_method(self): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
212 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
213 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
|
214 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
215 log.debug('Trying to find JSON-RPC method: %s', self._req_method) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
216 if self._req_method.startswith('_'): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
217 raise AttributeError("Method not allowed") |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
218 |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
219 try: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
220 func = getattr(self, self._req_method, None) |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
221 except UnicodeEncodeError: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
222 raise AttributeError("Problem decoding unicode in requested " |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
223 "method name.") |
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 if isinstance(func, types.MethodType): |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
226 return func |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
227 else: |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
228 raise AttributeError("No such method: %s" % self._req_method) |
1500 | 229 |