annotate rhodecode/controllers/api/__init__.py @ 4186:7e5f8c12a3fc kallithea-2.2.5-rebrand

First step in two-part process to rename directories to kallithea. This first step is to change all references in the files where they refer to the old directory name.
author Bradley M. Kuhn <bkuhn@sfconservancy.org>
date Wed, 02 Jul 2014 19:04:28 -0400
parents ffd45b185016
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
20 :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
21 :author: marcink
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
22 :copyright: (c) 2013 RhodeCode GmbH.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
23 :license: GPLv3, see LICENSE for more details.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
24 """
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 import inspect
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 import logging
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 import types
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 import urllib
1508
4aba7be311e8 API added checks for a valid repository on pull command
Marcin Kuzminski <marcin@python-works.com>
parents: 1504
diff changeset
30 import traceback
2656
c785ad556d44 added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents: 2379
diff changeset
31 import time
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1508
diff changeset
32
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 from paste.response import replace_header
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 from pylons.controllers import WSGIController
1661
fe5575f95850 API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
35
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
36 from webob.exc import HTTPError
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37
4186
7e5f8c12a3fc First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4116
diff changeset
38 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
39 from kallithea.model import meta
7e5f8c12a3fc First step in two-part process to rename directories to kallithea.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4116
diff changeset
40 from kallithea.lib.compat import izip_longest, json
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.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
42 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
43 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
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
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
47
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 class JSONRPCError(BaseException):
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 def __init__(self, message):
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 self.message = message
1708
fee9895fa46e changed API to match fully JSON-RPC specs
Marcin Kuzminski <marcin@python-works.com>
parents: 1693
diff changeset
52 super(JSONRPCError, self).__init__()
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 def __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
55 return safe_str(self.message)
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
2379
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents: 2151
diff changeset
58 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
59 """
fe5575f95850 API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
60 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
61
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
62 :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
63 :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
64 :param message:
1661
fe5575f95850 API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
65 """
fe5575f95850 API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
66 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
67 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
68 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
69 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
70 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
71 )
1661
fe5575f95850 API returns proper JSON response
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
72
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 class JSONRPCController(WSGIController):
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 A WSGI-speaking JSON-RPC controller class
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
77
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 See the specification:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 <http://json-rpc.org/wiki/specification>`.
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
80
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 Valid controller return values should be json-serializable objects.
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
82
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 Sub-classes should catch their exceptions and raise JSONRPCError
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 if they want to pass meaningful errors to the client.
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
85
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87
3126
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
88 def _get_ip_addr(self, environ):
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
89 return _get_ip(environ)
3126
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
90
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 def _get_method_args(self):
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 Return `self._rpc_args` to dispatched controller method
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 chosen by __call__
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 return self._rpc_args
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 def __call__(self, environ, start_response):
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 Parse the request body as JSON, look up the method on the
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 controller and if it exists, dispatch to it.
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 """
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
103 try:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
104 return self._handle_request(environ, start_response)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
105 finally:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
106 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
107
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
108 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
109 start = time.time()
3126
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
110 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
111 self._req_id = None
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 if 'CONTENT_LENGTH' not in environ:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 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
114 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
115 message="No Content-Length in request")
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 else:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 length = environ['CONTENT_LENGTH'] or 0
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 length = int(environ['CONTENT_LENGTH'])
1976
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
119 log.debug('Content-Length: %s' % length)
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 if length == 0:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 log.debug("Content-Length is 0")
2379
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents: 2151
diff changeset
123 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
124 message="Content-Length is 0")
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 raw_body = environ['wsgi.input'].read(length)
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 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
129 json_body = json.loads(raw_body)
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1508
diff changeset
130 except ValueError, e:
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
131 # 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
132 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
133 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
134 % (e, raw_body))
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135
1693
60249224be04 fix for api key lookup, reuse same function in user model
Marcin Kuzminski <marcin@python-works.com>
parents: 1661
diff changeset
136 # check AUTH based on API KEY
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 try:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 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
139 self._req_id = json_body['id']
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 self._req_method = json_body['method']
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
141 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
142 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
143 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
144
1976
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
145 log.debug(
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
146 'method: %s, params: %s' % (self._req_method,
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
147 self._request_params)
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
148 )
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1508
diff changeset
149 except KeyError, e:
2379
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents: 2151
diff changeset
150 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
151 message='Incorrect JSON query missing %s' % e)
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152
1693
60249224be04 fix for api key lookup, reuse same function in user model
Marcin Kuzminski <marcin@python-works.com>
parents: 1661
diff changeset
153 # 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
154 try:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 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
156 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
157 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
158 message='Invalid API KEY')
3146
c5169e445fb8 Full IP restrictions enabled
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
159
3125
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 2656
diff changeset
160 #check if we are allowed to use this IP
3146
c5169e445fb8 Full IP restrictions enabled
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
161 auth_u = AuthUser(u.user_id, self._req_api_key, ip_addr=ip_addr)
c5169e445fb8 Full IP restrictions enabled
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
162 if not auth_u.ip_allowed:
3125
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 2656
diff changeset
163 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
164 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
165 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
166 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
167
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1508
diff changeset
168 except Exception, e:
2379
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents: 2151
diff changeset
169 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
170 message='Invalid API KEY')
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 self._error = None
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173 try:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 self._func = self._find_method()
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 except AttributeError, e:
2379
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents: 2151
diff changeset
176 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
177 message=str(e))
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 # 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
180 # self.kargs and dispatch control to WGIController
1504
ed3254ac279b Added optional arguments into API
Marcin Kuzminski <marcin@python-works.com>
parents: 1500
diff changeset
181 argspec = inspect.getargspec(self._func)
ed3254ac279b Added optional arguments into API
Marcin Kuzminski <marcin@python-works.com>
parents: 1500
diff changeset
182 arglist = argspec[0][1:]
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
183 defaults = map(type, argspec[3] or [])
1504
ed3254ac279b Added optional arguments into API
Marcin Kuzminski <marcin@python-works.com>
parents: 1500
diff changeset
184 default_empty = types.NotImplementedType
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1508
diff changeset
185
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
186 # kw arguments required by this method
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
187 func_kwargs = dict(izip_longest(reversed(arglist), reversed(defaults),
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
188 fillvalue=default_empty))
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
190 # 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
191 # perms decorators to work they expect the controller class to have
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1489
diff changeset
192 # rhodecode_user attribute set
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
193 self.rhodecode_user = auth_u
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
194
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1489
diff changeset
195 # This attribute will need to be first param of a method that uses
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1489
diff changeset
196 # api_key, which is translated to instance of user at that name
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1489
diff changeset
197 USER_SESSION_ATTR = 'apiuser'
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1489
diff changeset
198
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1489
diff changeset
199 if USER_SESSION_ATTR not in arglist:
2379
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents: 2151
diff changeset
200 return jsonrpc_error(
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents: 2151
diff changeset
201 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
202 message='This method [%s] does not support '
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents: 2151
diff changeset
203 'authentication (missing %s param)' % (
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents: 2151
diff changeset
204 self._func.__name__, USER_SESSION_ATTR)
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents: 2151
diff changeset
205 )
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 # 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
208 for arg, default in func_kwargs.iteritems():
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1489
diff changeset
209 if arg == USER_SESSION_ATTR:
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
210 # USER_SESSION_ATTR is something translated from api key and
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1489
diff changeset
211 # 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
212 continue
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1508
diff changeset
213
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
214 # 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
215 # 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
216 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
217 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
218 retid=self._req_id,
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
219 message=(
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
220 'Missing non optional `%s` arg in JSON DATA' % arg
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
221 )
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
222 )
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
223
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
224 self._rpc_args = {USER_SESSION_ATTR: u}
3165
e1baadec6217 fixes issue #702 API methods without arguments fail when "args":null
Marcin Kuzminski <marcin@python-works.com>
parents: 3163
diff changeset
225
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
226 self._rpc_args.update(self._request_params)
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
227
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
228 self._rpc_args['action'] = self._req_method
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
229 self._rpc_args['environ'] = environ
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
230 self._rpc_args['start_response'] = start_response
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
231
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
232 status = []
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
233 headers = []
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
234 exc_info = []
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1708
diff changeset
235
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
236 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
237 status.append(new_status)
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
238 headers.extend(new_headers)
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
239 exc_info.append(new_exc_info)
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
240
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
241 output = WSGIController.__call__(self, environ, change_content)
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
242 output = list(output)
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
243 headers.append(('Content-Length', str(len(output[0]))))
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244 replace_header(headers, 'Content-Type', 'application/json')
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
245 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
246 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
247 self._get_ip_addr(environ),
2656
c785ad556d44 added extra logging into API calls
Marcin Kuzminski <marcin@python-works.com>
parents: 2379
diff changeset
248 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
249 )
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
250 return output
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 _dispatch_call(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 Implement dispatch interface specified by WSGIController
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
255 """
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
256 raw_response = ''
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
257 try:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
258 raw_response = self._inspect_call(self._func)
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
259 if isinstance(raw_response, HTTPError):
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
260 self._error = str(raw_response)
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1508
diff changeset
261 except JSONRPCError, 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
262 self._error = safe_str(e)
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1508
diff changeset
263 except Exception, 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
264 log.error('Encountered unhandled exception: %s'
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
265 % (traceback.format_exc(),))
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
266 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
267 self._error = safe_str(json_exc)
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
268
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
269 if self._error is not None:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
270 raw_response = None
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
271
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
272 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
273 try:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
274 return json.dumps(response)
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
275 except TypeError, e:
2151
12ceeda33339 #404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
276 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
277 return json.dumps(
6274adc06988 fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
278 dict(
2151
12ceeda33339 #404 API extensions for showing permission for users
Marcin Kuzminski <marcin@python-works.com>
parents: 1976
diff changeset
279 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
280 result=None,
6274adc06988 fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
281 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
282 )
6274adc06988 fixed RPC call for api that was missing request id
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
283 )
1445
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 def _find_method(self):
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
286 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
287 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
288 """
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3179
diff changeset
289 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
290 if self._req_method.startswith('_'):
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
291 raise AttributeError("Method not allowed")
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
292
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
293 try:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
294 func = getattr(self, self._req_method, None)
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
295 except UnicodeEncodeError:
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
296 raise AttributeError("Problem decoding unicode in requested "
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
297 "method name.")
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
298
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
299 if isinstance(func, types.MethodType):
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
300 return func
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
301 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
302 raise AttributeError("No such method: %s" % (self._req_method,))