diff --git a/src/random_name/app.py b/src/random_name/app.py index 1d78a52..cb0b937 100644 --- a/src/random_name/app.py +++ b/src/random_name/app.py @@ -1,6 +1,11 @@ +import uuid + from aiohttp import web from app.app import Application as BaseApplication, get_timestamp from faker import Faker +from faker.providers import internet +import json +from datetime import datetime class Application(BaseApplication): @@ -11,17 +16,41 @@ class Application(BaseApplication): self.router.add_get("/", self.index_handler) self.fake = Faker() + self.fake.add_provider(internet) async def index_handler(self, request): name = None + profile = None while True: name = self.fake.name() if await self.is_name_used(name): continue + + profile = self.fake.profile() + profile.update( + { + "uid": str(uuid.uuid4()), + "name": name, + "ipv4_private": self.fake.ipv4_private(), + "ipv4_public": self.fake.ipv4_public(), + "ipv6": self.fake.ipv6(True), + "mac_address": self.fake.mac_address(), + "username":self.fake.user_name(), + "description": self.fake.text(), + "created":get_timestamp() + } + ) break - await self.register_name_used(name) - return web.json_response(name, content_type="application/json") + profile = json.dumps(profile,default=str) + profile = json.loads(profile) + for key, value in profile.items(): + if type(value) == list: + profile[key] = str(value) + await self.insert('profile',profile) + await self.register_name_used(profile['name']) + profile = json.dumps(profile,indent=2) + return web.Response(text=profile,content_type="application/json") async def is_name_used(self, name): return (