api_response.py
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
from server.log import log
# Customize Formatted Response
# with status code 200 and the real status code and message in body
def ok(data=None, message=""):
    return {
        "status": "success",
        "code": 200,
        "data": data,
        "message": message
    }
def __response_with_code(code, message, friendly_message=""):
    log.debug("response with code: %d and message: %s" % (code, message))
    return {
        "status": "failed",
        "code": code,
        "message": message,
        "friendly_message": friendly_message
    }
def bad_request(message="",
                friendly_message=(
                        'Your request does not make sense'
                )):
    return __response_with_code(400, message, friendly_message)
def unauthorized(message="",
                 friendly_message=(
                         'Please login to get a valid token')):
    return __response_with_code(401, message, friendly_message)
def forbidden(message="",
              friendly_message=(
                      'You don\'t have the permission to access the request')):
    return __response_with_code(403, message, friendly_message)
def not_found(message="",
              friendly_message=(
                      'The requested URL was not found on the server.')):
    return __response_with_code(404, message, friendly_message)
def internal_server_error(message="",
                          friendly_message=(
                                  'Encountered an internal error ,'
                                  'unable to complete your request.'
                                  'Either the server is overloaded or there '
                                  'is an error in the application.'
                          )):
    return __response_with_code(500, message, friendly_message)
def insecurity_request(message="",
                       friendly_message=(
                           'Invalid request ipaddress which not in'
                           'platform\'s white list.'
                       )):
    return __response_with_code(412, message, friendly_message)