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