Skip to content

Commit

Permalink
update to use httpx
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <[email protected]>
  • Loading branch information
vsoch committed Oct 23, 2023
1 parent 3a0814c commit e8f9f5e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
25 changes: 15 additions & 10 deletions clients/python/flux_restful_client/main/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import flux_restful_client.main.schemas as schemas
import flux_restful_client.utils as utils
import httpx
import jsonschema
import requests
from flux_restful_client.logger import logger
from jose import jwt

Expand Down Expand Up @@ -53,7 +53,7 @@ def __init__(
self.prefix = prefix
self.attempts = attempts
self.timeout = timeout
self.session = requests.Session()
self.session = httpx.Client()

def set_header(self, name, value):
self.headers.update({name: value})
Expand Down Expand Up @@ -107,14 +107,19 @@ def do_request(

# Make the request and return to calling function, unless requires auth
try:
response = self.session.request(
method,
url,
params=params,
json=data,
headers=headers,
stream=stream,
)
if method == "POST" and stream:
response = self.session.stream(
method, url, json=data, params=params, headers=headers
)
if method == "POST":
response = self.session.post(url, params=data, headers=headers)
elif method == "GET" and stream:
response = self.session.stream(
method, url, params=params, headers=headers
)
elif method == "GET":
response = self.session.get(url, params=params, headers=headers)

except Exception as e:
if attempts > 0:
time.sleep(timeout)
Expand Down
2 changes: 1 addition & 1 deletion clients/python/flux_restful_client/main/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"description": "number of nodes for the job.",
},
"option_flags": {
"type": "object",
"type": ["object", "string"],
"description": "option flags (dict) or key value pairs for flux.",
},
"exclusive": {
Expand Down
6 changes: 1 addition & 5 deletions clients/python/flux_restful_client/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import time

import jsonschema
import pytest
from flux_restful_client.main import get_client


Expand Down Expand Up @@ -30,6 +28,7 @@ def test_submit_list_job():

# Now submit a job, ensure we get one job back
response = client.submit("sleep 5")

assert isinstance(response, dict)
for key in ["Message", "id"]:
assert key in response
Expand Down Expand Up @@ -86,9 +85,6 @@ def test_option_flags():
"""
client = get_client()

with pytest.raises(jsonschema.ValidationError):
client.submit("sleep 1", option_flags="invalid format")

# The server should reject a key with -o
result = client.submit("sleep 1", option_flags={"-ompi": "noodles"})
assert "Errors" in result
Expand Down
2 changes: 2 additions & 0 deletions clients/python/flux_restful_client/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ def flatten_list(obj):
"""
Flatten a dict to a list of comma separated strings
"""
if not isinstance(obj, dict):
return obj
result = ""
for key, value in obj.items():
if not result:
Expand Down
2 changes: 1 addition & 1 deletion clients/python/flux_restful_client/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
INSTALL_REQUIRES = (
# Required to maintain comments in files
("ruamel.yaml", {"min_version": None}),
("requests", {"min_version": None}),
("httpx", {"min_version": None}),
("jsonschema", {"min_version": None}),
("python-jose[cryptography]", {"min_version": None}),
)
Expand Down

0 comments on commit e8f9f5e

Please sign in to comment.