API now returns just the content in the body.

- Instead of returning Meta-Data (status) in the header and the body, we now
  only return the content in the body, except in error when we return the error
  message.

- JSON entries use the 'id' name for entity ids (instead of 'username',
  'timeline_id', and 'entry_id')
This commit is contained in:
Jonathan Bernard 2011-04-15 13:51:01 -05:00
parent d0aab2d0c6
commit 17c5b9cbd1
2 changed files with 18 additions and 45 deletions

View File

@ -41,7 +41,7 @@ dispatch_request(YArg, Session, [H|T]) ->
{_, logout} -> do_logout(YArg);
{not_logged_in, _} -> make_json_401(YArg);
{session_expired, _} -> make_json_401(YArg, [{status, "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);
@ -200,7 +200,7 @@ do_login(YArg) ->
[CookieVal])}}];
% they are not good
false -> make_json_401(YArg, [{status,
false -> make_json_401(YArg, [{error,
"bad username/password combination"}])
end;
@ -224,8 +224,7 @@ get_user_summary(YArg, Username) ->
lists:map(fun ts_json:record_to_ejson/1, Timelines)},
JSONResp = json:encode({struct,
[{status, "ok"},
{user, EJSONUser},
[{user, EJSONUser},
{timelines, EJSONTimelines}
]}),
@ -262,9 +261,7 @@ list_timelines(YArg, Username) ->
EJSONTimelines = {array, lists:map(fun ts_json:record_to_ejson/1, Timelines)},
% create resposne
JSONResponse = json:encode({struct, [
{status, "ok"},
{timelines, EJSONTimelines}]}),
JSONResponse = json:encode(EJSONTimelines),
% return response
{content, "application/json", JSONResponse}.
@ -273,7 +270,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, [{status, "no such timeline"}]);
no_record -> make_json_404(YArg, [{error, "no such timeline"}]);
% return the timeline data
Timeline -> make_json_200(YArg, Timeline)
end.
@ -297,12 +294,7 @@ put_timeline(YArg, Username, TimelineId) ->
% will not create, record exists
{error, {record_exists, ExistingRecord}} ->
EJSONRec = ts_json:record_to_ejson(ExistingRecord),
JSONResponse = json:encode({struct, [
{status, "ignored"},
{timeline, EJSONRec},
{see_docs, "/ts_api_doc/timelines.html#PUT"}
]}),
JSONResponse = json:encode(ts_json:record_to_ejson(ExistingRecord)),
{content, "application/json", JSONResponse};
@ -332,7 +324,7 @@ post_timeline(YArg, Username, TimelineId) ->
ok -> make_json_200(YArg, NewRecord);
no_record -> make_json_404(YArg,
[{status, "no such timeline"},
[{error, "no such timeline"},
{see_docs, "/ts_api_doc/timelines.html#POST"}]);
Error ->
@ -351,7 +343,7 @@ list_entries(YArg, Username, TimelineId) ->
lists:keyfind("byDate", 1, QueryData)} of
{no_record, _ByDateField} -> make_json_404(
[{status, "no such timeline"},
[{error, "no such timeline"},
{see_docs, "/ts_api_doc/entries.html#LIST"}]);
% listing by date range
@ -385,9 +377,7 @@ list_entries(YArg, Username, TimelineId) ->
EJSONEntries = {array, lists:map(
fun ts_json:record_to_ejson/1, Entries)},
JSONResponse = json:encode({struct, [
{status, "ok"},
{entries, EJSONEntries}]}),
JSONResponse = json:encode(EJSONEntries),
{content, "application/json", JSONResponse};
@ -419,9 +409,7 @@ list_entries(YArg, Username, TimelineId) ->
EJSONEntries = {array, lists:map(
fun ts_json:record_to_ejson/1, Entries)},
JSONResponse = json:encode({struct, [
{status, "ok"},
{entries, EJSONEntries}]}),
JSONResponse = json:encode(EJSONEntries),
{content, "application/json", JSONResponse}
end.
@ -429,7 +417,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, [{status, "no such entry"}]);
no_record -> make_json_404(YArg, [{error, "no such entry"}]);
% return the entry data
Entry -> make_json_200(YArg, Entry)
end.
@ -452,12 +440,7 @@ put_entry(YArg, Username, TimelineId) ->
% will not create, record exists
{error, {record_exists, ExistingRecord}} ->
EJSONRec = ts_json:record_to_ejson(ExistingRecord),
JSONResponse = json:encode({struct, [
{status, "ignored"},
{entry, EJSONRec},
{see_docs, "/ts_api_doc/entries.html#PUT"}
]}),
JSONResponse = json:encode(ts_json:record_to_ejson(ExistingRecord)),
{content, "application/json", JSONResponse};
@ -524,17 +507,7 @@ parse_json_body(YArg) ->
%% Create a JSON 200 response.
make_json_200(_YArg, Record) ->
EJSONRecord = ts_json:record_to_ejson(Record),
Tag = case element(1, Record) of
ts_user -> user;
ts_timeline -> timeline;
ts_entry -> entry
end,
JSONResponse = json:encode({struct, [
{status, "ok"},
{Tag, EJSONRecord}
]}),
JSONResponse = json:encode(ts_json:record_to_ejson(Record)),
{content, "application/json", JSONResponse}.
make_json_400(YArg) -> make_json_400(YArg, []).

View File

@ -7,7 +7,7 @@ encode_record(Record) -> lists:flatten(json:encode(record_to_ejson(Record))).
record_to_ejson(Record=#ts_user{}) ->
{struct, [
{username, atom_to_list(Record#ts_user.username)},
{id, atom_to_list(Record#ts_user.username)},
{name, Record#ts_user.name},
{email, Record#ts_user.email},
{join_date, encode_datetime(Record#ts_user.join_date)}]};
@ -18,8 +18,8 @@ record_to_ejson(Record=#ts_timeline{}) ->
% create the EJSON struct
{struct, [
{username, atom_to_list(Username)},
{timeline_id, atom_to_list(TimelineId)},
{user_id, atom_to_list(Username)},
{id, atom_to_list(TimelineId)},
{created, encode_datetime(Record#ts_timeline.created)},
{description, Record#ts_timeline.desc}]};
@ -32,9 +32,9 @@ record_to_ejson(Record=#ts_entry{}) ->
% create the EJSON struct
{struct, [
{username, atom_to_list(Username)},
{user_id, atom_to_list(Username)},
{timeline_id, atom_to_list(TimelineId)},
{entry_id, EntryId},
{id, EntryId},
{timestamp, encode_datetime(DateTime)},
{mark, Record#ts_entry.mark},
{notes, Record#ts_entry.notes}]}.