Merge branch 'api-redesign' into client-redesign
This commit is contained in:
		| @@ -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); | ||||||
| @@ -151,7 +151,7 @@ dispatch_entry(YArg, [UrlUsername, UrlTimelineId]) -> | |||||||
|  |  | ||||||
|     case HTTPMethod of |     case HTTPMethod of | ||||||
|         'GET'  -> list_entries(YArg, Username, TimelineId); |         'GET'  -> list_entries(YArg, Username, TimelineId); | ||||||
|         'PUT'  -> put_entry(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; |     end; | ||||||
|  |  | ||||||
| @@ -163,7 +163,7 @@ dispatch_entry(YArg, [UrlUsername, UrlTimelineId, UrlEntryId]) -> | |||||||
|  |  | ||||||
|     case HTTPMethod of |     case HTTPMethod of | ||||||
|         'GET'    -> get_entry(YArg, Username, TimelineId, EntryId); |         'GET'    -> get_entry(YArg, Username, TimelineId, EntryId); | ||||||
|         'POST'   -> post_entry(YArg, Username, TimelineId, EntryId); |         'PUT'    -> put_entry(YArg, Username, TimelineId, EntryId); | ||||||
|         'DELETE' -> delete_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; |     end; | ||||||
| @@ -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,12 +270,12 @@ 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. | ||||||
|  |  | ||||||
| put_timeline(YArg, Username, TimelineId) -> | post_timeline(YArg, Username, TimelineId) -> | ||||||
|  |  | ||||||
|     % parse the request body |     % parse the request body | ||||||
|     EJSON = parse_json_body(YArg), |     EJSON = parse_json_body(YArg), | ||||||
| @@ -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}; | ||||||
|  |  | ||||||
| @@ -311,7 +303,7 @@ put_timeline(YArg, Username, TimelineId) -> | |||||||
|             make_json_500(YArg, Error) |             make_json_500(YArg, Error) | ||||||
|     end. |     end. | ||||||
|  |  | ||||||
| post_timeline(YArg, Username, TimelineId) -> | put_timeline(YArg, Username, TimelineId) -> | ||||||
|     % parse the POST data |     % parse the POST data | ||||||
|     EJSON = parse_json_body(YArg), |     EJSON = parse_json_body(YArg), | ||||||
|     %{struct, Fields} = EJSON, |     %{struct, Fields} = EJSON, | ||||||
| @@ -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,12 +417,12 @@ 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. | ||||||
|  |  | ||||||
| put_entry(YArg, Username, TimelineId) -> | post_entry(YArg, Username, TimelineId) -> | ||||||
|  |  | ||||||
|     % parse the request body |     % parse the request body | ||||||
|     EJSON = parse_json_body(YArg), |     EJSON = parse_json_body(YArg), | ||||||
| @@ -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}; | ||||||
|  |  | ||||||
| @@ -466,7 +449,7 @@ put_entry(YArg, Username, TimelineId) -> | |||||||
|             make_json_500(YArg, OtherError) |             make_json_500(YArg, OtherError) | ||||||
|     end. |     end. | ||||||
|  |  | ||||||
| post_entry(YArg, Username, TimelineId, EntryId) ->  | put_entry(YArg, Username, TimelineId, EntryId) ->  | ||||||
|      |      | ||||||
|     % parse the POST data |     % parse the POST data | ||||||
|     EJSON = parse_json_body(YArg), |     EJSON = parse_json_body(YArg), | ||||||
| @@ -481,7 +464,7 @@ post_entry(YArg, Username, TimelineId, EntryId) -> | |||||||
|         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 entry"}, {see_docs, "/ts_api_doc/entries.html#POST"}]); |             [{error, "no such entry"}, {see_docs, "/ts_api_doc/entries.html#POST"}]); | ||||||
|  |  | ||||||
|         Error ->  |         Error ->  | ||||||
|             error_logger:error_report("TimeStamper: Unable to update entry: ~p", [Error]), |             error_logger:error_report("TimeStamper: Unable to update entry: ~p", [Error]), | ||||||
| @@ -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, []). | ||||||
|   | |||||||
| @@ -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}]}. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user