Add the JSON body to the user API responses.

main
absc 2024-08-09 22:35:53 +00:00
parent dcc7bc3ca3
commit 2d0d384d1e
1 changed files with 75 additions and 12 deletions

View File

@ -21,14 +21,22 @@ The username is passed in a cookie. The handler recover it from the
session. Cookies are:
```
dudename # the actual username
dudeauth # the authentication cookie
dudename: the actual username
dudeauth: the authentication cookie
```
If the session is not valid, all the requests will return `403 Forbidden` to
the client. In case a technical problem occurs, `500 Internal Server Error`
is returned.
In case of errors, all the methods returns a JSON response in the form:
```
{
"error": "error string"
}
```
This module accepts four methods:
- GET /api/v1/user
@ -56,6 +64,8 @@ JSON APIs
GET /api/v1/user
Response body:
```
{
"user" : "foo",
@ -79,6 +89,14 @@ PUT /api/v1/user
}
```
Response body:
```
{
"result": "created"
}
```
Response codes:
- 201 Created
@ -95,6 +113,14 @@ POST /api/v1/user
}
```
Response body:
```
{
"result": "updated"
}
```
Response codes:
- 200 OK
@ -103,6 +129,16 @@ Response codes:
DELETE /api/v1/user
Response body:
```
{
"result": "deleted"
}
```
Response codes:
- 202 Accepted
- 404 Not Found
@ -147,7 +183,10 @@ forbidden(Req, State) ->
case dudeswave_auth:authenticate({cookie, User, Auth}, Bucket) of
{error, service_unavailable} -> exit(service_unavailable);
true -> {false, Req, State};
true ->
Resp = json:encode(#{<<"error">> => <<"authentication required">>}),
Req0 = cowboy_req:reply(403, #{}, Resp, Req),
{false, Req0, State};
false -> {true, Req, State}
end
end.
@ -177,8 +216,14 @@ resource_exists(Req, State) ->
{ok, Bucket} = maps:find(bucket, State),
case dudeswave_auth:user_details(User, Bucket) of
[] -> {false, Req, State};
{error, Reason} -> exit(Reason);
[] ->
Resp = json:encode(#{<<"error">> => <<"user does not exists">>}),
Req0 = cowboy_req:reply(404, #{}, Resp, Req),
{false, Req0, State};
{error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, State};
Details ->
NewState = State#{
bucket => Bucket,
@ -203,11 +248,17 @@ delete_resource(Req, State) ->
#{dudename := User} = cowboy_req:match_cookies([dudename], Req),
case dudeswave_auth:delete(User, Bucket) of
ok -> {true, Req, State};
{error, _} -> {false, Req, State}
ok ->
Resp = json:encode(#{<<"result">> => <<"deleted">>}),
Req0 = cowboy_req:reply(200, #{}, Resp, Req),
{true, Req0, State};
{error, _} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, State}
end.
delete_completed(Req, State) -> {false, Req, State}.
delete_completed(Req, State) -> {true, Req, State}.
%
% Custom callbacks
@ -220,8 +271,14 @@ create_user(Req, State) ->
#{<<"password">> := Pass, <<"email">> := Email} = json:decode(cowboy_req:body(req)),
case dudeswave_auth:new_user(User, Pass, Email, Bucket) of
ok -> {true, Req, []};
{error, Reason} -> {false, Req, Reason}
ok ->
Resp = json:encode(#{<<"result">> => <<"created">>}),
Req0 = cowboy_req:reply(201, #{}, Resp, Req),
{true, Req0, []};
{error, Reason} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, Reason}
end.
modify_user(Req, State) ->
@ -232,8 +289,14 @@ modify_user(Req, State) ->
<<"name">> := Name} = json:decode(cowboy_req:body(req)),
case dudeswave_auth:update(User, Name, Email, Desc, Bucket) of
ok -> {true, Req, []};
{error, Reason} -> {false, Req, Reason}
ok ->
Resp = json:encode(#{<<"result">> => <<"details updated">>}),
Req0 = cowboy_req:reply(200, #{}, Resp, Req),
{true, Req0, []};
{error, Reason} ->
Resp = json:encode(#{<<"error">> => <<"internal server error">>}),
Req0 = cowboy_req:reply(500, #{}, Resp, Req),
{false, Req0, Reason}
end.
user_details(Req, State) ->