2012-08-17 23:23:02 +02:00
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
from pprint import pformat
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2012-08-18 23:03:31 +02:00
|
|
|
from . import sessions
|
|
|
|
from . import __version__
|
2013-09-21 23:46:15 +02:00
|
|
|
from .plugins import plugin_manager
|
2012-08-17 23:23:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
FORM = 'application/x-www-form-urlencoded; charset=utf-8'
|
|
|
|
JSON = 'application/json; charset=utf-8'
|
2012-08-18 23:03:31 +02:00
|
|
|
DEFAULT_UA = 'HTTPie/%s' % __version__
|
2012-08-17 23:23:02 +02:00
|
|
|
|
|
|
|
|
2012-09-17 02:15:00 +02:00
|
|
|
def get_response(args, config_dir):
|
2012-09-07 12:38:52 +02:00
|
|
|
"""Send the request and return a `request.Response`."""
|
2012-08-17 23:23:02 +02:00
|
|
|
|
|
|
|
requests_kwargs = get_requests_kwargs(args)
|
|
|
|
|
|
|
|
if args.debug:
|
2012-12-05 04:39:56 +01:00
|
|
|
sys.stderr.write('\n>>> requests.request(%s)\n\n'
|
|
|
|
% pformat(requests_kwargs))
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2012-09-17 00:37:36 +02:00
|
|
|
if not args.session and not args.session_read_only:
|
2012-12-01 15:55:58 +01:00
|
|
|
response = requests.request(**requests_kwargs)
|
2012-09-07 12:38:52 +02:00
|
|
|
else:
|
2012-12-01 15:55:58 +01:00
|
|
|
response = sessions.get_response(
|
2013-09-21 23:46:15 +02:00
|
|
|
args=args,
|
2012-09-17 02:15:00 +02:00
|
|
|
config_dir=config_dir,
|
2013-05-13 14:47:44 +02:00
|
|
|
session_name=args.session or args.session_read_only,
|
2013-05-13 11:54:49 +02:00
|
|
|
requests_kwargs=requests_kwargs,
|
2012-09-17 00:01:49 +02:00
|
|
|
read_only=bool(args.session_read_only),
|
2012-09-07 12:38:52 +02:00
|
|
|
)
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2012-12-01 15:55:58 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2012-08-17 23:23:02 +02:00
|
|
|
def get_requests_kwargs(args):
|
2012-09-07 12:38:52 +02:00
|
|
|
"""Translate our `args` into `requests.request` keyword arguments."""
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2012-12-17 17:02:27 +01:00
|
|
|
implicit_headers = {
|
|
|
|
'User-Agent': DEFAULT_UA
|
|
|
|
}
|
2012-08-18 23:03:31 +02:00
|
|
|
|
2012-08-17 23:23:02 +02:00
|
|
|
auto_json = args.data and not args.form
|
2013-04-12 20:27:26 +02:00
|
|
|
# FIXME: Accept is set to JSON with `http url @./file.txt`.
|
2012-08-17 23:23:02 +02:00
|
|
|
if args.json or auto_json:
|
2012-12-17 17:02:27 +01:00
|
|
|
implicit_headers['Accept'] = 'application/json'
|
2013-04-02 16:07:14 +02:00
|
|
|
if args.json or (auto_json and args.data):
|
2012-12-17 17:02:27 +01:00
|
|
|
implicit_headers['Content-Type'] = JSON
|
2012-08-17 23:23:02 +02:00
|
|
|
|
|
|
|
if isinstance(args.data, dict):
|
2012-12-01 15:55:58 +01:00
|
|
|
if args.data:
|
|
|
|
args.data = json.dumps(args.data)
|
|
|
|
else:
|
|
|
|
# We need to set data to an empty string to prevent requests
|
|
|
|
# from assigning an empty list to `response.request.data`.
|
|
|
|
args.data = ''
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2012-08-18 23:03:31 +02:00
|
|
|
elif args.form and not args.files:
|
2012-09-07 12:48:59 +02:00
|
|
|
# If sending files, `requests` will set
|
|
|
|
# the `Content-Type` for us.
|
2012-12-17 17:02:27 +01:00
|
|
|
implicit_headers['Content-Type'] = FORM
|
|
|
|
|
|
|
|
for name, value in implicit_headers.items():
|
|
|
|
if name not in args.headers:
|
|
|
|
args.headers[name] = value
|
2012-08-17 23:23:02 +02:00
|
|
|
|
|
|
|
credentials = None
|
|
|
|
if args.auth:
|
2013-09-21 23:46:15 +02:00
|
|
|
auth_plugin = plugin_manager.get_auth_plugin(args.auth_type)()
|
|
|
|
credentials = auth_plugin.get_auth(args.auth.key, args.auth.value)
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2014-01-28 16:16:48 +01:00
|
|
|
cert = None
|
2014-02-05 12:50:40 +01:00
|
|
|
if args.cert:
|
|
|
|
cert = args.cert
|
|
|
|
if args.certkey:
|
|
|
|
cert = (cert, args.certkey)
|
2014-01-28 16:16:48 +01:00
|
|
|
|
2012-08-17 23:23:02 +02:00
|
|
|
kwargs = {
|
2012-12-17 17:02:27 +01:00
|
|
|
'stream': True,
|
2012-08-17 23:23:02 +02:00
|
|
|
'method': args.method.lower(),
|
|
|
|
'url': args.url,
|
|
|
|
'headers': args.headers,
|
|
|
|
'data': args.data,
|
|
|
|
'verify': {
|
|
|
|
'yes': True,
|
|
|
|
'no': False
|
2012-08-21 15:45:22 +02:00
|
|
|
}.get(args.verify, args.verify),
|
2014-01-28 16:16:48 +01:00
|
|
|
'cert': cert,
|
2012-08-17 23:23:02 +02:00
|
|
|
'timeout': args.timeout,
|
|
|
|
'auth': credentials,
|
|
|
|
'proxies': dict((p.key, p.value) for p in args.proxy),
|
|
|
|
'files': args.files,
|
2012-09-07 11:58:39 +02:00
|
|
|
'allow_redirects': args.follow,
|
2012-08-18 23:03:31 +02:00
|
|
|
'params': args.params,
|
2012-08-17 23:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return kwargs
|