Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
synnefo
Commits
314c74e2
Commit
314c74e2
authored
Apr 20, 2011
by
Antony Chazapis
Browse files
Automatic handling of serialization format.
parent
29d840d2
Changes
3
Hide whitespace changes
Inline
Side-by-side
api/functions.py
View file @
314c74e2
...
...
@@ -31,8 +31,6 @@ def authenticate(request):
# raise Unauthorized()
response
=
HttpResponse
(
status
=
204
)
# TODO: Automate the Content-Type reply.
response
[
'Content-Type'
]
=
'text/plain; charset=UTF-8'
response
[
'X-Auth-Token'
]
=
'eaaafd18-0fed-4b3a-81b4-663c99ec1cbb'
# TODO: Do we support redirections?
#response['X-Storage-Url'] = 'https://storage.grnet.gr/pithos/v1.0/<some reference>'
...
...
@@ -66,6 +64,8 @@ def object_demux(request, v_account, v_container, v_object):
return
object_read
(
request
,
v_account
,
v_container
,
v_object
)
elif
request
.
method
==
'PUT'
:
return
object_write
(
request
,
v_account
,
v_container
,
v_object
)
elif
request
.
method
==
'POST'
:
return
object_update
(
request
,
v_account
,
v_container
,
v_object
)
elif
request
.
method
==
'DELETE'
:
return
object_delete
(
request
,
v_account
,
v_container
,
v_object
)
else
:
...
...
@@ -75,7 +75,7 @@ def object_demux(request, v_account, v_container, v_object):
def
account_meta
(
request
,
v_account
):
return
HttpResponse
(
"account_meta: %s"
%
v_account
)
@
api_method
(
'GET'
)
@
api_method
(
'GET'
,
format_allowed
=
True
)
def
container_list
(
request
,
v_account
):
return
HttpResponse
(
"container_list: %s"
%
v_account
)
...
...
@@ -107,6 +107,10 @@ def object_read(request, v_account, v_container, v_object):
def
object_write
(
request
,
v_account
,
v_container
,
v_object
):
return
HttpResponse
(
"object_write: %s %s %s"
%
(
v_account
,
v_container
,
v_object
))
@
api_method
(
'POST'
)
def
object_update
(
request
,
v_account
,
v_container
,
v_object
):
return
HttpResponse
(
"object_update: %s %s %s"
%
(
v_account
,
v_container
,
v_object
))
@
api_method
(
'DELETE'
)
def
object_delete
(
request
,
v_account
,
v_container
,
v_object
):
return
HttpResponse
(
"object_delete: %s %s %s"
%
(
v_account
,
v_container
,
v_object
))
...
...
api/models.py
View file @
314c74e2
...
...
@@ -29,4 +29,6 @@ class Object(models.Model):
class
Metadata
(
models
.
Model
):
object
=
models
.
ForeignKey
(
Object
)
name
=
models
.
CharField
(
max_length
=
256
)
value
=
models
.
CharField
(
max_length
=
1024
)
\ No newline at end of file
value
=
models
.
CharField
(
max_length
=
1024
)
date_created
=
models
.
DateTimeField
(
auto_now_add
=
True
)
date_modified
=
models
.
DateTimeField
(
auto_now
=
True
)
\ No newline at end of file
api/util.py
View file @
314c74e2
...
...
@@ -125,15 +125,13 @@ import logging
# raise BadRequest('Unsupported Content-Type.')
def
update_response_headers
(
request
,
response
):
# if request.serialization == 'xml':
# response['Content-Type'] = 'application/xml'
# elif request.serialization == 'atom':
# response['Content-Type'] = 'application/atom+xml'
# else:
# response['Content-Type'] = 'application/json'
#
response
[
'Content-Type'
]
=
'text/plain; charset=UTF-8'
response
[
'Server'
]
=
'GRNET Pithos v.0.1'
if
request
.
serialization
==
'xml'
:
response
[
'Content-Type'
]
=
'application/xml; charset=UTF-8'
elif
request
.
serialization
==
'json'
:
response
[
'Content-Type'
]
=
'application/json; charset=UTF-8'
else
:
response
[
'Content-Type'
]
=
'text/plain; charset=UTF-8'
if
settings
.
TEST
:
response
[
'Date'
]
=
format_date_time
(
time
())
...
...
@@ -167,40 +165,38 @@ def render_fault(request, fault):
update_response_headers
(
request
,
resp
)
return
resp
#
def request_serialization(request, at
om
_allowed=False):
#
"""Return the serialization format requested.
#
#
Valid formats are 'json', 'xml'
and 'atom' if `atom
_allowed` is True.
#
"""
#
#
path = request.path
#
#
if path.endswith('.json'):
#
return 'json'
#
elif path.endswith('.xml')
:
#
return '
xml
'
#
elif
atom_allowed and path.endswith('.atom')
:
#
return '
atom
'
#
def
request_serialization
(
request
,
form
at_allowed
=
False
):
"""Return the serialization format requested.
Valid formats are
'text' and
'json', 'xml'
if `format
_allowed` is True.
"""
if
not
format_allowed
:
return
'text'
format
=
request
.
GET
.
get
(
'format'
)
if
format
==
'json'
:
return
'
json
'
elif
format
==
'xml'
:
return
'
xml
'
# for item in request.META.get('HTTP_ACCEPT', '').split(','):
# accept, sep, rest = item.strip().partition(';')
# if accept == 'application/json':
# return 'json'
# elif accept == 'application/xml':
# return 'xml'
# elif atom_allowed and accept == 'application/atom+xml':
# return 'atom'
#
# return 'json'
return
'text'
def
api_method
(
http_method
=
None
,
at
om
_allowed
=
False
):
def
api_method
(
http_method
=
None
,
form
at_allowed
=
False
):
"""Decorator function for views that implement an API method."""
def
decorator
(
func
):
@
wraps
(
func
)
def
wrapper
(
request
,
*
args
,
**
kwargs
):
try
:
#
request.serialization = request_serialization(request, at
om
_allowed)
request
.
serialization
=
request_serialization
(
request
,
form
at_allowed
)
if
http_method
and
request
.
method
!=
http_method
:
raise
BadRequest
(
'Method not allowed.'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment