Using lists instead of atoms for things being turned into JSON.

This commit is contained in:
Jonathan Bernard 2013-09-21 15:52:29 -05:00
parent 100ca8fd74
commit 621e00deaa

View File

@ -32,7 +32,7 @@ out(YArg) ->
% ================================== %
%% Entry point to the TimeStamper API dispatch system
dispatch_request(YArg, _Session, []) -> make_json_404(YArg, [{see_docs, "/ts_api_doc"}]);
dispatch_request(YArg, _Session, []) -> make_json_404(YArg, [{"see_docs", "/ts_api_doc"}]);
dispatch_request(YArg, Session, [H|T]) ->
@ -41,13 +41,13 @@ dispatch_request(YArg, Session, [H|T]) ->
{_, "logout"} -> do_logout(YArg);
{not_logged_in, _} -> make_json_401(YArg);
{session_expired, _} -> make_json_401(YArg, [{error, "session expired"}]);
{session_expired, _} -> make_json_401(YArg, [{"error", "session expired"}]);
{_S, "app"} -> dispatch_app(YArg, Session, T);
{_S, "users"} -> dispatch_user(YArg, Session, T);
{_S, "timelines"} -> dispatch_timeline(YArg, Session, T);
{_S, "entries"} -> dispatch_entry(YArg, Session, T);
{_S, _Other} -> make_json_404(YArg, [{see_docs, "/ts_api_doc/"}])
{_S, _Other} -> make_json_404(YArg, [{"see_docs", "/ts_api_doc/"}])
end.
% -------- Dispatch for /app -------- %
@ -68,9 +68,9 @@ dispatch_app(YArg, Session, Params) ->
end;
{_BadMethod, ["user_summary", _UsernameStr]} ->
make_json_405(YArg, [{see_docs, "/ts_api_docs/app.html"}]);
make_json_405(YArg, [{"see_docs", "/ts_api_docs/app.html"}]);
_Other -> make_json_404(YArg, [{see_docs, "/ts_api_docs/app.html"}])
_Other -> make_json_404(YArg, [{"see_docs", "/ts_api_docs/app.html"}])
end.
@ -90,21 +90,21 @@ dispatch_user(YArg, Session, [Username]) ->
{'PUT', Username} -> put_user(YArg, Username);
{_BadMethod, Username} ->
make_json_405(YArg, [{see_docs, "/ts_api_doc/users.html"}]);
make_json_405(YArg, [{"see_docs", "/ts_api_doc/users.html"}]);
_Other -> make_json_401(YArg, [{see_docs, "/ts_api_doc/users.html"}])
_Other -> make_json_401(YArg, [{"see_docs", "/ts_api_doc/users.html"}])
end.
% -------- Dispatch for /timeline -------- %
dispatch_timeline(YArg, _Session, []) ->
make_json_404(YArg, [{see_docs, "/ts_api_doc/timelines.html"}]);
make_json_404(YArg, [{"see_docs", "/ts_api_doc/timelines.html"}]);
dispatch_timeline(YArg, Session, [Username|_T] = PathElements) ->
case Session#ts_api_session.username of
Username -> dispatch_timeline(YArg, PathElements);
_Other -> make_json_404(YArg, [{see_docs, "/ts_api_doc/users.html"}])
_Other -> make_json_404(YArg, [{"see_docs", "/ts_api_doc/users.html"}])
end.
% just username, list timelines
@ -114,7 +114,7 @@ dispatch_timeline(YArg, [Username]) ->
case HTTPMethod of
'OPTIONS' -> make_CORS_options(YArg, "GET");
'GET' -> list_timelines(YArg, Username);
_Other -> make_json_405(YArg, [{see_docs, "/ts_api_doc/timelines.html"}])
_Other -> make_json_405(YArg, [{"see_docs", "/ts_api_doc/timelines.html"}])
end;
dispatch_timeline(YArg, [Username, TimelineId]) ->
@ -125,22 +125,22 @@ dispatch_timeline(YArg, [Username, TimelineId]) ->
'GET' -> get_timeline(YArg, Username, TimelineId);
'PUT' -> put_timeline(YArg, Username, TimelineId);
'DELETE' -> delete_timeline(YArg, Username, TimelineId);
_Other -> make_json_405(YArg, [{see_docs, "/ts_api_doc/timelines.html"}])
_Other -> make_json_405(YArg, [{"see_docs", "/ts_api_doc/timelines.html"}])
end;
dispatch_timeline(YArg, _Other) ->
make_json_404(YArg, [{see_docs, "/ts_api_doc/timelines.html"}]).
make_json_404(YArg, [{"see_docs", "/ts_api_doc/timelines.html"}]).
% -------- Dispatch for /entry -------- %
dispatch_entry(YArg, _Session, []) ->
make_json_404(YArg, [{see_docs, "/ts_aip_doc/entries.html"}]);
make_json_404(YArg, [{"see_docs", "/ts_aip_doc/entries.html"}]);
dispatch_entry(YArg, Session, [Username|_T] = PathElements) ->
case Session#ts_api_session.username of
Username -> dispatch_entry(YArg, PathElements);
_Other -> make_json_404(YArg, [{see_docs, "/ts_api_doc/entries.html"}])
_Other -> make_json_404(YArg, [{"see_docs", "/ts_api_doc/entries.html"}])
end.
dispatch_entry(YArg, [Username, TimelineId]) ->
@ -150,7 +150,7 @@ dispatch_entry(YArg, [Username, TimelineId]) ->
'OPTIONS' -> make_CORS_options(YArg, "GET, POST");
'GET' -> list_entries(YArg, Username, TimelineId);
'POST' -> post_entry(YArg, Username, TimelineId);
_Other -> make_json_405(YArg, [{see_docs, "/ts_api_doc/entries.html"}])
_Other -> make_json_405(YArg, [{"see_docs", "/ts_api_doc/entries.html"}])
end;
dispatch_entry(YArg, [Username, TimelineId, UrlEntryId]) ->
@ -162,11 +162,11 @@ dispatch_entry(YArg, [Username, TimelineId, UrlEntryId]) ->
'GET' -> get_entry(YArg, Username, TimelineId, EntryId);
'PUT' -> put_entry(YArg, Username, TimelineId, EntryId);
'DELETE' -> delete_entry(YArg, Username, TimelineId, EntryId);
_Other -> make_json_405(YArg, [{see_docs, "/ts_api_doc/entries.html"}])
_Other -> make_json_405(YArg, [{"see_docs", "/ts_api_doc/entries.html"}])
end;
dispatch_entry(YArg, _Other) ->
make_json_404(YArg, [{see_docs, "/ts_api_doc/entries.html"}]).
make_json_404(YArg, [{"see_docs", "/ts_api_doc/entries.html"}]).
% ============================== %
% ======== IMPLEMENTATION ====== %
@ -195,14 +195,14 @@ do_login(YArg) ->
{header, ["Access-Control-Allow-Origin: ", get_origin_header(YArg)]},
{header, ["Access-Control-Allow-Credentials: ", "true"]},
{content, "application/json",
json:encode({struct, [{status, "ok"}]})}];
json:encode({struct, [{"status", "ok"}]})}];
% they are not good
false -> make_json_401(YArg, [{error,
false -> make_json_401(YArg, [{"error",
"bad username/password combination"}])
end;
_Other -> make_json_400(YArg, [{see_docs, "/ts_api_doc/login.html"}])
_Other -> make_json_400(YArg, [{"see_docs", "/ts_api_doc/login.html"}])
end.
do_logout(YArg) ->
@ -310,7 +310,7 @@ get_timeline(YArg, Username, TimelineId) ->
% look for timeline
case ts_timeline:lookup(Username, TimelineId) of
% no such timeline, return 404
no_record -> make_json_404(YArg, [{error, "no such timeline"}]);
no_record -> make_json_404(YArg, [{"error", "no such timeline"}]);
% return the timeline data
Timeline -> make_json_200_record(YArg, Timeline)
end.
@ -347,8 +347,8 @@ list_entries(YArg, Username, TimelineId) ->
lists:keyfind("byDate", 1, QueryData)} of
{no_record, _ByDateField} -> make_json_404(
[{error, "no such timeline"},
{see_docs, "/ts_api_doc/entries.html#LIST"}]);
[{"error", "no such timeline"},
{"see_docs", "/ts_api_doc/entries.html#LIST"}]);
% listing by date range
{Timeline, {"byDate", "true"}} ->
@ -425,7 +425,7 @@ list_entries(YArg, Username, TimelineId) ->
get_entry(YArg, Username, TimelineId, EntryId) ->
case ts_entry:lookup(Username, TimelineId, EntryId) of
% no such entry
no_record -> make_json_404(YArg, [{error, "no such entry"}]);
no_record -> make_json_404(YArg, [{"error", "no such entry"}]);
% return the entry data
Entry -> make_json_200_record(YArg, Entry)
end.
@ -539,7 +539,7 @@ make_json_200_record(YArg, Record) ->
make_json_400(YArg) -> make_json_400(YArg, []).
make_json_400(YArg, Fields) ->
F1 = case lists:keyfind(status, 1, Fields) of
false -> Fields ++ [{status, "bad request"}];
false -> Fields ++ [{"status", "bad request"}];
_Else -> Fields
end,
@ -551,7 +551,7 @@ make_json_401(YArg) -> make_json_401(YArg, []).
make_json_401(YArg, Fields) ->
% add default status if not provided
F1 = case lists:keyfind(status, 1, Fields) of
false -> Fields ++ [{status, "unauthorized"}];
false -> Fields ++ [{"status", "unauthorized"}];
_Else -> Fields
end,
@ -565,7 +565,7 @@ make_json_404(YArg) -> make_json_404(YArg, []).
make_json_404(YArg, Fields) ->
% add default status if not provided
F1 = case lists:keyfind(status, 1, Fields) of
false -> Fields ++ [{status, "not found"}];
false -> Fields ++ [{"status", "not found"}];
_Else -> Fields
end,
@ -580,7 +580,7 @@ make_json_405(YArg) -> make_json_405(YArg, []).
make_json_405(YArg, Fields) ->
% add default status if not provided
F1 = case lists:keyfind(status, 1, Fields) of
false -> Fields ++ [{status, "method not allowed"}];
false -> Fields ++ [{"status", "method not allowed"}];
_Else -> Fields
end,
@ -594,15 +594,15 @@ make_json_405(YArg, Fields) ->
make_json_500(YArg, Error) ->
io:format("Error: ~n~p", [Error]),
EJSON = {struct, [
{status, "internal server error"},
{error, lists:flatten(io_lib:format("~p", [Error]))}]},
{"status", "internal server error"},
{"error", lists:flatten(io_lib:format("~p", [Error]))}]},
[{status, 500}, {content, "application/json", json:encode(EJSON)},
{header, ["Access-Control-Allow-Origin: ", get_origin_header(YArg)]},
{header, ["Access-Control-Allow-Credentials: ", "true"]}].
make_json_500(YArg) ->
EJSON = {struct, [
{status, "internal server error"}]},
{"status", "internal server error"}]},
[{status, 500}, {content, "application/json", json:encode(EJSON)},
{header, ["Access-Control-Allow-Origin: ", get_origin_header(YArg)]},
{header, ["Access-Control-Allow-Credentials: ", "true"]}].