state Resource functions are referentially transparent and have a uniform interface. Many resource functions are optional and use reasonable defaults. + +
state Resource functions are referentially transparent and have a uniform interface. Many resource functions are optional and use reasonable defaults. + +
state Resource functions are referentially transparent and have a uniform interface. Many resource functions are optional and use reasonable defaults. + +
state Resource functions are referentially transparent and have a uniform interface. Many resource functions are optional and use reasonable defaults. + +
init/1 •Put the value of the Host request header inside the response body using an iolist() and: wrq:get_req_header(Key, ReqData) * Hint: header keys are lowercase strings Exercises
%% As many types as you want! content_types_provided(ReqData, State) -> {[{"text/html", to_html}, {"application/json", to_json}, {"text/xml", to_xml}], ReqData, State}. media type body-producer function Media-types callback
-> [{[""], ?MODULE, []}, {['*'], ?MODULE, []}]. Dispatching path segments resource module args to init/1 matches any number of segments The dispatch list is set before starting up the server in tweeter_sup.
option) to exclude application/json, compare the response. •Add the application/x-erlang-binary format to the resource. Use term_to_binary/1 to generate the body. Exercises
Dispatch rule that binds a path segment %% to the atom ‘key’ routes() -> [{["data", key], ?MODULE, []}]. %% Do a query to get the data using the %% bound dispatch path segment resource_exists(ReqData, State) -> Key = wrq:path_info(key, ReqData), case query_storage(Key) of undefined -> {false, ReqData, State}; Value -> {true, ReqData, State#state{data=Value}} end. resource_exists
After to_text(ReqData, Context) -> {{stream, {<<>>, fun stream/0}}, ReqData, Context}. %% Stream response as a "lazy sequence", with the %% Webmachine process waiting on messages. stream() -> receive {chat, Message} -> {[Message, "\n"], fun stream/0}; quit -> {<<>>, done} end. Streaming Responses
members of the process group Members = pg2:get_members(chat). %% Join the process group pg2:join(chat, self()). %% Send a message to all members [Member ! {chat, Msg} || Member <- Members]. OTP: Process Groups
fetch again with If-None-Match header. •Add a tweet via the UI, send same curl request as last step. •Add a last_modified callback, using ID of the latest tweet as the timestamp. Exercises