<commit_before>%%% ocs_rest_res_user.erl
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @copyright 2016 - 2017 SigScale Global Inc.
%%% @end
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @doc This library module implements resource handling functions
%%% 	for a REST server in the {@link //ocs. ocs} application.
%%%
-module(ocs_rest_res_user).
-copyright('Copyright (c) 2016 - 2017 SigScale Global Inc.').

-export([content_types_accepted/0, content_types_provided/0, get_params/0,
		get_user/1, get_user/0, post_user/1, put_user/3, delete_user/1]).

-include_lib("radius/include/radius.hrl").
-include_lib("inets/include/mod_auth.hrl").
-include("ocs.hrl").

-spec content_types_accepted() -> ContentTypes
	when
		ContentTypes :: list().
%% @doc Provides list of resource representations accepted.
content_types_accepted() ->
	["application/json"].

-spec content_types_provided() -> ContentTypes
	when
		ContentTypes :: list().
%% @doc Provides list of resource representations available.
content_types_provided() ->
	["application/json"].

-spec get_user() -> Result
	when
		Result :: {ok, Headers :: [tuple()], Body :: iolist()}
				| {error, ErrorCode :: integer()}.
%% @doc Body producing function for `GET /partyManagement/v1/individual'
%% requests.
get_user() ->
	{Port, Address, Directory, _Group} = get_params(),
	case mod_auth:list_users(Address, Port, Directory) of
		{error, _} ->
			{error, 500};
		{ok, Users} ->
			get_user1(Users, Address, Port, [])
	end.
%% @hidden
get_user1([H | T], Address, Port, Acc) ->
	{Port, Address, Directory, _Group} = get_params(),
	case mod_auth:get_user(H, Address, Port, Directory) of
		{ok, #httpd_user{username = Id, user_data = UserData}} ->
			Identity = {struct, [{"name", "username"}, {"value", Id}]},
			Characteristic = case lists:keyfind(locale, 1, UserData) of
				{_, Lang} ->
					LocaleAttr = {struct, [{"name", "locale"}, {"value", Lang}]},
					{array, [LocaleAttr, Identity]};
				false ->
					{array, []}
			end,
			RespObj = [{"id", Id}, {"href", "/partyManagement/v1/individual/" ++ Id},
				{"characteristic", Characteristic}],
			JsonObj  = {struct, RespObj},
			get_user1(T, Address, Port, [JsonObj | Acc]);
		{error, _Reason} ->
			get_user1(T, Address, Port, Acc)
	end;
get_user1([], _Address, _Port, Acc) ->
	Body = mochijson:encode({array, lists:reverse(Acc)}),
	Headers = [{content_type, "application/json"}],
	{ok, Headers, Body}.

-spec get_user(Id) -> Result
	when
		Id :: string(),
		Result :: {ok, Headers :: [tuple()], Body :: iolist()}
				| {error, ErrorCode :: integer()}.
%% @doc Body producing function for `GET /partyManagement/v1/individual/{id}'
%% requests.
get_user(Id) ->
	{Port, Address, Directory, _Group} = get_params(),
	case mod_auth:get_user(Id, Address, Port, Directory) of
		{ok, #httpd_user{username = Id, password = Password, user_data = UserData}} ->
			Identity = {struct, [{"name", "username"}, {"value", Id}]},
			PasswordAttr = {struct, [{"name", "password"}, {"value", Password}]},
			Characteristic = case lists:keyfind(locale, 1, UserData) of
				{_, Lang} ->
					LocaleAttr = {struct, [{"name", "locale"}, {"value", Lang}]},
					{array, [PasswordAttr, LocaleAttr, Identity]};
				false ->
					{array, [PasswordAttr]}
			end,
			RespObj = [{"id", Id}, {"href", "/partyManagement/v1/individual/" ++ Id},
				{"characteristic", Characteristic}],
			JsonObj  = {struct, RespObj},
			Body = mochijson:encode(JsonObj),
			Headers = [{content_type, "application/json"}],
			{ok, Headers, Body};
		{error, _Reason} ->
			{error, 404}
	end.

-spec post_user(RequestBody) -> Result
	when
		RequestBody :: list(),
		Result :: {ok, Headers :: [tuple()], Body :: iolist()}
			| {error, ErrorCode :: integer()}.
%% @doc Respond to `POST /partyManagement/v1/individual' and add a new `User'
%% resource.
post_user(RequestBody) ->
	{Port, Address, Directory, Group} = get_params(),
	try
		{struct, Object} = mochijson:decode(RequestBody),
		{_, ID} = lists:keyfind("id", 1, Object),<commit_msg>add Etag header in the HTTP response<commit_after>%%% ocs_rest_res_user.erl
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @copyright 2016 - 2017 SigScale Global Inc.
%%% @end
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @doc This library module implements resource handling functions
%%% 	for a REST server in the {@link //ocs. ocs} application.
%%%
-module(ocs_rest_res_user).
-copyright('Copyright (c) 2016 - 2017 SigScale Global Inc.').

-export([content_types_accepted/0, content_types_provided/0, get_params/0,
		get_user/1, get_user/0, post_user/1, put_user/3, delete_user/1]).

-include_lib("radius/include/radius.hrl").
-include_lib("inets/include/mod_auth.hrl").
-include("ocs.hrl").

-spec content_types_accepted() -> ContentTypes
	when
		ContentTypes :: list().
%% @doc Provides list of resource representations accepted.
content_types_accepted() ->
	["application/json"].

-spec content_types_provided() -> ContentTypes
	when
		ContentTypes :: list().
%% @doc Provides list of resource representations available.
content_types_provided() ->
	["application/json"].

-spec get_user() -> Result
	when
		Result :: {ok, Headers :: [tuple()], Body :: iolist()}
				| {error, ErrorCode :: integer()}.
%% @doc Body producing function for `GET /partyManagement/v1/individual'
%% requests.
get_user() ->
	{Port, Address, Directory, _Group} = get_params(),
	case mod_auth:list_users(Address, Port, Directory) of
		{error, _} ->
			{error, 500};
		{ok, Users} ->
			get_user1(Users, Address, Port, [])
	end.
%% @hidden
get_user1([H | T], Address, Port, Acc) ->
	{Port, Address, Directory, _Group} = get_params(),
	case mod_auth:get_user(H, Address, Port, Directory) of
		{ok, #httpd_user{username = Id, user_data = UserData}} ->
			Identity = {struct, [{"name", "username"}, {"value", Id}]},
			Characteristic = case lists:keyfind(locale, 1, UserData) of
				{_, Lang} ->
					LocaleAttr = {struct, [{"name", "locale"}, {"value", Lang}]},
					{array, [LocaleAttr, Identity]};
				false ->
					{array, []}
			end,
			RespObj = [{"id", Id}, {"href", "/partyManagement/v1/individual/" ++ Id},
				{"characteristic", Characteristic}],
			JsonObj  = {struct, RespObj},
			get_user1(T, Address, Port, [JsonObj | Acc]);
		{error, _Reason} ->
			get_user1(T, Address, Port, Acc)
	end;
get_user1([], _Address, _Port, Acc) ->
	Body = mochijson:encode({array, lists:reverse(Acc)}),
	Headers = [{content_type, "application/json"}],
	{ok, Headers, Body}.

-spec get_user(Id) -> Result
	when
		Id :: string(),
		Result :: {ok, Headers :: [tuple()], Body :: iolist()}
				| {error, ErrorCode :: integer()}.
%% @doc Body producing function for `GET /partyManagement/v1/individual/{id}'
%% requests.
get_user(Id) ->
	{Port, Address, Directory, _Group} = get_params(),
	case mod_auth:get_user(Id, Address, Port, Directory) of
		{ok, #httpd_user{username = Id, password = Password, user_data = UserData}} ->
			Identity = {struct, [{"name", "username"}, {"value", Id}]},
			PasswordAttr = {struct, [{"name", "password"}, {"value", Password}]},
			Characteristic = case lists:keyfind(locale, 1, UserData) of
				{_, Lang} ->
					LocaleAttr = {struct, [{"name", "locale"}, {"value", Lang}]},
					{array, [PasswordAttr, LocaleAttr, Identity]};
				false ->
					{array, [PasswordAttr]}
			end,
			LastModified = lists:keyfind(last_modified, 1, UserData),
			RespObj = [{"id", Id}, {"href", "/partyManagement/v1/individual/" ++ Id},
				{"characteristic", Characteristic}],
			JsonObj  = {struct, RespObj},
			Body = mochijson:encode(JsonObj),
			Headers = [{content_type, "application/json"}, {etag, etag(LastModified)}],
			{ok, Headers, Body};
		{error, _Reason} ->
			{error, 404}
	end.

-spec post_user(RequestBody) -> Result
	when
		RequestBody :: list(),
		Result :: {ok, Headers :: [tuple()], Body :: iolist()}
			| {error, ErrorCode :: integer()}.
%% @doc Respond to `POST /partyManagement/v1/individual' and add a new `User'
%% resource.
post_user(RequestBody) ->
	{Port, Address, Directory, Group} = get_params(),
	try
		{struct, Object} = mochijson:decode(RequestBody),
		{_, ID} = lists:keyfind("id", 1, Object),
{'fraction_non_alphanumeric': 0.2018554062699936, 'fraction_numerical': 0.010130091703988058, 'mean_word_length': 4.639807576668671, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6312587', 'n_tokens_mistral': 3322, 'n_tokens_neox': 2984, 'n_words': 845}