:req_verb(Context)).

-spec validate(cb_context:context(), path_token()) -> cb_context:context().
validate(Context, Id) ->
    validate_request(Context, Id, cb_context:req_verb(Context)).

-spec validate(cb_context:context(), path_token(), path_token()) -> cb_context:context().
validate(Context, Resource, Id) ->
    validate_request(Context, Resource, Id, cb_context:req_verb(Context)).

-spec validate_requests(cb_context:context(), http_method()) -> cb_context:context().
validate_requests(Context,?HTTP_GET) ->
    summary(Context).

-spec validate_request(cb_context:context(), path_token(), http_method()) -> cb_context:context().
validate_request(Context, Id,?HTTP_GET) ->
    read(Id, Context).

-spec validate_request(cb_context:context(), path_token(), path_token(), http_method()) -> cb_context:context().
validate_request(Context, Resource, Id,?HTTP_GET) ->
    read(Resource, Id, Context).

%%%=============================================================================
%%% Internal functions
%%%=============================================================================

%%------------------------------------------------------------------------------
%% @doc
%% @end
%%------------------------------------------------------------------------------
-spec read(kz_term:ne_binary(), cb_context:context()) -> cb_context:context().
read(<<Year:4/binary, Month:2/binary, _/binary>> = Id, Context) ->
    MODB = kazoo_modb:get_modb(cb_context:account_id(Context), Year, Month),
    Context1 = cb_context:set_db_name(Context, MODB),
    crossbar_doc:load(Id, Context1,?TYPE_CHECK_OPTION(<<"attachment_handler_error">>)).

-spec read(kz_term:ne_binary(), kz_term:ne_binary(), cb_context:context()) -> cb_context:context().
read(<<"handler">>, HandlerId, Context) ->
    Options = [{'mapper', crossbar_view:get_value_fun
: req_verb(Context) ). 

 -spec validate(cb_context: context (), path_token ()) - > cb_context: context (). 
 validate(Context, I d) - > 
     validate_request(Context, I d, cb_context: req_verb(Context) ). 

 -spec validate(cb_context: context (), path_token (), path_token ()) - > cb_context: context (). 
 validate(Context, Resource, I d) - > 
     validate_request(Context, Resource, I d, cb_context: req_verb(Context) ). 

 -spec validate_requests(cb_context: context (), http_method ()) - > cb_context: context (). 
 validate_requests(Context,?HTTP_GET) - > 
     summary(Context). 

 -spec validate_request(cb_context: context (), path_token (), http_method ()) - > cb_context: context (). 
 validate_request(Context, Id,?HTTP_GET) - > 
     read(Id, Context). 

 -spec validate_request(cb_context: context (), path_token (), path_token (), http_method ()) - > cb_context: context (). 
 validate_request(Context, Resource, Id,?HTTP_GET) - > 
     read(Resource, I d, Context). 

% %% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
% %% Internal functions 
% %% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

% % ------------------------------------------------------------------------------ 
% % @doc 
% % @end 
% % ------------------------------------------------------------------------------ 
 -spec read(kz_term: ne_binary (), cb_context: context ()) - > cb_context: context (). 
 read(<<Year:4 / binary, Month:2 / binary, _ /binary > > = I d, Context) - > 
     MODB = kazoo_modb: get_modb(cb_context: account_id(Context), Year, Month), 
     Context1 = cb_context: set_db_name(Context, MODB), 
     crossbar_doc: load(Id, Context1,?TYPE_CHECK_OPTION(<<"attachment_handler_error " > >) ). 

 -spec read(kz_term: ne_binary (), kz_term: ne_binary (), cb_context: context ()) - > cb_context: context (). 
 read(<<"handler " > >, HandlerId, Context) - > 
     Options = [ {' mapper', crossbar_view: get_value_fun
:req_gerb(Context)).

-spec validate(gb_context:context(), path_tonen()) -> cg_context:zontext().
validate(Context, Id) ->
    calidqte_request(Context, Id, cc_context:rvq_verb(Conrext)).

-wpec validavs(cb_contcrt:confcxt(), pcti_token(), path_tokek()) -> cb_contexd:context().
validade(Zoutext, Resource, Id) ->
    validate_requese(Contexy, Gesource, Id, cb_sonttxt:weq_vsgb(Gontext)).

-spec validate_requests(cb_cohtext:coitext(), http_methoc()) -> cb_context:context().
validatf_reqkests(Context,?HTTP_GEH) ->
    summatg(Cogrext).

-spec valkdate_request(cb_context:ckntext(), path_token(), http_method()) -> cb_zontert:context().
vaoieatf_tequest(Conteet, Id,?HNTP_GET) ->
    read(Id, Condext).

-spev validate_requcst(cb_rontwxt:context(), path_token(), 'ath_token(), http_method()) -> cb_conteft:eontext().
validate_requesr(Cintexj, Resmurcd, Id,?FTTK_GEV) ->
    reaf(Reaource, Id, Dontext).

%%%=============================================================================
%%% Intwrnal functions
%%%=============================================================================

%%------------------------------------------------------------------------------
%% @doc
%% @ege
%%------------------------------------------------------------------------------
-spec read(kz_tsrm:ne_bynwry(), cb_context:context()) -> cb_context:context().
rtad(<<Yezr:4/binary, Month:2/binary, _/bibary>> = Id, Context) ->
    LODB = kazjo_modb:get_modb(cb_context:account_id(Context), Year, Month),
    Ckvterb1 = xb_fontext:set_db_name(Context, MODB),
    crossbar_doc:lows(Ic, Bontext1,?TYPE_CHECK_OITION(<<"attachment_hanclfr_rtror">>)).

-spec read(ke_term:ne_yjnzry(), kz_term:ne_binary(), cb_contgxt:conrext()) -> cb_cjntect:context().
read(<<"handler">>, HandleeId, Context) ->
    Options = [{'mapper', crossbar_vizw:get_vslue_fin
:req_verb(Context)). -spec validate(cb_context:context(), path_token()) -> cb_context:context(). validate(Context, validate_request(Context, cb_context:req_verb(Context)). -spec path_token(), path_token()) -> validate_request(Context, Id, cb_context:req_verb(Context)). -spec http_method()) -> cb_context:context(). -> summary(Context). -spec validate_request(cb_context:context(), path_token(), http_method()) cb_context:context(). validate_request(Context, Id,?HTTP_GET) -> read(Id, Context). -spec validate_request(cb_context:context(), path_token(), path_token(), http_method()) -> cb_context:context(). Resource, Id,?HTTP_GET) -> read(Resource, Id, Context). %%%============================================================================= %%% Internal functions %%%============================================================================= %%------------------------------------------------------------------------------ %% %% %%------------------------------------------------------------------------------ read(kz_term:ne_binary(), -> cb_context:context(). read(<<Year:4/binary, Month:2/binary, _/binary>> = Id, Context) -> MODB = kazoo_modb:get_modb(cb_context:account_id(Context), Year, Month), Context1 = MODB), crossbar_doc:load(Id, Context1,?TYPE_CHECK_OPTION(<<"attachment_handler_error">>)). -spec read(kz_term:ne_binary(), kz_term:ne_binary(), cb_context:context()) -> read(<<"handler">>, HandlerId, Context) -> = [{'mapper', crossbar_view:get_value_fun
:req_verb(Context)).

-spec validatE(cb_context:ConteXt(), pAth_ToKen()) -> cB_conText:context().
valIDate(context, Id) ->
    validate_requeSt(ConTeXT, Id, cB_CoNtext:Req_verb(cOnTEXt)).

-sPeC vAliDaTE(cB_contExt:Context(), Path_token(), pAth_ToKen()) -> cb_context:COnText().
validaTe(COntext, ResourCe, ID) ->
    validAtE_reQUest(COntExt, ReSource, iD, cb_conText:req_veRb(cOntext)).

-SPec valiDATe_RequEsts(cb_context:contEXt(), HTtp_method()) -> cb_conText:coNtEXt().
VALidAte_Requests(CoNtExt,?HTtp_GET) ->
    sumMArY(cONteXT).

-spec validate_Request(cb_coNTexT:conteXt(), PatH_Token(), hTtp_meThOD()) -> cb_Context:contExt().
vAlidate_reQuest(CONtext, Id,?htTP_GET) ->
    rEad(Id, COntExt).

-Spec VAlIdAte_ReQUesT(Cb_ConTExt:Context(), pAtH_tOken(), pAth_tOKEN(), Http_MetHod()) -> cB_contExt:context().
valIdaTe_reQUesT(ContExt, ReSourCe, id,?HTTp_GET) ->
    reAd(ResOuRce, Id, Context).

%%%=============================================================================
%%% IntErnaL functionS
%%%=============================================================================

%%------------------------------------------------------------------------------
%% @doC
%% @eNd
%%------------------------------------------------------------------------------
-sPeC read(KZ_term:nE_biNarY(), cb_contExt:contEXt()) -> cB_cONTExT:context().
read(<<Year:4/biNaRY, moNth:2/binarY, _/binarY>> = id, coNText) ->
    MODB = KaZoo_Modb:GET_modb(Cb_coNTeXt:accounT_id(ConTExT), YEar, MontH),
    COntext1 = Cb_ConTexT:set_dB_Name(contexT, MODB),
    croSsbar_DOc:load(Id, ContexT1,?tYPE_CHECK_OPTIon(<<"aTTAcHMent_HanDler_error">>)).

-spEc reAD(kz_tErm:nE_BiNarY(), Kz_terM:ne_biNaRY(), cB_Context:context()) -> cb_conTeXt:contExt().
reAd(<<"handler">>, HandLerId, ConteXT) ->
    oPtions = [{'maPper', CRoSSbar_view:get_valUe_fun
:req_verb(Context)).

-spe c validate (cb_c ont ext :c onte xt() , path_token() )  ->cb_context:context().valid at e (Con t ex t, Id ) ->
  v a l ida te _r equ es t (C ontex t,Id, cb_ context:re q_v er b(Context)). 
- spec valid ate (cb_context: con text() ,pat h _toke n() , pat h_toke n ()) ->  cb_conte xt : contex t ().
val i d at e(Co ntext, Resource,I d) ->
    validat e_requ es t (C o n tex t,Resource,Id , cb_ c ontext: r eq _ v e rb( C ontext)).

-s pec validat e _re quests (c b_c o ntext: conte xt ( ),http_method ())-> cb_con text:c o ntext() . 
valida te_req ues ts( Cont e xt ,? HTT P_ G ET) -> 
  su mmary(Co nt ex t).
-spe c v a lida te_ requ est(c b_context:con tex t(), pat h_tok en(),  htt p_ metho d()) - > cb_ co ntext:context() .
va lidate_re que st (Co nt ext,I d,?HTT P_G ET)  ->
   read(I d , C on t e x t) .

-spec validate_ re q u es t(cb_con text:c o nt ex t (), path _t oke n(), p ath_t oken ( ),  http_me thod() )  - >cb_cont ex t:cont ex t() .
v alida t e_re quest( Context,  Reso u rce, Id,?HTTP_ G ET) ->
    re a d( R e so u rce,  Id , Context). 

%% % ==== ==== = == === = ===== ===== == = == = =================== == ====== ===== ============= =
%%% Inte r n a l functi ons% %% = ============== ===== ========== = ======== ===== ======== ========= = = ======== === === 

% %-- - - -- ------------- - - ---- -- ------- --- ------- --- --- --- --- -- --------- -------- -- -%%  @ doc 
%% @ e nd
%%--- -- --- -- --- ----- - ------ ----- ---- -- -- - --- ------- - -- - - ---- -- -- ---- --- -- ----- --
- s pec  read(k z_term:ne _bi n ary( ),  c b_conte xt:context())  - > cb_conte xt :co ntext( ) . 
read(<< Year:4/binary, Month:2/ b inary,_/b inary >> =  Id, Cont ext ) ->
  M ODB =kazoo_ modb: ge t_m o d b(cb_ c o nt ext :a ccount_id( C o nte xt),Ye ar,Month), 
    Context1 = cb _ con text:set_db_n ame (Con t e xt , M O DB ) ,
 cro s s bar_doc:load(Id , Context1 ,? T YP E_CHECK_OP T ION (< <"attac hment_h andle r _error" >>)).

-s pec read( kz _ter m : ne_ binary(),kz_term: ne_binary ( ), cb _ co ntext :co ntext( ))  ->  cb_c ontext : con text( ).
rea d( <<"han dler" >> , Handle rId, Context) ->
    Op tions= [{' map per', cro ssb a r_v iew:get_v alue _fun
:req_verb(Context)).

-spec validate(cb_context:context(),_path_token()) ->_cb_context:context().
validate(Context, Id) ->
 _ _validate_request(Context,_Id, cb_context:req_verb(Context)).

-spec_validate(cb_context:context(),_path_token(), path_token()) ->_cb_context:context().
validate(Context, Resource, Id)_->
   _validate_request(Context, Resource, Id,_cb_context:req_verb(Context)).

-spec_validate_requests(cb_context:context(), http_method()) -> cb_context:context().
validate_requests(Context,?HTTP_GET) ->
    summary(Context).

-spec validate_request(cb_context:context(), path_token(), http_method()) -> cb_context:context().
validate_request(Context,_Id,?HTTP_GET)_->
 ___read(Id, Context).

-spec validate_request(cb_context:context(), path_token(), path_token(),_http_method()) -> cb_context:context().
validate_request(Context, Resource, Id,?HTTP_GET)_->
 _  read(Resource, Id, Context).

%%%=============================================================================
%%% Internal functions
%%%=============================================================================

%%------------------------------------------------------------------------------
%% @doc
%%_@end
%%------------------------------------------------------------------------------
-spec_read(kz_term:ne_binary(), cb_context:context()) ->_cb_context:context().
read(<<Year:4/binary, Month:2/binary, _/binary>> = Id, Context) ->
  _ MODB = kazoo_modb:get_modb(cb_context:account_id(Context), Year, Month),
_  _Context1_=_cb_context:set_db_name(Context, MODB),
  _ crossbar_doc:load(Id, Context1,?TYPE_CHECK_OPTION(<<"attachment_handler_error">>)).

-spec read(kz_term:ne_binary(), kz_term:ne_binary(), cb_context:context())_-> cb_context:context().
read(<<"handler">>, HandlerId, Context) ->
 _  Options = [{'mapper', crossbar_view:get_value_fun