validators

This commit is contained in:
2023-10-14 09:38:12 +03:00
parent ee195d4ec7
commit d6e52d9465
9 changed files with 116 additions and 95 deletions

View File

@@ -15,15 +15,18 @@ async def check_auth(req):
query_type = "mutation" if INTERNAL_AUTH_SERVER else "query"
operation = "GetUserId"
headers = {
"Authorization": 'Bearer ' + token,
"Content-Type": "application/json"
}
headers = {"Authorization": "Bearer " + token, "Content-Type": "application/json"}
gql = {
"query": query_type + " " + operation + " { " + query_name + " { user { id } } " + " }",
"query": query_type
+ " "
+ operation
+ " { "
+ query_name
+ " { user { id } } "
+ " }",
"operationName": operation,
"variables": None
"variables": None,
}
async with AsyncClient() as client:
@@ -36,7 +39,10 @@ async def check_auth(req):
user_id = (
r.get("data", {}).get(query_name, {}).get("user", {}).get("id", None)
if INTERNAL_AUTH_SERVER
else r.get("data", {}).get(query_name, {}).get("user", {}).get("id", None)
else r.get("data", {})
.get(query_name, {})
.get("user", {})
.get("id", None)
)
is_authenticated = user_id is not None
return is_authenticated, user_id

View File

@@ -11,12 +11,16 @@ async def get_author(author_id):
gql = {
"query": "query GetAuthorById($author_id: Int!) { getAuthorById(author_id: $author_id) { id slug userpic name lastSeen } }",
"operation": "GetAuthorById",
"variables": {"author_id": author_id}
"variables": {"author_id": author_id},
}
async with AsyncClient() as client:
try:
response = await client.post(API_BASE, headers=headers, data=json.dumps(gql))
print(f"[services.core] get_author response: {response.status_code} {response.text}")
response = await client.post(
API_BASE, headers=headers, data=json.dumps(gql)
)
print(
f"[services.core] get_author response: {response.status_code} {response.text}"
)
if response.status_code != 200:
return None
r = response.json()
@@ -27,21 +31,19 @@ async def get_author(author_id):
return None
async def get_network(author_id:int, limit:int = 50, offset:int = 0) -> list:
async def get_network(author_id: int, limit: int = 50, offset: int = 0) -> list:
gql = {
"query": "query LoadAuthors($author_id: Int!, $limit: Int, $offset: Int) { authorFollowings(author_id: $author_id, limit: $limit, offset: $offset) { id slug userpic name } }",
"operation": "LoadAuthors",
"variables": {
"author_id": author_id,
"limit": limit,
"offset": offset
}
"variables": {"author_id": author_id, "limit": limit, "offset": offset},
}
followings = []
try:
async with AsyncClient() as client:
response = await client.post(API_BASE, headers=headers, data=json.dumps(gql))
response = await client.post(
API_BASE, headers=headers, data=json.dumps(gql)
)
if response.status_code != 200:
return []
r = response.json()
@@ -60,15 +62,14 @@ async def get_followers(author_id, amount):
gql = {
"query": "query LoadAuthors($author_id: Int!, $limit: Int, $offset: Int) { authorFollowers(author_id: $author_id, limit: $limit) { id slug userpic name } }",
"operation": "LoadAuthors",
"variables": {
"author_id": author_id,
"limit": amount
}
"variables": {"author_id": author_id, "limit": amount},
}
followers = []
try:
async with AsyncClient() as client:
response = await client.post(API_BASE, headers=headers, data=json.dumps(gql))
response = await client.post(
API_BASE, headers=headers, data=json.dumps(gql)
)
if response.status_code != 200:
return []
r = response.json()

View File

@@ -1,8 +1,9 @@
import json
from services.redis import redis
from validators.chat import Message
async def notify_message(message, chat_id: str):
async def notify_message(message: Message, chat_id: str):
channel_name = f"chat:{chat_id}"
data = {**message, "kind": "new_message"}
try:

View File

@@ -1,6 +1,7 @@
import redis.asyncio as aredis
from settings import REDIS_URL
class RedisCache:
def __init__(self, uri=REDIS_URL):
self._uri: str = uri
@@ -54,6 +55,7 @@ class RedisCache:
print(f"[redis] MGET {key} {keys}")
return await self._client.mget(key, *keys)
redis = RedisCache()
__all__ = ["redis"]