Skip to main content

HTTP Response code Python-format.

Project description

SIMPLE PYTHON JSON FORMAT

The repository contain a script to format your python response in a correct JSON format using the standard HTTP response codes. Well suitable to backend applications with REST API building concept in mind.

Install the ngobot-json package

pip install ngobot-json

import the ngobot-json package

from ngobot_json.response import Response

instantiate it for a task as shown below

response = Response()

1. Success Response

you can simply return json successful response format in this way.

e.g.

`response = Response()

print(response.Success()) `

Output:

{ "status":200, "message":"Successful", "data":{ "status":200, "message":"ok", "reason":"Successful" } }

You can modify the json success response format by passing two arguments: (1) the data to return as response and (2) the keyword indicator "success" to activate success response format.

e.g. User Account Created.

`

task = "User Account successfully created"

response = Response(task, "success")

print(response.Created())

`

Output:

`

{ "status":201, "message":"Created Successfully", "data":{ "status":200, "message":"ok", "reason":"User Account successfully created" } }

`

e.g. Process completed successfully

`task = "The Process is successfully completed"

response = Response(task, "success")

print(response.Success())`

Output:

{"status": 200, "message": "Successful", "data": {"status": 200, "message": "ok", "reason": "The Process is successfully completed"}}

2. Error Response

The error response follows the same pattern of the success modify response, but has a different keyword argument "error" to activate the error response format:

e.g.

`task = "Invalid username and password"

response = Response(task, "error")

print(response.Unauthorized())`

Output:

{ "status":401, "message":"Unauthorized", "data":{ "status":422, "message":"Unprocessable Entity", "reason":"Invalid username and password" } }

3. Adding External json return from another function or database. note that in this section we use keyword "data" to activate the json format with external json file.

e.g.

`task = { "status":201, "message":"Created Successfully", "data":{ "status":200, "message":"ok", "reason":"User Account successfully created" } }

response = Response(task, "data")

print(response.Success())`

Output:

{"status": 200, "message": "Successful", "data": {"status": 201, "message": "Created Successfully", "data": {"status": 200, "message": "ok", "reason": "User Account successfully created"}}}

4. List of All the supported HTTP codes functions to call.

comming soon.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ngobot_json-1.1.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

ngobot_json-1.1.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file ngobot_json-1.1.0.tar.gz.

File metadata

  • Download URL: ngobot_json-1.1.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for ngobot_json-1.1.0.tar.gz
Algorithm Hash digest
SHA256 1905457631a872e1d8b1533a8cff707e99b97ef21fca1892b2c7c39124ff274e
MD5 dffb18f3985df2be54ff3f9c29c8f8c7
BLAKE2b-256 34916ded2e0bb5044c453420b46dcb91651e5a63104aca3a4ed7053bf59a592a

See more details on using hashes here.

File details

Details for the file ngobot_json-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: ngobot_json-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for ngobot_json-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6bc2f3183ea95903f436f2599f85092c57742ac98b1c556b169e3d658385df6
MD5 eda7e3b4b012494e6a1c4144ab2c1d5b
BLAKE2b-256 22f5865e9779329b2312129678b82a20e7b903f73711077b8ab3b9b35e3e0016

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page