def _request()

in python/shared/research_pacs/shared/orthanc.py [0:0]


  def _request(self, method, full_path, raise_error=True, **kwargs):
    """
    Make a HTTP request to the Orthanc server.
    
    Args:
      method (str): HTTP method (GET, POST...)
      full_path (str): URL path that must not start with a /
      raise_error: Raise an exception if the response code is 4xx or 5xx
      **kwargs: Other keyword arguments passed to the `requests.request` function
      
    """
    url = '%s/%s' %(self._host, full_path)
    try:
      logger.debug(f'Sending a {method} request to {url}')
      response = requests.request(
        method = method,
        url = url,
        auth = requests.auth.HTTPBasicAuth(self._username, self._password),
        timeout = (5, 30),  # 5 seconds to connect, 30 seconds to read the first byte
        **kwargs
      )
      # Raise an exception if the HTTP request failed (4xx or 5xx status code)
      if raise_error is True:
        response.raise_for_status()
      return response
    except requests.exceptions.HTTPError as e:
      try:
        orthanc_error = response.json()['OrthancError']
      except:
        orthanc_error = ''
      err_msg = f'Failed to make a {method} request to {url} - ErrorCode={e.response.status_code} Error="{e}" OrthancError="{orthanc_error}"'
      logger.debug(err_msg)
      raise Exception(err_msg)
    except Exception as e:
      err_msg = f'Failed to make {method} request to {url} - Error={e}'
      logger.debug(err_msg)
      raise Exception(err_msg)