You've already forked httpie-cli
mirror of
https://github.com/httpie/cli.git
synced 2025-08-10 22:42:05 +02:00
Converted built-in formatters to formatter plugins.
Still work in progress and the API should be considered private for now.
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
from .base import AuthPlugin
|
||||
from .manager import PluginManager
|
||||
from .builtin import BasicAuthPlugin, DigestAuthPlugin
|
||||
from httpie.plugins.base import AuthPlugin, FormatterPlugin, ConverterPlugin
|
||||
from httpie.plugins.manager import PluginManager
|
||||
from httpie.plugins.builtin import BasicAuthPlugin, DigestAuthPlugin
|
||||
from httpie.output.formatters.headers import HeadersFormatter
|
||||
from httpie.output.formatters.json import JSONFormatter
|
||||
from httpie.output.formatters.xml import XMLFormatter
|
||||
from httpie.output.formatters.colors import ColorFormatter
|
||||
|
||||
|
||||
plugin_manager = PluginManager()
|
||||
plugin_manager.register(BasicAuthPlugin)
|
||||
plugin_manager.register(DigestAuthPlugin)
|
||||
|
||||
plugin_manager.register(BasicAuthPlugin,
|
||||
DigestAuthPlugin)
|
||||
plugin_manager.register(HeadersFormatter,
|
||||
JSONFormatter,
|
||||
XMLFormatter,
|
||||
ColorFormatter)
|
||||
|
@@ -1,14 +1,4 @@
|
||||
class AuthPlugin(object):
|
||||
"""
|
||||
Base auth plugin class.
|
||||
|
||||
See <https://github.com/jakubroztocil/httpie-ntlm> for an example auth plugin.
|
||||
|
||||
"""
|
||||
|
||||
# The value that should be passed to --auth-type
|
||||
# to use this auth plugin. Eg. "my-auth"
|
||||
auth_type = None
|
||||
class BasePlugin(object):
|
||||
|
||||
# The name of the plugin, eg. "My auth".
|
||||
name = None
|
||||
@@ -20,9 +10,64 @@ class AuthPlugin(object):
|
||||
# This be set automatically once the plugin has been loaded.
|
||||
package_name = None
|
||||
|
||||
|
||||
class AuthPlugin(BasePlugin):
|
||||
"""
|
||||
Base auth plugin class.
|
||||
|
||||
See <https://github.com/jkbr/httpie-ntlm> for an example auth plugin.
|
||||
|
||||
"""
|
||||
# The value that should be passed to --auth-type
|
||||
# to use this auth plugin. Eg. "my-auth"
|
||||
auth_type = None
|
||||
|
||||
def get_auth(self, username, password):
|
||||
"""
|
||||
Return a ``requests.auth.AuthBase`` subclass instance.
|
||||
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class ConverterPlugin(object):
|
||||
|
||||
def __init__(self, mime):
|
||||
self.mime = mime
|
||||
|
||||
def convert(self, content_bytes):
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def supports(cls, mime):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class FormatterPlugin(object):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
"""
|
||||
:param env: an class:`Environment` instance
|
||||
:param kwargs: additional keyword argument that some
|
||||
processor might require.
|
||||
|
||||
"""
|
||||
self.enabled = True
|
||||
self.kwargs = kwargs
|
||||
|
||||
def format_headers(self, headers):
|
||||
"""Return processed `headers`
|
||||
|
||||
:param headers: The headers as text.
|
||||
|
||||
"""
|
||||
return headers
|
||||
|
||||
def format_body(self, content, mime):
|
||||
"""Return processed `content`.
|
||||
|
||||
:param mime: E.g., 'application/atom+xml'.
|
||||
:param content: The body content as text
|
||||
|
||||
"""
|
||||
return content
|
||||
|
@@ -2,7 +2,7 @@ from base64 import b64encode
|
||||
|
||||
import requests.auth
|
||||
|
||||
from .base import AuthPlugin
|
||||
from httpie.plugins.base import AuthPlugin
|
||||
|
||||
|
||||
class BuiltinAuthPlugin(AuthPlugin):
|
||||
|
@@ -1,8 +1,12 @@
|
||||
from itertools import groupby
|
||||
from pkg_resources import iter_entry_points
|
||||
from httpie.plugins import AuthPlugin, FormatterPlugin, ConverterPlugin
|
||||
|
||||
|
||||
ENTRY_POINT_NAMES = [
|
||||
'httpie.plugins.auth.v1'
|
||||
'httpie.plugins.auth.v1',
|
||||
'httpie.plugins.formatter.v1',
|
||||
'httpie.plugins.converter.v1',
|
||||
]
|
||||
|
||||
|
||||
@@ -14,22 +18,41 @@ class PluginManager(object):
|
||||
def __iter__(self):
|
||||
return iter(self._plugins)
|
||||
|
||||
def register(self, plugin):
|
||||
self._plugins.append(plugin)
|
||||
|
||||
def get_auth_plugins(self):
|
||||
return list(self._plugins)
|
||||
|
||||
def get_auth_plugin_mapping(self):
|
||||
return dict((plugin.auth_type, plugin) for plugin in self)
|
||||
|
||||
def get_auth_plugin(self, auth_type):
|
||||
return self.get_auth_plugin_mapping()[auth_type]
|
||||
def register(self, *plugins):
|
||||
for plugin in plugins:
|
||||
self._plugins.append(plugin)
|
||||
|
||||
def load_installed_plugins(self):
|
||||
|
||||
for entry_point_name in ENTRY_POINT_NAMES:
|
||||
for entry_point in iter_entry_points(entry_point_name):
|
||||
plugin = entry_point.load()
|
||||
plugin.package_name = entry_point.dist.key
|
||||
self.register(entry_point.load())
|
||||
|
||||
# Auth
|
||||
def get_auth_plugins(self):
|
||||
return [plugin for plugin in self if issubclass(plugin, AuthPlugin)]
|
||||
|
||||
def get_auth_plugin_mapping(self):
|
||||
return dict((plugin.auth_type, plugin)
|
||||
for plugin in self.get_auth_plugins())
|
||||
|
||||
def get_auth_plugin(self, auth_type):
|
||||
return self.get_auth_plugin_mapping()[auth_type]
|
||||
|
||||
# Output processing
|
||||
def get_formatters(self):
|
||||
return [plugin for plugin in self
|
||||
if issubclass(plugin, FormatterPlugin)]
|
||||
|
||||
def get_formatters_grouped(self):
|
||||
groups = {}
|
||||
for group_name, group in groupby(
|
||||
self.get_formatters(),
|
||||
key=lambda p: getattr(p, 'group_name', 'format')):
|
||||
groups[group_name] = list(group)
|
||||
return groups
|
||||
|
||||
def get_converters(self):
|
||||
return [plugin for plugin in self
|
||||
if issubclass(plugin, ConverterPlugin)]
|
||||
|
Reference in New Issue
Block a user