Skip to content

Instantly share code, notes, and snippets.

@chilismaug
Forked from 0atman/static-json.py
Last active January 18, 2021 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilismaug/0a8af443fc42fbe4228c2c1e6bbda20c to your computer and use it in GitHub Desktop.
Save chilismaug/0a8af443fc42fbe4228c2c1e6bbda20c to your computer and use it in GitHub Desktop.
Simple flask-restful GET json stub with a "current weather" OpenWeather response doc
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class SimpleRest(Resource):
def get(self):
return {
'weather': [
{
'id': 800,
'main': 'Clear',
'description': 'clear sky',
'icon': '01d'
}
],
'coord': {
'lon': -122.08,
'lat': 37.39
},
'base': 'stations',
'main': {
'temp': 282.55,
'feels_like': 281.86,
'temp_min': 280.37,
'temp_max': 284.26,
'pressure': 1023,
'humidity': 100
},
'visibility': 16093,
'wind': {
'speed': 1.5,
'deg': 350
},
'clouds': {
'all': 1
},
'dt': 1560350645,
'sys': {
'type': 1,
'id': 5122,
'message': 0.0139,
'country': 'US',
'sunrise': 1560343627,
'sunset': 1560396563
},
'timezone': -25200,
'id': 420006353,
'name': 'Xanadu',
'cod': 200
}
api.add_resource(SimpleRest, '/')
if __name__ == '__main__':
app.run(port=8001, debug=True)
@chilismaug
Copy link
Author

chilismaug commented Jan 18, 2021

Here's how I use it in my Flask weather app - the "data" variable gets the stub weather when it's running in local dev mode

from urllib.request import urlopen

STUB_URL= ('http://localhost:8001') # so when running local let us have our one-trick weather API stubserver

        with urlopen(STUB_URL) as stuburl:
            data = json.loads(stuburl.read().decode('utf-8'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment