client_authentication.py 1.3 KB

1234567891011121314151617181920212223242526272829
  1. import requests
  2. from fate_flow.db.service_registry import ServiceRegistry
  3. from fate_flow.entity.types import RegistryServiceName
  4. from fate_flow.hook import HookManager
  5. from fate_flow.hook.common.parameters import ClientAuthenticationParameters, ClientAuthenticationReturn
  6. from fate_flow.settings import HOOK_SERVER_NAME
  7. @HookManager.register_client_authentication_hook
  8. def authentication(parm: ClientAuthenticationParameters) -> ClientAuthenticationReturn:
  9. service_list = ServiceRegistry.load_service(
  10. server_name=HOOK_SERVER_NAME,
  11. service_name=RegistryServiceName.CLIENT_AUTHENTICATION.value
  12. )
  13. if not service_list:
  14. raise Exception(f"client authentication error: no found server"
  15. f" {HOOK_SERVER_NAME} service client_authentication")
  16. service = service_list[0]
  17. response = getattr(requests, service.f_method.lower(), None)(
  18. url=service.f_url,
  19. json=parm.to_dict()
  20. )
  21. if response.status_code != 200:
  22. raise Exception(
  23. f"client authentication error: request authentication url failed, status code {response.status_code}")
  24. elif response.json().get("code") != 0:
  25. return ClientAuthenticationReturn(code=response.json().get("code"), message=response.json().get("msg"))
  26. return ClientAuthenticationReturn()