Skip to content

Commit dc61232

Browse files
author
ajoino
authored
Merge pull request #15 from arrowhead-f/Development
Development
2 parents ff14575 + 03aaa6e commit dc61232

40 files changed

+1106
-583
lines changed

README.md

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ Currently, it is working, but it's still missing many crucial features, such as:
1313
- Testing
1414
- Support for the following core services
1515
- Eventhandler
16-
- Gateway
17-
- Gatekeeper
18-
- Support for the following security modes (access policies):
19-
- Token security
20-
- Insecure security
16+
- Support for the TOKEN security modes (access policy):
2117

2218
As more Arrowhead Core Systems mature and are added to the official docker container, those will be added to this list.
2319

@@ -34,65 +30,3 @@ A guide on how to create your own certificates can be found on the [Arrowhead gi
3430
## How To Use
3531
Install the library with `pip install arrowhead-client`.
3632

37-
### Providing Services
38-
To provide services, import the `ProviderSystem`.
39-
Services can be added to the provider with the `provided_services` decorator.
40-
The `provided_services` decorator will create a service, and register the service with the provider.
41-
Then when the provider is started, using the `run_forever` method, the provider will automatically register the service with the service registry.
42-
43-
#### Code Example
44-
```python
45-
import datetime
46-
from arrowhead_client.system.provider import ProviderSystem
47-
48-
# Create provider
49-
time_provider = TimeProvider(
50-
'time_provider',
51-
'localhost',
52-
1337,
53-
'',
54-
keyfile='certificates/time_provider.key'
55-
certfile='certificates/time_provider.crt')
56-
57-
# Add service
58-
@time_provider.provided_service('echo', '/time/echo', 'HTTP-SECURE-JSON', 'GET')
59-
def echo():
60-
return {'now': str(datetime.datetime.now())}
61-
62-
if __name__ == '__main__':
63-
time_provider.run_forever()
64-
65-
```
66-
67-
### Consuming Services
68-
To consume services, use the `ConsumerSystem`.
69-
To find a consumed service, you first register a service definition using the `add_consumed_services` method.
70-
When the `ConsumerSystem` is started initialized, it queries the orchestrator and will register the first orchestrated service.
71-
That service is then consumed using the `consume_service` method.
72-
73-
The orchestration query _will fail_ if the orchestrator does not return a service, and crash as a consequence.
74-
The plan is to make this robust later.
75-
76-
#### Code Examples
77-
```python
78-
79-
from arrowhead_client.system.consumer import ConsumerSystem
80-
81-
time_consumer = ConsumerSystem(
82-
'consumer_test',
83-
'localhost',
84-
'1338',
85-
'',
86-
'certificates/consumer_test.key',
87-
'certificates/consumer_test.crt')
88-
89-
# Add orchestration rules
90-
time_consumer.add_orchestration_rule('echo', 'GET')
91-
92-
if __name__ == '__main__':
93-
# Consume service provided by the 'get_time' rule
94-
echo = time_consumer.consume_service('echo')
95-
print(echo['echo'])
96-
97-
```
98-

_version.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__lib_name__ = 'arrowhead-client'
2+
__version__ = '0.2.0a2'
3+
__author__ = 'Jacob Nilsson'
4+
__email__ = '[email protected]'

arrowhead_client/abc.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from abc import abstractmethod
2+
from typing import Any, Callable
3+
try:
4+
from typing import Protocol
5+
except ImportError:
6+
from typing_extensions import Protocol # type: ignore
7+
8+
9+
class BaseConsumer(Protocol):
10+
@abstractmethod
11+
def consume_service(
12+
self,
13+
service_uri: str,
14+
method: str,
15+
**kwargs) -> Any: # type: ignore
16+
raise NotImplementedError
17+
18+
@abstractmethod
19+
def extract_payload(
20+
self,
21+
service_response: Any,
22+
payload_type: str):
23+
raise NotImplementedError
24+
25+
26+
class BaseProvider(Protocol):
27+
@abstractmethod
28+
def add_provided_service(
29+
self,
30+
service_definition: str,
31+
service_uri: str,
32+
method: str,
33+
func: Callable,
34+
*func_args,
35+
**func_kwargs, ) -> None:
36+
pass
37+
38+
@abstractmethod
39+
def run_forever(self) -> None:
40+
pass

arrowhead_client/api.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,56 @@
1+
"""
2+
Arrowhead Client API module
3+
===========================
4+
5+
This module contains the api of the :code:`arrowhead_client` module.
6+
"""
17
from arrowhead_client.configuration import config
2-
from arrowhead_client.application import ArrowheadApplication
8+
from arrowhead_client.client import ArrowheadClient
39
from arrowhead_client.system import ArrowheadSystem
4-
from arrowhead_client.consumer import Consumer
5-
from arrowhead_client.provider import Provider
10+
from arrowhead_client.httpconsumer import HttpConsumer
11+
from arrowhead_client.httpprovider import HttpProvider
12+
from arrowhead_client.service import Service # noqa: F401
613
from arrowhead_client.logs import get_logger
714

15+
from gevent import pywsgi # type: ignore
16+
17+
18+
class ArrowheadHttpClient(ArrowheadClient):
19+
"""
20+
Arrowhead client using HTTP.
821
22+
Args:
23+
system_name: A string to assign the system name
24+
address: A string to assign the system address
25+
port: An int to assign the system port
26+
authentication_info: A string to assign the system authentication info
27+
keyfile: A string to assign the PEM keyfile
28+
certfile: A string to assign the PEM certfile
29+
"""
930

10-
class ArrowheadHttpApplication(ArrowheadApplication):
1131
def __init__(self,
1232
system_name: str,
1333
address: str,
1434
port: int,
1535
authentication_info: str = '',
1636
keyfile: str = '',
1737
certfile: str = ''):
38+
logger = get_logger(system_name, 'debug')
39+
wsgi_server = pywsgi.WSGIServer(
40+
(address, port),
41+
None,
42+
keyfile=keyfile,
43+
certfile=certfile,
44+
log=logger,
45+
)
1846
super().__init__(
1947
ArrowheadSystem(system_name, address, port, authentication_info),
20-
Consumer(keyfile, certfile),
21-
Provider(),
22-
get_logger(system_name, 'debug'),
48+
HttpConsumer(),
49+
HttpProvider(wsgi_server),
50+
logger,
2351
config,
2452
keyfile=keyfile,
2553
certfile=certfile
2654
)
2755
self._logger.info(f'{self.__class__.__name__} initialized at {self.system.address}:{self.system.port}')
28-
#TODO: This line is a hack and needs to be fixed
56+
# TODO: This line is a hack and needs to be fixed

0 commit comments

Comments
 (0)