1616
1717import json
1818import os
19- import requests
2019from shutil import which
2120import socket
2221import sys
2322from typing import Any , Optional , Union , Dict
23+ from tornado .httpclient import (
24+ AsyncHTTPClient ,
25+ HTTPRequest ,
26+ HTTPClientError
27+ )
2428
2529from cylc .flow import LOG
26- from cylc .flow .exceptions import ClientError , ClientTimeout
30+ from cylc .flow .exceptions import ClientError
2731from cylc .flow .network import encode_
2832from cylc .flow .network .client import WorkflowRuntimeClientBase
2933from cylc .flow .network .client_factory import CommsMeth
@@ -43,7 +47,7 @@ def __init__(
4347 port : Union [int , str , None ] = None ,
4448 timeout : Union [float , str , None ] = None ,
4549 ):
46- self .timeout = timeout
50+ self .timeout = timeout or self . DEFAULT_TIMEOUT
4751 # gather header info post start
4852 self .header = self .get_header ()
4953
@@ -72,36 +76,34 @@ async def async_request(
7276 if req_meta :
7377 msg ['meta' ].update (req_meta )
7478
75- LOG .debug ('http :send %s' , msg )
79+ LOG .debug ('https :send %s' , msg )
7680
7781 try :
78- res = requests .post (
79- api_info ["url" ] + 'cylc/graphql' ,
82+ request = HTTPRequest (
83+ url = api_info ["url" ] + 'cylc/graphql' ,
84+ method = 'POST' ,
8085 headers = {
8186 'Authorization' : f'token { api_info ["token" ]} ' ,
87+ 'Content-Type' : 'application/json' ,
8288 'meta' : encode_ (msg .get ('meta' , {})),
8389 },
84- json = {
85- 'query' : args ['request_string' ],
86- 'variables' : args .get ('variables' , {}),
87- },
88- timeout = self .timeout
89- )
90- res .raise_for_status ()
91- except requests .ConnectTimeout :
92- raise ClientTimeout (
93- 'Timeout waiting for server response.'
94- ' This could be due to network or server issues.'
95- ' Check the UI Server log.'
90+ body = json .dumps (
91+ {
92+ 'query' : args ['request_string' ],
93+ 'variables' : args .get ('variables' , {}),
94+ }
95+ ),
96+ request_timeout = float (self .timeout )
9697 )
97- except requests .ConnectionError as exc :
98+ res = await AsyncHTTPClient ().fetch (request )
99+ except HTTPClientError as exc :
98100 raise ClientError (
99- 'Unable to connect to UI Server or Hub .' ,
101+ 'Client error with Hub/UI- Server request .' ,
100102 f'{ exc } '
101103 )
102104
103- response = res . json ( )
104- LOG .debug ('http :recv %s' , response )
105+ response = json . loads ( res . body )
106+ LOG .debug ('https :recv %s' , response )
105107
106108 try :
107109 return response ['data' ]
0 commit comments