Skip to content

Commit

Permalink
KAZOO-4088: Use new pdf binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Defebvre committed Sep 16, 2015
1 parent f072ef6 commit e04072d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 28 deletions.
27 changes: 26 additions & 1 deletion applications/crossbar/src/api_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
,delete_resource/2
,delete_completed/2
,is_conflict/2
,to_json/2, to_binary/2, to_csv/2
,to_json/2, to_binary/2, to_csv/2, to_pdf/2
,from_json/2, from_binary/2, from_form/2
,multiple_choices/2
,generate_etag/2
Expand Down Expand Up @@ -681,6 +681,9 @@ to_json(Req0, Context0, 'undefined') ->
to_json(Req, Context, <<"csv">>) ->
lager:debug("overridding json with csv builder"),
to_csv(Req, Context);
to_json(Req, Context, <<"pdf">>) ->
lager:debug("overridding json with pdf builder"),
to_pdf(Req, Context);
to_json(Req, Context, Accept) ->
case to_fun(Context, Accept, 'to_json') of
'to_json' -> to_json(Req, Context, 'undefined');
Expand Down Expand Up @@ -762,6 +765,28 @@ to_csv(Req, Context) ->
}
end.

-spec to_pdf(cowboy_req:req(), cb_context:context()) ->
{binary(), cowboy_req:req(), cb_context:context()}.
to_pdf(Req, Context) ->
lager:debug("run: to_pdf"),
[{Mod, _Params}|_] = cb_context:req_nouns(Context),
Verb = cb_context:req_verb(Context),
Event = api_util:create_event_name(Context, [<<"to_pdf">>
,wh_util:to_lower_binary(Verb)
,Mod
]),
{Req1, Context1} = crossbar_bindings:fold(Event, {Req, Context}),
RespData = cb_context:resp_data(Context1),
RespHeaders = [{<<"Content-Type">>, <<"application/pdf">>}
,{<<"Content-Length">>, erlang:size(RespData)}
,{<<"Content-Disposition">>, <<"attachment; filename=\"file.pdf\"">>}
| cb_context:resp_headers(Context1)
],
{RespData
,api_util:set_resp_headers(Req1, cb_context:set_resp_headers(Context1, RespHeaders))
,Context1
}.

-spec accept_override(cb_context:context()) -> api_binary().
accept_override(Context) ->
cb_context:req_value(Context, <<"accept">>).
Expand Down
2 changes: 2 additions & 0 deletions applications/crossbar/src/crossbar_types.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
,{<<"application">>, <<"x-base64">>}
]).

-define(PDF_CONTENT_TYPES, [{<<"application">>, <<"pdf">>}]).

-define(JSONP_CONTENT_TYPE, <<"application/javascript">>).

-define(CROSSBAR_TYPES_INCLUDED, 'true').
Expand Down
65 changes: 38 additions & 27 deletions applications/crossbar/src/modules/cb_directories.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
-module(cb_directories).

-export([init/0
,allowed_methods/0, allowed_methods/1, allowed_methods/2
,resource_exists/0, resource_exists/1, resource_exists/2
,content_types_provided/3
,validate/1, validate/2, validate/3
,allowed_methods/0, allowed_methods/1
,resource_exists/0, resource_exists/1
,content_types_provided/2
,to_pdf/1
,validate/1, validate/2
,put/1
,post/2
,patch/2
Expand All @@ -26,8 +27,9 @@
-define(PVT_FUNS, [fun add_pvt_type/2]).
-define(CB_LIST, <<"directories/crossbar_listing">>).
-define(CB_USERS_LIST, <<"directories/users_listing">>).
-define(PDF, <<"pdf">>).
-define(ATTACHMENT_MIME_TYPES, [{<<"application">>, <<"pdf">>}]).

-type payload() :: {cowboy_req:req(), cb_context:context()}.
-export_type([payload/0]).

%%%===================================================================
%%% API
Expand All @@ -36,6 +38,7 @@ init() ->
_ = crossbar_bindings:bind(<<"*.allowed_methods.directories">>, ?MODULE, 'allowed_methods'),
_ = crossbar_bindings:bind(<<"*.resource_exists.directories">>, ?MODULE, 'resource_exists'),
_ = crossbar_bindings:bind(<<"*.content_types_provided.directories">>, ?MODULE, 'content_types_provided'),
_ = crossbar_bindings:bind(<<"*.to_pdf.get.directories">>, ?MODULE, 'to_pdf'),
_ = crossbar_bindings:bind(<<"*.validate.directories">>, ?MODULE, 'validate'),
_ = crossbar_bindings:bind(<<"*.execute.put.directories">>, ?MODULE, 'put'),
_ = crossbar_bindings:bind(<<"*.execute.post.directories">>, ?MODULE, 'post'),
Expand All @@ -53,13 +56,10 @@ init() ->
%%--------------------------------------------------------------------
-spec allowed_methods() -> http_methods().
-spec allowed_methods(path_token()) -> http_methods().
-spec allowed_methods(path_token(), path_token()) -> http_methods().
allowed_methods() ->
[?HTTP_GET, ?HTTP_PUT].
allowed_methods(_) ->
[?HTTP_GET, ?HTTP_POST, ?HTTP_PATCH, ?HTTP_DELETE].
allowed_methods(_, ?PDF) ->
[?HTTP_GET].

%%--------------------------------------------------------------------
%% @public
Expand All @@ -71,10 +71,8 @@ allowed_methods(_, ?PDF) ->
%%--------------------------------------------------------------------
-spec resource_exists() -> 'true'.
-spec resource_exists(path_token()) -> 'true'.
-spec resource_exists(path_token(), path_token()) -> 'true'.
resource_exists() -> 'true'.
resource_exists(_) -> 'true'.
resource_exists(_, ?PDF) -> 'true'.

%%--------------------------------------------------------------------
%% @public
Expand All @@ -84,15 +82,39 @@ resource_exists(_, ?PDF) -> 'true'.
%% Of the form {atom, [{Type, SubType}]} :: {to_json, [{<<"application">>, <<"json">>}]}
%% @end
%%--------------------------------------------------------------------
-spec content_types_provided(cb_context:context(), path_token(), path_token()) -> cb_context:context().
content_types_provided(Context, _Id, ?PDF) ->
-spec content_types_provided(cb_context:context(), path_token()) -> cb_context:context().
content_types_provided(Context, _Id) ->
case cb_context:req_verb(Context) of
?HTTP_GET ->
cb_context:add_content_types_provided(Context, [{'to_binary', ?ATTACHMENT_MIME_TYPES}]);
cb_context:add_content_types_provided(
Context
,[{'to_pdf', ?PDF_CONTENT_TYPES}]
);
_Verb ->
Context
end.

%%--------------------------------------------------------------------
%% @public
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec to_pdf(payload()) -> payload().
to_pdf({Req, Context}) ->
Nouns = cb_context:req_nouns(Context),
case props:get_value(<<"directories">>, Nouns, []) of
[] -> {Req, Context};
[Id|_] ->
Context1 = read(Id, Context),
case cb_context:resp_status(Context1) of
'success' -> {Req, get_pdf(Context1)};
_Status -> {Req, Context1}
end
end.




%%--------------------------------------------------------------------
%% @public
%% @doc
Expand All @@ -104,16 +126,12 @@ content_types_provided(Context, _Id, ?PDF) ->
%%--------------------------------------------------------------------
-spec validate(cb_context:context()) -> cb_context:context().
-spec validate(cb_context:context(), path_token()) -> cb_context:context().
-spec validate(cb_context:context(), path_token(), path_token()) -> cb_context:context().
validate(Context) ->
validate_directories(Context, cb_context:req_verb(Context)).

validate(Context, Id) ->
validate_directory(Context, Id, cb_context:req_verb(Context)).

validate(Context, Id, PathToken) ->
validate_directory(Context, Id, PathToken, cb_context:req_verb(Context)).

%%--------------------------------------------------------------------
%% @public
%% @doc
Expand Down Expand Up @@ -173,7 +191,6 @@ validate_directories(Context, ?HTTP_PUT) ->
%% @end
%%--------------------------------------------------------------------
-spec validate_directory(cb_context:context(), ne_binary(), path_token()) -> cb_context:context().
-spec validate_directory(cb_context:context(), ne_binary(), path_token(), path_token()) -> cb_context:context().
validate_directory(Context, Id, ?HTTP_GET) ->
read(Id, Context);
validate_directory(Context, Id, ?HTTP_POST) ->
Expand All @@ -183,13 +200,6 @@ validate_directory(Context, Id, ?HTTP_PATCH) ->
validate_directory(Context, Id, ?HTTP_DELETE) ->
read(Id, Context).

validate_directory(Context, Id, ?PDF, ?HTTP_GET) ->
Context1 = read(Id, Context),
case cb_context:resp_status(Context1) of
'success' -> get_pdf(Context1);
_Status -> Context1
end.

%%--------------------------------------------------------------------
%% @private
%% @doc
Expand All @@ -202,7 +212,8 @@ get_pdf(Context) ->
case kz_pdf:generate(AccountId, Data) of
{'error', Reason} ->
cb_context:add_system_error(Reason, Context);
{'ok', PDF} -> cb_context:set_resp_data(Context, PDF)
{'ok', PDF} ->
cb_context:set_resp_data(Context, PDF)

end.

Expand Down

0 comments on commit e04072d

Please sign in to comment.