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); | ||||
|  | ||||
|         {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); | ||||
| @@ -151,7 +151,7 @@ dispatch_entry(YArg, [UrlUsername, UrlTimelineId]) -> | ||||
|  | ||||
|     case HTTPMethod of | ||||
|         '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"}]) | ||||
|     end; | ||||
|  | ||||
| @@ -163,7 +163,7 @@ dispatch_entry(YArg, [UrlUsername, UrlTimelineId, UrlEntryId]) -> | ||||
|  | ||||
|     case HTTPMethod of | ||||
|         '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); | ||||
|         _Other   -> make_json_405(YArg, [{see_docs, "/ts_api_doc/entries.html"}]) | ||||
|     end; | ||||
| @@ -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,12 +270,12 @@ 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. | ||||
|  | ||||
| put_timeline(YArg, Username, TimelineId) -> | ||||
| post_timeline(YArg, Username, TimelineId) -> | ||||
|  | ||||
|     % parse the request body | ||||
|     EJSON = parse_json_body(YArg), | ||||
| @@ -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}; | ||||
|  | ||||
| @@ -311,7 +303,7 @@ put_timeline(YArg, Username, TimelineId) -> | ||||
|             make_json_500(YArg, Error) | ||||
|     end. | ||||
|  | ||||
| post_timeline(YArg, Username, TimelineId) -> | ||||
| put_timeline(YArg, Username, TimelineId) -> | ||||
|     % parse the POST data | ||||
|     EJSON = parse_json_body(YArg), | ||||
|     %{struct, Fields} = EJSON, | ||||
| @@ -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,12 +417,12 @@ 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. | ||||
|  | ||||
| put_entry(YArg, Username, TimelineId) -> | ||||
| post_entry(YArg, Username, TimelineId) -> | ||||
|  | ||||
|     % parse the request body | ||||
|     EJSON = parse_json_body(YArg), | ||||
| @@ -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}; | ||||
|  | ||||
| @@ -466,7 +449,7 @@ put_entry(YArg, Username, TimelineId) -> | ||||
|             make_json_500(YArg, OtherError) | ||||
|     end. | ||||
|  | ||||
| post_entry(YArg, Username, TimelineId, EntryId) ->  | ||||
| put_entry(YArg, Username, TimelineId, EntryId) ->  | ||||
|      | ||||
|     % parse the POST data | ||||
|     EJSON = parse_json_body(YArg), | ||||
| @@ -481,7 +464,7 @@ post_entry(YArg, Username, TimelineId, EntryId) -> | ||||
|         ok -> make_json_200(YArg, NewRecord); | ||||
|  | ||||
|         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_logger:error_report("TimeStamper: Unable to update entry: ~p", [Error]), | ||||
| @@ -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, []). | ||||
|   | ||||
| @@ -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}]}. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user