|
17 | 17 | instance. |
18 | 18 | """ |
19 | 19 | import json |
| 20 | +from ._2to3 import url_parse |
20 | 21 |
|
21 | 22 | from ._client_session import ( |
22 | 23 | BasicSession, |
@@ -93,6 +94,20 @@ def __init__(self, user, auth_token, admin_party=False, **kwargs): |
93 | 94 | self._auto_renew = kwargs.get('auto_renew', False) |
94 | 95 | self._use_basic_auth = kwargs.get('use_basic_auth', False) |
95 | 96 | self._use_iam = kwargs.get('use_iam', False) |
| 97 | + # If user/pass exist in URL, remove and set variables |
| 98 | + if not self._use_basic_auth and self.server_url: |
| 99 | + parsed_url = url_parse(kwargs.get('url')) |
| 100 | + # Note: To prevent conflicts with field names, the method |
| 101 | + # and attribute names of `url_parse` start with an underscore |
| 102 | + if parsed_url.port is None: |
| 103 | + self.server_url = parsed_url._replace( |
| 104 | + netloc="{}".format(parsed_url.hostname)).geturl() |
| 105 | + else: |
| 106 | + self.server_url = parsed_url._replace( |
| 107 | + netloc="{}:{}".format(parsed_url.hostname, parsed_url.port)).geturl() |
| 108 | + if (not user and not auth_token) and (parsed_url.username and parsed_url.password): |
| 109 | + self._user = parsed_url.username |
| 110 | + self._auth_token = parsed_url.password |
96 | 111 |
|
97 | 112 | connect_to_couch = kwargs.get('connect', False) |
98 | 113 | if connect_to_couch and self._DATABASE_CLASS == CouchDatabase: |
@@ -450,14 +465,10 @@ def __init__(self, cloudant_user, auth_token, **kwargs): |
450 | 465 | super(Cloudant, self).__init__(cloudant_user, auth_token, **kwargs) |
451 | 466 | self._client_user_header = {'User-Agent': USER_AGENT} |
452 | 467 | account = kwargs.get('account') |
453 | | - url = kwargs.get('url') |
454 | | - x_cloudant_user = kwargs.get('x_cloudant_user') |
455 | 468 | if account is not None: |
456 | 469 | self.server_url = 'https://{0}.cloudant.com'.format(account) |
457 | | - elif kwargs.get('url') is not None: |
458 | | - self.server_url = url |
459 | | - if x_cloudant_user is not None: |
460 | | - self._client_user_header['X-Cloudant-User'] = x_cloudant_user |
| 470 | + if kwargs.get('x_cloudant_user') is not None: |
| 471 | + self._client_user_header['X-Cloudant-User'] = kwargs.get('x_cloudant_user') |
461 | 472 |
|
462 | 473 | if self.server_url is None: |
463 | 474 | raise CloudantClientException(102) |
|
0 commit comments