codekingpro/portable-devtools
114k
1from typing import Tuple2 3 4class BasePlugin:5 # The name of the plugin, eg. "My auth".6 name = None7 8 # Optional short description. It will be shown in the help9 # under --auth-type.10 description = None11 12 # This be set automatically once the plugin has been loaded.13 package_name = None14 15 16class AuthPlugin(BasePlugin):17 """18 Base auth plugin class.19 20 See httpie-ntlm for an example auth plugin:21 22 <https://github.com/httpie/httpie-ntlm>23 24 See also `test_auth_plugins.py`25 26 """27 # The value that should be passed to --auth-type28 # to use this auth plugin. Eg. "my-auth"29 auth_type = None30 31 # Set to `False` to make it possible to invoke this auth32 # plugin without requiring the user to specify credentials33 # through `--auth, -a`.34 auth_require = True35 36 # By default the `-a` argument is parsed for `username:password`.37 # Set this to `False` to disable the parsing and error handling.38 auth_parse = True39 40 # Set to `True` to make it possible for this auth41 # plugin to acquire credentials from the user’s netrc file(s).42 # It is used as a fallback when the credentials are not provided explicitly43 # through `--auth, -a`. Enabling this will allow skipping `--auth, -a`44 # even when `auth_require` is set `True` (provided that netrc provides45 # credential for a given host).46 netrc_parse = False47 48 # If both `auth_parse` and `prompt_password` are set to `True`,49 # and the value of `-a` lacks the password part,50 # then the user will be prompted to type the password in.51 prompt_password = True52 53 # Will be set to the raw value of `-a` (if provided) before54 # `get_auth()` gets called. If the credentials came from a netrc file,55 # then this is `None`.56 raw_auth = None57 58 def get_auth(self, username: str = None, password: str = None):59 """60 If `auth_parse` is set to `True`, then `username`61 and `password` contain the parsed credentials.62 63 Use `self.raw_auth` to access the raw value passed through64 `--auth, -a`.65 66 Return a ``requests.auth.AuthBase`` subclass instance.67 68 """69 raise NotImplementedError()70 71 72class TransportPlugin(BasePlugin):73 """74 Requests transport adapter docs:75 76 <https://requests.readthedocs.io/en/latest/user/advanced/#transport-adapters>77 78 See httpie-unixsocket for an example transport plugin:79 80 <https://github.com/httpie/httpie-unixsocket>81 82 """83 84 # The URL prefix the adapter should be mount to.85 prefix = None86 87 def get_adapter(self):88 """89 Return a ``requests.adapters.BaseAdapter`` subclass instance to be90 mounted to ``self.prefix``.91 92 """93 raise NotImplementedError()94 95 96class ConverterPlugin(BasePlugin):97 """98 Possibly converts binary response data for prettified terminal display.99 100 See httpie-msgpack for an example converter plugin:101 102 <https://github.com/rasky/httpie-msgpack>.103 104 """105 106 def __init__(self, mime: str):107 self.mime = mime108 109 def convert(self, body: bytes) -> Tuple[str, str]:110 """111 Convert a binary body to a textual representation for the terminal112 and return a tuple containing the new Content-Type and content, e.g.:113 114 ('application/json', '{}')115 116 """117 raise NotImplementedError118 119 @classmethod120 def supports(cls, mime: str) -> bool:121 raise NotImplementedError122 123 124class FormatterPlugin(BasePlugin):125 """126 Possibly formats response body & headers for prettified terminal display.127 128 """129 group_name = 'format'130 131 def __init__(self, **kwargs):132 """133 :param env: an class:`Environment` instance134 :param kwargs: additional keyword argument that some135 formatters might require.136 137 """138 self.enabled = True139 self.kwargs = kwargs140 self.format_options = kwargs['format_options']141 142 def format_headers(self, headers: str) -> str:143 """Return processed `headers`144 145 :param headers: The headers as text.146 147 """148 return headers149 150 def format_body(self, content: str, mime: str) -> str:151 """Return processed `content`.152 153 :param mime: E.g., 'application/atom+xml'.154 :param content: The body content as text155 156 """157 return content158 159 def format_metadata(self, metadata: str) -> str:160 """Return processed `metadata`.161 162 :param metadata: The metadata as text.163 164 """165 return metadata166 