Monday, August 6, 2007

Blogger API gen_server !

I'm proud to announce the start of the Blogger API gen_server !

I'll use the code I already wrote for testing the blogger API, and put some OTP requirement around it... For the moment there's three gen_server call:
  • new: to create a new post
  • reset: to reset credentials, i.e. retrieve another AuthToken
  • auth: to retrieve the AuthToken (recomputing it if needed)

Extracted from the 'blogger_srv.erl' file:

auth(Username, Password) ->
gen_server:call(?MODULE, {auth, Username, Password}).

new(BlogId, Title, Tags, Content, {AuthorName, AuthorEmail} ) ->
gen_server:call(?MODULE, {post, BlogId, Title, Tags, Content, AuthorName, AuthorEmail}).

reset() ->
gen_server:cast(?MODULE, reset).

For example, the 'post' fun:

handle_call({post, BlogId, Title, Tags, Content, {AuthorName, AuthorEmail}}, _Node, State) ->
Requests = State#state.requests,
Auth = State#state.auth,
Data = entry_new(Title, {AuthorName, AuthorEmail}, Content, Tags),
case request(Auth, BlogId, Data) of
{ok, PostId} ->
{reply, {ok, PostId}, State#state{ requests = Requests + 1 } };

Msg ->
{reply, {err, Msg}, State#state{ requests = Requests + 1 } }

No comments: