Module dcso.portal.util.graphql
Expand source code
# Copyright (c) 2020, DCSO GmbH
import json
import ssl
from collections import namedtuple
from datetime import datetime, timezone
from os import environ
from typing import AnyStr, List, Optional, Union
from urllib.error import URLError
from urllib.parse import ParseResult, urlparse
from urllib.request import Request, urlopen
from dcso.glosom import Glosom
from ..exceptions import PortalAPIError, PortalAPIRequest
from ..util.temporal import decode_utc_iso8601
_ENV_SKIP_TLS_VERIFY = "DCSO_PORTAL_SKIP_TLS_VERIFY"
class GraphQLJSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, datetime):
return o.replace(tzinfo=timezone.utc).isoformat()
return super().default(o)
class GraphQLJSONDecoder(json.JSONDecoder):
def __init__(self, *args, **kwargs):
try:
del kwargs['object_hook']
except KeyError:
# ok when not in kwargs
pass
super().__init__(*args, **kwargs, object_hook=self.object_hook)
@staticmethod
def object_hook(o: dict) -> dict:
for k, v in o.items():
try:
o[k] = decode_utc_iso8601(v)
except ValueError as exc:
# not an ISO date formatted string; let others figure it out
pass
return o
class GraphQLRequest:
def __init__(self,
query: str,
api_url: Union[ParseResult, str],
variables: Optional[dict] = None,
fragments: Optional[List[str]] = None,
token: Optional[str] = None):
self.query: str = query
self.api_url: Union[ParseResult, str] = api_url
self.variables: dict = variables
self.fragments: List[str] = fragments
self.token: Optional[str] = token
def json(self) -> bytes:
q = self.query
if self.fragments:
q += '\n'.join(self.fragments)
r = {
'query': q
}
if self.variables:
r['variables'] = self.variables
return json.dumps(r, cls=GraphQLJSONEncoder).encode('utf-8')
def execute_raw(self) -> AnyStr:
"""Executes the GraphQL query and return the response from the wire as JSON.
This method is not to be used directly unless JSON must be process different.
Methods `execute_dict` and `execute` have a more Pythonic result, and easier
to use.
Raises PortalAPIRequest when request with API or decoding result fails.
"""
headers = {
'Content-Type': 'application/json'
}
if self.token:
headers['Authorization'] = 'Bearer ' + self.token
url = self.api_url
if isinstance(url, str):
url = urlparse(self.api_url)
req = Request(url.geturl(), headers=headers, method='POST', data=self.json())
ssl_ctx = None
if url.scheme == 'https':
ssl_ctx = ssl.SSLContext()
if environ.get(_ENV_SKIP_TLS_VERIFY):
ssl_ctx.verify_mode = ssl.CERT_NONE
try:
return urlopen(req, context=ssl_ctx).read()
except URLError as exc:
raise PortalAPIRequest(str(exc.reason))
def execute_dict(self) -> dict:
"""Executes the GraphQL request returning response as a dictionary.
Raises `PortalAPIError` When the GraphQL API endpoint returned an error.
When there was an issue with the request itself, or decoding JSON failed,
the `PortalAPIRequest` exception is raised.
"""
res = self.execute_raw()
if isinstance(res, bytes):
res = res.decode('utf-8')
try:
response = json.loads(res, cls=GraphQLJSONDecoder)
except json.JSONDecodeError as exc:
raise PortalAPIRequest("failed decoding API response: " + str(exc))
try:
first_error = response['errors'][0]
except (TypeError, KeyError, IndexError):
# all is good; return response
return response
try:
err = first_error['message']
code = ""
if 'extensions' in first_error:
if 'detail' in first_error['extensions']:
err += ' (' + first_error['extensions']['detail'] + ')'
code = first_error['extensions'].get('code', "")
g = Glosom(message=first_error['message'], code=code)
except KeyError as exc:
raise PortalAPIRequest(f"API request contained unusable error definition {exc}")
except AttributeError:
raise PortalAPIRequest(f"API request contained unusable error extensions")
else:
raise PortalAPIError(glosom=g)
def execute(self) -> namedtuple:
"""Executes the GraphQL request returning response as a namedtuple.
This method wraps around the `execute` method, but returns instead of
dict, a namedtuple which itself might contain namedtuples.
Raises `PortalAPIError` When the GraphQL API endpoint returned an error.
When there was an issue with the request itself, or decoding JSON failed,
the `PortalAPIRequest` exception is raised.
"""
return graphql_data_to_namedtuple(self.execute_dict()['data'])
def graphql_data_to_namedtuple(mapping: dict, name: str = 'data') -> namedtuple:
"""Transforms GraphQL response data and returns it as a namedtuple.
This method takes the mapping as GraphQL Response data and recursively goes
through it converting dict object to namedtuple, and array of objects as list.
The name of the namedtuple is the key of value it is created from. The
starting named tuple is by default called 'data'.
"""
if isinstance(mapping, dict):
for key, value in mapping.items():
if isinstance(value, (list, tuple)):
for idx, p in enumerate(value):
value[idx] = graphql_data_to_namedtuple(p, key)
else:
mapping[key] = graphql_data_to_namedtuple(value, key)
return namedtuple(name, field_names=mapping.keys())(*mapping.values())
return mapping
Functions
def graphql_data_to_namedtuple(mapping: dict, name: str = 'data') ‑>
-
Transforms GraphQL response data and returns it as a namedtuple.
This method takes the mapping as GraphQL Response data and recursively goes through it converting dict object to namedtuple, and array of objects as list.
The name of the namedtuple is the key of value it is created from. The starting named tuple is by default called 'data'.
Expand source code
def graphql_data_to_namedtuple(mapping: dict, name: str = 'data') -> namedtuple: """Transforms GraphQL response data and returns it as a namedtuple. This method takes the mapping as GraphQL Response data and recursively goes through it converting dict object to namedtuple, and array of objects as list. The name of the namedtuple is the key of value it is created from. The starting named tuple is by default called 'data'. """ if isinstance(mapping, dict): for key, value in mapping.items(): if isinstance(value, (list, tuple)): for idx, p in enumerate(value): value[idx] = graphql_data_to_namedtuple(p, key) else: mapping[key] = graphql_data_to_namedtuple(value, key) return namedtuple(name, field_names=mapping.keys())(*mapping.values()) return mapping
Classes
class GraphQLJSONDecoder (*args, **kwargs)
-
Simple JSON http://json.org decoder
Performs the following translations in decoding by default:
+---------------+-------------------+ | JSON | Python | +===============+===================+ | object | dict | +---------------+-------------------+ | array | list | +---------------+-------------------+ | string | str | +---------------+-------------------+ | number (int) | int | +---------------+-------------------+ | number (real) | float | +---------------+-------------------+ | true | True | +---------------+-------------------+ | false | False | +---------------+-------------------+ | null | None | +---------------+-------------------+
It also understands
NaN
,Infinity
, and-Infinity
as their correspondingfloat
values, which is outside the JSON spec.object_hook
, if specified, will be called with the result of every JSON object decoded and its return value will be used in place of the givendict
. This can be used to provide custom deserializations (e.g. to support JSON-RPC class hinting).object_pairs_hook
, if specified will be called with the result of every JSON object decoded with an ordered list of pairs. The return value ofobject_pairs_hook
will be used instead of thedict
. This feature can be used to implement custom decoders. Ifobject_hook
is also defined, theobject_pairs_hook
takes priority.parse_float
, if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal).parse_int
, if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float).parse_constant
, if specified, will be called with one of the following strings: -Infinity, Infinity, NaN. This can be used to raise an exception if invalid JSON numbers are encountered.If
strict
is false (true is the default), then control characters will be allowed inside strings. Control characters in this context are those with character codes in the 0-31 range, including'\t'
(tab),'\n'
,'\r'
and'\0'
.Expand source code
class GraphQLJSONDecoder(json.JSONDecoder): def __init__(self, *args, **kwargs): try: del kwargs['object_hook'] except KeyError: # ok when not in kwargs pass super().__init__(*args, **kwargs, object_hook=self.object_hook) @staticmethod def object_hook(o: dict) -> dict: for k, v in o.items(): try: o[k] = decode_utc_iso8601(v) except ValueError as exc: # not an ISO date formatted string; let others figure it out pass return o
Ancestors
- json.decoder.JSONDecoder
Static methods
def object_hook(o: dict) ‑> dict
-
Expand source code
@staticmethod def object_hook(o: dict) -> dict: for k, v in o.items(): try: o[k] = decode_utc_iso8601(v) except ValueError as exc: # not an ISO date formatted string; let others figure it out pass return o
class GraphQLJSONEncoder (*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
-
Extensible JSON http://json.org encoder for Python data structures.
Supports the following objects and types by default:
+-------------------+---------------+ | Python | JSON | +===================+===============+ | dict | object | +-------------------+---------------+ | list, tuple | array | +-------------------+---------------+ | str | string | +-------------------+---------------+ | int, float | number | +-------------------+---------------+ | True | true | +-------------------+---------------+ | False | false | +-------------------+---------------+ | None | null | +-------------------+---------------+
To extend this to recognize other objects, subclass and implement a
.default()
method with another method that returns a serializable object foro
if possible, otherwise it should call the superclass implementation (to raiseTypeError
).Constructor for JSONEncoder, with sensible defaults.
If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.
If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.
If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place.
If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.
If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.
If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.
If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if indent is
None
and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.If specified, default is a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the object or raise a
TypeError
.Expand source code
class GraphQLJSONEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, datetime): return o.replace(tzinfo=timezone.utc).isoformat() return super().default(o)
Ancestors
- json.encoder.JSONEncoder
Methods
def default(self, o)
-
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this::
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
Expand source code
def default(self, o): if isinstance(o, datetime): return o.replace(tzinfo=timezone.utc).isoformat() return super().default(o)
class GraphQLRequest (query: str, api_url: Union[urllib.parse.ParseResult, str], variables: Union[dict, NoneType] = None, fragments: Union[List[str], NoneType] = None, token: Union[str, NoneType] = None)
-
Expand source code
class GraphQLRequest: def __init__(self, query: str, api_url: Union[ParseResult, str], variables: Optional[dict] = None, fragments: Optional[List[str]] = None, token: Optional[str] = None): self.query: str = query self.api_url: Union[ParseResult, str] = api_url self.variables: dict = variables self.fragments: List[str] = fragments self.token: Optional[str] = token def json(self) -> bytes: q = self.query if self.fragments: q += '\n'.join(self.fragments) r = { 'query': q } if self.variables: r['variables'] = self.variables return json.dumps(r, cls=GraphQLJSONEncoder).encode('utf-8') def execute_raw(self) -> AnyStr: """Executes the GraphQL query and return the response from the wire as JSON. This method is not to be used directly unless JSON must be process different. Methods `execute_dict` and `execute` have a more Pythonic result, and easier to use. Raises PortalAPIRequest when request with API or decoding result fails. """ headers = { 'Content-Type': 'application/json' } if self.token: headers['Authorization'] = 'Bearer ' + self.token url = self.api_url if isinstance(url, str): url = urlparse(self.api_url) req = Request(url.geturl(), headers=headers, method='POST', data=self.json()) ssl_ctx = None if url.scheme == 'https': ssl_ctx = ssl.SSLContext() if environ.get(_ENV_SKIP_TLS_VERIFY): ssl_ctx.verify_mode = ssl.CERT_NONE try: return urlopen(req, context=ssl_ctx).read() except URLError as exc: raise PortalAPIRequest(str(exc.reason)) def execute_dict(self) -> dict: """Executes the GraphQL request returning response as a dictionary. Raises `PortalAPIError` When the GraphQL API endpoint returned an error. When there was an issue with the request itself, or decoding JSON failed, the `PortalAPIRequest` exception is raised. """ res = self.execute_raw() if isinstance(res, bytes): res = res.decode('utf-8') try: response = json.loads(res, cls=GraphQLJSONDecoder) except json.JSONDecodeError as exc: raise PortalAPIRequest("failed decoding API response: " + str(exc)) try: first_error = response['errors'][0] except (TypeError, KeyError, IndexError): # all is good; return response return response try: err = first_error['message'] code = "" if 'extensions' in first_error: if 'detail' in first_error['extensions']: err += ' (' + first_error['extensions']['detail'] + ')' code = first_error['extensions'].get('code', "") g = Glosom(message=first_error['message'], code=code) except KeyError as exc: raise PortalAPIRequest(f"API request contained unusable error definition {exc}") except AttributeError: raise PortalAPIRequest(f"API request contained unusable error extensions") else: raise PortalAPIError(glosom=g) def execute(self) -> namedtuple: """Executes the GraphQL request returning response as a namedtuple. This method wraps around the `execute` method, but returns instead of dict, a namedtuple which itself might contain namedtuples. Raises `PortalAPIError` When the GraphQL API endpoint returned an error. When there was an issue with the request itself, or decoding JSON failed, the `PortalAPIRequest` exception is raised. """ return graphql_data_to_namedtuple(self.execute_dict()['data'])
Methods
def execute(self) ‑>
-
Executes the GraphQL request returning response as a namedtuple.
This method wraps around the
execute
method, but returns instead of dict, a namedtuple which itself might contain namedtuples.Raises
PortalAPIError
When the GraphQL API endpoint returned an error. When there was an issue with the request itself, or decoding JSON failed, thePortalAPIRequest
exception is raised.Expand source code
def execute(self) -> namedtuple: """Executes the GraphQL request returning response as a namedtuple. This method wraps around the `execute` method, but returns instead of dict, a namedtuple which itself might contain namedtuples. Raises `PortalAPIError` When the GraphQL API endpoint returned an error. When there was an issue with the request itself, or decoding JSON failed, the `PortalAPIRequest` exception is raised. """ return graphql_data_to_namedtuple(self.execute_dict()['data'])
def execute_dict(self) ‑> dict
-
Executes the GraphQL request returning response as a dictionary.
Raises
PortalAPIError
When the GraphQL API endpoint returned an error. When there was an issue with the request itself, or decoding JSON failed, thePortalAPIRequest
exception is raised.Expand source code
def execute_dict(self) -> dict: """Executes the GraphQL request returning response as a dictionary. Raises `PortalAPIError` When the GraphQL API endpoint returned an error. When there was an issue with the request itself, or decoding JSON failed, the `PortalAPIRequest` exception is raised. """ res = self.execute_raw() if isinstance(res, bytes): res = res.decode('utf-8') try: response = json.loads(res, cls=GraphQLJSONDecoder) except json.JSONDecodeError as exc: raise PortalAPIRequest("failed decoding API response: " + str(exc)) try: first_error = response['errors'][0] except (TypeError, KeyError, IndexError): # all is good; return response return response try: err = first_error['message'] code = "" if 'extensions' in first_error: if 'detail' in first_error['extensions']: err += ' (' + first_error['extensions']['detail'] + ')' code = first_error['extensions'].get('code', "") g = Glosom(message=first_error['message'], code=code) except KeyError as exc: raise PortalAPIRequest(f"API request contained unusable error definition {exc}") except AttributeError: raise PortalAPIRequest(f"API request contained unusable error extensions") else: raise PortalAPIError(glosom=g)
def execute_raw(self) ‑> ~AnyStr
-
Executes the GraphQL query and return the response from the wire as JSON.
This method is not to be used directly unless JSON must be process different. Methods
execute_dict
andexecute
have a more Pythonic result, and easier to use.Raises PortalAPIRequest when request with API or decoding result fails.
Expand source code
def execute_raw(self) -> AnyStr: """Executes the GraphQL query and return the response from the wire as JSON. This method is not to be used directly unless JSON must be process different. Methods `execute_dict` and `execute` have a more Pythonic result, and easier to use. Raises PortalAPIRequest when request with API or decoding result fails. """ headers = { 'Content-Type': 'application/json' } if self.token: headers['Authorization'] = 'Bearer ' + self.token url = self.api_url if isinstance(url, str): url = urlparse(self.api_url) req = Request(url.geturl(), headers=headers, method='POST', data=self.json()) ssl_ctx = None if url.scheme == 'https': ssl_ctx = ssl.SSLContext() if environ.get(_ENV_SKIP_TLS_VERIFY): ssl_ctx.verify_mode = ssl.CERT_NONE try: return urlopen(req, context=ssl_ctx).read() except URLError as exc: raise PortalAPIRequest(str(exc.reason))
def json(self) ‑> bytes
-
Expand source code
def json(self) -> bytes: q = self.query if self.fragments: q += '\n'.join(self.fragments) r = { 'query': q } if self.variables: r['variables'] = self.variables return json.dumps(r, cls=GraphQLJSONEncoder).encode('utf-8')