Skip to content
This repository has been archived by the owner on Jan 21, 2025. It is now read-only.

Commit

Permalink
provide user and pwd
Browse files Browse the repository at this point in the history
  • Loading branch information
André Kühnert committed Nov 6, 2024
1 parent d8dbc85 commit 70fd0f5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
4 changes: 1 addition & 3 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def check_test_motion_group_available(request):
if not token:
pytest.fail("NOVA_ACCESS_TOKEN not set in the environment.")

headers = {
"Authorization": f"Bearer {token}"
}
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(nova_host, timeout=5, headers=headers)
response.raise_for_status()
except requests.RequestException as e:
Expand Down
22 changes: 18 additions & 4 deletions wandelbots/core/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,37 @@ class Instance:
def __init__(
self,
url="http://api-gateway.wandelbots.svc.cluster.local:8080",
user=None,
password=None,
access_token=None,
):
self._api_version = "v1"
self.access_token = access_token
self.user = (user,)
self.password = (password,)
self.url = self._parse_url(url)
self.logger = _get_logger(__name__)

def _parse_url(self, host: str) -> str:
"""remove any trailing slashes and validate scheme"""
_url = host.rstrip("/")
is_basic_set = not self.user or not self.password
is_token_set = not self.access_token

if is_basic_set and is_token_set:
raise ValueError(
"please choose either user and password or access token access"
)

if _url.startswith("https"):
if not self.access_token:
raise ValueError("User and password are required for https connections")
if not is_token_set and not is_basic_set:
raise ValueError(
"Access token or user and password are required for https connections"
)
elif _url.startswith("http"):
if self.access_token:
if is_basic_set or is_token_set:
raise ValueError(
"User and password are not required for http connections"
"Access token and/or user and password are not required for http connections"
)
elif "wandelbots.io" in _url:
_url = "https://" + _url
Expand Down
22 changes: 18 additions & 4 deletions wandelbots/request/asyncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def _get_auth_header(instance: Instance) -> Optional[Dict[str, str]]:
return None


def _get_auth(instance: Instance) -> Optional[httpx.BasicAuth]:
if instance.has_auth():
return httpx.BasicAuth(username=instance.user, password=instance.password)
return None


def _handle_request_error(err):
if isinstance(err, httpx.HTTPStatusError):
if err.response.status_code == 401:
Expand All @@ -28,7 +34,9 @@ def _handle_request_error(err):


async def get(url: str, instance: Instance) -> Tuple[int, Optional[Dict]]:
async with httpx.AsyncClient(headers=_get_auth_header(instance)) as client:
async with httpx.AsyncClient(
headers=_get_auth_header(instance), auth=_get_auth(instance)
) as client:
try:
response = await client.get(url, timeout=TIMEOUT)
response.raise_for_status()
Expand All @@ -39,7 +47,9 @@ async def get(url: str, instance: Instance) -> Tuple[int, Optional[Dict]]:


async def delete(url: str, instance: Instance) -> int:
async with httpx.AsyncClient(headers=_get_auth_header(instance)) as client:
async with httpx.AsyncClient(
headers=_get_auth_header(instance), auth=_get_auth(instance)
) as client:
try:
response = await client.delete(url, timeout=TIMEOUT)
response.raise_for_status()
Expand All @@ -52,7 +62,9 @@ async def delete(url: str, instance: Instance) -> int:
async def post(
url: str, instance: Instance, data: Dict = {}
) -> Tuple[int, Optional[Dict]]:
async with httpx.AsyncClient(headers=_get_auth_header(instance)) as client:
async with httpx.AsyncClient(
headers=_get_auth_header(instance), auth=_get_auth(instance)
) as client:
try:
response = await client.post(url, json=data, timeout=TIMEOUT)
response.raise_for_status()
Expand All @@ -65,7 +77,9 @@ async def post(
async def put(
url: str, instance: Instance, data: Dict = {}
) -> Tuple[int, Optional[Dict]]:
async with httpx.AsyncClient(headers=_get_auth_header(instance)) as client:
async with httpx.AsyncClient(
headers=_get_auth_header(instance), auth=_get_auth(instance)
) as client:
try:
response = await client.put(url, json=data, timeout=TIMEOUT)
response.raise_for_status()
Expand Down
33 changes: 29 additions & 4 deletions wandelbots/request/syncs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import httpx
import requests
from typing import Dict, Tuple, Optional
from wandelbots.util.logger import _get_logger
Expand All @@ -13,6 +14,12 @@ def _get_auth_header(instance: Instance) -> Optional[Dict[str, str]]:
return None


def _get_auth(instance: Instance) -> Optional[httpx.BasicAuth]:
if instance.has_auth():
return httpx.BasicAuth(username=instance.user, password=instance.password)
return None


def _handle_request_error(err):
if isinstance(err, requests.HTTPError):
if err.response.status_code == 401:
Expand All @@ -31,7 +38,12 @@ def _handle_request_error(err):

def get(url: str, instance: Instance) -> Tuple[int, Optional[Dict]]:
try:
response = requests.get(url, timeout=TIMEOUT, headers=_get_auth_header(instance))
response = requests.get(
url,
timeout=TIMEOUT,
headers=_get_auth_header(instance),
auth=_get_auth(instance),
)
response.raise_for_status()
return response.status_code, response.json()
except requests.RequestException as err:
Expand All @@ -41,7 +53,12 @@ def get(url: str, instance: Instance) -> Tuple[int, Optional[Dict]]:

def delete(url: str, instance: Instance) -> int:
try:
response = requests.delete(url, timeout=TIMEOUT, headers=_get_auth_header(instance))
response = requests.delete(
url,
timeout=TIMEOUT,
headers=_get_auth_header(instance),
auth=_get_auth(instance),
)
response.raise_for_status()
return response.status_code
except requests.RequestException as err:
Expand All @@ -52,7 +69,11 @@ def delete(url: str, instance: Instance) -> int:
def post(url: str, instance: Instance, data: Dict = {}) -> Tuple[int, Optional[Dict]]:
try:
response = requests.post(
url, json=data, timeout=TIMEOUT, headers=_get_auth_header(instance)
url,
json=data,
timeout=TIMEOUT,
headers=_get_auth_header(instance),
auth=_get_auth(instance),
)
response.raise_for_status()
return response.status_code, response.json()
Expand All @@ -64,7 +85,11 @@ def post(url: str, instance: Instance, data: Dict = {}) -> Tuple[int, Optional[D
def put(url: str, instance: Instance, data: Dict = {}) -> Tuple[int, Optional[Dict]]:
try:
response = requests.put(
url, json=data, timeout=TIMEOUT, headers=_get_auth_header(instance)
url,
json=data,
timeout=TIMEOUT,
headers=_get_auth_header(instance),
auth=_get_auth(instance),
)
response.raise_for_status()
return response.status_code, response.json()
Expand Down

0 comments on commit 70fd0f5

Please sign in to comment.