Python urllib3.util.retry.Retry() Examples

The following are 30 code examples of urllib3.util.retry.Retry(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib3.util.retry , or try the search function .
Example #1
Source File: GooglePhotosDownload.py    From gphotos-sync with MIT License 8 votes vote down vote up
def find_bad_items(self, batch: Mapping[str, DatabaseMedia]):
        """
        a batch get failed. Now do all of its contents as individual
        gets so we can work out which ID(s) cause the failure
        """
        for item_id, media_item in batch.items():
            try:
                log.debug("BAD ID Retry on %s (%s)", item_id, media_item.relative_path)
                response = self._api.mediaItems.get.execute(mediaItemId=item_id)
                media_item_json = response.json()
                self.download_file(media_item, media_item_json)
            except RequestException as e:
                self.bad_ids.add_id(
                    str(media_item.relative_path), media_item.id, media_item.url, e
                )
                self.files_download_failed += 1
                log.error(
                    "FAILURE %d in get of %s BAD ID",
                    self.files_download_failed,
                    media_item.relative_path,
                ) 
Example #2
Source File: requests.py    From pyms with GNU General Public License v3.0 6 votes vote down vote up
def requests(self, session: requests.Session):
        """
        A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
        second try without a delay). urllib3 will sleep for: {backoff factor} * (2 ^ ({number of total retries} - 1))
        seconds. If the backoff_factor is 0.1, then sleep() will sleep for [0.0s, 0.2s, 0.4s, ...] between retries.
        It will never be longer than Retry.BACKOFF_MAX. By default, backoff is disabled (set to 0).
        :param session:
        :return:
        """
        session_r = session or requests.Session()
        max_retries = Retry(
            total=self.retries,
            read=self.retries,
            connect=self.retries,
            backoff_factor=0.3,
            status_forcelist=self.status_retries,
        )
        adapter = HTTPAdapter(max_retries=max_retries)
        session_r.mount('http://', adapter)
        session_r.mount('https://', adapter)
        return session_r 
Example #3
Source File: request_api.py    From pyupbit with Apache License 2.0 6 votes vote down vote up
def requests_retry_session(retries=5, backoff_factor=0.3, status_forcelist=(500, 502, 504), session=None):
    """

    :param retries:
    :param backoff_factor:
    :param status_forcelist:
    :param session:
    :return:
    """
    s = 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)
    s.mount('http://', adapter)
    s.mount('https://', adapter)
    return s 
Example #4
Source File: config.py    From django-auth-adfs with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__(self):
        self._config_timestamp = None
        self._mode = None

        self.authorization_endpoint = None
        self.signing_keys = None
        self.token_endpoint = None
        self.end_session_endpoint = None
        self.issuer = None

        method_whitelist = frozenset([
            'HEAD', 'GET', 'PUT', 'DELETE', 'OPTIONS', 'TRACE', 'POST'
        ])

        retry = Retry(
            total=settings.RETRIES,
            read=settings.RETRIES,
            connect=settings.RETRIES,
            backoff_factor=0.3,
            method_whitelist=method_whitelist
        )
        self.session = requests.Session()
        adapter = requests.adapters.HTTPAdapter(max_retries=retry)
        self.session.mount('https://', adapter)
        self.session.verify = settings.CA_BUNDLE 
Example #5
Source File: _requests.py    From ucloud-sdk-python3 with Apache License 2.0 6 votes vote down vote up
def _load_adapter(
        self, max_retries: typing.Optional[int] = None
    ) -> HTTPAdapter:
        if max_retries is None and self._adapter is not None:
            return self._adapter

        max_retries = max_retries or 0
        adapter = HTTPAdapter()
        adapter.max_retries = Retry(
            total=max_retries,
            read=max_retries,
            connect=max_retries,
            backoff_factor=self.backoff_factor,
            status_forcelist=self.status_forcelist,
        )
        return adapter 
Example #6
Source File: requestor.py    From cw-sdk-python with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__(self, rest_endpoint, user_agent, opts={}):
        # Must have options
        self.user_agent = user_agent
        self.rest_endpoint = rest_endpoint
        if not rest_endpoint.startswith("https"):
            log('Warning: API endpoint must start with "https".', is_error=True)
        if not user_agent:
            log("Warning: User-Agent header must be set.", is_error=True)
        # Options with defaults, overridden by kwargs
        self.verify_ssl = opts.get("verify_ssl", True)
        self.connect_timeout = opts.get("connect_timeout", 5)
        self.read_timeout = opts.get("read_timeout", 20)
        self.max_retries = opts.get("max_retries", 5)
        # Make all API calls share the same session
        self.api_client = requests.Session()
        # Apply a backoff for failing requests, up to self.max_retries
        # 1st waiting will be 0.1s, then 0.2s, 0.4s, etc, following this formula:
        # {backoff factor} * (2 ** ({number of retries so far} - 1))
        retries = Retry(
            total=self.max_retries,
            backoff_factor=0.1,
            status_forcelist=[500, 502, 503, 504],
        )
        a = requests.adapters.HTTPAdapter(max_retries=retries)
        self.api_client.mount("https://", a) 
Example #7
Source File: requests.py    From renku-python with Apache License 2.0 6 votes vote down vote up
def retry(
    total_requests=10, backoff_factor=1, statuses=(500, 502, 503, 504, 429)
):
    """Default HTTP session for requests."""
    _session = requests.Session()

    retries = Retry(
        total=total_requests,
        backoff_factor=backoff_factor,
        status_forcelist=list(statuses)
    )

    _session.mount('http://', HTTPAdapter(max_retries=retries))
    _session.mount('https://', HTTPAdapter(max_retries=retries))

    yield _session 
Example #8
Source File: utils.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def check_github_users(usernames, retries=5, backoff_factor=0.3, status_forcelist=(500, 502, 503, 504)):
    """
    Check if provided usernames exist in Github
    :param usernames: list of usernames
    :param retries: number of retries
    :param backoff_factor: backoff to apply between retries
    :param status_forcelist: HTTP status codes that should be retried
    :return: list of usernames that exist in Github
    """
    session = 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('https://', adapter)
    return [
        username for username in usernames
        if session.get('https://github.com/{}.keys'.format(username)).status_code == 200
    ] 
Example #9
Source File: tokens.py    From tcex with Apache License 2.0 6 votes vote down vote up
def retry_session(retries=3, backoff_factor=0.8, status_forcelist=(500, 502, 504)):
    """Add retry to Requests Session.

    https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#urllib3.util.retry.Retry
    """
    session = Session()
    retries = Retry(
        total=retries,
        read=retries,
        connect=retries,
        backoff_factor=backoff_factor,
        status_forcelist=status_forcelist,
    )
    # mount all https requests
    session.mount('https://', HTTPAdapter(max_retries=retries))
    return session 
Example #10
Source File: tokens.py    From tcex with Apache License 2.0 5 votes vote down vote up
def renew_token(self, token):
        """Renew expired ThreatConnect Token.

        This method will renew a token and update the token_map with new token and expiration.

        Args:
            token (str): The ThreatConnect API token.
            token_expires (int): The token expiration timestamp.
        """
        api_token_data = {}
        self.log.in_token_renewal = True  # pause API logging

        # log token information
        try:
            params = {'expiredToken': token}
            url = f'{self.token_url}/appAuth'
            r = self.session.get(url, params=params, verify=self.verify)

            if not r.ok:
                err_reason = r.text or r.reason
                err_msg = (
                    f'Token Retry Error. API status code: {r.status_code}, '
                    f'API message: {err_reason}, '
                    f'Token: {self.printable_token(token)}.'
                )
                self.log.error(err_msg)
                raise RuntimeError(1042, err_msg)
        except exceptions.SSLError:  # pragma: no cover
            raise RuntimeError('Token renewal failed with an SSL Error.')

        # process response for token
        try:
            api_token_data = r.json()
        except (AttributeError, ValueError) as e:  # pragma: no cover
            raise RuntimeError(f'Token renewal failed ({e}).')
        finally:
            self.log.in_token_renewal = False

        return api_token_data 
Example #11
Source File: test_local.py    From chalice with Apache License 2.0 5 votes vote down vote up
def http_session():
    session = requests.Session()
    retry = Retry(
        # How many connection-related errors to retry on.
        connect=10,
        # A backoff factor to apply between attempts after the second try.
        backoff_factor=2,
        method_whitelist=['GET', 'POST', 'PUT'],
    )
    session.mount('http://', HTTPAdapter(max_retries=retry))
    return HTTPFetcher(session) 
Example #12
Source File: vault.py    From aomi with MIT License 5 votes vote down vote up
def __init__(self, _url=None, token=None, _cert=None, _verify=True,
                 _timeout=30, _proxies=None, _allow_redirects=True,
                 _session=None):
        self.version = None
        self.vault_addr = os.environ.get('VAULT_ADDR')
        if not self.vault_addr:
            raise aomi.exceptions.AomiError('VAULT_ADDR is undefined or empty')

        if not self.vault_addr.startswith("http"):
            raise aomi.exceptions.AomiError('VAULT_ADDR must be a URL')

        ssl_verify = True
        if 'VAULT_SKIP_VERIFY' in os.environ:
            if os.environ['VAULT_SKIP_VERIFY'] == '1':
                import urllib3
                urllib3.disable_warnings()
                ssl_verify = False

        self.initial_token = None
        self.operational_token = None
        session = requests.Session()
        retries = Retry(total=5,
                        backoff_factor=0.5)
        adapter = HTTPAdapter(max_retries=retries)
        session.mount('https://', adapter)
        session.mount('http://', adapter)
        super(Client, self).__init__(url=self.vault_addr,
                                     verify=ssl_verify,
                                     session=session) 
Example #13
Source File: installer.py    From aqtinstall with MIT License 5 votes vote down vote up
def retrieve_archive(self, package: QtPackage):
        archive = package.archive
        url = package.url
        name = package.name
        start_time = time.perf_counter()
        self.logger.info("Downloading {}...".format(name))
        self.logger.debug("Download URL: {}".format(url))
        session = requests.Session()
        retry = Retry(connect=5, backoff_factor=0.5)
        adapter = HTTPAdapter(max_retries=retry)
        session.mount('http://', adapter)
        session.mount('https://', adapter)
        try:
            r = session.get(url, allow_redirects=False, stream=True)
            if r.status_code == 302:
                newurl = altlink(r.url, r.headers['Location'], logger=self.logger)
                self.logger.info('Redirected URL: {}'.format(newurl))
                r = session.get(newurl, stream=True)
        except requests.exceptions.ConnectionError as e:
            self.logger.error("Connection error: %s" % e.args)
            raise e
        else:
            try:
                with open(archive, 'wb') as fd:
                    for chunk in r.iter_content(chunk_size=8196):
                        fd.write(chunk)
                        fd.flush()
                if self.command is None:
                    with open(archive, 'rb') as fd:
                        self.extract_archive(fd)
            except Exception as e:
                exc = sys.exc_info()
                self.logger.error("Download error: %s" % exc[1])
                raise e
            else:
                if self.command is not None:
                    self.extract_archive_ext(archive)
        os.unlink(archive)
        self.logger.info("Finish installation of {} in {}".format(archive, time.perf_counter() - start_time)) 
Example #14
Source File: core.py    From pybithumb with MIT License 5 votes vote down vote up
def _requests_retry_session(retries=5, backoff_factor=0.3,
                                status_forcelist=(500, 502, 504), session=None):
        s = 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)
        s.mount('http://', adapter)
        s.mount('https://', adapter)
        return s 
Example #15
Source File: _gcsfs.py    From gcsfs with MIT License 5 votes vote down vote up
def __init__(self,
                 bucket_name: str,
                 root_path: str = None,
                 create: bool = False,
                 client: Client = None,
                 retry: int = 5,
                 strict: bool = True):
        super().__init__()
        self._bucket_name = bucket_name
        if not root_path:
            root_path = ""
        self.root_path = root_path
        self._prefix = relpath(normpath(root_path)).rstrip(self.DELIMITER)

        self.strict = strict

        self.client = client
        if self.client is None:
            self.client = Client()

        if retry:
            adapter = HTTPAdapter(max_retries=Retry(total=retry,
                                                    status_forcelist=[429, 502, 503, 504],
                                                    method_whitelist=False,  # retry on any HTTP method
                                                    backoff_factor=0.5))
            self.client._http.mount("https://", adapter)

        self.bucket = self.client.bucket(self._bucket_name)

        if self._prefix != "":
            if create:
                root_marker = self._get_blob(self._prefix + GCSFS.DELIMITER)
                if root_marker is None:
                    blob = self.bucket.blob(self._prefix + GCSFS.DELIMITER)
                    blob.upload_from_string(b"")
            elif strict and self._get_blob(self._prefix + GCSFS.DELIMITER) is None:
                raise errors.CreateFailed("Root path \"{}\" does not exist".format(root_path)) 
Example #16
Source File: ReceiveCZCE.py    From ParadoxTrading with MIT License 5 votes vote down vote up
def __init__(self):
        super().__init__()
        self.session = requests.Session()
        a = requests.adapters.HTTPAdapter(
            max_retries=Retry(method_whitelist=frozenset(['GET', 'POST']))
        )
        self.session.mount('http://', a) 
Example #17
Source File: trigger.py    From infrabox with MIT License 5 votes vote down vote up
def get_commits(url, token):
    headers = {
        "Authorization": "token " + token,
        "User-Agent": "InfraBox"
    }

    s = requests.Session()

    retries = Retry(total=5,
                    backoff_factor=0.1,
                    status_forcelist=[500, 502, 503, 504])

    s.mount('http://', HTTPAdapter(max_retries=retries))

    # TODO(ib-steffen): allow custom ca bundles
    r = requests.get(url + '?per_page=100', headers=headers, verify=False)
    result = []
    result.extend(r.json())

    p = get_next_page(r)
    while p:
        r = requests.get(p, headers=headers, verify=False)
        p = get_next_page(r)
        result.extend(r.json())

    return result 
Example #18
Source File: test_requests.py    From gphotos-sync with MIT License 5 votes vote down vote up
def test_retries_500(self):
        retries = 5
        timeout = 2

        session = Session()
        start = datetime.now()
        result = session.get("https://httpbin.org/status/500", timeout=timeout)
        self.assertEqual(result.status_code, 500)
        elapsed = datetime.now() - start

        retry = Retry(
            total=retries,
            backoff_factor=0.1,
            status_forcelist=[500, 502, 503, 504],
            method_whitelist=frozenset(["GET", "POST"]),
            raise_on_status=False,
            respect_retry_after_header=True,
        )

        session.mount("https://", HTTPAdapter(max_retries=retry))

        start = datetime.now()
        result = session.get("https://httpbin.org/status/500", timeout=timeout)
        elapsed2 = datetime.now() - start
        self.assertEqual(result.status_code, 500)
        self.assertGreater(elapsed2, elapsed * (retries - 1)) 
Example #19
Source File: trigger.py    From InfraBox with Apache License 2.0 5 votes vote down vote up
def get_commits(url, token):
    headers = {
        "Authorization": "token " + token,
        "User-Agent": "InfraBox"
    }

    s = requests.Session()

    retries = Retry(total=5,
                    backoff_factor=0.1,
                    status_forcelist=[500, 502, 503, 504])

    s.mount('http://', HTTPAdapter(max_retries=retries))

    # TODO(ib-steffen): allow custom ca bundles
    r = requests.get(url + '?per_page=100', headers=headers, verify=False)
    result = []
    result.extend(r.json())

    p = get_next_page(r)
    while p:
        r = requests.get(p, headers=headers, verify=False)
        p = get_next_page(r)
        result.extend(r.json())

    return result 
Example #20
Source File: test_requests.py    From gphotos-sync with MIT License 5 votes vote down vote up
def test_retries_timeout(self):
        retries = 3
        timeout = 1
        retry_error = False

        session = Session()
        retry = Retry(
            total=retries,
            backoff_factor=0.1,
            status_forcelist=[500, 502, 503, 504],
            method_whitelist=frozenset(["GET", "POST"]),
            raise_on_status=False,
            respect_retry_after_header=True,
        )

        session.mount("https://", HTTPAdapter(max_retries=retry))

        start = datetime.now()
        try:
            _ = session.get("https://httpbin.org/delay/5", timeout=timeout)
        except exceptions.ConnectionError as e:
            retry_error = True
            print(e)

        elapsed = datetime.now() - start
        self.assertEqual(retry_error, True)
        self.assertGreater(elapsed.seconds, retries * timeout) 
Example #21
Source File: http.py    From threat_intel with MIT License 5 votes vote down vote up
def __init__(
        self, default_headers=None, max_requests=10, rate_limit=0,
        req_timeout=None, max_retry=10, total_retry=100, drop_404s=False,
    ):
        """Create the MultiRequest.

        Args:
            default_headers - A dict of headers which will be added to every request
            max_requests - Maximum number of requests to issue at once
            rate_limit - Maximum number of requests to issue per second
            req_timeout - Maximum number of seconds to wait without reading a response byte before deciding an error has occurred
            max_retry - The total number of attempts to retry a single batch of requests
            total_retry - The total number of request retries that can be made through the entire session
        Note there is a difference between `max_retry` and `total_retry`:
            - `max_retry` refers to how many times a batch of requests will be re-issued collectively
            - `total_retry` refers to a limit on the total number of outstanding requests made
            Once the latter is exhausted, no failed request within the whole session will be retried.
        """
        self._default_headers = default_headers
        self._max_requests = max_requests
        self._req_timeout = req_timeout or 25.0
        self._max_retry = max_retry
        self._drop_404s = drop_404s
        self._rate_limiter = RateLimiter(rate_limit) if rate_limit else None
        self._availability_limiter = AvailabilityLimiter(total_retry) if total_retry else None
        self._session = FuturesSession(max_workers=max_requests)
        retries = Retry(total=0, status_forcelist=[500, 502, 503, 504], raise_on_status=True)
        self._session.mount(
            'https://', SSLAdapter(
                max_retries=retries, pool_maxsize=max_requests, pool_connections=max_requests,
            ),
        ) 
Example #22
Source File: tc_session.py    From tcex with Apache License 2.0 5 votes vote down vote up
def __init__(self, tcex):
        """Initialize the Class properties."""
        super().__init__()
        self.tcex = tcex

        # properties
        self.args = self.tcex.default_args
        self.auth = None
        self.token = self.tcex.token

        # Update User-Agent
        self.headers.update({'User-Agent': 'TcEx'})

        # Set Proxy
        if self.args.tc_proxy_tc:
            self.proxies = self.tcex.proxies
            self.tcex.log.trace(
                f'Using proxy host {self.args.tc_proxy_host}:'
                f'{self.args.tc_proxy_port} for ThreatConnect API.'
            )

        # Add Retry
        self.retry()

        # Set Verify
        self.verify = self.args.tc_verify 
Example #23
Source File: __init__.py    From uiautomator2 with MIT License 5 votes vote down vote up
def __init__(self):
        super(TimeoutRequestsSession, self).__init__()
        # refs: https://stackoverflow.com/questions/33895739/python-requests-cant-load-any-url-remote-end-closed-connection-without-respo
        # refs: https://stackoverflow.com/questions/15431044/can-i-set-max-retries-for-requests-request

        # Is retry necessary, maybe not, so I closed it at 2020/05/29
        # retries = Retry(total=3, connect=3, backoff_factor=0.5)
        # adapter = requests.adapters.HTTPAdapter(max_retries=retries)
        # self.mount("http://", adapter)
        # self.mount("https://", adapter) 
Example #24
Source File: tc_session.py    From tcex with Apache License 2.0 5 votes vote down vote up
def retry(self, retries=3, backoff_factor=0.3, status_forcelist=(500, 502, 504)):
        """Add retry to Requests Session

        https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#urllib3.util.retry.Retry
        """
        retries = Retry(
            total=retries,
            read=retries,
            connect=retries,
            backoff_factor=backoff_factor,
            status_forcelist=status_forcelist,
        )
        # mount all https requests
        self.mount('https://', adapters.HTTPAdapter(max_retries=retries)) 
Example #25
Source File: test_kubernetes_executor.py    From airflow with Apache License 2.0 5 votes vote down vote up
def _get_session_with_retries(self):
        session = requests.Session()
        retries = Retry(total=3, backoff_factor=1)
        session.mount('http://', HTTPAdapter(max_retries=retries))
        session.mount('https://', HTTPAdapter(max_retries=retries))
        return session 
Example #26
Source File: api.py    From api with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, api_key, proxies=None, persistent=True):
        """
        Set default URLs and create session object

        :param proxies: {} dict for proxy supporting. Example: {"https": "myproxy.com:3128"}
        :param api_key: string with Vulners API key. You can obtain one from the https://vulners.com
        :param persistent: Boolean. Regulates cookie storage policy. If set to true - will save down session cookie for reuse.
        """
        self.vulners_urls = dict((key, self.vulners_hostname + self.api_endpoints.get(key)) for key in self.api_endpoints)

        # Requests opener. If persistent option is active - try to load
        self.__opener = requests.session()
        if persistent:
            self.__opener.cookies = PersistentCookieJar()
        # Setup pool size and Keep Alive
        retries = Retry(total=self.retry_count,
                        backoff_factor=self.backoff_factor,
                        status_forcelist=self.retry_codes,
                        method_whitelist = ['POST', 'GET']
                        )
        adapter = HTTPAdapter(
            pool_connections=100,
            pool_maxsize=100,
            max_retries=retries)
        self.__opener.mount(self.vulners_hostname, adapter)
        self.__opener.headers.update({'Connection': 'Keep-Alive'})
        #
        self.__opener.headers.update({'User-Agent': 'Vulners Python API %s' % api_version})
        if proxies is not None:
            if not isinstance(proxies, dict):
                raise TypeError("Proxies must be a dict type")
            self.__opener.proxies.update(proxies)

        # API key validation

        if not api_key:
            raise ValueError("API key must be provided. You can obtain one for free at https://vulners.com")

        if api_key and not isinstance(api_key, string_types):
            raise TypeError("api_key parameter must be a string value")

        self.__api_key = api_key

        if api_key and not self.__validKey(api_key):
            raise ValueError("Wrong Vulners API key. Please, follow https://vulners.com to obtain correct one.") 
Example #27
Source File: core.py    From gitlabform with MIT License 4 votes vote down vote up
def __init__(self, config_path=None, config_string=None):

        if config_path and config_string:
            logging.fatal('Please initialize with either config_path or config_string, not both.')
            sys.exit(1)

        if config_path:
            configuration = ConfigurationCore(config_path=config_path)
        else:
            configuration = ConfigurationCore(config_string=config_string)

        self.url = configuration.get("gitlab|url", os.getenv("GITLAB_URL"))
        self.token = configuration.get("gitlab|token", os.getenv("GITLAB_TOKEN"))
        self.ssl_verify = configuration.get("gitlab|ssl_verify", True)

        self.session = requests.Session()

        retries = Retry(total=3,
                        backoff_factor=0.25,
                        status_forcelist=[500, 502, 503, 504])

        self.session.mount('http://', HTTPAdapter(max_retries=retries))
        self.session.mount('https://', HTTPAdapter(max_retries=retries))
        self.session.verify = self.ssl_verify

        try:
            version = self._make_requests_to_api("version")
            logging.info("Connected to GitLab version: %s (%s)" % (version['version'], version['revision']))
        except Exception as e:
            raise TestRequestFailedException(e)
        try:
            api_version = configuration.get("gitlab|api_version")
            if api_version != 4:
                raise ApiVersionIncorrectException()
            logging.info("Config file is declared to be compatible with GitLab API v4")
        except KeyNotFoundException:
            logging.fatal("Aborting. GitLabForm 1.0.0 has switched from GitLab API v3 to v4 in which some parameter "
                          "names have changed. By its design GitLabForm reads some parameter names directly from "
                          "config.yml so you need to update those names by yourself. See changes in config.yml "
                          "in this diff to see what had to be changed there: "
                          "https://github.com/egnyte/gitlabform/pull/28/files . "
                          "After updating your config.yml please add 'api_version' key to 'gitlab' section and set it "
                          "to 4 to indicate that your config is v4-compatible.")
            sys.exit(3) 
Example #28
Source File: authorize.py    From gphotos-sync with MIT License 4 votes vote down vote up
def authorize(self):
        """ Initiates OAuth2 authentication and authorization flow
        """
        token = self.load_token()

        if token:
            self.session = OAuth2Session(
                self.client_id,
                token=token,
                auto_refresh_url=self.token_uri,
                auto_refresh_kwargs=self.extra,
                token_updater=self.save_token,
            )
        else:
            self.session = OAuth2Session(
                self.client_id,
                scope=self.scope,
                redirect_uri=self.redirect_uri,
                auto_refresh_url=self.token_uri,
                auto_refresh_kwargs=self.extra,
                token_updater=self.save_token,
            )

            # Redirect user to Google for authorization
            authorization_url, _ = self.session.authorization_url(
                authorization_base_url, access_type="offline", prompt="select_account"
            )
            print("Please go here and authorize,", authorization_url)

            # Get the authorization verifier code from the callback url
            response_code = input("Paste the response token here:")

            # Fetch the access token
            self.token = self.session.fetch_token(
                self.token_uri, client_secret=self.client_secret, code=response_code
            )
            self.save_token(self.token)

        # set up the retry bevaiour for the authorized session
        retries = Retry(
            total=self.max_retries,
            backoff_factor=0.5,
            status_forcelist=[500, 502, 503, 504],
            method_whitelist=frozenset(["GET", "POST"]),
            raise_on_status=False,
            respect_retry_after_header=True,
        )
        # apply the retry behaviour to our session by repalcing the default HTTPAdapter
        self.session.mount("https://", HTTPAdapter(max_retries=retries)) 
Example #29
Source File: godaddy.py    From lexicon with MIT License 4 votes vote down vote up
def _request(self, action='GET', url='/', data=None, query_params=None):
        if not data:
            data = {}
        if not query_params:
            query_params = {}

        # When editing DNS zone, API is unavailable for few seconds
        # (until modifications are propagated).
        # In this case, call to API will return 409 HTTP error.
        # We use the Retry extension to retry the requests until
        # we get a processable reponse (402 HTTP status, or an HTTP error != 409)
        retries = Retry(
            total=10,
            backoff_factor=0.5,
            status_forcelist=[409],
            method_whitelist=frozenset(
                ['GET', 'PUT', 'POST', 'DELETE', 'PATCH'])
        )

        session = requests.Session()
        session.mount('https://', HTTPAdapter(max_retries=retries))

        result = session.request(action, self.api_endpoint + url,
                                 params=query_params,
                                 data=json.dumps(data),
                                 headers={
                                     'Content-Type': 'application/json',
                                     'Accept': 'application/json',
                                     # GoDaddy use a key/secret pair to authenticate
                                     'Authorization': 'sso-key {0}:{1}'.format(
                                         self._get_provider_option(
                                             'auth_key'),
                                         self._get_provider_option('auth_secret'))
                                 })

        result.raise_for_status()

        try:
            # Return the JSON body response if exists.
            return result.json()
        except ValueError:
            # For some requests command (eg. PUT), GoDaddy will not
            # return any JSON, just an HTTP status without body.
            return None 
Example #30
Source File: http_urllib3.py    From Mastering-Elasticsearch-7.0 with MIT License 4 votes vote down vote up
def perform_request(
        self, method, url, params=None, body=None, timeout=None, ignore=(), headers=None
    ):
        url = self.url_prefix + url
        if params:
            url = "%s?%s" % (url, urlencode(params))
        full_url = self.host + url

        start = time.time()
        try:
            kw = {}
            if timeout:
                kw["timeout"] = timeout

            # in python2 we need to make sure the url and method are not
            # unicode. Otherwise the body will be decoded into unicode too and
            # that will fail (#133, #201).
            if not isinstance(url, str):
                url = url.encode("utf-8")
            if not isinstance(method, str):
                method = method.encode("utf-8")

            request_headers = self.headers
            if headers:
                request_headers = request_headers.copy()
                request_headers.update(headers)
            if self.http_compress and body:
                try:
                    body = gzip.compress(body)
                except AttributeError:
                    # oops, Python2.7 doesn't have `gzip.compress` let's try
                    # again
                    body = gzip.zlib.compress(body)

            response = self.pool.urlopen(
                method, url, body, retries=Retry(False), headers=request_headers, **kw
            )
            duration = time.time() - start
            raw_data = response.data.decode("utf-8")
        except Exception as e:
            self.log_request_fail(
                method, full_url, url, body, time.time() - start, exception=e
            )
            if isinstance(e, UrllibSSLError):
                raise SSLError("N/A", str(e), e)
            if isinstance(e, ReadTimeoutError):
                raise ConnectionTimeout("TIMEOUT", str(e), e)
            raise ConnectionError("N/A", str(e), e)

        # raise errors based on http status codes, let the client handle those if needed
        if not (200 <= response.status < 300) and response.status not in ignore:
            self.log_request_fail(
                method, full_url, url, body, duration, response.status, raw_data
            )
            self._raise_error(response.status, raw_data)

        self.log_request_success(
            method, full_url, url, body, response.status, raw_data, duration
        )

        return response.status, response.getheaders(), raw_data