This project provides a simple API to handle the creation and saving of weather facts.
The API exposes two endpoints /fact and /favorites that can be used to retrieve and save random weather facts.
Python 3.0or above.pipfor installing dependencies.
git clone https://github.com/4475f9e4-1337-4c5b-bc13-6534b2c3a92d/weather_facts.git && cd weather_factsmake buildpython -m venv .venv
. .venv/bin/activate
pip install .python -m venv .venv
. .venv/Scripts/activate
pip install .To run the server in development mode:
./run.sh devor directly:
python server.pyTo run the server in production mode using waitress-serve:
./run.shTo specify a custom host and port, set the HOST and PORT environment variables:
HOST=0.0.0.0 PORT=8080 ./run.shTo interact with the API, clients should send HTTP requests to the appropriate endpoint while ensuring the correct headers are set.
The Accept: application/json header should be included with all requests to indicate that the client expects a JSON-formatted response. For POST requests that include a JSON payload in the request body, the Content-Type: application/json header must also be set to inform the server of the data format. The APIs response will include a JSON body, which clients can parse to extract the returned data. See below code samples for usage and payloads expectations.
curl -X GET http://localhost:5000/fact \
-H 'Accept: application/json'import requests
headers = { 'Accept': 'application/json' }
r = requests.get('http://localhost:5000/fact', headers = headers)
print(r.json())| Status | Meaning | Description | Body |
|---|---|---|---|
| 200 | OK | Successful operation | JSON |
{ "fact": "A 2003 heatwave turned grapes to raisins before they were picked from the vine!" }curl -X GET http://localhost:5000/favorites \
-H 'Accept: application/json'import requests
headers = { 'Accept': 'application/json' }
r = requests.get('http://localhost:5000/favorites', headers = headers)
print(r.json())| Status | Meaning | Description | Body |
|---|---|---|---|
| 200 | OK | Successful operation | JSON |
| 500 | Internal Server Error | Server error | JSON |
{
"favorites": {
"9e6b2d09b197c310": "Some favorite fact",
"673795abb85fb4db": "Another favorite fact"
}
}{ "error": "Internal server error" }curl -X POST http://localhost:5000/favorites \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{ "fact": "My new favorite fact" }'import requests
headers = { 'Accept': 'application/json' }
data = { "fact": "My new favorite fact" }
r = requests.post('http://localhost:5000/favorites', headers = headers, json = data)
print(r.json())| Name | In | Type | Required | Description |
|---|---|---|---|---|
fact |
body | JSON | true | an object with a fact property to save |
{ "fact": "A 2003 heatwave turned grapes to raisins before they were picked from the vine!" }| Status | Meaning | Description | Body |
|---|---|---|---|
| 200 | OK | Successful operation | JSON |
| 400 | Bad Request | Body parameter incorrect | JSON |
| 500 | Internal Server Error | Server error | JSON |
{ "id": "9e6b2d09b197c310" }{ "error": "Bad request" }{ "error": "Internal server error" }curl -X DELETE http://localhost:5000/favorites/9e6b2d09b197c310 \
-H 'Accept: application/json'import requests
headers = { 'Accept': 'application/json' }
r = requests.delete('http://localhost:5000/favorites/9e6b2d09b197c310', headers = headers)
print(r.json())| Name | In | Type | Required | Description |
|---|---|---|---|---|
id |
path | string | true | id of the fact to delete |
| Status | Meaning | Description | Body |
|---|---|---|---|
| 200 | OK | Successful operation | JSON |
| 500 | Internal Server Error | Server error | JSON |
{ "n": 1 } // n is the number of deleted facts{ "error": "Internal server error" }