Skip to content

Commit

Permalink
use existing Tornado library
Browse files Browse the repository at this point in the history
  • Loading branch information
dwsutherland committed Dec 16, 2022
1 parent 2f19a0d commit fb2eeb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
46 changes: 24 additions & 22 deletions cylc/uiserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

import json
import os
import requests
from shutil import which
import socket
import sys
from typing import Any, Optional, Union, Dict
from tornado.httpclient import (
AsyncHTTPClient,
HTTPRequest,
HTTPClientError
)

from cylc.flow import LOG
from cylc.flow.exceptions import ClientError, ClientTimeout
from cylc.flow.exceptions import ClientError
from cylc.flow.network import encode_
from cylc.flow.network.client import WorkflowRuntimeClientBase
from cylc.flow.network.client_factory import CommsMeth
Expand All @@ -43,7 +47,7 @@ def __init__(
port: Union[int, str, None] = None,
timeout: Union[float, str, None] = None,
):
self.timeout = timeout
self.timeout = timeout or self.DEFAULT_TIMEOUT
# gather header info post start
self.header = self.get_header()

Expand Down Expand Up @@ -72,36 +76,34 @@ async def async_request(
if req_meta:
msg['meta'].update(req_meta)

LOG.debug('http:send %s', msg)
LOG.debug('https:send %s', msg)

try:
res = requests.post(
api_info["url"] + 'cylc/graphql',
request = HTTPRequest(
url=api_info["url"] + 'cylc/graphql',
method='POST',
headers={
'Authorization': f'token {api_info["token"]}',
'Content-Type': 'application/json',
'meta': encode_(msg.get('meta', {})),
},
json={
'query': args['request_string'],
'variables': args.get('variables', {}),
},
timeout=self.timeout
)
res.raise_for_status()
except requests.ConnectTimeout:
raise ClientTimeout(
'Timeout waiting for server response.'
' This could be due to network or server issues.'
' Check the UI Server log.'
body=json.dumps(
{
'query': args['request_string'],
'variables': args.get('variables', {}),
}
),
request_timeout=float(self.timeout)
)
except requests.ConnectionError as exc:
res = await AsyncHTTPClient().fetch(request)
except HTTPClientError as exc:
raise ClientError(
'Unable to connect to UI Server or Hub.',
'Client error with Hub/UI-Server request.',
f'{exc}'
)

response = res.json()
LOG.debug('http:recv %s', response)
response = json.loads(res.body)
LOG.debug('https:recv %s', response)

try:
return response['data']
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ install_requires =
jupyter_server>=1.10.2
tornado>=6.1.0 # matches jupyter_server value
traitlets>=5.2.1 # required for logging_config (5.2.0 had bugs)
requests==2.28.*

# Transitive dependencies that we directly (lightly) use:
pyzmq
Expand Down

0 comments on commit fb2eeb9

Please sign in to comment.