Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Py-json-server

This server is used to get and set jsons in a mongoDB and even randomize. The DB is a mongoDB You will need a mongoDB and a way to run docker / python

Features

  • Get all of the items in a specific api (collection)
  • Get a specific Json with a specific URL (data in a collection)
  • Get a Json with randomize fields that have been configured in the input
  • Randomizble fields - string, int, float, timestamp, GEO position, ENUMS, object
  • Get an save the randomize response back in the mongoDB
  • Get a Json with relative randomize fields and save it back in the mongoDB
  • Get a Json with a fake url, so you can add every URI or params at the end of it

Environment Variables

To run this project, you will need to add the following environment variables to your .env file
MONGO_CONNECTION The connection string of the mongo, default: mongodb://localhost:27017

MONGO_DB_NAME The name of the DB in the Mongo, default: Shimon

Deployment

To deploy this project run (don't forget the environment variables)

  flask run --host=0.0.0.0

Or use run the docker image

  docker run --env MONGO_DB_NAME=test --env MONGO_CONNECTION=mongodb://localhost:27017 
  --name py-json-server py-json-server:2

API Reference

Get all items for an API

  GET /all/<api>
  POST /all/<api>
Parameter Type Description
api string Required. The name of the collection

Get item

for getting the item from the DB, if it has the override option the returned json will be randomize as configured in the DB.

  GET /<api>/<name?>/<version?>
  POST /<api>/<name?>/<version?>
Parameter Type Description
api string Required. The name of the collection
name string Not Required. The name of the item
version string Not Required. The version of the item

Get a relative randomize item

for getting the item from the DB, if it has the override option, the returned json will be randomize as configured in the DB. if the re-override option is configured, the values will be relative to the previous ones the returned values will be saved in the DB.

  GET /override/<api>/<name?>/<version?>
  POST /override/<api>/<name?>/<version?>
Parameter Type Description
api string Required. The name of the collection
name string Not Required. The name of the item
version string Not Required. The version of the item

Reset the re-override option for the item

Reset the re-override to False for the chosen item

  GET /resetOverride/<api>/<name?>/<version?>
Parameter Type Description
api string Required. The name of the collection
name string Not Required. The name of the item
version string Not Required. The version of the item

Add item

  POST add/<api>/<name?>/<version?>
Parameter Type Description Default
api string Required. The name of the collection
name string Not Required. The name of the item 'default'
version string Not Required. The version of the item '1'

Get item with a fake URL after the fake param (can be used as a DNS/base URL)

get or override depends on the prefix of the URL

  GET <api>/<name?>/<version?>/fake/<every string>
  GET <override>/<api>/<name?>/<version?>/fake/<every string>
Parameter Type Description
api string Required. The name of the collection
name string Not Required. The name of the item
version string Not Required. The version of the item
every string string Not Required. Whatever string and params you want

Supported Randoms

randomInt

Replaces the desired key's value with a random integer

Argument Type Default Description
min int 0 The min value of the random
max int 100 The max value of the random
"overrides": {
	"id": {
		"type": "randomInt",
		"params": {
			"min": 1,
			"max": 1000
		}
	}
}

randomFloat

Replaces the desired key's value with a random float number

Argument Type Default Description
min float 0.0 The min value of the random
max float 100.0 The max value of the random
decPlace int 2 The number of decimals to use when rounding the number
"overrides": {
	"age": {
		"type": "randomFloat",
		"params": {
			"min": 0.0,
			"max": 120.0,
            "decPlace": 1
		}
	}
}

randomString

Replaces the desired key's value with a randomize string from the input chars

Argument Type Default Description
size int 8 The min value of the random
chars sequence printable A sequence like a list, a tuple, a range of numbers etc.

printable - pre-initialized string in python. A set of punctuation, digits, ascii_letters and whitespace

More premade sets: (from string.pyi)

  • ascii_letters
  • ascii_lowercase
  • ascii_uppercase
  • capwords
  • digits
  • hexdigits
  • octdigits
  • printable
  • punctuation
  • whitespace
  • Formatter
  • Template

chars - can be a every string, not only the premade sets

"overrides": {
	"username": {
		"type": "randomString",
		"params": {
			"size": 10,
            "chars": "ascii_lowercase"
		}
	}
}

randomGEO

Replaces the desired key's value with a randomize tuple of 2 float numbers

Argument Type Default Description
minX float 32.0 The min value of the first elemnt
maxX float 33.5 The max value of the first elemnt
minY float 35.0 The min value of the second elemnt
maxY float 36.5 The max value of the second elemnt
"overrides": {
	"location": {
		"type": "randomGEO",
		"params": {
			"minX": 33.3,
            "maxX": 34.2,
            "minY": 35.0,
            "maxY": 36.8
		}
	}
}

result example:

"location": [
	33.168916,
	35.172589
]

randomTimestamp

Replaces the desired key's value with a randomize timestamp between the current time and the time with delta seconds before the current time

Argument Type Default Description
delta int 4 The number of seconds before the cuurent time, the minimun random timestamp can be

The calculation is random_between(time.now().asSeconds() - delta, time.now())

"overrides": {
	"lastUpdateTime": {
		"type": "randomTimestamp",
		"params": {
			"delta": 4
		}
	}
}

randomUUID

Replaces the desired key's value with a randomize UUID (like the java one) This options has no params option

"overrides": {
	"uuid": {
		"type": "randomUUID",
	}
}

randomEnum

Replaces the desired key's value with a one of the elements in the input array

Argument Type Default Description
enums array [] The elements that can be the desired value
"favoriteColor": {
	"location": {
		"type": "randomEnum",
		"params": {
			"enums": [
                "green", "blue", "yellow", 1, 
                {
				"more": "black"
				}
            ]
		}
	}
}

insertObject

Replaces the desired key's value with an object, can be used to insert an object to a certain key

Argument Type Default Description
obj object None The object that we want to set as the value
"overrides": {
	"child": {
		"type": "insertObject",
		"params": {
			"obj": {
					"name": "son",
					"age": 12
				}
		}
	}
}

Supported Relative Randoms

For changing the current value with a relative new value

keepValue

Keeps the current value This options has no params option

"overrides": {
	"uuid": {
		"type": "randomUUID",
	},
	"re-override": {
		"type": "keepValue"
	}
}

relativeInt

Adds to the desired key's value a random integer

Argument Type Default Description
min int 0 The min value of the random to be added
max int 100 The max value of the random to be added
"overrides": {
	"numberOfOrders": {
		"type": "randomInt",
		"params": {
			"min": 1,
			"max": 1000
		}
	},
	"re-override": {
		"type": "relativeInt",
		"params": {
			"min": 0,
			"max": 5
		}
	}
}

relativeFloat

Adds to the desired key's value a random float

Argument Type Default Description
min float 0.0 The min value of the random
max float 100.0 The max value of the random
decPlace int 2 The number of decimals to use when rounding the number
"overrides": {
	"tokens": {
		"type": "randomFloat",
		"params": {
			"min": 0.0,
			"max": 100.0,
            "decPlace": 1
		}
	},
	"re-override": {
		"type": "relativeFloat",
		"params": {
			"min": -0.5,
			"max": 0.5
		}
	}
}

relativeGEO

Adds to the desired key's value a randomize tuple of 2 float numbers

Argument Type Default Description
minX float 32.0 The min value of the first elemnt
maxX float 33.5 The max value of the first elemnt
minY float 35.0 The min value of the second elemnt
maxY float 36.5 The max value of the second elemnt
"overrides": {
	"location": {
		"type": "randomGEO",
		"params": {
			"minX": 33.3,
            "maxX": 34.2,
            "minY": 35.0,
            "maxY": 36.8
		}
	},
	"re-override": {
		"type": "relativeGEO",
		"params": {
			"minX": -0.5,
            "maxX": 0.5,
            "minY": -0.3,
            "maxY": 0.3
		}
	}
}

result example:

"location": [
	33.168916,
	35.172589
]

Usage/Examples

object in the collection

{
  "_id": {
    "$oid": "6340384a7f4fab1902653301"
  },
  "name": "full",
  "version": "1",
  "data": {
    "action_name": "mobile signup",
    "info": [
      {
        "name": "test_signUp",
        "parameters": {
          "id": 12354,
          "username": "max@getappcard.com",
          "password": "12345",
          "mobileLater": "123454231",
          "mobile": "1e2w1e2w",
          "card": "1232313",
          "cardLater": "1234321234321",
          "age": 42.1,
          "lastUpdateTime": null,
          "location": [
            33.168916,
            35.172589
          ]
        }
      }
    ],
    "fields": [
      {
        "MOB_header": "My stores",
        "favoriteColor": "orange"
      },
      {
        "url": "/stores/my"
      }
    ],
    "child": "jhon"
  },

  "re-override": true,
  "overrides": {
    "id": {
      "type": "randomInt",
      "params": {
        "min": 1,
        "max": 1000
      },
	  "re-override": {
        "type": "keepValue"
      }
    },
    "username": {
      "type": "randomString",
      "params": {
        "size": 9,
        "chars": "ascii_lowercase"
      }
    },
    "password": {
      "type": "randomString",
      "params": {
        "size": 10
      }
    },
    "age": {
      "type": "randomFloat",
      "params": {
        "min": 13,
        "max": 99,
        "decPlace": 1
      },
	  "re-override": {
        "type": "relativeInt",
        "params": {
          "min": -3,
          "max": 0
        }
      }
    },
	"tokens": {
      "type": "randomFloat",
      "params": {
        "min": 13,
        "max": 99,
        "decPlace": 1
      },
	  "re-override": {
        "type": "relativeFloat",
        "params": {
          "min": -3.1,
          "max": 6.2
        }
      }
    },
    "lastUpdateTime": {
      "type": "randomTimestamp",
      "params": {
        "delta": 10
      }
    },
    "location": {
      "type": "randomGEO",
      "params": {
        "minX": 33.3,
        "maxX": 34.2
      },
	  "re-override": {
		"type": "relativeGEO",
		"params": {
			"minX": -0.5,
            "maxX": 0.5,
            "minY": -0.3,
            "maxY": 0.3
		}
	}
    },
    "favoriteColor": {
      "type": "randomEnum",
      "params": {
        "enums": [
          "green",
          "blue",
          "yellow",
          1,
          {
            "more": "black"
          }
        ]
      }
    },
    "child": {
      "type": "insertObject",
      "params": {
        "obj": {
          "name": "son",
          "age": 12
        }
      }
    }
  }
}

An example respose to a request for that json:

{
	"action_name": "mobile signup",
	"child": {
		"age": 12,
		"name": "son"
	},
	"functions": [
		{
			"name": "test_signUp",
			"parameters": {
				"age": 56.6,
				"card": "1232313",
				"cardLater": "1234321234321",
				"id": 303,
				"lastUpdateTime": "2022-10-09T15:53:52.152303",
				"location": [
					33.168916,
					35.172589
				],
				"mobile": "1e2w1e2w",
				"mobileLater": "123454231",
				"password": "wsQLW\tcLxG",
				"username": "ctzqhbbwq"
			}
		}
	],
	"validations": [
		{
			"MOB_header": "My stores",
			"favoriteColor": "green"
		},
		{
			"url": "/stores/my"
		}
	]
}

Run Locally

Clone the project

  git clone https://github.com/DDragon1/py-json-server.git

Go to the project directory

  cd py-json-server

Install dependencies

pip install dnspython pymongo Flask

Start the server

flask run --host=0.0.0.0

Roadmap

  • add override options with randromize output

  • add re-override option with +- randroms

  • save the current randroms so the ID's won't change

  • add changes by positions in tree

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages