async def get_not_friends_from_person( response: Response, person: str = "Gustavo", ): """ Get second connection friends from person """ if not person: raise HTTPException(status_code=HTTP_400_BAD_REQUEST) if not person in g().friends.keys(): raise HTTPException(status_code=HTTP_404_NOT_FOUND) graph_svc = GraphService(g().friends) return graph_svc.get_friend_of_friend_mine(person=person)
async def get_all_people(response: Response): """ Get all people from network graph **Expected result [Ana, Maria, Vinicius, Luiza, João, Carlos] """ graph_svc = GraphService(g().friends) result = await graph_svc.get_people() return result
async def put_new_person_and_friends( response: Response, person: str, friends: List[str] = Query(["Gustavo"]), ): """ Add new person to network graph """ graph_svc = GraphService(g().friends) saved = graph_svc.save_new_person(person=person, friends=friends) if not saved: raise HTTPException(HTTP_400_BAD_REQUEST) return {"success": saved}
def save_new_person(self, person: str, friends: List[str]) -> bool: """Save new person on dictonary Args: person (str): person first name friends (List[str]): list of friends """ if person in friends: return False intersection = set(self._g_dict.keys()).intersection(set(friends)) if len(friends) != len(intersection): return False self._g_dict[person] = friends g().friends = self._g_dict.copy() return True
async def startup(): async with aiofiles.open(FILEPATH, "r") as tmp_file: # in Python3.6 + the dict keep the same order preserving aspect when created g().friends = json.loads(await tmp_file.read())
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # __init__.py # @Author : Gustavo Freitas ([email protected]) # @Link : https://github.com/sharkguto from graphox.helpers.requestvars import g from fastapi.testclient import TestClient from graphox.config import FILEPATH import ujson as json from graphox import app client = TestClient(app) # force load of json to global object in context with open(FILEPATH, "r") as tmp_file: g().friends = json.loads(tmp_file.read())