-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add options and is_busy functionality to server
- Re-factor server code structure - Rename arguments request and repository - Add options to handler functions - Add minimal is_busy logic
- Loading branch information
1 parent
ea7b430
commit 9e0a209
Showing
8 changed files
with
476 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
""" | ||
api_delete( | ||
request::HTTP.Request, | ||
repository::AbstractDict, | ||
options::Options, | ||
) | ||
Delete an object from repository. | ||
Request header must contain field 'alias'. The value of field | ||
'alias' is the object alias of requested object in the server | ||
repository. | ||
""" | ||
function api_delete( | ||
request::HTTP.Request, | ||
repository::AbstractDict, | ||
options::Options, | ||
) | ||
res = _check_for_header_field(request, "alias") | ||
if isa(res, HTTP.Message) | ||
return res | ||
end | ||
res = _check_for_alias_in_repository(res, repository) | ||
if isa(res, HTTP.Message) | ||
return res | ||
end | ||
alias = res | ||
obj_type = string(typeof(repository[alias])) | ||
delete!(repository, alias) | ||
msg = "Deleted object with alias " * alias * " and object type " * obj_type * "." | ||
return HTTP.Response(_NO_HTTP_ERROR, JSON3.write(msg)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
|
||
""" | ||
api_get( | ||
request::HTTP.Request, | ||
repository::AbstractDict, | ||
options::Options, | ||
) | ||
Return the object with requested alias header field in body of response. | ||
This operation does not modify the server repository. | ||
Request header must contain fields 'alias' and 'op'. The value of field | ||
'alias' is the object alias of requested object in the server | ||
repository. Value of field 'op' is 'COPY' or 'BUILD'. | ||
For COPY operation, the requested data is directly written to JSON format. | ||
For BUILD operation, the requested data is first serialised using DiffFusion | ||
serialise operation. Then the serialised object is written to JSON format. | ||
The resulting JSON object is returned via the response body. | ||
""" | ||
function api_get( | ||
request::HTTP.Request, | ||
repository::AbstractDict, | ||
options::Options, | ||
) | ||
# | ||
res = _check_for_header_field(request, "alias") | ||
if isa(res, HTTP.Message) | ||
return res | ||
end | ||
res = _check_for_alias_in_repository(res, repository) | ||
if isa(res, HTTP.Message) | ||
return res | ||
end | ||
alias = res | ||
res = _check_for_header_field(request, "op") | ||
if isa(res, HTTP.Message) | ||
return res | ||
end | ||
op = uppercase(res) | ||
if !(op in _OPERATIONS) | ||
return _error_operation_not_implemented(op) | ||
end | ||
obj = repository[alias] | ||
local out_obj | ||
if op==_BUILD_OP | ||
try | ||
out_obj = DiffFusion.serialise(obj) | ||
catch e | ||
return _error_object_serialisation_fail(alias, e) | ||
end | ||
else | ||
out_obj = obj | ||
end | ||
local json3_string | ||
try | ||
json3_string = JSON3.write(out_obj) | ||
catch e | ||
return _error_create_json_string_fail(alias, e) | ||
end | ||
# finally return object... | ||
return HTTP.Response(_NO_HTTP_ERROR, json3_string) | ||
end | ||
|
||
|
||
""" | ||
api_bulk_get( | ||
request::HTTP.Request, | ||
repository::AbstractDict, | ||
options::Options, | ||
) | ||
Return a list of objects for a list of aliases. | ||
This operation does not modify the server repository. | ||
Request header must contain field 'op'. Value of field 'op' is | ||
'COPY' or 'BUILD'. | ||
Request body must contain a list of 'alias'. The list of 'alias' | ||
is iterated. For each 'alias' the corresponding object is retrieved | ||
from the server repository. | ||
Handling of each individual object follows method `api_get`. | ||
The result is a `Vector` of objects. This result is written to | ||
JSON and returned via the response body. | ||
""" | ||
function api_bulk_get( | ||
request::HTTP.Request, | ||
repository::AbstractDict, | ||
options::Options, | ||
) | ||
# we need to retrieve the list of alias from the request body | ||
local json3_obj | ||
try | ||
json3_obj = JSON3.read(request.body) | ||
catch e | ||
return _error_create_json_fail(alias, e) | ||
end | ||
if !isa(json3_obj, AbstractVector) | ||
return _error_bulk_list_not_found(json3_obj) | ||
end | ||
for alias in json3_obj | ||
res = _check_for_alias_in_repository(alias, repository) | ||
if isa(res, HTTP.Message) | ||
return res | ||
end | ||
end | ||
# | ||
res = _check_for_header_field(request, "op") | ||
if isa(res, HTTP.Message) | ||
return res | ||
end | ||
op = uppercase(res) | ||
if !(op in _OPERATIONS) | ||
return _error_operation_not_implemented(op) | ||
end | ||
res_list = [] | ||
for alias in json3_obj | ||
obj = repository[alias] | ||
local out_obj | ||
if op==_BUILD_OP | ||
try | ||
out_obj = DiffFusion.serialise(obj) | ||
catch e | ||
return _error_object_serialisation_fail(alias, e) | ||
end | ||
else | ||
out_obj = obj | ||
end | ||
res_list = vcat(res_list, [out_obj]) | ||
end | ||
# | ||
local json3_string | ||
try | ||
json3_string = JSON3.write(res_list) | ||
catch e | ||
return _error_create_json_string_fail("[]", e) | ||
end | ||
# finally return objects... | ||
return HTTP.Response(_NO_HTTP_ERROR, json3_string) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
""" | ||
api_get_info(request::HTTP.Request) | ||
Return an info string about the API as response. | ||
""" | ||
function api_get_info(request::HTTP.Request) | ||
return HTTP.Response(_NO_HTTP_ERROR, _INFO_STRING) | ||
end | ||
|
||
|
||
""" | ||
api_get_aliases( | ||
request::HTTP.Request, | ||
repository::AbstractDict, | ||
options::Options, | ||
) | ||
Return all aliases from the repository in body of response. | ||
""" | ||
function api_get_aliases( | ||
request::HTTP.Request, | ||
repository::AbstractDict, | ||
options::Options, | ||
) | ||
return HTTP.Response(_NO_HTTP_ERROR, JSON3.write(keys(repository))) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
""" | ||
mutable struct Options | ||
is_busy::Bool | ||
end | ||
A container that holds flags and data which control the behaviour of the router. | ||
The options object is created at inception of the router and passed to the | ||
various handler functions. | ||
`Options` element `is_busy` specifies that the server currently does not accept | ||
POST requests. The flag may be set via the *is_busy* header field in a POST | ||
request. The flag may be de-activated via the *is_busy* header field in | ||
(subsequent) GET request. | ||
""" | ||
mutable struct Options | ||
is_busy::Bool | ||
end | ||
|
||
|
||
""" | ||
initial_options( | ||
is_busy::Bool = false, | ||
) | ||
Create an `Options` object and initialise with default settings. | ||
""" | ||
function initial_options( | ||
is_busy::Bool = false, | ||
) | ||
return Options(is_busy) | ||
end |
Oops, something went wrong.