<commit_before>-module(wfh2_worker_handler).

-include("../include/worker_state.hrl").

-export([init/3
        , rest_init/2
        , content_types_accepted/2
        , content_types_provided/2
        , allowed_methods/2
        , resource_exists/2
        , get_json/2
        , put_json/2]).

-type(location () :: in_office | out_of_office).

-record(rest_state, {worker_id :: atom(), action :: binary(), location ::
                     location(), info:: binary() | undefined}).

init(_Proto, _Req, _Opts) ->
  {upgrade, protocol, cowboy_rest}.

rest_init(Req, _State) ->
  {ok, Req, #rest_state{}}.

allowed_methods(Req, State) ->
  {[<<"GET">>, <<"PUT">>, <<"POST">>], Req, State}.

content_types_accepted(Req, State) ->
  {[{{<<"application">>, <<"json">>, []}, put_json}],
   Req, State}.

content_types_provided(Req, State) ->
  {[{{<<"application">>, <<"json">>, []}, get_json}], Req, State}.

get_json(Req, State) ->
  WorkerId = State#rest_state.worker_id,
  {ok, WorkerState} = wfh2_worker:get_worker_state(WorkerId),
  WorkerProfile = sprof_cache:get_profile_for(WorkerId),
  Body = wfh2_serialisation:encode_status({WorkerState, WorkerProfile}),
  {Body, Req, State}.

get_post_request_data(Req) ->
  {Action, Req2} = cowboy_req:binding(action, Req),
  {WorkerId, Req3} = cowboy_req:binding(worker_id, Req2),
  {ok, BodyRaw, Req4} = cowboy_req:body(Req3),
  Body = jsx:decode(BodyRaw, [return_maps, {labels, atom}]),
  {{Action, WorkerId}, Body, Req4}.

resource_exists(Req, State) -><commit_msg>adds cors to single worker resource<commit_after>-module(wfh2_worker_handler).

-include("../include/worker_state.hrl").

-export([init/3
        , rest_init/2
        , content_types_accepted/2
        , content_types_provided/2
        , allowed_methods/2
        , resource_exists/2
        , options/2
        , get_json/2
        , put_json/2]).

-type(location () :: in_office | out_of_office).

-record(rest_state, {worker_id :: atom(), action :: binary(), location ::
                     location(), info:: binary() | undefined}).

init(_Proto, _Req, _Opts) ->
  {upgrade, protocol, cowboy_rest}.

rest_init(Req, _State) ->
  {ok, Req, #rest_state{}}.

allowed_methods(Req, State) ->
  {[<<"OPTIONS">>, <<"GET">>, <<"PUT">>, <<"POST">>], Req, State}.

content_types_accepted(Req, State) ->
  {[{{<<"application">>, <<"json">>, []}, put_json}],
   Req, State}.

content_types_provided(Req, State) ->
  case cowboy_req:method(Req) of
    {<<"GET">>, Req2} ->
      {[{{<<"application">>, <<"json">>, []}, get_json}], Req2, State};
    {<<"OPTIONS">>, Req2} ->
      {[{{<<"application">>, <<"json">>, []}, options}], Req2, State}
  end.

options(Req,State) ->
  Req1 = cowboy_req:set_resp_header(<<"access-control-allow-methods">>, <<"GET, OPTIONS">>, Req),
  Req2 = cowboy_req:set_resp_header(<<"access-control-allow-origin">>, <<"*">>, Req1),
  {ok, Req2, State}.

get_json(Req, State) ->
  WorkerId = State#rest_state.worker_id,
  {ok, WorkerState} = wfh2_worker:get_worker_state(WorkerId),
  WorkerProfile = sprof_cache:get_profile_for(WorkerId),
  Body = wfh2_serialisation:encode_status({WorkerState, WorkerProfile}),
  {Body, Req, State}.

get_post_request_data(Req) ->
  {Action, Req2} = cowboy_req:binding(action, Req),
  {WorkerId, Req3} = cowboy_req:binding(worker_id, Req2),
  {ok, BodyRaw, Req4} = cowboy_req:body(Req3),
  Body = jsx:decode(BodyRaw, [return_maps, {labels, atom}]),
  {{Action, WorkerId}, Body, Req4}.

resource_exists(Req, State) ->
{'fraction_non_alphanumeric': 0.19925351708297445, 'fraction_numerical': 0.012345679012345678, 'mean_word_length': 4.270801815431165, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4875795', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1297, 'n_words': 220}