#521 Use timeout and verify request arguments consistently
Merged 4 years ago by lholecek. Opened 4 years ago by lholecek.
lholecek/greenwave dist-git-timeout  into  master

file modified
+1 -4
@@ -436,10 +436,7 @@ 

      answers = []

      verbose_results = []

      applicable_policies = []

-     retriever_args = dict(

-         when=when,

-         timeout=current_app.config['REQUESTS_TIMEOUT'],

-         verify=current_app.config['REQUESTS_VERIFY'])

+     retriever_args = {'when': when}

      results_retriever = ResultsRetriever(

          ignore_ids=ignore_results,

          url=current_app.config['RESULTSDB_API_URL'],

@@ -6,6 +6,8 @@ 

  from urllib3.util.retry import Retry

  from urllib3.exceptions import ProxyError, SSLError

  

+ from flask import current_app, has_app_context

+ 

  from greenwave import __version__

  

  
@@ -25,6 +27,12 @@ 

  class RequestsSession(requests.Session):

      def request(self, *args, **kwargs):  # pylint:disable=arguments-differ

          req_url = kwargs.get('url', args[1])

+ 

+         kwargs.setdefault('headers', {'Content-Type': 'application/json'})

+         if has_app_context():

+             kwargs.setdefault('timeout', current_app.config['REQUESTS_TIMEOUT'])

+             kwargs.setdefault('verify', current_app.config['REQUESTS_VERIFY'])

+ 

          try:

              return super().request(*args, **kwargs)

          except (ConnectTimeout, RetryError) as e:

file modified
+6 -18
@@ -8,7 +8,6 @@ 

  

  import logging

  import re

- import json

  import socket

  

  from urllib.parse import urlparse
@@ -25,10 +24,8 @@ 

  

  

  class BaseRetriever:

-     def __init__(self, ignore_ids, when, timeout, verify, url):

+     def __init__(self, ignore_ids, when, url):

          self.ignore_ids = ignore_ids

-         self.timeout = timeout

-         self.verify = verify

          self.url = url

  

          if when:
@@ -41,7 +38,7 @@ 

          return [item for item in items if item['id'] not in self.ignore_ids]

  

      def _retrieve_data(self, params):

-         response = self._make_request(params, verify=self.verify, timeout=self.timeout)

+         response = self._make_request(params)

          response.raise_for_status()

          return response.json()['data']

  
@@ -105,8 +102,7 @@ 

      def _make_request(self, params, **request_args):

          return requests_session.post(

              self.url + '/waivers/+filtered',

-             headers={'Content-Type': 'application/json'},

-             data=json.dumps({'filters': params}),

+             json={'filters': params},

              **request_args)

  

  
@@ -176,9 +172,7 @@ 

          "rev": rev

      }

      url = current_app.config['DIST_GIT_URL_TEMPLATE'].format(**data)

-     response = requests_session.request('HEAD', url,

-                                         headers={'Content-Type': 'application/json'},

-                                         timeout=60)

+     response = requests_session.request('HEAD', url)

      if response.status_code == 404:

          return None

  
@@ -186,19 +180,13 @@ 

          raise BadGateway('Error occurred looking for gating.yaml file in the dist-git repo.')

  

      # gating.yaml found...

-     response = requests_session.request('GET', url,

-                                         headers={'Content-Type': 'application/json'},

-                                         timeout=60)

+     response = requests_session.request('GET', url)

      response.raise_for_status()

      return response.content

  

  

  # NOTE - not cached.

  def retrieve_decision(greenwave_url, data):

-     timeout = current_app.config['REQUESTS_TIMEOUT']

-     verify = current_app.config['REQUESTS_VERIFY']

-     headers = {'Content-Type': 'application/json'}

-     response = requests_session.post(greenwave_url, headers=headers, data=json.dumps(data),

-                                      timeout=timeout, verify=verify)

+     response = requests_session.post(greenwave_url, json=data)

      response.raise_for_status()

      return response.json()

@@ -37,8 +37,6 @@ 

          super(DummyResultsRetriever, self).__init__(

              ignore_ids=[],

              when='',

-             timeout=0,

-             verify=False,

              url='')

          self.subject = subject

          self.testcase = testcase

@@ -123,9 +123,7 @@ 

              retrieve_yaml_remote_rule("deadbeaf", "pkg", "")

  

              expected_call = mock.call(

-                 'HEAD',

-                 'https://src.fedoraproject.org/pkg/raw/deadbeaf/f/gating.yaml',

-                 headers={'Content-Type': 'application/json'}, timeout=60)

+                 'HEAD', 'https://src.fedoraproject.org/pkg/raw/deadbeaf/f/gating.yaml')

              assert session.request.mock_calls == [expected_call]

  

  

@@ -7,8 +7,6 @@ 

  _DUMMY_RETRIEVER_ARGUMENTS = dict(

      ignore_ids=[],

      when=None,

-     timeout=None,

-     verify=None,

      url=None,

  )

  

JIRA: FACTORY-5484
Signed-off-by: Lukas Holecek hluk@email.cz

use literal there i.e. {'when': when}

+1 after changing previous

rebased onto 82d25a8

4 years ago

@gnaponie Can you review, please? I think this needs more eyes! :)

This seems correct to me. +1
@lholecek feel free to merge it.

Commit 568ae9c fixes this pull-request

Pull-Request has been merged by lholecek

4 years ago

Pull-Request has been merged by lholecek

4 years ago