Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Fake server
- ===========
- This script provides a web server with fake data as it might be sent by the
- real API.
- This script is meant to be used by frontend designers.
- """
- import json
- import os
- import random
- import time
- from flask import Flask, jsonify, request
- from flask_jwt import JWT, jwt_required, current_identity
- from werkzeug.exceptions import BadRequest, NotFound
- class User:
- """
- Simple user class.
- """
- def __init__(self, id, username, password, is_admin):
- self.id = id
- self.username = username
- self.password = password
- self.is_admin = is_admin
- # define a few test users
- users = [
- User(0, "admin", "admin", True),
- User(1, "test", "test", False),
- ]
- # create and configure app
- app = Flask(__name__)
- app.config["SECRET_KEY"] = "test"
- def authentication_handler(username, password):
- for user in users:
- if user.username == username and user.password == password:
- return user
- else:
- return None
- def identity_handler(payload):
- for user in users:
- if user.id == payload["identity"]:
- return user
- else:
- return None
- jwt = JWT(app,
- authentication_handler=authentication_handler,
- identity_handler=identity_handler)
- @app.after_request
- def add_cors_headers(response):
- # Adds CORS headers to allow frontend designers to conveniently work on
- # their project
- response.headers.add_header("Access-Control-Allow-Origin", "*")
- response.headers.add_header("Access-Control-Allow-Methods",
- "GET, POST, PUT, DELETE, OPTIONS")
- response.headers.add_header("Access-Control-Allow-Headers",
- "Content-Type, X-Requested-With, Authorization")
- return response
- # @app.before_request
- # def delay_request():
- # """
- # Sleep a random amount of time before serving the request to simulate
- # server processing time .
- # """
- #
- # time.sleep(random.randint(500, 3000) / 1000.0)
- def load_fake_data(filename):
- """
- Loads JSON data from a file in the fake_data directory.
- """
- path = os.path.abspath(os.path.join(os.getcwd(), "fake_data", filename))
- with open(path) as f:
- return json.load(f)
- @app.route("/")
- @jwt_required()
- def index():
- return "your username: %s" % current_identity.username
- @app.route("/v1/docker/repositories")
- @jwt_required()
- def repositories():
- return jsonify(load_fake_data("repositories.json"))
- @app.route("/v1/docker/containers")
- @jwt_required()
- def containers():
- return jsonify(load_fake_data("containers.json"))
- @app.route("/v1/docker/container/<container_id>")
- @jwt_required()
- def container_from_id(container_id):
- data = load_fake_data("containers.json")
- for container in data:
- if container["id"] == container_id:
- return jsonify(container)
- raise NotFound()
- @app.route("/v1/docker/container/<container_id>", methods=["DELETE"])
- @jwt_required()
- def delete_container(container_id):
- data = load_fake_data("containers.json")
- for container in data:
- if container["id"] == container_id:
- return "Container successfully deleted!"
- raise NotFound()
- @app.route("/v1/docker/container/create", methods=["POST"])
- @jwt_required()
- def create_container():
- # TODO: improve this to actually create a container
- request_data = request.get_json()
- # TODO: improve the verification
- try:
- for attribute in ["image", "tag", "owner"]:
- if attribute not in request_data:
- raise BadRequest()
- except:
- raise BadRequest()
- return "Creation would probably have been successful!"
- if __name__ == "__main__":
- app.run(use_reloader=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement