From ceea61c1ed6bffed77a503de8eb5f70ee824ba4f Mon Sep 17 00:00:00 2001 From: absc Date: Wed, 7 Aug 2024 22:10:45 +0000 Subject: [PATCH] Add the username to the JSON response to GET. In addition to this, version the API with the classic "api/v1" path. --- dudeswave/src/dudeswave_app.erl | 2 +- dudeswave/src/dudeswave_user_handler.erl | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/dudeswave/src/dudeswave_app.erl b/dudeswave/src/dudeswave_app.erl index f5a106e..09cb839 100644 --- a/dudeswave/src/dudeswave_app.erl +++ b/dudeswave/src/dudeswave_app.erl @@ -40,7 +40,7 @@ start(_Type, StartArgs) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/user/:username", dudeswave_user_handler, #{bucket => ?USERSBUCK, + {"/api/v1/user/:username", dudeswave_user_handler, #{bucket => ?USERSBUCK, cookies => ?COOKIESBUCK}}, {"/", dudeswave_handler, #{bucket => ?APPBUCK}} ]} diff --git a/dudeswave/src/dudeswave_user_handler.erl b/dudeswave/src/dudeswave_user_handler.erl index 48ac315..8e1a144 100644 --- a/dudeswave/src/dudeswave_user_handler.erl +++ b/dudeswave/src/dudeswave_user_handler.erl @@ -17,14 +17,13 @@ -moduledoc """ JSON API to manage users. -The username should be passed as one of the tokens of the request path, like +The username should be passed as the last token in the request, like: ``` /user/foo -/user/foo/details ``` -However, the first form is preferred. +Where `foo` is the actual username. The user parameter must be called `username` as this module expects it in order to work properly. All the requests must be done with a valid @@ -63,6 +62,7 @@ GET /user/:username ``` { + "user" : "foo", "email": "foo@example.com", "description": "A wonderful user", "name": "Fantastic Foo" @@ -110,7 +110,6 @@ DELETE /user/:username - 202 Accepted - 404 Not Found - """. -behaviour(cowboy_handler). @@ -243,8 +242,9 @@ modify_user(Req, State) -> user_details(Req, State) -> #{details := Details} = State, + Data = Details#{user => cowboy_req:binding(username, Req)}, - {iolist_to_binary(json:encode(Details)), Req, State}. + {iolist_to_binary(json:encode(Data)), Req, State}. % % gen_server callbacks