inspected

This commit is contained in:
2023-10-14 17:55:51 +03:00
parent 154633c114
commit 34dd4ec140
14 changed files with 148 additions and 199 deletions

View File

@@ -1,4 +1,3 @@
import json
from functools import wraps
from httpx import AsyncClient, HTTPError
@@ -19,19 +18,13 @@ async def check_auth(req):
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,
}
async with AsyncClient() as client:
response = await client.post(AUTH_URL, headers=headers, data=json.dumps(gql))
response = await client.post(AUTH_URL, headers=headers, json=gql)
print(f"[services.auth] response: {response.status_code} {response.text}")
if response.status_code != 200:
return False, None
@@ -40,10 +33,7 @@ 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

@@ -1,43 +1,38 @@
import json
from httpx import AsyncClient
from settings import API_BASE
from validators.member import ChatMember
headers = {"Content-Type": "application/json"}
async def get_author(author_id):
gql = {
"query": '''query GetAuthorById($author_id: Int!) {
getAuthorById(author_id: $author_id) { id slug userpic name lastSeen }
}''',
"query": """query GetAuthorById($author_id: Int!) {
getAuthorById(author_id: $author_id) {
id slug userpic name lastSeen
}
}""",
"operation": "GetAuthorById",
"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.status_code} {response.text}")
if response.status_code != 200:
return None
r = response.json()
author = r.get("data", {}).get("getAuthorById")
return author
except Exception:
response = await client.post(API_BASE, headers=headers, json=gql)
print(f"[services.core] get_author: {response.status_code} {response.text}")
if response.status_code != 200:
return None
r = response.json()
author: ChatMember | None = r.get("data", {}).get("getAuthorById")
return author
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
"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},
}
@@ -45,9 +40,7 @@ async def get_network(author_id: int, limit: int = 50, offset: int = 0) -> list:
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, json=gql)
if response.status_code != 200:
return []
r = response.json()
@@ -64,25 +57,21 @@ async def get_network(author_id: int, limit: int = 50, offset: int = 0) -> list:
async def get_followers(author_id, amount):
gql = {
"query": '''query LoadAuthors($author_id: Int!, $limit: Int, $offset: Int) {
"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},
}
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, json=gql)
if response.status_code != 200:
return []
r = response.json()
followers = r.get("data", {}).get("authorFollowers", [])
return r.get("data", {}).get("authorFollowers", [])
except Exception as e:
print(e)
followers = []
return followers
return []

View File

@@ -1,7 +1,7 @@
import json
from services.redis import redis
from validators.inbox import Message
from services.rediscache import redis
from validators.chat import Message
async def notify_message(message: Message, chat_id: str):