-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhello_client.py
More file actions
108 lines (91 loc) · 4.63 KB
/
hello_client.py
File metadata and controls
108 lines (91 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import logging
from intersect_sdk import (
INTERSECT_JSON_VALUE,
ControlPlaneConfig,
IntersectClient,
IntersectClientCallback,
IntersectClientConfig,
IntersectDirectMessageParams,
default_intersect_lifecycle_loop,
)
from intersect_sdk.config.shared import BrokerConfig
logging.basicConfig(level=logging.INFO)
logging.getLogger('pika').setLevel(logging.WARNING)
def simple_client_callback(
_source: str, _operation: str, _has_error: bool, payload: INTERSECT_JSON_VALUE
) -> None:
"""This simply prints the response from the service to your console.
As we don't want to engage in a back-and-forth, we simply throw an exception to break out of the message loop.
Ways to continue listening to messages or sending messages will be explored in other examples.
Params:
_source: the source of the response message. In this case it will always be from the hello_service.
_operation: the name of the function we called in the original message. In this case it will always be "say_hello_to_name".
_has_error: Boolean value which represents an error. Since there is never an error in this example, it will always be "False".
payload: Value of the response from the Service. The typing of the payload varies, based on the operation called and whether or not
_has_error was set to "True". In this case, since we do not have an error, we can defer to the operation's response type. This response type is
"str", so the type will be "str". The value will always be "Hello, hello_client!".
Note that the payload will always be a deserialized Python object, but the types are fairly limited: str, bool, float, int, None, List[T], and Dict[str, T]
are the only types the payload can have. "T" in this case can be any of the 7 types just mentioned.
"""
print(payload)
# raise exception to break out of message loop - we only send and wait for one message
raise Exception
if __name__ == '__main__':
"""
step one: create configuration class, which handles user input validation - see the IntersectClientConfig class documentation for more info
In most cases, everything under from_config_file should come from a configuration file, command line arguments, or environment variables.
"""
broker_configs = [
{'host': 'localhost', 'port': 5672},
]
brokers = [
ControlPlaneConfig(
protocol='amqp0.9.1',
username='intersect_username',
password='intersect_password',
brokers=[BrokerConfig(**broker) for broker in broker_configs],
)
]
"""
step two: construct the initial messages you want to send. In this case we will only send a single starting message.
- The destination should match info.title in the service's schema. Another way to look at this is to see the service's
HierarchyConfig, and then write it as a single string: <ORGANIZATION>.<FACILITY>.<SYSTEM>.<SUBSYSTEM>.<SERVICE> .
If you don't have a subsystem, use a "-" character instead.
- The operation should match one of the channels listed in the schema. In this case, 'say_hello_to_name' is the only
operation exposed in the service.
- The payload should represent what you want to send to the service's operation. You can determine the valid format
from the service's schema. In this case, we're sending it a simple string. As long as the payload is a string,
you'll get a message back.
"""
initial_messages = [
IntersectDirectMessageParams(
destination='hello-organization.hello-facility.hello-system.hello-subsystem.hello-service',
operation='HelloExample.say_hello_to_name',
payload='hello_client',
)
]
config = IntersectClientConfig(
initial_message_event_config=IntersectClientCallback(messages_to_send=initial_messages),
brokers=brokers,
)
"""
step three: create the client.
We also need a callback to handle incoming user messages.
"""
client = IntersectClient(
config=config,
user_callback=simple_client_callback,
)
"""
step four - start lifecycle loop. The only necessary parameter is your client.
with certain applications (i.e. REST APIs) you'll want to integrate the client in the existing lifecycle,
instead of using this one.
In that case, just be sure to call client.startup() and client.shutdown() at appropriate stages.
"""
default_intersect_lifecycle_loop(
client,
)
"""
When running the loop, you should have 'Hello, hello_client!' printed to your console.
Note that the client will automatically exit.
"""