Remove GraphQL object in favor of helper functions
This commit is contained in:
@@ -11,7 +11,6 @@ from collections import OrderedDict
|
|||||||
from ._util import *
|
from ._util import *
|
||||||
from .models import *
|
from .models import *
|
||||||
from . import _graphql
|
from . import _graphql
|
||||||
from ._graphql import GraphQL
|
|
||||||
from ._state import State
|
from ._state import State
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
@@ -158,8 +157,8 @@ class Client(object):
|
|||||||
|
|
||||||
def graphql_requests(self, *queries):
|
def graphql_requests(self, *queries):
|
||||||
"""
|
"""
|
||||||
:param queries: Zero or more GraphQL objects
|
:param queries: Zero or more dictionaries
|
||||||
:type queries: GraphQL
|
:type queries: dict
|
||||||
|
|
||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
:return: A tuple containing json graphql queries
|
:return: A tuple containing json graphql queries
|
||||||
@@ -324,7 +323,7 @@ class Client(object):
|
|||||||
|
|
||||||
def _forcedFetch(self, thread_id, mid):
|
def _forcedFetch(self, thread_id, mid):
|
||||||
params = {"thread_and_message_id": {"thread_id": thread_id, "message_id": mid}}
|
params = {"thread_and_message_id": {"thread_id": thread_id, "message_id": mid}}
|
||||||
return self.graphql_request(GraphQL(doc_id="1768656253222505", params=params))
|
return self.graphql_request(_graphql.from_doc_id("1768656253222505", params))
|
||||||
|
|
||||||
def fetchThreads(self, thread_location, before=None, after=None, limit=None):
|
def fetchThreads(self, thread_location, before=None, after=None, limit=None):
|
||||||
"""
|
"""
|
||||||
@@ -436,7 +435,7 @@ class Client(object):
|
|||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
params = {"search": name, "limit": limit}
|
params = {"search": name, "limit": limit}
|
||||||
j = self.graphql_request(GraphQL(query=_graphql.SEARCH_USER, params=params))
|
j = self.graphql_request(_graphql.from_query(_graphql.SEARCH_USER, params))
|
||||||
|
|
||||||
return [User._from_graphql(node) for node in j[name]["users"]["nodes"]]
|
return [User._from_graphql(node) for node in j[name]["users"]["nodes"]]
|
||||||
|
|
||||||
@@ -450,7 +449,7 @@ class Client(object):
|
|||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
params = {"search": name, "limit": limit}
|
params = {"search": name, "limit": limit}
|
||||||
j = self.graphql_request(GraphQL(query=_graphql.SEARCH_PAGE, params=params))
|
j = self.graphql_request(_graphql.from_query(_graphql.SEARCH_PAGE, params))
|
||||||
|
|
||||||
return [Page._from_graphql(node) for node in j[name]["pages"]["nodes"]]
|
return [Page._from_graphql(node) for node in j[name]["pages"]["nodes"]]
|
||||||
|
|
||||||
@@ -465,7 +464,7 @@ class Client(object):
|
|||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
params = {"search": name, "limit": limit}
|
params = {"search": name, "limit": limit}
|
||||||
j = self.graphql_request(GraphQL(query=_graphql.SEARCH_GROUP, params=params))
|
j = self.graphql_request(_graphql.from_query(_graphql.SEARCH_GROUP, params))
|
||||||
|
|
||||||
return [Group._from_graphql(node) for node in j["viewer"]["groups"]["nodes"]]
|
return [Group._from_graphql(node) for node in j["viewer"]["groups"]["nodes"]]
|
||||||
|
|
||||||
@@ -480,7 +479,7 @@ class Client(object):
|
|||||||
:raises: FBchatException if request failed
|
:raises: FBchatException if request failed
|
||||||
"""
|
"""
|
||||||
params = {"search": name, "limit": limit}
|
params = {"search": name, "limit": limit}
|
||||||
j = self.graphql_request(GraphQL(query=_graphql.SEARCH_THREAD, params=params))
|
j = self.graphql_request(_graphql.from_query(_graphql.SEARCH_THREAD, params))
|
||||||
|
|
||||||
rtn = []
|
rtn = []
|
||||||
for node in j[name]["threads"]["nodes"]:
|
for node in j[name]["threads"]["nodes"]:
|
||||||
@@ -706,7 +705,7 @@ class Client(object):
|
|||||||
"load_read_receipts": False,
|
"load_read_receipts": False,
|
||||||
"before": None,
|
"before": None,
|
||||||
}
|
}
|
||||||
queries.append(GraphQL(doc_id="2147762685294928", params=params))
|
queries.append(_graphql.from_doc_id("2147762685294928", params))
|
||||||
|
|
||||||
j = self.graphql_requests(*queries)
|
j = self.graphql_requests(*queries)
|
||||||
|
|
||||||
@@ -771,7 +770,7 @@ class Client(object):
|
|||||||
"load_read_receipts": True,
|
"load_read_receipts": True,
|
||||||
"before": before,
|
"before": before,
|
||||||
}
|
}
|
||||||
j = self.graphql_request(GraphQL(doc_id="1860982147341344", params=params))
|
j = self.graphql_request(_graphql.from_doc_id("1860982147341344", params))
|
||||||
|
|
||||||
if j.get("message_thread") is None:
|
if j.get("message_thread") is None:
|
||||||
raise FBchatException("Could not fetch thread {}: {}".format(thread_id, j))
|
raise FBchatException("Could not fetch thread {}: {}".format(thread_id, j))
|
||||||
@@ -828,7 +827,7 @@ class Client(object):
|
|||||||
"includeDeliveryReceipts": True,
|
"includeDeliveryReceipts": True,
|
||||||
"includeSeqID": False,
|
"includeSeqID": False,
|
||||||
}
|
}
|
||||||
j = self.graphql_request(GraphQL(doc_id="1349387578499440", params=params))
|
j = self.graphql_request(_graphql.from_doc_id("1349387578499440", params))
|
||||||
|
|
||||||
rtn = []
|
rtn = []
|
||||||
for node in j["viewer"]["message_threads"]["nodes"]:
|
for node in j["viewer"]["message_threads"]["nodes"]:
|
||||||
@@ -933,7 +932,7 @@ class Client(object):
|
|||||||
return Plan._from_fetch(j)
|
return Plan._from_fetch(j)
|
||||||
|
|
||||||
def _getPrivateData(self):
|
def _getPrivateData(self):
|
||||||
j = self.graphql_request(GraphQL(doc_id="1868889766468115"))
|
j = self.graphql_request(_graphql.from_doc_id("1868889766468115", {}))
|
||||||
return j["viewer"]
|
return j["viewer"]
|
||||||
|
|
||||||
def getPhoneNumbers(self):
|
def getPhoneNumbers(self):
|
||||||
@@ -1570,7 +1569,7 @@ class Client(object):
|
|||||||
"surface": "ADMIN_MODEL_APPROVAL_CENTER",
|
"surface": "ADMIN_MODEL_APPROVAL_CENTER",
|
||||||
}
|
}
|
||||||
j = self.graphql_request(
|
j = self.graphql_request(
|
||||||
GraphQL(doc_id="1574519202665847", params={"data": data})
|
_graphql.from_doc_id("1574519202665847", {"data": data})
|
||||||
)
|
)
|
||||||
|
|
||||||
def acceptUsersToGroup(self, user_ids, thread_id=None):
|
def acceptUsersToGroup(self, user_ids, thread_id=None):
|
||||||
|
@@ -4,7 +4,7 @@ from __future__ import unicode_literals
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
from . import _util
|
from . import _util
|
||||||
from ._exception import FBchatException, FBchatUserError
|
from ._exception import FBchatException
|
||||||
|
|
||||||
# Shameless copy from https://stackoverflow.com/a/8730674
|
# Shameless copy from https://stackoverflow.com/a/8730674
|
||||||
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
|
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
|
||||||
@@ -33,7 +33,7 @@ def queries_to_json(*queries):
|
|||||||
"""
|
"""
|
||||||
rtn = {}
|
rtn = {}
|
||||||
for i, query in enumerate(queries):
|
for i, query in enumerate(queries):
|
||||||
rtn["q{}".format(i)] = query.value
|
rtn["q{}".format(i)] = query
|
||||||
return json.dumps(rtn)
|
return json.dumps(rtn)
|
||||||
|
|
||||||
|
|
||||||
@@ -62,16 +62,20 @@ def response_to_json(content):
|
|||||||
return rtn
|
return rtn
|
||||||
|
|
||||||
|
|
||||||
class GraphQL(object):
|
def from_query(query, params):
|
||||||
def __init__(self, query=None, doc_id=None, params=None):
|
return {"priority": 0, "q": query, "query_params": params}
|
||||||
if params is None:
|
|
||||||
params = {}
|
|
||||||
if query is not None:
|
def from_query_id(query_id, params):
|
||||||
self.value = {"priority": 0, "q": query, "query_params": params}
|
return {"query_id": query_id, "query_params": params}
|
||||||
elif doc_id is not None:
|
|
||||||
self.value = {"doc_id": doc_id, "query_params": params}
|
|
||||||
else:
|
def from_doc(doc, params):
|
||||||
raise FBchatUserError("A query or doc_id must be specified")
|
return {"doc": doc, "query_params": params}
|
||||||
|
|
||||||
|
|
||||||
|
def from_doc_id(doc_id, params):
|
||||||
|
return {"doc_id": doc_id, "query_params": params}
|
||||||
|
|
||||||
|
|
||||||
FRAGMENT_USER = """
|
FRAGMENT_USER = """
|
||||||
|
Reference in New Issue
Block a user