Skip to content

Commit 0ae5e51

Browse files
kannappanrdeekoder
authored andcommitted
Allow users to use custom http client in the constructor (#589)
Minio constructor has been modified to take a custom http client from the user. This prevents the constructor from having too many options. The next version has been bumped to 3.0.0, since this change breaks backward compatibility. Fixes #588
1 parent 6809787 commit 0ae5e51

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

docs/API.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ s3Client = Minio('s3.amazonaws.com',
4444
## 1. Constructor
4545

4646
<a name="Minio"></a>
47-
### Minio(endpoint, access_key=None, secret_key=None, secure=True)
47+
### Minio(endpoint, access_key=None, secret_key=None, secure=True, region=None, http_client=None)
4848

4949
| |
5050
|---|
51-
| `Minio(endpoint, access_key=None, secret_key=None, secure=True, region=None, timeout=None)` |
51+
| `Minio(endpoint, access_key=None, secret_key=None, secure=True, region=None, http_client=None)` |
5252
| Initializes a new client object. |
5353

5454
__Parameters__
@@ -61,7 +61,7 @@ __Parameters__
6161
| `secret_key` | _string_ | Secret key for the object storage endpoint. (Optional if you need anonymous access). |
6262
| `secure` |_bool_ | Set this value to `True` to enable secure (HTTPS) access. (Optional defaults to `True`). |
6363
| `region` |_string_ | Set this value to override automatic bucket location discovery. (Optional defaults to `None`). |
64-
| `timeout` |_float_ | Set this value to control how long requests are allowed to run before being aborted. (Optional defaults to `None`) |
64+
| `http_client` |_urllib3.poolmanager.PoolManager_ | Set this value to use custom http client instead of using default http client. (Optional defaults to `None`) |
6565

6666
__Example__
6767

@@ -76,6 +76,28 @@ minioClient = Minio('play.minio.io:9000',
7676
secret_key='zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG')
7777
```
7878

79+
```py
80+
from minio import Minio
81+
from minio.error import ResponseError
82+
import urllib3
83+
84+
httpClient = urllib3.ProxyManager(
85+
'https://proxy_host.sampledomain.com:8119/'
86+
timeout=urllib3.Timeout.DEFAULT_TIMEOUT,
87+
cert_reqs='CERT_REQUIRED',
88+
retries=urllib3.Retry(
89+
total=5,
90+
backoff_factor=0.2,
91+
status_forcelist=[500, 502, 503, 504]
92+
)
93+
)
94+
minioClient = Minio('your_hostname.sampledomain.com:9000',
95+
access_key='ACCESS_KEY',
96+
secret_key='SECRET_KEY',
97+
secure=True,
98+
http_client=httpClient)
99+
```
100+
79101
### AWS S3
80102

81103
```py

minio/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
__title__ = 'minio-py'
3131
__author__ = 'Minio, Inc.'
32-
__version__ = '2.2.7'
32+
__version__ = '3.0.0'
3333
__license__ = 'Apache 2.0'
3434
__copyright__ = 'Copyright 2015, 2016, 2017 Minio, Inc.'
3535

minio/api.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,19 @@ class Minio(object):
125125
are allowed to run before being aborted.
126126
:return: :class:`Minio <Minio>` object
127127
"""
128-
129128
def __init__(self, endpoint, access_key=None,
130129
secret_key=None, secure=True,
131130
region=None,
132-
timeout=None,
133-
certificate_bundle=certifi.where()):
131+
http_client=None):
134132

135133
# Validate endpoint.
136134
is_valid_endpoint(endpoint)
137135

136+
# Validate http client has correct base class.
137+
if http_client and not isinstance(http_client, urllib3.poolmanager.PoolManager):
138+
raise InvalidArgumentError('HTTP client should be of instance `urllib3.poolmanager.PoolManager`')
139+
140+
138141
# Default is a secured connection.
139142
endpoint_url = 'https://' + endpoint
140143
if not secure:
@@ -156,19 +159,19 @@ def __init__(self, endpoint, access_key=None,
156159
self._user_agent = _DEFAULT_USER_AGENT
157160
self._trace_output_stream = None
158161

159-
self._conn_timeout = urllib3.Timeout.DEFAULT_TIMEOUT if not timeout \
160-
else urllib3.Timeout(timeout)
161-
162-
self._http = urllib3.PoolManager(
163-
timeout=self._conn_timeout,
164-
cert_reqs='CERT_REQUIRED',
165-
ca_certs=certificate_bundle,
166-
retries=urllib3.Retry(
167-
total=5,
168-
backoff_factor=0.2,
169-
status_forcelist=[500, 502, 503, 504]
162+
if not http_client:
163+
self._http = urllib3.PoolManager(
164+
timeout=urllib3.Timeout.DEFAULT_TIMEOUT,
165+
cert_reqs='CERT_REQUIRED',
166+
ca_certs=certifi.where(),
167+
retries=urllib3.Retry(
168+
total=5,
169+
backoff_factor=0.2,
170+
status_forcelist=[500, 502, 503, 504]
171+
)
170172
)
171-
)
173+
else:
174+
self._http = http_client
172175

173176
# Set application information.
174177
def set_app_info(self, app_name, app_version):

0 commit comments

Comments
 (0)