Skip to content

Commit

Permalink
httpx fixes - removed SSL verification
Browse files Browse the repository at this point in the history
Belapi was failing due to SSL verification issues. Removed that
from the httpx client and updated everything to use that httpx Client.
  • Loading branch information
wshayes committed Mar 9, 2020
1 parent c5460d8 commit d3fe3d6
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 28 deletions.
4 changes: 2 additions & 2 deletions bel/edge/edges.py
Expand Up @@ -14,7 +14,6 @@
from typing import Any, List, Mapping, MutableSequence

# Third Party Imports
import httpx
import structlog

# Local Imports
Expand All @@ -23,6 +22,7 @@
import bel.lang.belobj
import bel.utils as utils
from bel.Config import config
from bel.utils import http_client

log = structlog.getLogger(__name__)

Expand Down Expand Up @@ -442,7 +442,7 @@ def orthologize_context(
"""

url = f'{config["bel_api"]["servers"]["api_url"]}/terms/{orthologize_target}'
r = httpx.get(url)
r = http_client.get(url)
species_label = r.json().get("label", "unlabeled")

orthologized_from = {}
Expand Down
4 changes: 2 additions & 2 deletions bel/edge/pipeline.py
Expand Up @@ -15,14 +15,14 @@
import urllib

# Third Party Imports
import httpx
import structlog

# Local Imports
import bel.db.arangodb as arangodb
import bel.edge.edges
import bel.nanopub.files as files
import bel.utils as utils
from bel.utils import http_client

log = structlog.getLogger(__name__)

Expand Down Expand Up @@ -71,7 +71,7 @@ def process_nanopub(
if token:
headers = {"Authorization": f"Bearer {token}"}

r = httpx.get(nanopub_url, headers=headers)
r = http_client.get(nanopub_url, headers=headers)

nanopub = r.json()

Expand Down
5 changes: 2 additions & 3 deletions bel/lang/ast.py
Expand Up @@ -16,15 +16,14 @@
from typing import Any, List, Mapping

# Third Party Imports
import httpx
import structlog
import yaml

# Local Imports
import bel.terms.orthologs
import bel.terms.terms
from bel.Config import config
from bel.utils import url_path_param_quoting
from bel.utils import http_client, url_path_param_quoting

log = structlog.getLogger(__name__)

Expand Down Expand Up @@ -718,7 +717,7 @@ def convert_nsarg(

request_url = api_url.format(url_path_param_quoting(nsarg))

r = httpx.get(request_url, params=params, timeout=10)
r = http_client.get(request_url, params=params, timeout=10)

if r and r.status_code == 200:
nsarg = r.json().get("term_id", nsarg)
Expand Down
10 changes: 5 additions & 5 deletions bel/lang/bel_specification.py
Expand Up @@ -22,14 +22,14 @@
from typing import Any, List, Mapping

# Third Party Imports
import httpx
import jinja2
import structlog
import tatsu
import yaml

# Local Imports
from bel.Config import config
from bel.utils import http_client

log = structlog.getLogger(__name__)

Expand Down Expand Up @@ -222,7 +222,7 @@ def github_belspec_files(spec_dir, force: bool = False):
if github_access_token:
params = {"access_token": github_access_token}

r = httpx.get(repo_url, params=params)
r = http_client.get(repo_url, params=params)
if r.status_code == 200:
results = r.json()
for f in results:
Expand All @@ -232,7 +232,7 @@ def github_belspec_files(spec_dir, force: bool = False):
if "yaml" not in fn and "yml" in fn:
fn = fn.replace("yml", "yaml")

r = httpx.get(url, params=params, allow_redirects=True)
r = http_client.get(url, params=params, allow_redirects=True)
if r.status_code == 200:
open(f"{spec_dir}/{fn}", "wb").write(r.content)
else:
Expand Down Expand Up @@ -561,7 +561,7 @@ def get_ebnf_template():

try:
# Get download url for template file
r = httpx.get(repo_url, params=params)
r = http_client.get(repo_url, params=params)

if r.status_code == 200:
template_url = r.json()["download_url"]
Expand All @@ -570,7 +570,7 @@ def get_ebnf_template():

# Get template file
try:
r = httpx.get(template_url, params=params, allow_redirects=True)
r = http_client.get(template_url, params=params, allow_redirects=True)
if r.status_code == 200:
open(local_fp, "wt").write(r.text)
else:
Expand Down
5 changes: 2 additions & 3 deletions bel/lang/completion.py
Expand Up @@ -15,14 +15,13 @@
from typing import Any, List, Mapping, Optional, Tuple

# Third Party Imports
import httpx
from structlog import get_logger

# Local Imports
import bel.lang.bel_specification as bel_specification
import bel.lang.partialparse as pparse
from bel.Config import config
from bel.utils import url_path_param_quoting
from bel.utils import http_client, url_path_param_quoting

log = get_logger()

Expand Down Expand Up @@ -215,7 +214,7 @@ def nsarg_completions(
}
log.info("NSArg completion", api_url=config["bel_api"]["servers"]["api_url"], url=url)

r = httpx.get(url, params=params)
r = http_client.get(url, params=params)

if r.status_code == 200:
ns_completions = r.json()
Expand Down
5 changes: 2 additions & 3 deletions bel/lang/semantics.py
Expand Up @@ -5,12 +5,11 @@
from typing import List, Tuple

# Third Party Imports
import httpx
import structlog

# Local Imports
from bel.lang.ast import BELAst, Function, NSArg, StrArg
from bel.utils import url_path_param_quoting
from bel.utils import http_client, url_path_param_quoting

log = structlog.getLogger()

Expand Down Expand Up @@ -275,7 +274,7 @@ def validate_arg_values(ast, bo):
else:
request_url = bo.api_url + "/terms/{}".format(url_path_param_quoting(term_id))
log.info(f"Validate Arg Values url {request_url}")
r = httpx.get(request_url)
r = http_client.get(request_url)
if r and r.status_code == 200:
result = r.json()
# function signature term value_types doesn't match up with API term entity_types
Expand Down
4 changes: 2 additions & 2 deletions bel/nanopub/nanopubs.py
Expand Up @@ -4,14 +4,14 @@
from typing import Any, Iterable, List, Mapping, Tuple

# Third Party Imports
import httpx
import jsonschema
from cityhash import CityHash64

# Local Imports
import bel.edge.edges
import bel.lang.belobj
from bel.Config import config
from bel.utils import http_client

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -98,7 +98,7 @@ def validate_context(self, context: Mapping[str, Any]) -> Tuple[bool, List[Tuple

url = f'{self.endpoint}/terms/{context["id"]}'

res = httpx.get(url)
res = http_client.get(url)
if res.status_code == 200:
return (True, [])
else:
Expand Down
9 changes: 4 additions & 5 deletions bel/nanopub/pubmed.py
Expand Up @@ -15,14 +15,13 @@
from typing import Any, Mapping

# Third Party Imports
import httpx
import structlog
from lxml import etree

# Local Imports
import bel.lang.ast as bel_ast
from bel.Config import config
from bel.utils import url_path_param_quoting
from bel.utils import http_client, url_path_param_quoting

log = structlog.getLogger(__name__)

Expand Down Expand Up @@ -69,7 +68,7 @@ def get_pubtator(pmid):
Re-configure the denotations into an annotation dictionary format
and collapse duplicate terms so that their spans are in a list.
"""
r = httpx.get(PUBTATOR_TMPL.replace("PMID", pmid), timeout=10)
r = http_client.get(PUBTATOR_TMPL.replace("PMID", pmid), timeout=10)
if r and r.status_code == 200:
pubtator = r.json()[0]
else:
Expand Down Expand Up @@ -283,7 +282,7 @@ def get_pubmed(pmid: str) -> Mapping[str, Any]:

try:
pubmed_url = PUBMED_TMPL.replace("PMID", str(pmid))
r = httpx.get(pubmed_url)
r = http_client.get(pubmed_url)
content = r.content
log.info(f"Getting Pubmed URL {pubmed_url}")
root = etree.fromstring(content)
Expand Down Expand Up @@ -332,7 +331,7 @@ def enhance_pubmed_annotations(pubmed: Mapping[str, Any]) -> Mapping[str, Any]:
for nsarg in pubmed["annotations"]:
url = f'{config["bel_api"]["servers"]["api_url"]}/terms/{url_path_param_quoting(nsarg)}'
log.info(f"URL: {url}")
r = httpx.get(url)
r = http_client.get(url)
log.info(f"Result: {r}")
new_nsarg = ""
if r and r.status_code == 200:
Expand Down
11 changes: 10 additions & 1 deletion bel/utils.py
Expand Up @@ -20,6 +20,15 @@
log = get_logger()


def get_http_client():
"""Client for http requests"""

return httpx.Client(verify=False)


http_client = get_http_client()


def timespan(start_time):
"""Return time in milliseconds from start_time"""

Expand All @@ -31,7 +40,7 @@ def timespan(start_time):
def download_file(url):
"""Download file"""

response = httpx.get(url, stream=True)
response = http_client.get(url, stream=True)
fp = tempfile.NamedTemporaryFile()
for chunk in response.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
Expand Down
10 changes: 8 additions & 2 deletions tests/lang/test_completion.py
Expand Up @@ -165,7 +165,10 @@ def test_completion_arg_fn_5():

def test_completion_arg_ns_prefix():

if bel.httpx.get(f"{config['bel_api']['servers']['api_url']}/simple_status").status_code != 200:
if (
bel.http_client.get(f"{config['bel_api']['servers']['api_url']}/simple_status").status_code
!= 200
):
pytest.xfail("BEL.bio API Test environment is not setup")

completions = bel.lang.completion.bel_completion(
Expand All @@ -181,7 +184,10 @@ def test_completion_arg_ns_prefix():

def test_completion_arg_ns_val():

if bel.httpx.get(f"{config['bel_api']['servers']['api_url']}/simple_status").status_code != 200:
if (
bel.http_client.get(f"{config['bel_api']['servers']['api_url']}/simple_status").status_code
!= 200
):
pytest.xfail("BEL.bio API Test environment is not setup")

completions = bel.lang.completion.bel_completion(
Expand Down

0 comments on commit d3fe3d6

Please sign in to comment.