sgqlc 16.4__tar.gz → 17__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sgqlc-16.4 → sgqlc-17}/PKG-INFO +26 -13
- {sgqlc-16.4 → sgqlc-17}/README.rst +19 -1
- {sgqlc-16.4 → sgqlc-17}/doc/source/conf.py +2 -2
- {sgqlc-16.4 → sgqlc-17}/pyproject.toml +25 -17
- {sgqlc-16.4 → sgqlc-17}/sgqlc/__init__.py +1 -1
- {sgqlc-16.4 → sgqlc-17}/sgqlc/endpoint/http.py +17 -3
- sgqlc-17/sgqlc/endpoint/httpx.py +253 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/endpoint/requests.py +8 -2
- {sgqlc-16.4 → sgqlc-17}/sgqlc/types/__init__.py +5 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/types/datetime.py +16 -4
- {sgqlc-16.4 → sgqlc-17}/tests/test-endpoint-http.py +85 -23
- sgqlc-17/tests/test-endpoint-httpx.py +701 -0
- {sgqlc-16.4 → sgqlc-17}/tests/test-endpoint-requests.py +12 -0
- {sgqlc-16.4 → sgqlc-17}/AUTHORS +0 -0
- {sgqlc-16.4 → sgqlc-17}/LICENSE +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/_static/.keepme +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/_static/logo-sgqlc.png +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/favicon-sgqlc.png +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/index.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.codegen.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.endpoint.base.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.endpoint.http.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.endpoint.requests.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.endpoint.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.endpoint.websocket.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.introspection.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.operation.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.types.datetime.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.types.relay.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.types.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/doc/source/sgqlc.types.uuid.rst +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/codegen/__init__.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/codegen/__main__.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/codegen/operation.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/codegen/schema.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/endpoint/__init__.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/endpoint/base.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/endpoint/websocket.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/introspection/__init__.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/introspection/__main__.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/operation/__init__.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/types/relay.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/sgqlc/types/uuid.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/tests/test-endpoint-websocket.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/tests/test-introspection.py +0 -0
- {sgqlc-16.4 → sgqlc-17}/tests/test-operation-codegen.py +0 -0
{sgqlc-16.4 → sgqlc-17}/PKG-INFO
RENAMED
|
@@ -1,34 +1,29 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
2
|
Name: sgqlc
|
|
3
|
-
Version:
|
|
3
|
+
Version: 17
|
|
4
4
|
Summary: Simple GraphQL Client
|
|
5
|
-
Home-page: http://github.com/profusion/sgqlc
|
|
6
5
|
License: ISCL
|
|
7
6
|
Keywords: graphql,client,http,endpoint
|
|
8
7
|
Author: Gustavo Sverzut Barbieri
|
|
9
8
|
Author-email: barbieri@profusion.mobi
|
|
10
|
-
Requires-Python: >=3.8,<
|
|
9
|
+
Requires-Python: >=3.8,<4
|
|
11
10
|
Classifier: Development Status :: 4 - Beta
|
|
12
11
|
Classifier: Environment :: Console
|
|
13
12
|
Classifier: Environment :: Web Environment
|
|
14
13
|
Classifier: Intended Audience :: Developers
|
|
15
14
|
Classifier: License :: OSI Approved :: ISC License (ISCL)
|
|
16
|
-
Classifier: License :: Other/Proprietary License
|
|
17
15
|
Classifier: Operating System :: OS Independent
|
|
18
16
|
Classifier: Programming Language :: Python
|
|
19
|
-
Classifier: Programming Language :: Python :: 3
|
|
20
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
21
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
22
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
25
17
|
Classifier: Topic :: Utilities
|
|
18
|
+
Provides-Extra: httpx
|
|
26
19
|
Provides-Extra: requests
|
|
27
20
|
Provides-Extra: websocket
|
|
28
|
-
Requires-Dist: graphql-core (>=3.2.
|
|
21
|
+
Requires-Dist: graphql-core (>=3.2.6,<3.3.0)
|
|
22
|
+
Requires-Dist: httpx ; extra == "httpx"
|
|
29
23
|
Requires-Dist: requests ; extra == "requests"
|
|
30
24
|
Requires-Dist: websocket-client ; extra == "websocket"
|
|
31
25
|
Project-URL: Documentation, https://sgqlc.readthedocs.io/
|
|
26
|
+
Project-URL: Homepage, http://github.com/profusion/sgqlc
|
|
32
27
|
Project-URL: Repository, http://github.com/profusion/sgqlc
|
|
33
28
|
Description-Content-Type: text/x-rst
|
|
34
29
|
|
|
@@ -278,6 +273,24 @@ hand-written query (see more at ``examples/basic/01_http_endpoint.py``):
|
|
|
278
273
|
endpoint = HTTPEndpoint(url, headers)
|
|
279
274
|
data = endpoint(query, variables)
|
|
280
275
|
|
|
276
|
+
To make an asynchronous query, use the `httpx` library with `HTTPXEndpoint`
|
|
277
|
+
(see more at ``examples/basic/04_httpx_endpoint.py``):
|
|
278
|
+
|
|
279
|
+
.. code-block:: python
|
|
280
|
+
|
|
281
|
+
from sgqlc.endpoint.httpx import HTTPXEndpoint
|
|
282
|
+
import httpx
|
|
283
|
+
|
|
284
|
+
async def main():
|
|
285
|
+
url = 'http://server.com/graphql'
|
|
286
|
+
headers = {'Authorization': 'bearer TOKEN'}
|
|
287
|
+
|
|
288
|
+
query = 'query { ... }'
|
|
289
|
+
variables = {'varName': 'value'}
|
|
290
|
+
|
|
291
|
+
endpoint = HTTPXEndpoint(url, headers, client=httpx.AsyncClient())
|
|
292
|
+
data = await endpoint(query, variables)
|
|
293
|
+
|
|
281
294
|
|
|
282
295
|
However, writing GraphQL queries and later interpreting the results
|
|
283
296
|
may be cumbersome. That's solved by our ``sgqlc.types``, which is
|
|
@@ -541,7 +554,7 @@ Run the tests (one of the below):
|
|
|
541
554
|
|
|
542
555
|
Keep 100% coverage. You can look at the coverage report at
|
|
543
556
|
``cover/index.html``. To do that, prefer
|
|
544
|
-
`doctest <https://docs.python.org/3.
|
|
557
|
+
`doctest <https://docs.python.org/3.13/library/doctest.html>`_
|
|
545
558
|
so it serves as
|
|
546
559
|
both documentation and test. However we use
|
|
547
560
|
`pytest <https://docs.pytest.org/>`_ to write explicit tests that would be
|
|
@@ -244,6 +244,24 @@ hand-written query (see more at ``examples/basic/01_http_endpoint.py``):
|
|
|
244
244
|
endpoint = HTTPEndpoint(url, headers)
|
|
245
245
|
data = endpoint(query, variables)
|
|
246
246
|
|
|
247
|
+
To make an asynchronous query, use the `httpx` library with `HTTPXEndpoint`
|
|
248
|
+
(see more at ``examples/basic/04_httpx_endpoint.py``):
|
|
249
|
+
|
|
250
|
+
.. code-block:: python
|
|
251
|
+
|
|
252
|
+
from sgqlc.endpoint.httpx import HTTPXEndpoint
|
|
253
|
+
import httpx
|
|
254
|
+
|
|
255
|
+
async def main():
|
|
256
|
+
url = 'http://server.com/graphql'
|
|
257
|
+
headers = {'Authorization': 'bearer TOKEN'}
|
|
258
|
+
|
|
259
|
+
query = 'query { ... }'
|
|
260
|
+
variables = {'varName': 'value'}
|
|
261
|
+
|
|
262
|
+
endpoint = HTTPXEndpoint(url, headers, client=httpx.AsyncClient())
|
|
263
|
+
data = await endpoint(query, variables)
|
|
264
|
+
|
|
247
265
|
|
|
248
266
|
However, writing GraphQL queries and later interpreting the results
|
|
249
267
|
may be cumbersome. That's solved by our ``sgqlc.types``, which is
|
|
@@ -507,7 +525,7 @@ Run the tests (one of the below):
|
|
|
507
525
|
|
|
508
526
|
Keep 100% coverage. You can look at the coverage report at
|
|
509
527
|
``cover/index.html``. To do that, prefer
|
|
510
|
-
`doctest <https://docs.python.org/3.
|
|
528
|
+
`doctest <https://docs.python.org/3.13/library/doctest.html>`_
|
|
511
529
|
so it serves as
|
|
512
530
|
both documentation and test. However we use
|
|
513
531
|
`pytest <https://docs.pytest.org/>`_ to write explicit tests that would be
|
|
@@ -61,9 +61,9 @@ author = 'ProFUSION Sistemas e Solucoes LTDA'
|
|
|
61
61
|
# built documents.
|
|
62
62
|
#
|
|
63
63
|
# The short X.Y version.
|
|
64
|
-
version = '
|
|
64
|
+
version = '17'
|
|
65
65
|
# The full version, including alpha/beta/rc tags.
|
|
66
|
-
release = '
|
|
66
|
+
release = '17'
|
|
67
67
|
|
|
68
68
|
# The language for content autogenerated by Sphinx. Refer to documentation
|
|
69
69
|
# for a list of supported languages.
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
[tool.black]
|
|
2
2
|
line-length = 79
|
|
3
3
|
skip-string-normalization = true
|
|
4
|
-
target-version = ['py38', 'py39', 'py310', 'py311', 'py312']
|
|
4
|
+
target-version = ['py38', 'py39', 'py310', 'py311', 'py312', 'py313']
|
|
5
5
|
force-exclude = '/(doc/|examples/.*(schema|operations)[.]py)'
|
|
6
6
|
|
|
7
7
|
[tool.pytest.ini_options]
|
|
8
8
|
addopts = '--import-mode=importlib --doctest-modules --cov=sgqlc --cov-fail-under=100 --cov-report=term-missing --cov-report=xml --cov-report=html:cover'
|
|
9
|
+
asyncio_mode = 'auto'
|
|
10
|
+
asyncio_default_fixture_loop_scope = 'function'
|
|
9
11
|
python_files = ['test-*.py', 'test_*.py']
|
|
10
12
|
testpaths = [
|
|
11
13
|
'sgqlc',
|
|
@@ -25,17 +27,14 @@ norecursedirs = [
|
|
|
25
27
|
'utils',
|
|
26
28
|
]
|
|
27
29
|
|
|
28
|
-
[
|
|
30
|
+
[project]
|
|
29
31
|
name = 'sgqlc'
|
|
30
|
-
version = '
|
|
32
|
+
version = '17'
|
|
31
33
|
description = 'Simple GraphQL Client'
|
|
32
34
|
readme = 'README.rst'
|
|
33
35
|
authors = [
|
|
34
|
-
'Gustavo Sverzut Barbieri
|
|
36
|
+
{ name = 'Gustavo Sverzut Barbieri', email = 'barbieri@profusion.mobi' },
|
|
35
37
|
]
|
|
36
|
-
homepage = 'http://github.com/profusion/sgqlc'
|
|
37
|
-
repository = 'http://github.com/profusion/sgqlc'
|
|
38
|
-
documentation = 'https://sgqlc.readthedocs.io/'
|
|
39
38
|
license = 'ISCL'
|
|
40
39
|
keywords = ['graphql', 'client', 'http', 'endpoint']
|
|
41
40
|
classifiers=[
|
|
@@ -48,6 +47,12 @@ classifiers=[
|
|
|
48
47
|
'Programming Language :: Python',
|
|
49
48
|
'Topic :: Utilities',
|
|
50
49
|
]
|
|
50
|
+
requires-python = '>=3.8,<4'
|
|
51
|
+
dependencies = [
|
|
52
|
+
'graphql-core~=3.2.6'
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[tool.poetry]
|
|
51
56
|
packages = [{ include = 'sgqlc' }]
|
|
52
57
|
include = [
|
|
53
58
|
{ path = 'AUTHORS', format = 'sdist' },
|
|
@@ -55,26 +60,29 @@ include = [
|
|
|
55
60
|
{ path = 'tests', format = 'sdist' },
|
|
56
61
|
]
|
|
57
62
|
|
|
58
|
-
[
|
|
59
|
-
|
|
63
|
+
[project.urls]
|
|
64
|
+
Homepage = 'http://github.com/profusion/sgqlc'
|
|
65
|
+
Repository = 'http://github.com/profusion/sgqlc'
|
|
66
|
+
Documentation = 'https://sgqlc.readthedocs.io/'
|
|
60
67
|
|
|
61
|
-
[
|
|
62
|
-
|
|
63
|
-
python = '>=3.8,<3.13'
|
|
64
|
-
graphql-core = '^3.2.4'
|
|
65
|
-
websocket-client = { version = '*', optional = true }
|
|
66
|
-
requests = { version = '*', optional = true }
|
|
68
|
+
[project.scripts]
|
|
69
|
+
sgqlc-codegen = 'sgqlc.codegen:main'
|
|
67
70
|
|
|
68
|
-
[
|
|
71
|
+
[project.optional-dependencies]
|
|
69
72
|
websocket = ['websocket-client']
|
|
70
73
|
requests = ['requests']
|
|
74
|
+
httpx = ['httpx']
|
|
71
75
|
|
|
72
76
|
[tool.poetry.group.dev.dependencies]
|
|
73
|
-
coveralls = '*'
|
|
74
77
|
pytest = '*'
|
|
75
78
|
pytest-cov = '*'
|
|
79
|
+
pytest-asyncio = '*'
|
|
76
80
|
sphinx = '*'
|
|
77
81
|
sphinx-argparse = '*'
|
|
82
|
+
websocket-client = '*'
|
|
83
|
+
requests = '*'
|
|
84
|
+
httpx = '*'
|
|
85
|
+
respx = '*'
|
|
78
86
|
|
|
79
87
|
[tool.poetry.group.docs.dependencies]
|
|
80
88
|
sphinx = '*'
|
|
@@ -59,6 +59,8 @@ class HTTPEndpoint(BaseEndpoint):
|
|
|
59
59
|
:exc:`json.JSONDecodeError` those are stored in the
|
|
60
60
|
"exception" key.
|
|
61
61
|
|
|
62
|
+
:headers: dictionary of HTTP response headers.
|
|
63
|
+
|
|
62
64
|
.. note::
|
|
63
65
|
|
|
64
66
|
Both ``data`` and ``errors`` may be returned, for instance if
|
|
@@ -164,8 +166,9 @@ class HTTPEndpoint(BaseEndpoint):
|
|
|
164
166
|
:type timeout: float
|
|
165
167
|
|
|
166
168
|
:return: dict with optional fields ``data`` containing the GraphQL
|
|
167
|
-
returned data as nested dict
|
|
168
|
-
|
|
169
|
+
returned data as nested dict, ``headers`` with a dictionary of
|
|
170
|
+
response headers, and ``errors`` with an array of errors.
|
|
171
|
+
Note that both ``data`` and ``errors`` may be returned!
|
|
169
172
|
:rtype: dict
|
|
170
173
|
'''
|
|
171
174
|
query, req = self._prepare(
|
|
@@ -179,9 +182,20 @@ class HTTPEndpoint(BaseEndpoint):
|
|
|
179
182
|
|
|
180
183
|
try:
|
|
181
184
|
with self.urlopen(req, timeout=timeout or self.timeout) as f:
|
|
182
|
-
|
|
185
|
+
headers = f.headers
|
|
186
|
+
content_encoding = headers.get('Content-Encoding')
|
|
187
|
+
if content_encoding and 'gzip' in content_encoding:
|
|
188
|
+
import gzip
|
|
189
|
+
|
|
190
|
+
content = gzip.decompress(f.read())
|
|
191
|
+
del headers['Content-Encoding']
|
|
192
|
+
else:
|
|
193
|
+
content = f.read()
|
|
194
|
+
body = content.decode('utf-8')
|
|
183
195
|
try:
|
|
184
196
|
data = json.loads(body)
|
|
197
|
+
if data and headers:
|
|
198
|
+
data['headers'] = dict(headers)
|
|
185
199
|
if data and data.get('errors'):
|
|
186
200
|
return self._log_graphql_error(query, data)
|
|
187
201
|
return data
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
'''
|
|
2
|
+
Synchronous or Asynchronous Endpoint using httpx
|
|
3
|
+
================================================
|
|
4
|
+
|
|
5
|
+
This endpoint implements GraphQL client using the :mod:`httpx` library.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
This module provides command line utility:
|
|
9
|
+
|
|
10
|
+
.. code-block:: console
|
|
11
|
+
|
|
12
|
+
$ python3 -m sgqlc.endpoint.httpx http://server.com/ '{ queryHere { ... } }'
|
|
13
|
+
|
|
14
|
+
It's pretty much like :class:`sgqlc.endpoint.http.HTTPEndpoint`, but
|
|
15
|
+
using the :mod:`httpx`. This allows you to make asynchronous requests using
|
|
16
|
+
:mod:`httpx.AsyncClient` or re-using a global client for better connection
|
|
17
|
+
pooling.
|
|
18
|
+
|
|
19
|
+
Example using :class:`sgqlc.endpoint.httpx.HTTPXEndpoint`:
|
|
20
|
+
|
|
21
|
+
.. literalinclude:: ../../examples/basic/04_httpx_endpoint.py
|
|
22
|
+
:language: python
|
|
23
|
+
|
|
24
|
+
The ``query`` may be given as ``bytes`` or ``str`` as in the example, but
|
|
25
|
+
it may be a :class:`sgqlc.operation.Operation`, which will serialize as
|
|
26
|
+
string while also providing convenience to interepret the results.
|
|
27
|
+
|
|
28
|
+
See `more examples <https://github.com/profusion/sgqlc/tree/master/examples>`_.
|
|
29
|
+
|
|
30
|
+
:license: ISC
|
|
31
|
+
'''
|
|
32
|
+
|
|
33
|
+
__docformat__ = 'reStructuredText en'
|
|
34
|
+
|
|
35
|
+
__all__ = ('HTTPXEndpoint',)
|
|
36
|
+
|
|
37
|
+
import json
|
|
38
|
+
import httpx
|
|
39
|
+
|
|
40
|
+
from .base import add_query_to_url
|
|
41
|
+
from .http import HTTPEndpoint
|
|
42
|
+
from typing import Optional, Union, Dict
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class HTTPXEndpoint(HTTPEndpoint):
|
|
46
|
+
'''GraphQL endpoint access via httpx.
|
|
47
|
+
|
|
48
|
+
This helper is very thin, just setups the correct HTTP request to
|
|
49
|
+
GraphQL endpoint, handling logging of HTTP and GraphQL errors. The
|
|
50
|
+
object is callable with parameters: ``query``, ``variables``,
|
|
51
|
+
``operation_name``, ``extra_headers`` and ``timeout``.
|
|
52
|
+
|
|
53
|
+
The user of this class should create GraphQL queries and interpret the
|
|
54
|
+
resulting object, created from JSON data, with top level properties:
|
|
55
|
+
|
|
56
|
+
:data: object matching the GraphQL requests, or ``null`` if only
|
|
57
|
+
errors were returned.
|
|
58
|
+
|
|
59
|
+
:headers: dictionary of HTTP response headers.
|
|
60
|
+
|
|
61
|
+
:errors: list of errors, which are objects with the key "message" and
|
|
62
|
+
optionally others, such as "location" (for errors matching GraphQL
|
|
63
|
+
input). Instead of raising exceptions, such as
|
|
64
|
+
:exc:`requests.exceptions.HTTPError` or
|
|
65
|
+
:exc:`json.JSONDecodeError` those are stored in the
|
|
66
|
+
"exception" key.
|
|
67
|
+
|
|
68
|
+
:client: The httpx.Client() or httpx.AsyncClient(). If you are using an
|
|
69
|
+
AsyncClient, the endpoint call will return a coroutine you can await.
|
|
70
|
+
|
|
71
|
+
.. note::
|
|
72
|
+
|
|
73
|
+
Both ``data`` and ``errors`` may be returned, for instance if
|
|
74
|
+
a null-able field fails, it will be returned as null (Python
|
|
75
|
+
``None``) in data the associated error in the array.
|
|
76
|
+
|
|
77
|
+
The class has its own :class:`logging.Logger` which is used to
|
|
78
|
+
debug, info, warning and errors. Error logging and conversion to
|
|
79
|
+
uniform data structure similar to GraphQL, with ``{"errors": [...]}``
|
|
80
|
+
is done by :func:`HTTPXEndpoint._log_httpx_error()` own method,
|
|
81
|
+
``BaseEndpoint._log_json_error()`` and
|
|
82
|
+
``BaseEndpoint._log_graphql_error()``. This last one will show the
|
|
83
|
+
snippets of GraphQL that failed execution.
|
|
84
|
+
|
|
85
|
+
'''
|
|
86
|
+
|
|
87
|
+
def __init__(
|
|
88
|
+
self,
|
|
89
|
+
*args,
|
|
90
|
+
client: Optional[httpx.Client] = None,
|
|
91
|
+
**kwargs,
|
|
92
|
+
):
|
|
93
|
+
'''
|
|
94
|
+
:param client: The existing httpx.Client to use.
|
|
95
|
+
:type client: httpx.Client
|
|
96
|
+
'''
|
|
97
|
+
super().__init__(*args, **kwargs)
|
|
98
|
+
self.client = client or httpx.Client()
|
|
99
|
+
|
|
100
|
+
def __call__(
|
|
101
|
+
self,
|
|
102
|
+
query: Union[bytes, str],
|
|
103
|
+
variables: Optional[Dict] = None,
|
|
104
|
+
operation_name: Optional[str] = None,
|
|
105
|
+
extra_headers: Optional[Dict[str, str]] = None,
|
|
106
|
+
timeout: Optional[int] = None,
|
|
107
|
+
):
|
|
108
|
+
'''Calls the GraphQL endpoint.
|
|
109
|
+
|
|
110
|
+
:param query: the GraphQL query or mutation to execute. Note
|
|
111
|
+
that this is converted using ``bytes()``, thus one may pass
|
|
112
|
+
an object implementing ``__bytes__()`` method to return the
|
|
113
|
+
query, eventually in more compact form (no indentation, etc).
|
|
114
|
+
:type query: :class:`str` or :class:`bytes`.
|
|
115
|
+
|
|
116
|
+
:param variables: variables (dict) to use with
|
|
117
|
+
``query``. This is only useful if the query or
|
|
118
|
+
mutation contains ``$variableName``.
|
|
119
|
+
:type variables: dict
|
|
120
|
+
|
|
121
|
+
:param operation_name: if more than one operation is listed in
|
|
122
|
+
``query``, then it should specify the one to be executed.
|
|
123
|
+
:type operation_name: str
|
|
124
|
+
|
|
125
|
+
:param extra_headers: dict with extra HTTP headers to use.
|
|
126
|
+
:type extra_headers: dict
|
|
127
|
+
|
|
128
|
+
:param timeout: overrides the default timeout.
|
|
129
|
+
:type timeout: float
|
|
130
|
+
|
|
131
|
+
:return: dict with optional fields ``data`` containing the GraphQL
|
|
132
|
+
returned data as nested dict, ``headers`` with a dictionary of
|
|
133
|
+
response headers, and ``errors`` with an array of errors.
|
|
134
|
+
Note that both ``data`` and ``errors`` may be returned!
|
|
135
|
+
:rtype: dict
|
|
136
|
+
|
|
137
|
+
'''
|
|
138
|
+
query, req = self._prepare(
|
|
139
|
+
query=query,
|
|
140
|
+
variables=variables,
|
|
141
|
+
operation_name=operation_name,
|
|
142
|
+
extra_headers=extra_headers,
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
req.extensions['timeout'] = httpx.Timeout(
|
|
146
|
+
timeout or self.timeout
|
|
147
|
+
).as_dict()
|
|
148
|
+
|
|
149
|
+
if isinstance(self.client, httpx.AsyncClient):
|
|
150
|
+
|
|
151
|
+
async def runner():
|
|
152
|
+
try:
|
|
153
|
+
response = await self.client.send(req)
|
|
154
|
+
return self._parse_httpx_response(query, response)
|
|
155
|
+
except httpx.HTTPError as exc:
|
|
156
|
+
return self._log_httpx_error(query, req, exc)
|
|
157
|
+
|
|
158
|
+
return runner()
|
|
159
|
+
elif isinstance(self.client, httpx.Client):
|
|
160
|
+
try:
|
|
161
|
+
response = self.client.send(req)
|
|
162
|
+
return self._parse_httpx_response(query, response)
|
|
163
|
+
except httpx.HTTPError as exc:
|
|
164
|
+
return self._log_httpx_error(query, req, exc)
|
|
165
|
+
|
|
166
|
+
def _parse_httpx_response(self, query, response):
|
|
167
|
+
response.raise_for_status()
|
|
168
|
+
|
|
169
|
+
try:
|
|
170
|
+
data = response.json()
|
|
171
|
+
if data and data.get('errors'):
|
|
172
|
+
return self._log_graphql_error(query, data)
|
|
173
|
+
return data
|
|
174
|
+
except json.JSONDecodeError as exc:
|
|
175
|
+
return self._log_json_error(response.text, exc)
|
|
176
|
+
|
|
177
|
+
def _log_httpx_error(self, query, request, exc):
|
|
178
|
+
self.logger.error('%s: %s', request.url, exc)
|
|
179
|
+
|
|
180
|
+
content_type = exc.response.headers.get('Content-Type', '')
|
|
181
|
+
body = exc.response.text
|
|
182
|
+
if not content_type.startswith('application/json'):
|
|
183
|
+
return {
|
|
184
|
+
'data': None,
|
|
185
|
+
'errors': [
|
|
186
|
+
{
|
|
187
|
+
'message': str(exc),
|
|
188
|
+
'exception': exc,
|
|
189
|
+
'status': exc.response.status_code,
|
|
190
|
+
'headers': dict(exc.response.headers),
|
|
191
|
+
'body': body,
|
|
192
|
+
}
|
|
193
|
+
],
|
|
194
|
+
}
|
|
195
|
+
else:
|
|
196
|
+
# GraphQL servers return 400 and {'errors': [...]}
|
|
197
|
+
# if only errors was returned, no {'data': ...}
|
|
198
|
+
|
|
199
|
+
try:
|
|
200
|
+
data = exc.response.json()
|
|
201
|
+
except json.JSONDecodeError as exc:
|
|
202
|
+
return self._log_json_error(body, exc)
|
|
203
|
+
|
|
204
|
+
if isinstance(data, dict) and data.get('errors'):
|
|
205
|
+
data.update(
|
|
206
|
+
{
|
|
207
|
+
'exception': exc,
|
|
208
|
+
'status': exc.response.status_code,
|
|
209
|
+
'headers': dict(exc.response.headers),
|
|
210
|
+
}
|
|
211
|
+
)
|
|
212
|
+
return self._log_graphql_error(query, data)
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
'data': None,
|
|
216
|
+
'errors': [
|
|
217
|
+
{
|
|
218
|
+
'message': str(exc),
|
|
219
|
+
'exception': exc,
|
|
220
|
+
'status': exc.response.status_code,
|
|
221
|
+
'headers': dict(exc.response.headers),
|
|
222
|
+
'body': body,
|
|
223
|
+
}
|
|
224
|
+
],
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
def get_http_post_request(self, query, variables, operation_name, headers):
|
|
228
|
+
'''Create an HTTP POST request for the query.'''
|
|
229
|
+
return self.client.build_request(
|
|
230
|
+
method='POST',
|
|
231
|
+
url=self.url,
|
|
232
|
+
headers=headers,
|
|
233
|
+
json={
|
|
234
|
+
'query': query,
|
|
235
|
+
'variables': variables,
|
|
236
|
+
'operationName': operation_name,
|
|
237
|
+
},
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
def get_http_get_request(self, query, variables, operation_name, headers):
|
|
241
|
+
'''Create an HTTP GET request for the query.'''
|
|
242
|
+
params = {'query': query}
|
|
243
|
+
if operation_name:
|
|
244
|
+
params['operationName'] = operation_name
|
|
245
|
+
|
|
246
|
+
if variables:
|
|
247
|
+
params['variables'] = json.dumps(variables)
|
|
248
|
+
|
|
249
|
+
url = add_query_to_url(self.url, params)
|
|
250
|
+
|
|
251
|
+
return self.client.build_request(
|
|
252
|
+
method='GET', url=url, headers=headers
|
|
253
|
+
)
|
|
@@ -57,6 +57,8 @@ class RequestsEndpoint(BaseEndpoint):
|
|
|
57
57
|
:data: object matching the GraphQL requests, or ``null`` if only
|
|
58
58
|
errors were returned.
|
|
59
59
|
|
|
60
|
+
:headers: dictionary of HTTP response headers.
|
|
61
|
+
|
|
60
62
|
:errors: list of errors, which are objects with the key "message" and
|
|
61
63
|
optionally others, such as "location" (for errors matching GraphQL
|
|
62
64
|
input). Instead of raising exceptions, such as
|
|
@@ -198,8 +200,10 @@ class RequestsEndpoint(BaseEndpoint):
|
|
|
198
200
|
:type timeout: float
|
|
199
201
|
|
|
200
202
|
:return: dict with optional fields ``data`` containing the GraphQL
|
|
201
|
-
returned data as nested dict
|
|
202
|
-
|
|
203
|
+
returned data as nested dict, ``headers`` with a dictionary of
|
|
204
|
+
response headers with lower-cased keys, and ``errors`` with an
|
|
205
|
+
array of errors. Note that both ``data`` and ``errors`` may be
|
|
206
|
+
returned!
|
|
203
207
|
:rtype: dict
|
|
204
208
|
'''
|
|
205
209
|
query, req = self._prepare(
|
|
@@ -219,6 +223,8 @@ class RequestsEndpoint(BaseEndpoint):
|
|
|
219
223
|
try:
|
|
220
224
|
f.raise_for_status()
|
|
221
225
|
data = f.json()
|
|
226
|
+
if data:
|
|
227
|
+
data['headers'] = dict(f.headers)
|
|
222
228
|
if data and data.get('errors'):
|
|
223
229
|
return self._log_graphql_error(query, data)
|
|
224
230
|
return data
|
|
@@ -2924,26 +2924,31 @@ class Int(Scalar):
|
|
|
2924
2924
|
|
|
2925
2925
|
class Float(Scalar):
|
|
2926
2926
|
'Maps GraphQL ``Float`` to Python ``float``.'
|
|
2927
|
+
|
|
2927
2928
|
converter = float
|
|
2928
2929
|
|
|
2929
2930
|
|
|
2930
2931
|
class String(Scalar):
|
|
2931
2932
|
'Maps GraphQL ``String`` to Python ``str``.'
|
|
2933
|
+
|
|
2932
2934
|
converter = str
|
|
2933
2935
|
|
|
2934
2936
|
|
|
2935
2937
|
class Boolean(Scalar):
|
|
2936
2938
|
'Maps GraphQL ``Boolean`` to Python ``bool``.'
|
|
2939
|
+
|
|
2937
2940
|
converter = bool
|
|
2938
2941
|
|
|
2939
2942
|
|
|
2940
2943
|
class ID(Scalar):
|
|
2941
2944
|
'Maps GraphQL ``ID`` to Python ``str``.'
|
|
2945
|
+
|
|
2942
2946
|
converter = str
|
|
2943
2947
|
|
|
2944
2948
|
|
|
2945
2949
|
class UnknownType(Type):
|
|
2946
2950
|
'Type found in the response that was not present in schema'
|
|
2951
|
+
|
|
2947
2952
|
__auto_register = False # do not expose this in Schema, just subclasses
|
|
2948
2953
|
|
|
2949
2954
|
|
|
@@ -112,6 +112,12 @@ class Time(Scalar):
|
|
|
112
112
|
datetime.time(12, 34, 56, tzinfo=...(...-1, ...70200)))
|
|
113
113
|
>>> Time('123456+0530') # doctest: +ELLIPSIS
|
|
114
114
|
datetime.time(12, 34, 56, tzinfo=...(...19800)))
|
|
115
|
+
>>> Time('12:34') # HH:MM, missing seconds, naive = no timezone
|
|
116
|
+
datetime.time(12, 34)
|
|
117
|
+
>>> Time('12:34Z')
|
|
118
|
+
datetime.time(12, 34, tzinfo=datetime.timezone.utc)
|
|
119
|
+
>>> Time('12:34-05:30') # doctest: +ELLIPSIS
|
|
120
|
+
datetime.time(12, 34, tzinfo=...(...-1, ...70200)))
|
|
115
121
|
|
|
116
122
|
Pre-converted values are allowed:
|
|
117
123
|
|
|
@@ -132,7 +138,7 @@ class Time(Scalar):
|
|
|
132
138
|
'''
|
|
133
139
|
|
|
134
140
|
_re_parse = re.compile(
|
|
135
|
-
r'^(?P<H>\d{2}):?(?P<M>\d{2}):?(?P<S>\d{2})(?P<MS>|[.]\d+)'
|
|
141
|
+
r'^(?P<H>\d{2}):?(?P<M>\d{2})(:?(?P<S>\d{2}))?(?P<MS>|[.]\d+)'
|
|
136
142
|
r'(?P<TZ>|Z|(?P<TZH>[+-]\d{2}):?(?P<TZM>\d{2}))$'
|
|
137
143
|
)
|
|
138
144
|
|
|
@@ -146,7 +152,7 @@ class Time(Scalar):
|
|
|
146
152
|
|
|
147
153
|
hour = int(m.group('H'))
|
|
148
154
|
minute = int(m.group('M'))
|
|
149
|
-
second = int(m.group('S'))
|
|
155
|
+
second = int(m.group('S') or 0)
|
|
150
156
|
microsecond = int(m.group('MS')[1:] or 0)
|
|
151
157
|
tzinfo = None
|
|
152
158
|
if m.group('TZ') == 'Z':
|
|
@@ -242,6 +248,12 @@ class DateTime(Scalar):
|
|
|
242
248
|
datetime.datetime(2018, 1, 2, 12, 34, 56, tzinfo=..., ...70200)))
|
|
243
249
|
>>> DateTime('20180102T123456+0530') # doctest: +ELLIPSIS
|
|
244
250
|
datetime.datetime(2018, 1, 2, 12, 34, 56, tzinfo=...(...19800)))
|
|
251
|
+
>>> DateTime('2018-01-02T12:34') # HH:MM, missing seconds, no timezone
|
|
252
|
+
datetime.datetime(2018, 1, 2, 12, 34)
|
|
253
|
+
>>> DateTime('2018-01-02T12:34Z')
|
|
254
|
+
datetime.datetime(2018, 1, 2, 12, 34, tzinfo=datetime.timezone.utc)
|
|
255
|
+
>>> DateTime('2018-01-02T12:34-05:30') # doctest: +ELLIPSIS
|
|
256
|
+
datetime.datetime(2018, 1, 2, 12, 34, tzinfo=..., ...70200)))
|
|
245
257
|
|
|
246
258
|
Pre-converted values are allowed:
|
|
247
259
|
|
|
@@ -264,7 +276,7 @@ class DateTime(Scalar):
|
|
|
264
276
|
|
|
265
277
|
_re_parse = re.compile(
|
|
266
278
|
r'^(?P<Y>\d{4})-?(?P<m>\d{2})-?(?P<d>\d{2})T'
|
|
267
|
-
r'(?P<H>\d{2}):?(?P<M>\d{2}):?(?P<S>\d{2})(?P<MS>|[.]\d+)'
|
|
279
|
+
r'(?P<H>\d{2}):?(?P<M>\d{2})(:?(?P<S>\d{2}))?(?P<MS>|[.]\d+)'
|
|
268
280
|
r'(?P<TZ>|Z|(?P<TZH>[+-]\d{2}):?(?P<TZM>\d{2}))$'
|
|
269
281
|
)
|
|
270
282
|
|
|
@@ -283,7 +295,7 @@ class DateTime(Scalar):
|
|
|
283
295
|
day = int(m.group('d'))
|
|
284
296
|
hour = int(m.group('H'))
|
|
285
297
|
minute = int(m.group('M'))
|
|
286
|
-
second = int(m.group('S'))
|
|
298
|
+
second = int(m.group('S') or 0)
|
|
287
299
|
microsecond = int(m.group('MS')[1:] or 0)
|
|
288
300
|
tzinfo = None
|
|
289
301
|
if m.group('TZ') == 'Z':
|