Skip to content

Commit beb5963

Browse files
authored
Merge pull request #10 from flavienbwk/develop
Fixed docker volumes problems and added HTTP response codes
2 parents 75ff287 + ab54644 commit beb5963

File tree

5 files changed

+56
-21
lines changed

5 files changed

+56
-21
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.vscode/
22

33
web/app/node_modules/
4-
ldap-data/
4+
ldap/
55
logs/
66
migrations/
77

api/app/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@
33
from flask import Flask
44

55
from utils.Database import Database
6+
from utils.ApiResponse import ApiResponse
67

78
from config import config_by_name
89

910
FLASK_LEVEL = os.environ.get("FLASK_LEVEL", "dev")
1011

1112
database = Database()
1213

14+
def page_not_found(e):
15+
apiResponse = ApiResponse()
16+
apiResponse.setMessage("Page not found")
17+
apiResponse.setHTTPCode(404)
18+
return apiResponse.getResponse()
19+
1320
def create_app():
1421
app = Flask(__name__)
22+
app.register_error_handler(404, page_not_found)
1523
app.config.from_object(config_by_name[FLASK_LEVEL])
1624
database.initDatabase(app)
1725
return app

api/app/manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def after_request(response):
3333
response.headers.add('Access-Control-Allow-Origin', '*')
3434
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,X-Api-Auth-Token')
3535
response.headers.add('Access-Control-Allow-Methods', 'GET,POST,PUT,OPTIONS')
36-
return ApiResponse.formatFlaskResponse(response)
36+
response, http_code = ApiResponse.formatFlaskResponse(response)
37+
return app.make_response((response, http_code))
3738

3839
if __name__ == '__main__':
3940
manager.run()

api/app/utils/ApiResponse.py

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,28 @@ class ApiResponse():
55
"""
66
A class that formats correctly the expected
77
response from the web applicaiton.
8+
9+
@author : github.com/flavienbwk | berwic_f
810
"""
911

1012
def __init__(self) -> None:
1113
self.error = True
1214
self.message = ""
1315
self.details = {}
16+
self.http_code = 400
1417

15-
def setAll(self, error: bool, message: str, details: dict) -> None:
18+
def setAll(self, error: bool, message: str, details: dict, http_code: int = 400) -> None:
1619
self.error = error
1720
self.message = message
1821
self.details = details
22+
self.http_code = http_code
1923

2024
def setSuccess(self) -> None:
25+
self.http_code = 200
2126
self.error = False
2227

2328
def setError(self) -> None:
29+
self.http_code = 400
2430
self.error = True
2531

2632
def setMessage(self, message: str) -> None:
@@ -29,11 +35,16 @@ def setMessage(self, message: str) -> None:
2935
def setDetails(self, details: dict) -> None:
3036
self.details = details
3137

32-
def getResponse(self) -> {"error": bool, "message": str, "details": {}}:
38+
def setHTTPCode(self, http_code: int) -> None:
39+
self.http_code = http_code
40+
41+
def getResponse(self) -> {"http_code": int, "error": bool, "message": str, "data": {}}:
3342
return {
43+
"flask_api": True,
44+
"http_code": self.http_code,
3445
"error": self.error,
3546
"message": self.message,
36-
"details": self.details
47+
"data": self.details
3748
}
3849

3950
@staticmethod
@@ -45,24 +56,37 @@ def formatFlaskResponse(response):
4556
Switching from the Flask "errors" key to
4657
response format with "error" & "message".
4758
48-
Adding an empty "details" object if no details
59+
Adding an empty "data" object if no details
4960
are returned, to remain consistent.
5061
"""
5162
try:
63+
# Checking if response is from our Flask API.
64+
# Maybe it is from Swagger UI
5265
response_data = json.loads(response.get_data())
53-
response.headers.add('Content-Type', 'application/json')
54-
if "errors" in response_data:
55-
response_data["message"] = ApiResponse.stringifyFlaskErrors(response_data["errors"])
56-
response_data["error"] = True
57-
del(response_data["errors"])
58-
if "details" not in response_data:
59-
response_data["details"] = {}
60-
response.set_data(json.dumps(response_data))
66+
if "flask_api" not in response_data:
67+
return response, response._status_code
68+
else:
69+
del response_data["flask_api"]
6170
except ValueError:
62-
# Response is not JSON, probably HTML
63-
# Swagger UI returns HTML for example
64-
pass
65-
return response
71+
return response, response._status_code
72+
73+
http_code = 200
74+
response.headers.add('Content-Type', 'application/json')
75+
if "errors" in response_data:
76+
response_data["message"] = ApiResponse.stringifyFlaskErrors(response_data["errors"])
77+
response_data["error"] = True
78+
del(response_data["errors"])
79+
if "data" not in response_data:
80+
response_data["data"] = {}
81+
if "error" not in response_data:
82+
response_data["error"] = True
83+
if "http_code" in response_data:
84+
http_code = response_data["http_code"] if response_data["error"] is True else 200
85+
del response_data["http_code"]
86+
else:
87+
http_code = 400 if response_data["error"] is True else 200
88+
response.set_data(json.dumps(response_data))
89+
return response, http_code
6690

6791
@staticmethod
6892
def stringifyFlaskErrors(obj: object) -> str:
@@ -75,8 +99,9 @@ def stringifyFlaskErrors(obj: object) -> str:
7599
return final_message
76100

77101
def __repr__(self) -> str:
78-
return "<ApiResponse(error='{}', message='{}', details={})>".format(
102+
return "<ApiResponse(error='{}', message='{}', details={}, http_code={})>".format(
79103
self.error,
80104
self.message,
81-
len(self.details)
105+
len(self.details),
106+
self.http_code
82107
)

docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ services:
1515
LDAP_DOMAIN: "mycompany.com"
1616
LDAP_ADMIN_PASSWORD: "adminpwd"
1717
volumes:
18-
- './ldap-data:/var/lib/ldap'
18+
- './ldap/data:/var/lib/ldap'
19+
- './ldap/slapd:/etc/ldap/slapd.d'
1920

2021
phpldapadmin:
2122
image: osixia/phpldapadmin:0.9.0

0 commit comments

Comments
 (0)