Skip to content

http_config support to token manager and update ssl error #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
[![Build Status](https://travis-ci.com/IBM/python-sdk-core.svg?branch=master)](https://travis-ci.com/IBM/python-sdk-core)
[![codecov](https://codecov.io/gh/IBM/python-sdk-core/branch/master/graph/badge.svg)](https://codecov.io/gh/IBM/python-sdk-core)
[![Latest Stable Version](https://img.shields.io/pypi/v/ibm-cloud-sdk-core.svg)](https://pypi.python.org/pypi/ibm-cloud-sdk-core)
[![CLA assistant](https://cla-assistant.io/readme/badge/ibm/python-sdk-core)](https://cla-assistant.io/ibm/python-sdk-core)

# python-sdk-core
This project contains the core functionality used by Python SDK's generated by the IBM OpenAPI 3 SDK Generator (openapi-sdkgen).
Python code generated by openapi-sdkgen will depend on the function contained in this project.

# Notice
Support for Python versions 2.x and versions <= 3.4 is deprecated and will be officially dropped in the next major release, which is expected to be end of September, 2019.

## Installation

To install, use `pip` or `easy_install`:
Expand Down
11 changes: 7 additions & 4 deletions ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
import requests
from requests.structures import CaseInsensitiveDict
from .version import __version__
from .utils import has_bad_first_or_last_char, remove_null_values, cleanup_values, read_external_sources
from .utils import has_bad_first_or_last_char, remove_null_values, cleanup_values
from .detailed_response import DetailedResponse
from .api_exception import ApiException
from .authenticators import Authenticator
from .jwt_token_manager import JWTTokenManager
from http.cookiejar import CookieJar
import logging

Expand All @@ -36,9 +37,9 @@
class BaseService(object):

SDK_NAME = 'ibm-python-sdk-core'
ERROR_MSG_DISABLE_SSL = 'If you\'re trying to call a service on ICP or Cloud Pak for Data, you may not have a valid SSL certificate. '\
'If you need to access the service without setting that up, try using the disable_ssl_verification option in your authentication '\
'configuration and/or setting set_disable_ssl_verification(True) on your service.'
ERROR_MSG_DISABLE_SSL = 'The connection failed because the SSL certificate is not valid. To use a self-signed certificate, '\
'disable verification of the server\'s SSL certificate by invoking the set_disable_ssl_verification(True) '\
'on your service instance and/ or use the disable_ssl_verification option of the authenticator.'

def __init__(self,
service_url=None,
Expand Down Expand Up @@ -84,6 +85,8 @@ def set_http_config(self, http_config):
"""
if isinstance(http_config, dict):
self.http_config = http_config
if self.authenticator and hasattr(self.authenticator, 'token_manager') and isinstance(self.authenticator.token_manager, JWTTokenManager):
self.authenticator.token_manager.http_config = http_config
else:
raise TypeError("http_config parameter must be a dictionary")

Expand Down
4 changes: 4 additions & 0 deletions ibm_cloud_sdk_core/jwt_token_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, url, disable_ssl_verification=False, token_name=None):
self.token_name = token_name
self.token_info = {}
self.time_for_new_token = None
self.http_config = {}

def get_token(self):
"""
Expand Down Expand Up @@ -118,6 +119,9 @@ def _request(self,
data=None,
auth_tuple=None,
**kwargs):
kwargs = dict({"timeout": 60}, **kwargs)
kwargs = dict(kwargs, **self.http_config)

if self.disable_ssl_verification:
kwargs['verify'] = False

Expand Down
18 changes: 18 additions & 0 deletions test/test_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,21 @@ def test_service_url_not_set():
with pytest.raises(ValueError) as err:
service.prepare_request('POST', url='')
assert str(err.value) == 'The service_url is required'


def test_setting_proxy():
service = BaseService('test', authenticator=IAMAuthenticator('wonder woman'))
assert service.authenticator is not None
assert service.authenticator.token_manager.http_config == {}

http_config = {
"proxies": {
"http": "user:password@host:port"
}
}
service.set_http_config(http_config)
assert service.authenticator.token_manager.http_config == http_config

service2 = BaseService('test', authenticator=BasicAuthenticator('marvellous', 'mrs maisel'))
service2.set_http_config(http_config)
assert service2.authenticator is not None