Skip to content

Commit

Permalink
Request retries (#17)
Browse files Browse the repository at this point in the history
* added request retry sessions and exception catch

* tested locally

* version bump and cleanup of param input
  • Loading branch information
Tomnl authored Aug 27, 2020
1 parent 8d89e96 commit 3953731
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion msnpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@

__author__ = 'Ralf Weber ([email protected])'
__credits__ = 'Ralf Weber ([email protected])'
__version__ = '1.0.0a8'
__version__ = '1.0.0a9'
__license__ = 'GPLv3'
40 changes: 37 additions & 3 deletions msnpy/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#
import signal
import os
import sys
import copy
import collections
import re
Expand All @@ -30,6 +31,8 @@
import networkx as nx
import pandas as pd
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from .processing import mz_tol, mz_pair_diff_tol


Expand All @@ -47,9 +50,39 @@ class ApiMfdb:
def __init__(self, url="https://mfdb.bham.ac.uk"):
self.url = url
self.url_mass_range = '{}/api/formula/mass_range/'.format(self.url)
r = requests.get('{}/api/formula/mass/?mass=71.03711&tol=1&tol_unit=ppm&rules=1'.format(self.url))
r = self.request_call('{}/api/formula/mass/?mass=71.03711&tol=1&tol_unit=ppm&rules=1'.format(self.url))
r.raise_for_status()

def requests_retry_session(self, retries: int = 5,
backoff_factor: float = 1,
status_forcelist: tuple = (500, 502, 504),
session=None):

# https://www.peterbe.com/plog/best-practice-with-retries-with-requests
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session

def request_call(self, url: str, params: dict = None):
session = self.requests_retry_session()
# Leave exceptions to fail for now as want to check when error occurs
# in galaxy
# try:
response = session.get(url, params=params)
# except Exception as x:
#
session.close()
return response

def select_mf(self, min_tol: float, max_tol: float, adducts: dict = None, rules: bool = True):

mf_out = []
Expand All @@ -59,11 +92,12 @@ def select_mf(self, min_tol: float, max_tol: float, adducts: dict = None, rules:

params = {"lower": min_tol - adduct_mass,
"upper": max_tol - adduct_mass, "rules": int(rules)}
response = requests.get(self.url_mass_range, params=params)

response = self.request_call(self.url_mass_range, params=params)
# Check response is ok
if not response:
continue

resp_d = response.json()

# check records key in response dict/json
Expand Down

0 comments on commit 3953731

Please sign in to comment.