Skip to content

Commit

Permalink
[api][s]: remove use of bucket parameter and use default bucket.
Browse files Browse the repository at this point in the history
  • Loading branch information
rufuspollock committed Apr 29, 2011
1 parent 6afb935 commit 4a2ca5d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
7 changes: 4 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ And a new upload page at /upload.
Metadata API
------------

/api/storage/metadata/<bucket>/<label>
/api/storage/metadata/<label>

* GET will return the metadata
* POST will add/update metadata
Expand All @@ -36,7 +36,8 @@ Auth API

Get credentials for doing operations on storage (usually directly)::

/api/storage/auth/{request|form}/{bucket}/{label}
/api/storage/auth/{request|form}/{label}

Details in docstrings in ckanext/storage/controller.py.
Details in docstrings in ckanext/storage/controller.py or by visiting
api/storage when running this extension.

8 changes: 4 additions & 4 deletions ckanext/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ class Storage(SingletonPlugin):
def after_map(self, route_map):
c = "ckanext.storage.controller:StorageAPIController"
route_map.connect('storage_api', "/api/storage", controller=c, action="index")
route_map.connect("/api/storage/metadata/{bucket}/{label}", controller=c, action="set_metadata",
route_map.connect("/api/storage/metadata/{label}", controller=c, action="set_metadata",
conditions={"method": ["PUT", "POST"]})
route_map.connect("/api/storage/metadata/{bucket}/{label}", controller=c, action="get_metadata",
route_map.connect("/api/storage/metadata/{label}", controller=c, action="get_metadata",
conditions={"method": ["GET"]})
route_map.connect('storage_api_auth_request',
"/api/storage/auth/request/{bucket}/{label:.*}",
"/api/storage/auth/request/{label:.*}",
controller=c,
action="auth_request"
)
route_map.connect('storage_api_auth_form',
"/api/storage/auth/form/{bucket}/{label:.*}",
"/api/storage/auth/form/{label:.*}",
controller=c,
action="auth_form"
)
Expand Down
39 changes: 20 additions & 19 deletions ckanext/storage/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,20 @@ class StorageAPIController(BaseController):
@jsonpify
def index(self):
info = {
'metadata/{bucket}/{label}': {
'metadata/{label}': {
'description': 'Get or set metadata for this item in storage',
'methods': ['GET', 'POST']
},
'auth/request/{bucket}/{label}': {
'description': 'Get authorization key valid for 15m',
'methods': ['GET', 'POST']
'auth/request/{label}': {
'description': self.auth_request.__doc__,
},
'auth/form/{bucket}/{label}': {
'description': 'Get authorization key valid for 15m',
'methods': ['GET', 'POST']
'auth/form/{label}': {
'description': self.auth_form.__doc__,
}
}
return info

def set_metadata(self, bucket, label):
def set_metadata(self, label):
bucket = BUCKET
if not label.startswith("/"): label = "/" + label

try:
Expand Down Expand Up @@ -102,7 +100,8 @@ def set_metadata(self, bucket, label):
return self.get_metadata(bucket, label)

@jsonpify
def get_metadata(self, bucket, label):
def get_metadata(self, label):
bucket = BUCKET
if not label.startswith("/"): label = "/" + label
if not self.ofs.exists(bucket, label):
abort(404)
Expand All @@ -112,15 +111,19 @@ def get_metadata(self, bucket, label):
return metadata

def _authorize(self, method, bucket, key):
# TODO: implement
pass
if not method in ['POST', 'GET', 'PUT', 'DELETE']:
abort(400)
# do not allow overwriting
if method != 'GET':
if self.ofs.exists(bucket, key):
abort(401)
# TODO: now user auth

@jsonpify
def auth_request(self, bucket, label):
def auth_request(self, label):
'''Provide authentication information for a request so a client can
interact with backend storage directly.
:param bucket: bucket name.
:param label: label.
:param kwargs: sent either via query string for GET or json-encoded
dict for POST). Interpreted as http headers for request plus an
Expand All @@ -138,6 +141,7 @@ def auth_request(self, bucket, label):
headers dictionary containing an Authorization field which is good for
15m.
'''
bucket = BUCKET
if request.POST:
try:
data = fix_stupid_pylons_encoding(request.body)
Expand Down Expand Up @@ -168,11 +172,10 @@ def auth_request(self, bucket, label):
}

@jsonpify
def auth_form(self, bucket, label):
def auth_form(self, label):
'''Provide fields for a form upload to storage including
authentication.
:param bucket: bucket name.
:param label: label.
:param kwargs: sent either via query string for GET or json-encoded
dict for POST. Possible key values are as for arguments to this
Expand All @@ -181,6 +184,7 @@ def auth_form(self, bucket, label):
:return: json-encoded dictionary with action parameter and fields list.
'''
bucket = BUCKET
if request.POST:
try:
data = fix_stupid_pylons_encoding(request.body)
Expand All @@ -204,9 +208,6 @@ def auth_form(self, bucket, label):
**headers
)

import base64
import hashlib
import hmac
import uuid
try:
import json
Expand Down
8 changes: 4 additions & 4 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ def test_index(self):
assert len(res.json) == 3

def test_auth_form(self):
url = url_for('storage_api_auth_form', bucket='xyz', label='abc')
url = url_for('storage_api_auth_form', label='abc')
res = self.app.get(url)
assert res.json['fields'][-1]['value'] == 'abc'

url = url_for('storage_api_auth_form', bucket='xyz', label='abc/xxx')
url = url_for('storage_api_auth_form', label='abc/xxx')
res = self.app.get(url)
assert res.json['fields'][-1]['value'] == 'abc/xxx'

url = url_for('storage_api_auth_form', bucket='xyz', label='abc',
url = url_for('storage_api_auth_form', label='abc',
success_action_redirect='abc')
res = self.app.get(url)
exp = {u'name': u'success_action_redirect', u'value': u'abc'}
assert exp == res.json['fields'][0], res.json

def test_auth_request(self):
url = url_for('storage_api_auth_request', bucket='xyz', label='abc')
url = url_for('storage_api_auth_request', label='abc')
res = self.app.get(url)
assert res.json['method'] == 'POST'
assert res.json['headers']['Authorization']
Expand Down

0 comments on commit 4a2ca5d

Please sign in to comment.