forked from wandb/wandb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): use a persistent session object for GraphQL requests (wand…
…b#5075) * Make local (altered) copy of RequestHTTPTransport as GraphQLSession * Use GraphQLSession instead of RequestHTTPTransport * Add mypy ignores for vendored gql, graphql-core * Remove unused '# type: ignore' declarations for wandb_{gql,graphql} * Use new yea-wandb branch in tox.ini
- Loading branch information
1 parent
a4aa143
commit 6d3afec
Showing
7 changed files
with
79 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""A simple GraphQL client for sending queries and mutations. | ||
Note: This was originally wandb/vendor/gql-0.2.0/wandb_gql/transport/requests.py | ||
The only substantial change is to re-use a requests.Session object. | ||
""" | ||
|
||
from typing import Any, Callable, Dict, Optional, Tuple, Union | ||
|
||
import requests | ||
from wandb_gql.transport.http import HTTPTransport | ||
from wandb_graphql.execution import ExecutionResult | ||
from wandb_graphql.language import ast | ||
from wandb_graphql.language.printer import print_ast | ||
|
||
|
||
class GraphQLSession(HTTPTransport): | ||
def __init__( | ||
self, | ||
url: str, | ||
auth: Optional[Union[Tuple[str, str], Callable]] = None, | ||
use_json: bool = False, | ||
timeout: Optional[Union[int, float]] = None, | ||
**kwargs: Any, | ||
) -> None: | ||
"""Setup a session for sending GraphQL queries and mutations. | ||
Args: | ||
url (str): The GraphQL URL | ||
auth (tuple or callable): Auth tuple or callable for Basic/Digest/Custom HTTP Auth | ||
use_json (bool): Send request body as JSON instead of form-urlencoded | ||
timeout (int, float): Specifies a default timeout for requests (Default: None) | ||
""" | ||
super().__init__(url, **kwargs) | ||
self.session = requests.Session() | ||
self.session.auth = auth | ||
self.default_timeout = timeout | ||
self.use_json = use_json | ||
|
||
def execute( | ||
self, | ||
document: ast.Node, | ||
variable_values: Optional[Dict] = None, | ||
timeout: Optional[Union[int, float]] = None, | ||
) -> ExecutionResult: | ||
query_str = print_ast(document) | ||
payload = {"query": query_str, "variables": variable_values or {}} | ||
|
||
data_key = "json" if self.use_json else "data" | ||
post_args = { | ||
"headers": self.headers, | ||
"cookies": self.cookies, | ||
"timeout": timeout or self.default_timeout, | ||
data_key: payload, | ||
} | ||
request = self.session.post(self.url, **post_args) | ||
request.raise_for_status() | ||
|
||
result = request.json() | ||
data, errors = result.get("data"), result.get("errors") | ||
if data is None and errors is None: | ||
raise RuntimeError(f"Received non-compatible response: {result}") | ||
return ExecutionResult(data=data, errors=errors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters