potiuk commented on a change in pull request #12558:
URL: https://github.com/apache/airflow/pull/12558#discussion_r537812437



##########
File path: airflow/hooks/base_hook.py
##########
@@ -90,3 +93,78 @@ def get_hook(cls, conn_id: str) -> "BaseHook":
     def get_conn(self) -> Any:
         """Returns connection for the hook."""
         raise NotImplementedError()
+
+
+class DiscoverableHook(Protocol):
+    """
+    This protocol Just describes what Hook classes that want to be discovered 
by ProvidersManager can
+    implement. It is not used by any of the Hooks, but simply methods and 
class fields described here
+    are implemented by those Hooks.
+
+    The conn_name_attr, default_conn_name, conn_type should be implemented by 
those
+    Hooks that want to be automatically mapped from the connection_type -> 
Hook when get_hook method
+    is called with connection_type.
+
+    Additionally hook_name should be set when you want the hook to have a 
custom name in the UI selection
+    Name. If not specified, conn_name will be used.
+
+    The "get_ui_field_behaviour" and "get_connection_form_widgets" are 
optional - override them if you want
+    to customize the Connection Form screen. You can add extra widgets to 
parse your extra fields via the
+    get_connection_form_widgets method as well as hide or relabel the fields 
or pre-fill
+    them with placeholders via get_ui_field_behaviour method.
+
+    Note that the "get_ui_field_behaviour" and "get_connection_form_widgets" 
need to be set by each class
+    in the class hierarchy in order to apply widget customizations.
+
+    For example, even if you want to use the fields from your parent class, 
you must explicitly
+    have a method on *your* class:
+
+    .. code-block:: python
+
+        @classmethod
+        def get_ui_field_behaviour(cls):
+            return super().get_ui_field_behaviour()
+
+    You also need to add the Hook class name to list 'hook_class_names' in 
provider.yaml in case you
+    build an internal provider or to return it in dictionary returned by 
provider_info entrypoint in the
+    package you prepare.
+
+    You can see some examples in airflow/providers/jdbc/hooks/jdbc.py.
+
+    """
+
+    conn_name_attr: str
+    default_conn_name: str
+    conn_type: str
+    hook_name: str
+
+    @staticmethod
+    def get_connection_form_widgets() -> Dict[str, Field]:
+        """
+        Returns dictionary of widgets to be added for the hook to handle extra 
values.
+
+        If you have class hierarchy, usually the widgets needed by your class 
are already
+        added by the base class, so there is no need to implement this method. 
It might
+        actually result in warning in the logs if you try to add widgets that 
have already
+        been added by the base class.
+
+        """
+        ...
+
+    @staticmethod
+    def get_ui_field_behaviour() -> Dict:
+        """
+        Returns dictionary describing customizations to implement in 
javascript handling the
+        connection form. Should be compliant with 
airflow/customized_form_fields.schema.json'
+
+
+        If you change conn_type in a derived class, you should also
+        implement this method and return field customizations appropriate to 
your Hook. This
+        is because the child hook will have usually different conn_type and 
the customizations
+        are per connection type.
+
+        .. seealso::
+            
:class:`~airflow.providers.google.cloud.hooks.compute_ssh.ComputeSSH`

Review comment:
       Cool!




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to