2021-10-07 14:41:03 +02:00
|
|
|
|
from typing import Tuple
|
2021-10-06 19:24:10 +02:00
|
|
|
|
|
2013-09-21 23:46:15 +02:00
|
|
|
|
|
2021-10-06 19:24:10 +02:00
|
|
|
|
class BasePlugin:
|
2013-09-21 23:46:15 +02:00
|
|
|
|
# The name of the plugin, eg. "My auth".
|
|
|
|
|
name = None
|
|
|
|
|
|
2020-05-26 10:07:53 +02:00
|
|
|
|
# Optional short description. It will be shown in the help
|
2013-09-21 23:46:15 +02:00
|
|
|
|
# under --auth-type.
|
|
|
|
|
description = None
|
|
|
|
|
|
|
|
|
|
# This be set automatically once the plugin has been loaded.
|
|
|
|
|
package_name = None
|
|
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
|
|
|
|
|
|
class AuthPlugin(BasePlugin):
|
|
|
|
|
"""
|
|
|
|
|
Base auth plugin class.
|
|
|
|
|
|
2020-05-26 10:07:53 +02:00
|
|
|
|
See httpie-ntlm for an example auth plugin:
|
|
|
|
|
|
|
|
|
|
<https://github.com/httpie/httpie-ntlm>
|
2014-04-28 23:33:30 +02:00
|
|
|
|
|
2016-11-23 22:01:58 +01:00
|
|
|
|
See also `test_auth_plugins.py`
|
|
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
|
"""
|
|
|
|
|
# The value that should be passed to --auth-type
|
|
|
|
|
# to use this auth plugin. Eg. "my-auth"
|
|
|
|
|
auth_type = None
|
|
|
|
|
|
2016-11-23 22:01:58 +01:00
|
|
|
|
# Set to `False` to make it possible to invoke this auth
|
|
|
|
|
# plugin without requiring the user to specify credentials
|
|
|
|
|
# through `--auth, -a`.
|
|
|
|
|
auth_require = True
|
|
|
|
|
|
|
|
|
|
# By default the `-a` argument is parsed for `username:password`.
|
|
|
|
|
# Set this to `False` to disable the parsing and error handling.
|
|
|
|
|
auth_parse = True
|
|
|
|
|
|
2020-06-16 11:05:00 +02:00
|
|
|
|
# Set to `True` to make it possible for this auth
|
|
|
|
|
# plugin to acquire credentials from the user’s netrc file(s).
|
|
|
|
|
# It is used as a fallback when the credentials are not provided explicitly
|
|
|
|
|
# through `--auth, -a`. Enabling this will allow skipping `--auth, -a`
|
|
|
|
|
# even when `auth_require` is set `True` (provided that netrc provides
|
|
|
|
|
# credential for a given host).
|
|
|
|
|
netrc_parse = False
|
|
|
|
|
|
2016-11-23 22:01:58 +01:00
|
|
|
|
# If both `auth_parse` and `prompt_password` are set to `True`,
|
|
|
|
|
# and the value of `-a` lacks the password part,
|
|
|
|
|
# then the user will be prompted to type the password in.
|
|
|
|
|
prompt_password = True
|
|
|
|
|
|
|
|
|
|
# Will be set to the raw value of `-a` (if provided) before
|
2020-06-16 11:05:00 +02:00
|
|
|
|
# `get_auth()` gets called. If the credentials came from a netrc file,
|
|
|
|
|
# then this is `None`.
|
2016-11-23 22:01:58 +01:00
|
|
|
|
raw_auth = None
|
|
|
|
|
|
2021-10-06 19:24:10 +02:00
|
|
|
|
def get_auth(self, username: str = None, password: str = None):
|
2013-09-21 23:46:15 +02:00
|
|
|
|
"""
|
2016-11-23 22:01:58 +01:00
|
|
|
|
If `auth_parse` is set to `True`, then `username`
|
|
|
|
|
and `password` contain the parsed credentials.
|
|
|
|
|
|
2016-11-23 22:29:36 +01:00
|
|
|
|
Use `self.raw_auth` to access the raw value passed through
|
|
|
|
|
`--auth, -a`.
|
|
|
|
|
|
2013-09-21 23:46:15 +02:00
|
|
|
|
Return a ``requests.auth.AuthBase`` subclass instance.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
raise NotImplementedError()
|
2014-04-28 23:33:30 +02:00
|
|
|
|
|
|
|
|
|
|
2015-02-05 15:25:00 +01:00
|
|
|
|
class TransportPlugin(BasePlugin):
|
|
|
|
|
"""
|
2020-05-26 10:07:53 +02:00
|
|
|
|
Requests transport adapter docs:
|
|
|
|
|
|
|
|
|
|
<https://requests.readthedocs.io/en/latest/user/advanced/#transport-adapters>
|
|
|
|
|
|
|
|
|
|
See httpie-unixsocket for an example transport plugin:
|
2015-02-05 15:25:00 +01:00
|
|
|
|
|
2020-05-26 10:07:53 +02:00
|
|
|
|
<https://github.com/httpie/httpie-unixsocket>
|
2015-02-05 15:25:00 +01:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# The URL prefix the adapter should be mount to.
|
|
|
|
|
prefix = None
|
|
|
|
|
|
|
|
|
|
def get_adapter(self):
|
|
|
|
|
"""
|
|
|
|
|
Return a ``requests.adapters.BaseAdapter`` subclass instance to be
|
|
|
|
|
mounted to ``self.prefix``.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
2019-08-31 18:33:54 +02:00
|
|
|
|
class ConverterPlugin(BasePlugin):
|
2020-05-26 10:07:53 +02:00
|
|
|
|
"""
|
2021-10-06 19:31:43 +02:00
|
|
|
|
Possibly converts binary response data for prettified terminal display.
|
2020-05-26 10:07:53 +02:00
|
|
|
|
|
|
|
|
|
See httpie-msgpack for an example converter plugin:
|
|
|
|
|
|
|
|
|
|
<https://github.com/rasky/httpie-msgpack>.
|
|
|
|
|
|
|
|
|
|
"""
|
2014-04-28 23:33:30 +02:00
|
|
|
|
|
2021-10-06 19:24:10 +02:00
|
|
|
|
def __init__(self, mime: str):
|
2014-04-28 23:33:30 +02:00
|
|
|
|
self.mime = mime
|
|
|
|
|
|
2021-10-06 19:28:21 +02:00
|
|
|
|
def convert(self, body: bytes) -> Tuple[str, str]:
|
2021-10-06 19:24:10 +02:00
|
|
|
|
"""
|
2021-10-06 19:31:43 +02:00
|
|
|
|
Convert a binary body to a textual representation for the terminal
|
|
|
|
|
and return a tuple containing the new Content-Type and content, e.g.:
|
|
|
|
|
|
2021-10-06 19:24:10 +02:00
|
|
|
|
('application/json', '{}')
|
|
|
|
|
|
|
|
|
|
"""
|
2014-04-28 23:33:30 +02:00
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2021-10-06 19:24:10 +02:00
|
|
|
|
def supports(cls, mime: str) -> bool:
|
2014-04-28 23:33:30 +02:00
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
2019-08-31 18:33:54 +02:00
|
|
|
|
class FormatterPlugin(BasePlugin):
|
2020-05-26 10:07:53 +02:00
|
|
|
|
"""
|
|
|
|
|
Possibly formats response body & headers for prettified terminal display.
|
|
|
|
|
|
|
|
|
|
"""
|
2019-09-01 11:45:47 +02:00
|
|
|
|
group_name = 'format'
|
2014-04-28 23:33:30 +02:00
|
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
:param env: an class:`Environment` instance
|
|
|
|
|
:param kwargs: additional keyword argument that some
|
2020-05-26 10:07:53 +02:00
|
|
|
|
formatters might require.
|
2014-04-28 23:33:30 +02:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
self.enabled = True
|
|
|
|
|
self.kwargs = kwargs
|
2020-05-27 15:58:15 +02:00
|
|
|
|
self.format_options = kwargs['format_options']
|
2014-04-28 23:33:30 +02:00
|
|
|
|
|
2019-08-31 18:33:54 +02:00
|
|
|
|
def format_headers(self, headers: str) -> str:
|
2014-04-28 23:33:30 +02:00
|
|
|
|
"""Return processed `headers`
|
|
|
|
|
|
|
|
|
|
:param headers: The headers as text.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return headers
|
|
|
|
|
|
2019-08-31 18:33:54 +02:00
|
|
|
|
def format_body(self, content: str, mime: str) -> str:
|
2014-04-28 23:33:30 +02:00
|
|
|
|
"""Return processed `content`.
|
|
|
|
|
|
|
|
|
|
:param mime: E.g., 'application/atom+xml'.
|
|
|
|
|
:param content: The body content as text
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return content
|
2021-12-23 21:13:25 +01:00
|
|
|
|
|
|
|
|
|
def format_metadata(self, metadata: str) -> str:
|
|
|
|
|
"""Return processed `metadata`.
|
|
|
|
|
|
|
|
|
|
:param metadata: The metadata as text.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return metadata
|