1 """Copyright 2019 Deutsche Telekom.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
7 http://www.apache.org/licenses/LICENSE-2.0
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
15 from logging import Logger, getLogger
16 from types import TracebackType
17 from typing import Iterable, Optional, Type
24 ssl_channel_credentials,
26 from proto.BluePrintProcessing_pb2 import ExecutionServiceInput, ExecutionServiceOutput
27 from proto.BluePrintProcessing_pb2_grpc import BluePrintProcessingServiceStub
29 from .authorization import AuthTokenInterceptor
33 """Resource resoulution client class."""
39 # TLS/SSL configuration
40 use_ssl: bool = False,
41 root_certificates: bytes = None,
42 private_key: bytes = None,
43 certificate_chain: bytes = None,
44 # Authentication header configuration
45 use_header_auth: bool = False,
46 header_auth_token: str = None,
48 """Client class initialization.
50 :param server_address: Address to server to connect.
51 :param use_ssl: Boolean flag to determine if secure channel should be created or not. Keyword argument.
52 :param root_certificates: The PEM-encoded root certificates. None if it shouldn't be used. Keyword argument.
53 :param private_key: The PEM-encoded private key as a byte string, or None if no private key should be used.
55 :param certificate_chain: The PEM-encoded certificate chain as a byte string to use or or None if
56 no certificate chain should be used. Keyword argument.
57 :param use_header_auth: Boolean flag to determine if authorization headed shoud be added for every call or not.
59 :param header_auth_token: Authorization token value. Keyword argument.
61 self.logger: Logger = getLogger(__name__)
63 self.channel: Channel = secure_channel(
64 server_address, ssl_channel_credentials(root_certificates, private_key, certificate_chain)
66 self.logger.debug(f"Create secure channel to connect with {server_address}")
68 self.channel: Channel = insecure_channel(server_address)
69 self.logger.debug(f"Create insecure channel to connect to {server_address}")
71 self.channel: Channel = intercept_channel(self.channel, AuthTokenInterceptor(header_auth_token))
72 self.stub: BluePrintProcessingServiceStub = BluePrintProcessingServiceStub(self.channel)
74 def close(self) -> None:
75 """Close client session.
77 Closes client's channel.
79 self.logger.debug("Close channel connection")
82 def __enter__(self) -> Channel:
83 """Enter Client instance context.
85 Return Client instance. In the context user can call methods to communicate with server.
86 On exit connection with the server is going to be closed.
88 self.logger.debug("Enter Client instance context")
93 unused_exc_type: Optional[Type[BaseException]],
94 unused_exc_value: Optional[BaseException],
95 unused_traceback: Optional[TracebackType],
97 """Exit Client instance context.
99 Close connection with the server.
101 self.logger.debug("Exit Client instance context")
104 def process(self, messages: Iterable[ExecutionServiceInput]) -> Iterable[ExecutionServiceOutput]:
105 """Send messages to server and return responses.
107 :param messages: Iterable messages to send
108 :return: Iterable responses
110 for message in self.stub.process(messages):
111 self.logger.debug(f"Get response message: {message}")