-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhello_service.py
More file actions
99 lines (80 loc) · 3.29 KB
/
hello_service.py
File metadata and controls
99 lines (80 loc) · 3.29 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
import logging
from intersect_sdk import (
ControlPlaneConfig,
HierarchyConfig,
IntersectBaseCapabilityImplementation,
IntersectService,
IntersectServiceConfig,
default_intersect_lifecycle_loop,
intersect_message,
intersect_status,
)
from intersect_sdk.config.shared import BrokerConfig
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class HelloServiceCapabilityImplementation(IntersectBaseCapabilityImplementation):
"""Rudimentary capability implementation example.
All capability implementations are required to have an @intersect_status decorated function,
but we do not use it here.
The operation we are calling is `say_hello_to_name` , so the message being sent will need to have
an operationId of `say_hello_to_name`. The operation expects a string sent to it in the payload,
and will send a string back in its own payload.
"""
intersect_sdk_capability_name = 'HelloExample'
@intersect_status()
def status(self) -> str:
"""Basic status function which returns a hard-coded string."""
return 'Up'
@intersect_message()
def say_hello_to_name(self, name: str) -> str:
"""Takes in a string parameter and says 'Hello' to the parameter!"""
return f'Hello, {name}!'
if __name__ == '__main__':
"""
step one: create configuration class, which handles validation - see the IntersectServiceConfig 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': 1883},
]
brokers = [
ControlPlaneConfig(
protocol='mqtt3.1.1',
username='intersect_username',
password='intersect_password',
brokers=[BrokerConfig(**broker) for broker in broker_configs],
)
]
config = IntersectServiceConfig(
hierarchy=HierarchyConfig(
organization='hello-organization',
facility='hello-facility',
system='hello-system',
subsystem='hello-subsystem',
service='hello-service',
),
brokers=brokers,
)
"""
step two - create your own capability implementation class.
You have complete control over how you construct this class, as long as it has decorated functions with
@intersect_message and @intersect_status, and that these functions are appropriately type-annotated.
"""
capability = HelloServiceCapabilityImplementation()
"""
step three - create service from both the configuration and your own capability
"""
service = IntersectService([capability], config)
"""
step four - start lifecycle loop. The only necessary parameter is your service.
with certain applications (i.e. REST APIs) you'll want to integrate the service in the existing lifecycle,
instead of using this one.
In that case, just be sure to call service.startup() and service.shutdown() at appropriate stages.
"""
logger.info('Starting hello_service, use Ctrl+C to exit.')
default_intersect_lifecycle_loop(
service,
)
"""
Note that the service will run forever until you explicitly kill the application (i.e. Ctrl+C)
"""