diff options
author | Jannis Pohlmann <jannis.pohlmann@codethink.co.uk> | 2013-01-02 16:28:28 +0000 |
---|---|---|
committer | Jannis Pohlmann <jannis.pohlmann@codethink.co.uk> | 2013-01-02 17:06:30 +0000 |
commit | 22d1bfbc91a46134dd6c9410b86a3cb3ba250887 (patch) | |
tree | 6594a29910668e0845fed69d37747b98102d5014 /morph-cache-server | |
parent | 74df327db1727a26986cdf63294ae93cf3106081 (diff) | |
download | morph-22d1bfbc91a46134dd6c9410b86a3cb3ba250887.tar.gz |
Handle batch sha1/file queries using POST requests
This commit adds support for resolving multiple refs into SHA1s and
requesting multiple files at once, each using a single POST request.
The (repo, ref) and (repo, ref, filename) parameters are passed to
the POST requests as JSON dictionaries in a list. The response to
both types of requests is a JSON list with the same dictionaries
again, to which a "sha1" and "data" field are added, respectively.
The file contents returned by "POST /1.0/files" requests are
base64-encoded and need to be decoded at the receiver's end. This
is because the contents may be binary or contain quotes and
therefore cause JSON syntax errors.
Diffstat (limited to 'morph-cache-server')
-rwxr-xr-x | morph-cache-server | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/morph-cache-server b/morph-cache-server index 04a5710c..d3e42c62 100755 --- a/morph-cache-server +++ b/morph-cache-server @@ -16,7 +16,9 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import base64 import cliapp +import json import logging import os import urllib @@ -197,6 +199,31 @@ class MorphCacheServer(cliapp.Application): except Exception, e: response.status = 404 logging.debug('%s' % e) + + @app.post('/sha1s') + def sha1s(): + result = [] + for pair in request.json: + repo = pair['repo'] + ref = pair['ref'] + try: + sha1, tree = repo_cache.resolve_ref(repo, ref) + result.append({ + 'repo': '%s' % repo, + 'ref': '%s' % ref, + 'sha1': '%s' % sha1, + 'tree': '%s' % tree + }) + except Exception, e: + logging.debug('%s' % e) + result.append({ + 'repo': '%s' % repo, + 'ref': '%s' % ref, + 'error': '%s' % e + }) + response.set_header('Cache-Control', 'no-cache') + response.set_header('Content-Type', 'application/json') + return json.dumps(result) @app.get('/files') def file(): @@ -211,6 +238,32 @@ class MorphCacheServer(cliapp.Application): response.status = 404 logging.debug('%s' % e) + @app.post('/files') + def files(): + result = [] + for pair in request.json: + repo = pair['repo'] + ref = pair['ref'] + filename = pair['filename'] + try: + content = repo_cache.cat_file(repo, ref, filename) + result.append({ + 'repo': '%s' % repo, + 'ref': '%s' % ref, + 'filename': '%s' % filename, + 'data': '%s' % base64.b64encode(content), + }) + except Exception, e: + logging.debug('%s' % e) + result.append({ + 'repo': '%s' % repo, + 'ref': '%s' % ref, + 'filename': '%s' % filename, + 'error': '%s' % e + }) + response.set_header('Content-Type', 'application/json') + return json.dumps(result) + @app.get('/trees') def tree(): repo = self._unescape_parameter(request.query.repo) |