def db_update_user(user_profile: dict, search_by_email: bool = False) -> dict: """ Updates a user in the database via GraphQL :param dict user_profile: The user details :return dict: The response from the GraphQL server """ search_logic = { "cognito_user_id": { "_eq": user_profile["cognito_user_id"] } } # Search by email if provided the flag if search_by_email: search_logic = {"email": {"_eq": user_profile["email"]}} response = run_query( query=GRAPHQL_UPDATE_USER, variables={ "userBoolExp": { **search_logic }, "user": user_profile, }, ) return response.json()
def db_create_user(user_profile: dict) -> dict: """ Creates a user in the database via GraphQL :param dict user_profile: The user details :return dict: The response from the GraphQL server """ response = run_query(query=GRAPHQL_CREATE_USER, variables={"users": [user_profile]}) return response.json()
def test_run_query(self): from graphql import run_query # Test for valid secret and endpoint response = run_query( query="{}", variables={}, alternative_conf={ "HASURA_HTTPS_ENDPOINT": "http://localhost:5000", "HASURA_ADMIN_SECRET": "SUPER_SECRET_HERE" } ) assert response.status_code == 200 assert response.json().get("query") == "ok" # Test for bad secret response_bad_secret = run_query( query="{}", variables={}, alternative_conf={ "HASURA_HTTPS_ENDPOINT": "http://localhost:5000", "HASURA_ADMIN_SECRET": "INVALID_SECRET" } ) assert response_bad_secret.status_code == 500 # Test for bad URL response_bad_url = run_query( query="{}", variables={}, alternative_conf={ "HASURA_HTTPS_ENDPOINT": "http://localhost:5000/v1/graphql/run_query/nowhere", } ) assert response_bad_url.status_code == 404
def db_deactivate_user(user_cognito_id: str) -> dict: """ Deactivates a user in the database via GraphQL :param str user_cognito_id: The cognito id of the user :return dict: The response from the GraphQL server """ response = run_query( query=GRAPHQL_DEACTIVATE_USER, variables={ "userBoolExp": { "cognito_user_id": { "_eq": user_cognito_id } } }, ) return response.json()
def db_user_exists(user_email: str) -> tuple: """ Runs a search in the database for any users with the email or user_cognito_uuid provided. :param str user_email: The email to search :return tuple: """ # Find the user try: response = run_query(query=GRAPHQL_USER_EXISTS, variables={ "userEmail": user_email }).json() except: return False, None # Check if response is not a valid response if not isinstance(response, dict): return False, None # If we have a response but it is not data, then it's an error. Return false. if "data" not in response or "moped_users" not in response["data"]: return False, None # If the list is empty, then the user does not exist if len(response["data"]["moped_users"]) == 0: return False, None # Select the first element (it should be the only element) moped_user = response["data"]["moped_users"][0] # Check if the user is in fact what we are looking for if moped_user["email"] == user_email: return True, moped_user["cognito_user_id"] # It's not return False, None
}], "initialPrefs": {}, "keywordVariant": "location_keywords_v2_llr_order_30_ko", "limit": 20, "locationId": 550726, "needKeywords": False, "offset": offset, "prefs": None, "prefsCacheKey": "locationReviewPrefs" } if __name__ == "__main__": total_count = 1072 offset = 0 reviews = [] while offset < total_count: data = run_query(endpoint, query, initial_variables( offset))['data']['locations'][0]['reviewListPage']['reviews'] reviews += data offset += 20 def mapper(review): return list(map(lambda key: review[keys[key]], header)) pd.DataFrame(list(map(mapper, reviews))).to_csv('output.csv', header=header, index=None)