Continuing work on the API.

The id_counter module now includes the record directly in the source.
Fixed many typos and small syntax errors.
Added ts_api:dispatch_user/2 to handle different HTTP methods.
Added method placeholder stubs to ts_api.
Implemented ts_api:list_entries/3.
Added ts_user record and ts_user module.
Implemented ts_entry:list/4, the more generic guts of the other list functions.
Created ts_entry:list_asc/3 and ts_entry:list_desc/3.
Fixed ts_json:encode_datetime/1.
This commit is contained in:
Jonathan Bernard
2011-02-02 16:57:58 -06:00
parent 098cd4cbb9
commit 6fe9184c8e
15 changed files with 263 additions and 64 deletions

View File

@ -1,17 +1,19 @@
-module(ts_api).
-compile(export_all).
-include("ts_db_records/hrl").
-include("ts_db_records.hrl").
-include("yaws_api.hrl").
out(YArg) ->
%get the app mod data
PathString = YArg#arg.apmoddata,
PathString = YArg#arg.appmoddata,
% split the path
PathElements = cast PathString of
PathElements = case PathString of
undefined -> []; %handle no end slash: /ts_api
_Any -> re:split(PathString, "/", [{return, list}])
end,
% process request
dispatch_request(YArg, PathElements).
@ -28,15 +30,26 @@ dispatch_request(YArg, [H|T]) ->
Username = path_element_to_atom(H),
case T of
% no additional parameters, show user info
[] -> get_user_info(YArg, Username);
[] -> dispatch_user(YArg, Username);
[Param|Params] ->
Timeline = path_element_to_atom(Param)
Timeline = path_element_to_atom(Param),
dispatch_timeline(YArg, Username, Timeline, Params)
end.
dispatch_user(YArg, Username) ->
Req = YArg#arg.req,
HTTPMethod = Req#http_request.method,
case HTTPMethod of
'GET' -> get_user(YArg, Username);
'PUT' -> put_user(YArg, Username);
'POST' -> put_user(YArg, Username);
'DELETE' -> delete_user(YArg, Username)
end.
% no entries, show timeline pages
dispatch_timeline(YArg, Username, Timeline, []) ->
Req = YArg#arg.arg,
Req = YArg#arg.req,
HTTPMethod = Req#http_request.method,
case HTTPMethod of
@ -51,17 +64,17 @@ dispatch_timeline(YArg, Username, Timeline, [H|T]) ->
Param = path_element_to_atom(H),
case Param of ->
case Param of
list -> list_entries(YArg, Username, Timeline);
by_id -> dispatch_entry_by_id(YArg, Username, Timeline, T);
by_date -> dispatch_entry_by_date(YArg, Username, Timeline, T);
_Other -> make_json_404(YArg)
end.
dispatch_entry_by_id(YArg, Username, Timeline, []) ->
make_json_404(YArg, [{note, "An entry id is expected in the URL."}];
dispatch_entry_by_id(YArg, _Username, _Timeline, []) ->
make_json_404(YArg, [{note, "An entry id is expected in the URL."}]);
dispatch_entry_by_id(YArg, Username, Timeline, [H|T]) ->
dispatch_entry_by_id(YArg, Username, Timeline, [H|_T]) ->
EventId = list_to_integer(H), % TODO: guard against bad input
get_entry_by_id(YArg, Username, Timeline, EventId).
@ -72,6 +85,14 @@ dispatch_entry_by_date(YArg, Username, Timeline, Params) -> todo.
% ======== IMPLEMENTATION ====== %
% ============================== %
get_user(YArg, Username) -> todo.
put_user(YArg, Username) -> todo.
post_user(YArg, Username) -> todo.
delete_user(YArg, Username) -> todo.
get_timeline(YArg, Username, TimelineId) ->
case ts_timeline:lookup(Username, TimelineId) of
@ -100,7 +121,7 @@ put_timeline(YArg, Username, TimelineId) ->
% record created
ok ->
EJSONRec = ts_json:record_to_ejson(NewRecord)
EJSONRec = ts_json:record_to_ejson(NewRecord),
JSONReturn = json:encode({struct, [
{status, "ok"},
{timeline, EJSONRec}
@ -157,12 +178,25 @@ list_entries(YArg, Username, Timeline) ->
% read or default the Start
Start = case lists:keyfind(start, 1, PostData) of
{start, StartVal} -> StartVal; false -> 0 end.
{start, StartVal} -> StartVal; false -> 0 end,
% read or default the Length
Length = case lists:keyfind(length, 1, PostData)
Length = case lists:keyfind(length, 1, PostData) of
{length, LengthVal} -> LengthVal; false -> 50 end,
% read or default the sort order
SortOrder = case lists:keyfind(order, 1, PostData) of
{order, "asc"} -> asc;
{order, "desc"} -> desc;
_Other -> asc
end,
Entries = case SortOrder of
asc -> ts_entry:list_asc({Username, Timeline}, Start, Length);
desc -> ts_entry:list_desc({Username, Timeline}, Start, Length)
end
.
get_entry_by_id(YArg, Username, Timeline, EventId) ->
case ts_entry:lookup(Username, Timeline, EventId) of
@ -197,14 +231,14 @@ 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,
% add the path they requested
F2 = F1 ++ [{path, (YArg#arg.req)#http_request.path}],
F2 = F1 ++ [{path, element(2, (YArg#arg.req)#http_request.path)}],
[{status, 404}, {content, "application/json", json:encode({struct, Fields})}].
[{status, 404}, {content, "application/json", json:encode({struct, F2})}].
make_json_405(YArg) -> make_json_405(YArg, []).
make_json_405(YArg, Fields) ->
@ -217,7 +251,7 @@ make_json_405(YArg, Fields) ->
% add the path they requested
F2 = F1 ++ [{path, (YArg#arg.req)#http_request.path}],
[{status, 405}, {content, "application/json", json:encode({struct, Fields})}].
[{status, 405}, {content, "application/json", json:encode({struct, F2})}].
make_json_500(_YArg) ->
EJSON = {struct, [