-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from ECN/staging
Add HTTP response simulator to API
- Loading branch information
Showing
4 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |