From 24011a50613000a9b6e472c76567cfc61142d9e0 Mon Sep 17 00:00:00 2001 From: Jeff Potter Date: Tue, 2 Mar 2021 11:50:22 -0500 Subject: [PATCH 1/3] Add support for operation_name optional arg --- jebenaclient/jebenaclient.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/jebenaclient/jebenaclient.py b/jebenaclient/jebenaclient.py index 15e0c8b..be93747 100755 --- a/jebenaclient/jebenaclient.py +++ b/jebenaclient/jebenaclient.py @@ -25,6 +25,9 @@ from jebenaclient import run_query run_query(gql_query_string, variables=...) +When running in Python, we recommend setting the operation_name for logging: + run_query(gql_query_string, operation_name="fetch_display_name") + For GQL Schema help, see documentation on the Jebena API Server by visiting (using a web browser) the API endpoint you are using. @@ -100,6 +103,7 @@ class JebenaCliGQLPermissionDenied(JebenaCliException): def run_query( query, + operation_name=None, variables=None, api_endpoint=None, api_key_name=None, @@ -108,7 +112,7 @@ def run_query( return_instead_of_raise_on_errors=False, skip_logging_transient_errors=False ): - # type: (str, dict, str, str, str, bool, bool, bool) -> dict + # type: (str, str, dict, str, str, str, bool, bool, bool) -> dict """Send a GQL query to the Jebena API Server and return the server reply. OS Environ variables should be set for JEBENA_API_KEY_NAME, JEBENA_API_SECRET_KEY, @@ -117,6 +121,8 @@ def run_query( :param query: GQL query string to run. + :param operation_name: (Optional) A name for the GQL operation; useful for logging / debugging. + :param variables: Key-value dictionary of variables for query. :param api_endpoint: The URL of your Jebena API server. When not passed, the @@ -199,6 +205,8 @@ def run_query( query = wrapped_query["query"] if "variables" in wrapped_query: variables = wrapped_query["variables"] + if "operationName" in wrapped_query: + operation_name = wrapped_query["operationName"] except Exception: # noqa # For Python 2 compatibility: don't try to catch 'json.decoder.JSONDecodeError' pass @@ -206,6 +214,7 @@ def run_query( parsed_response = _execute_gql_query( api_endpoint, query, + operation_name=operation_name, variables=variables, allow_insecure_https=allow_insecure_https, api_key_name=api_key_name, @@ -249,6 +258,7 @@ def run_query( def _execute_gql_query( api_endpoint, query, + operation_name=None, variables=None, allow_insecure_https=False, api_key_name=None, @@ -256,7 +266,7 @@ def _execute_gql_query( retries_allowed=2, skip_logging_transient_errors=False ): - # type: (str, str, dict, bool, str, str, int, bool) -> dict + # type: (str, str, str, dict, bool, str, str, int, bool) -> dict """Send a GQL query to the server and return the GQL response.""" if not api_key_name: raise JebenaCliMissingKeyException( @@ -272,6 +282,8 @@ def _execute_gql_query( "query": query, "variables": variables, } + if operation_name: + data["operationName"] = operation_name headers = { "Accept": "application/json", "Authorization": "ApiKey %s/%s" % (api_key_name, api_secret_key), From b3f33bab085b26eb0e1183c4b96f7a317c0a79ee Mon Sep 17 00:00:00 2001 From: Jeff Potter Date: Tue, 2 Mar 2021 14:42:39 -0500 Subject: [PATCH 2/3] Bump version number --- jebenaclient/jebenaclient.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jebenaclient/jebenaclient.py b/jebenaclient/jebenaclient.py index be93747..5123045 100755 --- a/jebenaclient/jebenaclient.py +++ b/jebenaclient/jebenaclient.py @@ -57,7 +57,8 @@ # 0.7.1 20210128: Address socket timeout issue. # 0.8.0 20210204: Make script Python 2.7 compatible. # 0.8.1 20210217: Handle some flake8 / mypy issues in a Py 2.7 compatible way. -__version__ = "0.8.1" +# 0.8.2 20210302: Add support for GQL "operationName" parameter +__version__ = "0.8.2" import http.client import json From 645e772d1ba085c497f2e55e8dff177b742f2148 Mon Sep 17 00:00:00 2001 From: Jeff Potter Date: Tue, 2 Mar 2021 14:47:52 -0500 Subject: [PATCH 3/3] Add test for wrapped-query with operationName parameter --- Makefile | 1 + jebenaclient/jebenaclient.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/Makefile b/Makefile index 649b774..2d30540 100644 --- a/Makefile +++ b/Makefile @@ -4,3 +4,4 @@ test: @# in both Python 2.7 and 3.x. echo 'query { me { person { displayName } } }' | python2.7 -m jebenaclient echo 'query { me { person { displayName } } }' | python3 -m jebenaclient + echo '{ "query": "query getDisplayName { me { person { displayName } } }", "variables": {"foo": "bar"}, "operationName": "getDisplayName" }' | python3 -m jebenaclient diff --git a/jebenaclient/jebenaclient.py b/jebenaclient/jebenaclient.py index 5123045..b22cd52 100755 --- a/jebenaclient/jebenaclient.py +++ b/jebenaclient/jebenaclient.py @@ -40,6 +40,13 @@ "variables": {"foo": "bar"} } +Or, with an operation name: +{ + "query": "query operationName { me { person { displayName } } }", + "variables": {"foo": "bar"}, + "operationName": "getDisplayName" +} + """ # Version history: