Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Paste
P176
vault web api factorization using django rest framework
Active
Public
Actions
Authored by
anlambert
on Sep 12 2017, 2:35 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
None
Subscribers
None
from
django.http
import
HttpResponse
from
swh.web.api.utils
import
get_query_params
,
reverse
from
swh.web.api
import
apidoc
as
api_doc
from
swh.web.api.apiurls
import
api_route
from
swh.web.api
import
service
from
swh.web.api.views
import
(
_api_lookup
,
_doc_exc_id_not_found
,
_doc_exc_bad_id
)
@api_route
(
r'/vault/directory/(?P<dir_id>.+)/raw/'
,
'vault-fetch'
)
@api_route
(
r'/vault/revision_gitfast/(?P<rev_id>.+)/raw/'
,
'vault-fetch'
)
@api_doc.route
(
'/vault/raw'
,
handle_response
=
True
,
tags
=
[
'hidden'
])
@api_doc.arg
(
'dir_id'
,
default
=
''
,
argtype
=
api_doc
.
argtypes
.
sha1_git
,
argdoc
=
"The directory's sha1 identifier"
)
@api_doc.arg
(
'dir_id'
,
default
=
''
,
argtype
=
api_doc
.
argtypes
.
sha1_git
,
argdoc
=
"The directory's sha1 identifier"
)
@api_doc.arg
(
'rev_id'
,
default
=
''
,
argtype
=
api_doc
.
argtypes
.
sha1_git
,
argdoc
=
"The revision's sha1_git identifier"
)
@api_doc.raises
(
exc
=
api_doc
.
excs
.
badinput
,
doc
=
_doc_exc_bad_id
)
@api_doc.raises
(
exc
=
api_doc
.
excs
.
notfound
,
doc
=
_doc_exc_id_not_found
)
@api_doc.returns
(
rettype
=
api_doc
.
rettypes
.
octet_stream
,
retdoc
=
'the cooked directory tarball'
)
def
api_vault_fetch
(
request
,
dir_id
=
None
,
rev_id
=
None
):
"""Fetch the archive of a directoy or a revision identified by its id."""
if
dir_id
:
obj_type
=
'directory'
obj_id
=
dir_id
notfound_msg
=
"Directory with ID '{}' not found."
.
format
(
dir_id
)
filename
=
'{}.tar.gz'
.
format
(
dir_id
)
else
:
obj_type
=
'revision_gitfast'
obj_id
=
rev_id
notfound_msg
=
"Revision with ID '{}' not found."
.
format
(
rev_id
)
filename
=
'{}.gitfast.gz'
.
format
(
rev_id
)
res
=
_api_lookup
(
service
.
vault_fetch
,
obj_type
,
obj_id
,
notfound_msg
=
notfound_msg
)
response
=
HttpResponse
(
res
,
content_type
=
'application/gzip'
)
response
[
'Content-disposition'
]
=
'attachment; filename=
%s
'
%
filename
return
response
@api_route
(
r'/vault/directory/(?P<dir_id>.+)/'
,
'vault-cook'
,
methods
=
[
'GET'
,
'POST'
])
@app.route
(
r'/vault/revision_gitfast/(?P<rev_id>.+)/'
,
'vault-cook'
,
methods
=
[
'GET'
,
'POST'
])
@api_doc.route
(
'/vault/'
,
tags
=
[
'hidden'
])
@api_doc.arg
(
'dir_id'
,
default
=
''
,
argtype
=
api_doc
.
argtypes
.
sha1_git
,
argdoc
=
"The directory's sha1 identifier"
)
@doc.arg
(
'rev_id'
,
default
=
''
,
argtype
=
api_doc
.
argtypes
.
sha1_git
,
argdoc
=
"The revision's sha1_git identifier"
)
@api_doc.param
(
'email'
,
default
=
None
,
argtype
=
api_doc
.
argtypes
.
int
,
doc
=
"e-mail to notify when the bundle is ready"
)
@api_doc.raises
(
exc
=
api_doc
.
excs
.
badinput
,
doc
=
_doc_exc_bad_id
)
@api_doc.raises
(
exc
=
api_doc
.
excs
.
notfound
,
doc
=
_doc_exc_id_not_found
)
@api_doc.returns
(
rettype
=
api_doc
.
rettypes
.
dict
,
retdoc
=
'dictionary mapping containing the status of the cooking'
)
def
api_vault_cook
(
request
,
dir_id
=
None
,
rev_id
=
None
):
"""Requests an archive of a swh object (directory or revision) identified by its id.
To import a directory in the current directory, use::
$ tar xvf path/to/directory.tar.gz
To import a revision in the current directory, use::
$ git init
$ zcat path/to/revision.gitfast.gz | git fast-import
"""
email
=
get_query_params
(
request
)
.
get
(
'email'
)
if
dir_id
:
obj_type
=
'revision_gitfast'
obj_id
=
dir_id
notfound_msg
=
"Directory with ID '{}' not found."
.
format
(
dir_id
)
kwargs
=
{
'dir_id'
:
dir_id
}
else
:
obj_type
=
'directory'
obj_id
=
rev_id
notfound_msg
=
"Revision with ID '{}' not found."
.
format
(
rev_id
)
kwargs
=
{
'rev_id'
:
rev_id
}
def
_enrich_dir_cook
(
res
):
res
[
'fetch_url'
]
=
reverse
(
'vault-fetch'
,
kwargs
=
kwargs
)
return
res
return
_api_lookup
(
service
.
vault_cook
,
obj_type
,
obj_id
,
email
,
notfound_msg
=
notfound_msg
,
enrich_fn
=
_enrich_dir_cook
)
Event Timeline
anlambert
created this paste.
Sep 12 2017, 2:35 PM
2017-09-12 14:35:35 (UTC+2)
Log In to Comment