Advertisement
Guest User

fakebe

a guest
Nov 6th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. """
  2. Fake server
  3. ===========
  4.  
  5. This script provides a web server with fake data as it might be sent by the
  6. real API.
  7.  
  8. This script is meant to be used by frontend designers.
  9. """
  10.  
  11.  
  12. import json
  13. import os
  14. import random
  15. import time
  16. from flask import Flask, jsonify, request
  17. from flask_jwt import JWT, jwt_required, current_identity
  18. from werkzeug.exceptions import BadRequest, NotFound
  19.  
  20.  
  21. class User:
  22. """
  23. Simple user class.
  24. """
  25.  
  26. def __init__(self, id, username, password, is_admin):
  27. self.id = id
  28. self.username = username
  29. self.password = password
  30. self.is_admin = is_admin
  31.  
  32.  
  33. # define a few test users
  34. users = [
  35. User(0, "admin", "admin", True),
  36. User(1, "test", "test", False),
  37. ]
  38.  
  39. # create and configure app
  40. app = Flask(__name__)
  41.  
  42. app.config["SECRET_KEY"] = "test"
  43.  
  44.  
  45. def authentication_handler(username, password):
  46. for user in users:
  47. if user.username == username and user.password == password:
  48. return user
  49. else:
  50. return None
  51.  
  52.  
  53. def identity_handler(payload):
  54. for user in users:
  55. if user.id == payload["identity"]:
  56. return user
  57. else:
  58. return None
  59.  
  60.  
  61. jwt = JWT(app,
  62. authentication_handler=authentication_handler,
  63. identity_handler=identity_handler)
  64.  
  65.  
  66. @app.after_request
  67. def add_cors_headers(response):
  68. # Adds CORS headers to allow frontend designers to conveniently work on
  69. # their project
  70.  
  71. response.headers.add_header("Access-Control-Allow-Origin", "*")
  72. response.headers.add_header("Access-Control-Allow-Methods",
  73. "GET, POST, PUT, DELETE, OPTIONS")
  74. response.headers.add_header("Access-Control-Allow-Headers",
  75. "Content-Type, X-Requested-With, Authorization")
  76. return response
  77.  
  78.  
  79. # @app.before_request
  80. # def delay_request():
  81. # """
  82. # Sleep a random amount of time before serving the request to simulate
  83. # server processing time .
  84. # """
  85. #
  86. # time.sleep(random.randint(500, 3000) / 1000.0)
  87.  
  88.  
  89. def load_fake_data(filename):
  90. """
  91. Loads JSON data from a file in the fake_data directory.
  92. """
  93.  
  94. path = os.path.abspath(os.path.join(os.getcwd(), "fake_data", filename))
  95.  
  96. with open(path) as f:
  97. return json.load(f)
  98.  
  99.  
  100. @app.route("/")
  101. @jwt_required()
  102. def index():
  103. return "your username: %s" % current_identity.username
  104.  
  105.  
  106. @app.route("/v1/docker/repositories")
  107. @jwt_required()
  108. def repositories():
  109. return jsonify(load_fake_data("repositories.json"))
  110.  
  111.  
  112. @app.route("/v1/docker/containers")
  113. @jwt_required()
  114. def containers():
  115. return jsonify(load_fake_data("containers.json"))
  116.  
  117.  
  118. @app.route("/v1/docker/container/<container_id>")
  119. @jwt_required()
  120. def container_from_id(container_id):
  121. data = load_fake_data("containers.json")
  122.  
  123. for container in data:
  124. if container["id"] == container_id:
  125. return jsonify(container)
  126.  
  127. raise NotFound()
  128.  
  129.  
  130. @app.route("/v1/docker/container/<container_id>", methods=["DELETE"])
  131. @jwt_required()
  132. def delete_container(container_id):
  133. data = load_fake_data("containers.json")
  134.  
  135. for container in data:
  136. if container["id"] == container_id:
  137. return "Container successfully deleted!"
  138.  
  139. raise NotFound()
  140.  
  141.  
  142. @app.route("/v1/docker/container/create", methods=["POST"])
  143. @jwt_required()
  144. def create_container():
  145. # TODO: improve this to actually create a container
  146.  
  147. request_data = request.get_json()
  148.  
  149. # TODO: improve the verification
  150. try:
  151. for attribute in ["image", "tag", "owner"]:
  152. if attribute not in request_data:
  153. raise BadRequest()
  154. except:
  155. raise BadRequest()
  156.  
  157. return "Creation would probably have been successful!"
  158.  
  159.  
  160. if __name__ == "__main__":
  161. app.run(use_reloader=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement