Skip to content
Snippets Groups Projects
Commit b783fd0e authored by Neda Moeini's avatar Neda Moeini
Browse files

Added a custom HTTP IPV4 adaptor to force IPV4.

parent fada137f
Branches
Tags
1 merge request!37Feature/dboard3 921
import concurrent.futures
import logging
import socket
import requests
from flask import current_app
import concurrent.futures
from requests.adapters import HTTPAdapter
from urllib3.poolmanager import PoolManager
logger = logging.getLogger(__name__)
......@@ -10,6 +13,23 @@ GRANT_TYPE = 'client_credentials'
SCOPE = 'openid profile email aarc'
class IPv4Adapter(HTTPAdapter):
"""A custom adapter that forces the use of IPv4."""
def init_poolmanager(self, connections, maxsize, block=False, **pool_kwargs):
pool_kwargs['socket_options'] = [(socket.IPPROTO_IP, socket.IP_TOS, 0)]
self.poolmanager = PoolManager(
num_pools=connections,
maxsize=maxsize,
block=block,
**pool_kwargs
)
def proxy_manager_for(self, proxy, **proxy_kwargs):
proxy_kwargs['socket_options'] = [(socket.IPPROTO_IP, socket.IP_TOS, 0)]
return super().proxy_manager_for(proxy, **proxy_kwargs)
def get_token_endpoint(discovery_endpoint_url: str) -> str:
response = requests.get(discovery_endpoint_url)
response.raise_for_status()
......@@ -36,7 +56,12 @@ def make_request(body: dict, token: str) -> dict:
config = current_app.config['INVENTORY_PROVIDER_CONFIG']
api_url = f'{config["orchestrator"]["url"]}/api/graphql'
headers = {'Authorization': f'Bearer {token}'}
response = requests.post(api_url, headers=headers, json=body)
session = requests.Session()
# Mount the adapter to force IPv4
adapter = IPv4Adapter()
session.mount('http://', adapter)
session.mount('https://', adapter)
response = session.post(api_url, headers=headers, json=body)
response.raise_for_status()
return response.json()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment