Skip to content
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

wip implementation of --base-url argument #1377

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
30 changes: 2 additions & 28 deletions httpie/cli/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ def _process_request_type(self):
}

def _process_url(self):
self.args.url = self.args.base_url + self.args.url

if self.args.url.startswith('://'):
# Paste URL & add space shortcut: `http ://pie.dev` → `http://pie.dev`
self.args.url = self.args.url[3:]
Expand Down Expand Up @@ -412,39 +414,11 @@ def _guess_method(self):

"""
if self.args.method is None:
# Invoked as `http URL'.
assert not self.args.request_items
if self.has_input_data:
self.args.method = HTTP_POST
else:
self.args.method = HTTP_GET

# FIXME: False positive, e.g., "localhost" matches but is a valid URL.
elif not re.match('^[a-zA-Z]+$', self.args.method):
# Invoked as `http URL item+'. The URL is now in `args.method`
# and the first ITEM is now incorrectly in `args.url`.
try:
# Parse the URL as an ITEM and store it as the first ITEM arg.
self.args.request_items.insert(0, KeyValueArgType(
*SEPARATOR_GROUP_ALL_ITEMS).__call__(self.args.url))

except argparse.ArgumentTypeError as e:
if self.args.traceback:
raise
self.error(e.args[0])

else:
# Set the URL correctly
self.args.url = self.args.method
# Infer the method
has_data = (
self.has_input_data
or any(
item.sep in SEPARATOR_GROUP_DATA_ITEMS
for item in self.args.request_items)
)
self.args.method = HTTP_POST if has_data else HTTP_GET

def _parse_items(self):
"""
Parse `args.request_items` into `args.headers`, `args.data`,
Expand Down
47 changes: 29 additions & 18 deletions httpie/cli/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,6 @@
Only URL is required.
""",
)

positional_arguments.add_argument(
dest='method',
metavar='METHOD',
nargs=Qualifiers.OPTIONAL,
default=None,
short_help='The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).',
help="""
The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).

This argument can be omitted in which case HTTPie will use POST if there
is some data to be sent, otherwise GET:

$ http example.org # => GET
$ http example.org hello=world # => POST

""",
)
positional_arguments.add_argument(
dest='url',
metavar='URL',
Expand All @@ -78,6 +60,8 @@
$ http :3000 # => http://localhost:3000
$ http :/foo # => http://localhost/foo

Prefixed with --base-url before default scheme or shorthand processing take place.

""",
)
positional_arguments.add_argument(
Expand Down Expand Up @@ -697,12 +681,39 @@ def format_auth_help(auth_plugins_mapping, *, isolation_mode: bool = False):

network = options.add_group('Network')

network.add_argument(
'--method',
'-X',
metavar='METHOD',
default=None,
short_help='The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).',
help="""
The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).

This argument can be omitted in which case HTTPie will use POST if there
is some data to be sent, otherwise GET:

$ http example.org # => GET
$ http example.org hello=world # => POST

""",
)
network.add_argument(
'--offline',
default=False,
action='store_true',
short_help='Build the request and print it but don’t actually send it.'
)
network.add_argument(
'--base-url',
default='',
short_help='String to prepend to the request URL.',
help="""
String to prepend to the request URL before --default-scheme and/or localhost
shorthand are processed. If specified multiple times, last value is used.

"""
)
network.add_argument(
'--proxy',
default=[],
Expand Down
5 changes: 5 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,8 @@ def test_default_scheme_option(self, httpbin_secure):
def test_scheme_when_invoked_as_https(self, httpbin_secure):
url = f'{httpbin_secure.host}:{httpbin_secure.port}'
assert HTTP_OK in http(url, program_name='https')


class TestBaseUrl:
def test_base_url(self):
assert False, 'Needs tests'