-
Notifications
You must be signed in to change notification settings - Fork 166
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
stop using requests #1586
Draft
evgeni
wants to merge
11
commits into
develop
Choose a base branch
from
fake-requests
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
stop using requests #1586
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ed0bead
stop using requests
evgeni a655e95
don't to_bytes username and password, it's done for us by the wrapper
evgeni 2c10c4c
move stuff to own file
evgeni 155a7ed
better error
evgeni 1f4d4df
move callback to fake requests
evgeni 00b2315
migrate inventory to fake requests
evgeni b11465b
no need for requests.exceptions
evgeni 29f8e3f
don't import requests
evgeni 0e6619b
drop requests from dependencies
evgeni 31c0ccf
make pylint happy (this needs a change to vendor.py)
evgeni a24d194
better error handing
evgeni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
python3-rpm [(platform:redhat platform:base-py3)] | ||
python38-requests [platform:centos-8 platform:rhel-8] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# pylint: disable=raise-missing-from | ||
# pylint: disable=super-with-arguments | ||
|
||
from __future__ import absolute_import, division, print_function | ||
__metaclass__ = type | ||
|
||
|
||
import json | ||
from ansible.module_utils._text import to_native | ||
from ansible.module_utils import six | ||
from ansible.module_utils.urls import Request | ||
try: | ||
from ansible.module_utils.urls import prepare_multipart | ||
except ImportError: | ||
def prepare_multipart(fields): | ||
raise NotImplementedError | ||
|
||
|
||
class RequestException(Exception): | ||
def __init__(self, msg, response): | ||
super(RequestException, self).__init__(msg) | ||
self.response = response | ||
|
||
|
||
class RequestResponse(object): | ||
def __init__(self, resp): | ||
self._resp = resp | ||
self._body = None | ||
|
||
@property | ||
def status_code(self): | ||
if hasattr(self._resp, 'status'): | ||
status = self._resp.status | ||
elif hasattr(self._resp, 'code'): | ||
status = self._resp.code | ||
else: | ||
status = self._resp.getcode() | ||
return status | ||
|
||
@property | ||
def headers(self): | ||
return self._resp.headers | ||
|
||
@property | ||
def url(self): | ||
if hasattr(self._resp, 'url'): | ||
url = self._resp.url | ||
elif hasattr(self._resp, 'geturl'): | ||
url = self._resp.geturl() | ||
else: | ||
url = "" | ||
return url | ||
|
||
@property | ||
def reason(self): | ||
if hasattr(self._resp, 'reason'): | ||
reason = self._resp.reason | ||
else: | ||
reason = "" | ||
return reason | ||
|
||
@property | ||
def body(self): | ||
if self._body is None: | ||
try: | ||
self._body = self._resp.read() | ||
except Exception: | ||
pass | ||
return self._body | ||
|
||
@property | ||
def text(self): | ||
return to_native(self.body) | ||
|
||
def raise_for_status(self): | ||
http_error_msg = "" | ||
|
||
if 400 <= self.status_code < 500: | ||
http_error_msg = "{0} Client Error: {1} for url: {2}".format(self.status_code, self.reason, self.url) | ||
elif 500 <= self.status_code < 600: | ||
http_error_msg = "{0} Server Error: {1} for url: {2}".format(self.status_code, self.reason, self.url) | ||
|
||
if http_error_msg: | ||
raise RequestException(http_error_msg, response=self) | ||
|
||
def json(self, **kwargs): | ||
return json.loads(to_native(self.body), **kwargs) | ||
|
||
|
||
class RequestSession(Request): | ||
@property | ||
def auth(self): | ||
return (self.url_username, self.url_password) | ||
|
||
@auth.setter | ||
def auth(self, value): | ||
self.url_username, self.url_password = value | ||
self.force_basic_auth = True | ||
|
||
@property | ||
def verify(self): | ||
return self.validate_certs | ||
|
||
@verify.setter | ||
def verify(self, value): | ||
self.validate_certs = value | ||
|
||
@property | ||
def cert(self): | ||
return (self.client_cert, self.client_key) | ||
|
||
@cert.setter | ||
def cert(self, value): | ||
self.client_cert, self.client_key = value | ||
|
||
def request(self, method, url, **kwargs): | ||
validate_certs = kwargs.pop('verify', None) | ||
params = kwargs.pop('params', None) | ||
if params: | ||
url += '?' + six.moves.urllib.parse.urlencode(params) | ||
headers = kwargs.pop('headers', None) | ||
data = kwargs.pop('data', None) | ||
if data: | ||
if not isinstance(data, six.string_types): | ||
data = six.moves.urllib.parse.urlencode(data, doseq=True) | ||
files = kwargs.pop('files', None) | ||
if files: | ||
ansible_files = {k: {'filename': v[0], 'content': v[1].read(), 'mime_type': v[2]} for (k, v) in files.items()} | ||
_content_type, data = prepare_multipart(ansible_files) | ||
if 'json' in kwargs: | ||
# it can be `json=None`… | ||
data = json.dumps(kwargs.pop('json')) | ||
if headers is None: | ||
headers = {} | ||
headers['Content-Type'] = 'application/json' | ||
try: | ||
result = self.open(method, url, validate_certs=validate_certs, data=data, headers=headers, **kwargs) | ||
return RequestResponse(result) | ||
except six.moves.urllib.error.HTTPError as e: | ||
return RequestResponse(e) | ||
|
||
def get(self, url, **kwargs): | ||
return self.request('GET', url, **kwargs) | ||
|
||
def post(self, url, data=None, json=None, **kwargs): | ||
return self.request('POST', url, data=data, json=json, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
requests>=2.4.2 | ||
PyYAML |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was done as part of #609, but it seems ansible's
Request
does that for us instead.