Skip to content

Commit

Permalink
Merge pull request #44 from ECN/staging
Browse files Browse the repository at this point in the history
Add HTTP response simulator to API
  • Loading branch information
campb303 authored Jul 12, 2021
2 parents e605dba + a59ea8b commit c3bb828
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
19 changes: 19 additions & 0 deletions docs-src/api/Simulating Responses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Returns the requested HTTP response with a JSON payload containing the response phrase. Valid response codes and phrases are pulled from the [Python `http` library index](https://docs.python.org/3/library/http.html#http.HTTPStatus).

## Endpoint
```
GET /api/error/{response_code}
```
### Parameters
Name | Value
- | -
`response_code` | The HTTP response code to be returned.

## Example
```bash
curl {{production_url}}/api/error/301
```
```jsonc
// Expected Output
{"message": "Moved Permanently"}
```
5 changes: 3 additions & 2 deletions src/webqueue2api/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Api
from flask_jwt_extended import JWTManager
from .config import config
from .resources import Login, RefreshAccessToken, Item, Queue, QueueList
from .resources import Login, RefreshAccessToken, Item, Queue, QueueList, HTTPResponse

app = Flask(__name__)
api = Api(app)
Expand Down Expand Up @@ -34,4 +34,5 @@
api.add_resource(RefreshAccessToken, "/api/tokens/refresh")
api.add_resource(Item, "/api/data/<string:queue>/<int:number>")
api.add_resource(Queue, "/api/data/<string:queues>")
api.add_resource(QueueList, "/api/data/get_queues")
api.add_resource(QueueList, "/api/data/get_queues")
api.add_resource(HTTPResponse, "/api/error/<int:response_code>")
3 changes: 2 additions & 1 deletion src/webqueue2api/api/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
from .refresh_access_token import RefreshAccessToken
from .item import Item
from .queue import Queue
from .queue_list import QueueList
from .queue_list import QueueList
from .http_response import HTTPResponse
25 changes: 25 additions & 0 deletions src/webqueue2api/api/resources/http_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from flask_restful import Resource
from http import HTTPStatus



class HTTPResponse(Resource):
def get(self, response_code: int) -> tuple:
"""Returns the passed HTTP response code and a JSON payload containing a description of the response.
Args:
error_code (int): The HTTP response to return.
Returns:
tuple: HTTP response descriptions as JSON and HTTP response code.
"""
# Build list of HTTP responses from http library
responses = {}
for status in HTTPStatus:
responses[status.value] = status.phrase

# Check for requested response code in reponses
if response_code not in responses.keys():
return ({"message": f"No entry for response code {response_code} found."}, 404)

return ({"message": f"{responses[response_code]}"}, response_code)

0 comments on commit c3bb828

Please sign in to comment.