|
| 1 | +""" |
| 2 | +Hello World Service with RSA Encryption |
| 3 | +
|
| 4 | +This example demonstrates how to use RSA encryption with INTERSECT services. |
| 5 | +The service will encrypt responses to clients using the client's public key. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | + |
| 10 | +from intersect_sdk import ( |
| 11 | + HierarchyConfig, |
| 12 | + IntersectBaseCapabilityImplementation, |
| 13 | + IntersectService, |
| 14 | + IntersectServiceConfig, |
| 15 | + default_intersect_lifecycle_loop, |
| 16 | + intersect_message, |
| 17 | + intersect_status, |
| 18 | +) |
| 19 | + |
| 20 | +logging.basicConfig(level=logging.INFO) |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +class HelloServiceCapabilityImplementation(IntersectBaseCapabilityImplementation): |
| 25 | + """Capability implementation with RSA encryption support.""" |
| 26 | + |
| 27 | + intersect_sdk_capability_name = 'HelloExample' |
| 28 | + |
| 29 | + @intersect_status() |
| 30 | + def status(self) -> str: |
| 31 | + """Basic status function which returns a hard-coded string.""" |
| 32 | + return 'Up' |
| 33 | + |
| 34 | + @intersect_message() |
| 35 | + def say_hello_to_name(self, name: str) -> str: |
| 36 | + """Takes in a string parameter and says 'Hello' to the parameter!""" |
| 37 | + return f'Hello, {name}!' |
| 38 | + |
| 39 | + |
| 40 | +if __name__ == '__main__': |
| 41 | + """ |
| 42 | + Create a service with RSA encryption enabled. |
| 43 | + |
| 44 | + The service automatically generates an RSA key pair internally. |
| 45 | + When clients request RSA encryption, the service will: |
| 46 | + 1. Fetch the client's public key |
| 47 | + 2. Encrypt the response using the client's public key |
| 48 | + 3. The client decrypts using its private key |
| 49 | + """ |
| 50 | + |
| 51 | + from_config_file = { |
| 52 | + 'brokers': [ |
| 53 | + { |
| 54 | + 'username': 'intersect_username', |
| 55 | + 'password': 'intersect_password', |
| 56 | + 'port': 1883, |
| 57 | + 'protocol': 'mqtt5.0', |
| 58 | + } |
| 59 | + ], |
| 60 | + 'hierarchy': { |
| 61 | + 'organization': 'hello-organization', |
| 62 | + 'facility': 'hello-facility', |
| 63 | + 'system': 'hello-system', |
| 64 | + 'subsystem': 'hello-subsystem', |
| 65 | + 'service': 'hello-service', |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + config = IntersectServiceConfig(**from_config_file) |
| 70 | + |
| 71 | + """ |
| 72 | + step two: create instances of your capabilities, which you will inject into the service |
| 73 | + """ |
| 74 | + capabilities = [HelloServiceCapabilityImplementation()] |
| 75 | + |
| 76 | + """ |
| 77 | + step three: create the service using your capability and configuration |
| 78 | + """ |
| 79 | + intersect_service = IntersectService(capabilities, config) |
| 80 | + |
| 81 | + """ |
| 82 | + step four: utilize the default_intersect_lifecycle_loop function to manage startup, |
| 83 | + the user lifecycle loop, and shutdown automatically. You must pass it the service |
| 84 | + and a function containing your optional lifecycle code to execute. |
| 85 | + """ |
| 86 | + default_intersect_lifecycle_loop(intersect_service) |
0 commit comments