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