Coverage for python/lsst/daf/butler/remote_butler/_factory.py: 6%

36 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-13 09:56 +0000

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28from __future__ import annotations 

29 

30__all__ = ("RemoteButlerFactory",) 

31 

32import httpx 

33from lsst.daf.butler.repo_relocation import replaceRoot 

34 

35from .._butler_config import ButlerConfig 

36from .._butler_instance_options import ButlerInstanceOptions 

37from ._authentication import get_authentication_token_from_environment 

38from ._config import RemoteButlerConfigModel 

39from ._http_connection import RemoteButlerHttpConnection 

40from ._remote_butler import RemoteButler, RemoteButlerCache 

41 

42 

43class RemoteButlerFactory: 

44 """Factory for instantiating RemoteButler instances bound to a user's Rubin 

45 Science Platform Gafaelfawr access token. All Butler instances created by 

46 this factory share a common HTTP connection pool. 

47 

48 Parameters 

49 ---------- 

50 server_url : `str` 

51 The URL of the Butler server that RemoteButler instances created by 

52 this factory will connect to. 

53 http_client : `httpx.Client`, optional 

54 The httpx connection pool that RemoteButler instances created by this 

55 factory will use for making HTTP requests. If omitted, creates a new 

56 connection pool. 

57 

58 Notes 

59 ----- 

60 Most users should not directly call this constructor -- instead use 

61 ``create_factory_from_config`` or ``create_factory_for_url``. 

62 """ 

63 

64 def __init__(self, server_url: str, http_client: httpx.Client | None = None): 

65 self.server_url = server_url 

66 if http_client is not None: 

67 self.http_client = http_client 

68 else: 

69 self.http_client = httpx.Client() 

70 self._cache = RemoteButlerCache() 

71 

72 @staticmethod 

73 def create_factory_from_config(config: ButlerConfig) -> RemoteButlerFactory: 

74 # There is a convention in Butler config files where <butlerRoot> in a 

75 # configuration option refers to the directory containing the 

76 # configuration file. We allow this for the remote butler's URL so 

77 # that the server doesn't have to know which hostname it is being 

78 # accessed from. 

79 server_url_key = ("remote_butler", "url") 

80 if server_url_key in config: 

81 config[server_url_key] = replaceRoot(config[server_url_key], config.configDir) 

82 remote_config = RemoteButlerConfigModel.model_validate(config) 

83 return RemoteButlerFactory.create_factory_for_url(str(remote_config.remote_butler.url)) 

84 

85 @staticmethod 

86 def create_factory_for_url(server_url: str) -> RemoteButlerFactory: 

87 return RemoteButlerFactory(server_url) 

88 

89 def create_butler_for_access_token( 

90 self, access_token: str, *, butler_options: ButlerInstanceOptions | None = None 

91 ) -> RemoteButler: 

92 if butler_options is None: 

93 butler_options = ButlerInstanceOptions() 

94 return RemoteButler( 

95 connection=RemoteButlerHttpConnection( 

96 http_client=self.http_client, server_url=self.server_url, access_token=access_token 

97 ), 

98 options=butler_options, 

99 cache=self._cache, 

100 ) 

101 

102 def create_butler_with_credentials_from_environment( 

103 self, *, butler_options: ButlerInstanceOptions | None = None 

104 ) -> RemoteButler: 

105 token = get_authentication_token_from_environment(self.server_url) 

106 if token is None: 

107 raise RuntimeError( 

108 "Attempting to connect to Butler server," 

109 " but no access credentials were found in the environment." 

110 ) 

111 return self.create_butler_for_access_token(token, butler_options=butler_options)