import json
import numbers
import os
import random
import string


from flask import Flask, jsonify


app = Flask(__name__)
SIZE = 12


def get_rnd_string():
    chars = string.ascii_uppercase + string.digits
    return ''.join(random.choice(chars) for _ in range(SIZE))


def get_rnd_number():
    return random.randint(0, 2048)


def get_rnd_bool():
    return bool(random.getrandbits(1))


def define_collection(jobj):
    num_objs = random.randint(0, 20)
    col = []
    for x in range(1, num_objs):
        col.append(define_object(jobj))
    return col


def define_object(jobj):
    rnd_obj = {}
    for key, value in jobj.items():
        if type(value) is str:
            rnd_obj[key] = get_rnd_string()
        if isinstance(value, numbers.Number):
            rnd_obj[key] = get_rnd_number()
        if isinstance(value, (dict)):
            rnd_obj[key] = define_object(value)
        if isinstance(value, (list)):
            # we just need to analyse the first object
            rnd_obj[key] = define_collection(value[0])

    return rnd_obj


def generate_object(obj_schema):
    rnd_obj = {}
    for key in obj_schema:
        elem = obj_schema[key]
        if elem['type'] == 'string':
            rnd_obj[key] = get_rnd_string()
        if elem['type'] == 'number':
            rnd_obj[key] = get_rnd_number()
        if elem['type'] == 'bool':
            rnd_obj[key] = get_rnd_bool()
        if elem['type'] == 'null':
            rnd_obj[key] = None
        if elem['type'] == 'object':
            rnd_obj[key] = generate_object(elem['schema'])
        if elem['type'] == 'list':
            rnd_obj[key] = generate_collection(elem['schema'])
    return rnd_obj


def generate_collection(obj_schema):
    num_objs = random.randint(0, 20)
    col = []
    for x in range(1, num_objs):
        col.append(generate_object(obj_schema))
    return col


def load_object():
    base = os.environ.get("JSON")
    app.logger.debug(base)
    if base:
        return json.loads(base)
    else:
        with open('/data/object.json') as data_file:
            return json.load(data_file)


def load_schema():
    schema = os.environ.get("SCHEMA")
    if schema:
        return json.loads(schema)
    else:
        with open('/data/schema.json') as data_file:
            return json.load(data_file)


@app.route('/', methods=['GET'])
def hello():
    return "docmock v0.1"


endpoint = os.environ.get("ENDPOINT")
jobj = load_object()

if not endpoint:
    schema = load_schema()
    endpoint = schema['endpoint']


@app.route(endpoint, methods=['GET'])
def endpoint():
    return jsonify(define_collection(jobj))
    # return jsonify(generate_collection(schema['schema']))

if __name__ == '__main__':

    app.run(
        host='0.0.0.0',
        port=5000,
        debug=True,
        threaded=True)

@@ -128,16 +128,95 @@
 E = 12%0A%0A
+SMAX = os.environ.get(%22MAX%22)%0A%0Aif SMAX:%0A    MAX = int(SMAX)%0Aelse:%0A    MAX = 20%0A%0A
 %0Adef get
@@ -389,25 +389,25 @@
 dom.randint(
-0
+1
 , 2048)%0A%0A%0Ade
@@ -516,37 +516,38 @@
  random.randint(
-0, 20
+1, MAX
 )%0A    col = %5B%5D%0A 
@@ -1834,13 +1834,14 @@
 int(
-0, 20
+1, MAX
 )%0A  
@@ -2654,24 +2654,41 @@
 endpoint():%0A
+    if jobj:%0A    
     return j
@@ -2723,17 +2723,34 @@
 j))%0A    
-#
+if schema:%0A       
  return 
@@ -2795,16 +2795,66 @@
 hema'%5D))
+%0A    return %22Define json object or schema, please%22
 %0A%0Aif __n

return at least 1