Skip to content

Commit

Permalink
Add /me endpoint (#61)
Browse files Browse the repository at this point in the history
* Add me endpoint
Rework Dockerfile for faster rebuild during debug

* Add new endpoint to README

* updated postman collection and openapi specs

---------

Co-authored-by: erev0s <projects@erev0s.com>
  • Loading branch information
alexkutsan and erev0s authored Nov 24, 2024
1 parent d42800d commit c410965
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.11-alpine as builder
RUN apk --update add bash nano g++
COPY . /vampi
COPY ./requirements.txt /vampi/requirements.txt
WORKDIR /vampi
RUN pip install -r requirements.txt

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ A quick rundown of the actions included can be seen in the following table:
|:----------:|:-----------------------------:|:--------------------------------------------------:|
| GET | /createdb | Creates and populates the database with dummy data |
| GET | / | VAmPI home |
| GET | /me | Displays the user that is logged in |
| GET | /users/v1 | Displays all users with basic information |
| GET | /users/v1/_debug | Displays all details for all users |
| POST | /users/v1/register | Register new user |
Expand Down
16 changes: 16 additions & 0 deletions api_views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ def debug():
return_value = jsonify({'users': User.get_all_users_debug()})
return return_value

def me():
resp = token_validator(request.headers.get('Authorization'))
if "error" in resp:
return Response(error_message_helper(resp), 401, mimetype="application/json")
else:
user = User.query.filter_by(username=resp['sub']).first()
responseObject = {
'status': 'success',
'data': {
'username': user.username,
'email': user.email,
'admin': user.admin
}
}
return Response(json.dumps(responseObject), 200, mimetype="application/json")


def get_by_username(username):
if User.get_user(username):
Expand Down
82 changes: 65 additions & 17 deletions openapi_specs/VAmPI.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"info": {
"_postman_id": "07b56784-02d4-47a0-9bcd-03b5bc52f8dd",
"_postman_id": "2b4774dd-b3fb-4a63-81f4-353643bbb641",
"name": "VAmPI",
"description": "OpenAPI v3 specs for VAmPI",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "10538030"
},
"item": [
{
Expand Down Expand Up @@ -151,10 +152,10 @@
]
}
},
"_postman_previewlanguage": null,
"header": null,
"_postman_previewlanguage": "Text",
"header": [],
"cookie": [],
"body": null
"body": ""
}
]
},
Expand Down Expand Up @@ -359,8 +360,8 @@
]
}
},
"_postman_previewlanguage": null,
"header": null,
"_postman_previewlanguage": "Text",
"header": [],
"cookie": [],
"body": "{\n \"message\": \"Successfully registered. Login to receive an auth token.\",\n \"status\": \"success\"\n}"
}
Expand Down Expand Up @@ -459,8 +460,8 @@
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": null,
"header": null,
"_postman_previewlanguage": "Text",
"header": [],
"cookie": [],
"body": "{\n \"status\": \"fail\",\n \"message\": \"Password is not correct for the given username.\"\n}"
},
Expand Down Expand Up @@ -501,13 +502,60 @@
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": null,
"header": null,
"_postman_previewlanguage": "Text",
"header": [],
"cookie": [],
"body": "{\n \"status\": \"fail\",\n \"message\": \"Username does not exist\"\n}"
}
]
},
{
"name": "Retrieves currently logged in user",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});"
],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{auth_token}}",
"type": "string"
}
]
},
"method": "GET",
"header": [
{
"key": "Accept",
"value": "application/json"
}
],
"url": {
"raw": "{{baseUrl}}/me",
"host": [
"{{baseUrl}}"
],
"path": [
"me"
]
},
"description": "Displays user by username"
},
"response": []
},
{
"name": "Add new book",
"event": [
Expand Down Expand Up @@ -744,10 +792,10 @@
]
}
},
"_postman_previewlanguage": null,
"header": null,
"_postman_previewlanguage": "Text",
"header": [],
"cookie": [],
"body": null
"body": ""
}
]
},
Expand Down Expand Up @@ -862,10 +910,10 @@
]
}
},
"_postman_previewlanguage": null,
"header": null,
"_postman_previewlanguage": "Text",
"header": [],
"cookie": [],
"body": null
"body": ""
}
]
},
Expand Down
48 changes: 48 additions & 0 deletions openapi_specs/openapi3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,54 @@ paths:
message:
type: string
example: 'Password is not correct for the given username.'
/me:
get:
security:
- bearerAuth: []
tags:
- users
summary: Retrieves currently logged in user
description: Displays information about the currently authenticated user
operationId: api_views.users.me
responses:
'200':
description: Display current user info
content:
application/json:
schema:
type: object
properties:
data:
type: object
properties:
admin:
type: boolean
example: false
email:
type: string
example: 'mail1@mail.com'
username:
type: string
example: 'name1'
status:
type: string
example: 'success'
'401':
description: Unauthorized access due to expired, invalid, or missing token
content:
application/json:
schema:
type: object
properties:
status:
type: string
example: 'fail'
message:
type: string
enum:
- 'Signature expired. Please log in again.'
- 'Invalid token. Please log in again.'

/users/v1/{username}:
get:
tags:
Expand Down

0 comments on commit c410965

Please sign in to comment.