Commit c33b46d3 authored by Sebastian Seufert's avatar Sebastian Seufert
Browse files

- refined response handling as suggested by MS -> now json dict

parent 72a8449e
......@@ -12,6 +12,10 @@ This include provides simple formatting predicates that form a json response.
501 -> not implemented
*/
action_reply_success_dict(Code, Dict) :-
reply_json_dict(Dict, [status(Code)]).
/** Remove later
action_reply_success(Code, Msg) :-
format(atom(Reply), 'Endpoint: ~q', [Msg]),
reply_json(Reply, [status(Code)]).
......@@ -19,18 +23,7 @@ action_reply_success(Code, Msg) :-
action_reply_success(Code, Endpoint, Action) :-
Reply =_{endpoint:Endpoint,action:Action},
reply_json(Reply, [status(Code)]).
action_reply_success(Code, Msg, Reply) :-
format(atom(Reply), 'Endpoint: ~q', [Msg]),
reply_json(Reply, [status(Code)]).
action_reply_success(Code, Endpoint, Action, Reply) :-
Reply =_{endpoint:Endpoint,action:Action},
reply_json(Reply, [status(Code)]).
action_reply_success(Code, Endpoint, Action, Details, Reply) :-
Reply =_{endpoint:Endpoint,action:Action, details:Details},
reply_json(Reply, [status(Code)]).
*/
generic_reply(Method, Pred, Info) :-
format('Status: 201~n'),
......
......@@ -142,16 +142,13 @@ set_bg(Request) :-
sum_list(Accs,Total)
; assert_dict(Dict, Total)
),
(
Diff is (L - Total),
(
% Total is 0 when no facts were added since already present
Total is 0
-> action_reply_success(204, set_bg, 'fact(s) already present, no change')
; L == Total
-> format(atom(Message), 'Received: ~q, Added: ~q', [L,Total]),
action_reply_success(201, set_bg, Message)
; Diff is (L - Total),
format(atom(Message), 'Received: ~q, Added: ~q, Skipped: ~q (already present)', [L,Total, Diff]),
action_reply_success(201, set_bg, Message)
-> action_reply_success_dict(204, _{received:L,added:Total,skipped:Diff})
; action_reply_success_dict(201, _{received:L,added:Total,skipped:Diff})
).
/**
......@@ -200,12 +197,10 @@ clear_bg(Req) :-
),
(
Failed == []
-> format(atom(Message), 'Received: ~q, Removed: ~q', [Length,Length]),
action_reply_success(200, clear_bg, Message)
-> action_reply_success_dict(200, _{received:Length,removed:Length})
; length(Failed, FailedCnt),
Diff is (Length - FailedCnt),
format(atom(Message), 'Received: ~q, Removed: ~q, Skipped: ~q (not found)', [Length, Diff, FailedCnt]),
action_reply_success(200, clear_bg, Message)
action_reply_success_dict(200, _{received:Length,removed:Diff,skipped:FailedCnt})
).
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment