Skip to content

Commit

Permalink
Merge pull request #15 from arrowhead-f/Development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
ajoino authored Nov 10, 2020
2 parents ff14575 + 03aaa6e commit dc61232
Show file tree
Hide file tree
Showing 40 changed files with 1,106 additions and 583 deletions.
68 changes: 1 addition & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ Currently, it is working, but it's still missing many crucial features, such as:
- Testing
- Support for the following core services
- Eventhandler
- Gateway
- Gatekeeper
- Support for the following security modes (access policies):
- Token security
- Insecure security
- Support for the TOKEN security modes (access policy):

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

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

### Providing Services
To provide services, import the `ProviderSystem`.
Services can be added to the provider with the `provided_services` decorator.
The `provided_services` decorator will create a service, and register the service with the provider.
Then when the provider is started, using the `run_forever` method, the provider will automatically register the service with the service registry.

#### Code Example
```python
import datetime
from arrowhead_client.system.provider import ProviderSystem

# Create provider
time_provider = TimeProvider(
'time_provider',
'localhost',
1337,
'',
keyfile='certificates/time_provider.key'
certfile='certificates/time_provider.crt')

# Add service
@time_provider.provided_service('echo', '/time/echo', 'HTTP-SECURE-JSON', 'GET')
def echo():
return {'now': str(datetime.datetime.now())}

if __name__ == '__main__':
time_provider.run_forever()

```

### Consuming Services
To consume services, use the `ConsumerSystem`.
To find a consumed service, you first register a service definition using the `add_consumed_services` method.
When the `ConsumerSystem` is started initialized, it queries the orchestrator and will register the first orchestrated service.
That service is then consumed using the `consume_service` method.

The orchestration query _will fail_ if the orchestrator does not return a service, and crash as a consequence.
The plan is to make this robust later.

#### Code Examples
```python

from arrowhead_client.system.consumer import ConsumerSystem

time_consumer = ConsumerSystem(
'consumer_test',
'localhost',
'1338',
'',
'certificates/consumer_test.key',
'certificates/consumer_test.crt')

# Add orchestration rules
time_consumer.add_orchestration_rule('echo', 'GET')

if __name__ == '__main__':
# Consume service provided by the 'get_time' rule
echo = time_consumer.consume_service('echo')
print(echo['echo'])

```

4 changes: 4 additions & 0 deletions _version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__lib_name__ = 'arrowhead-client'
__version__ = '0.2.0a2'
__author__ = 'Jacob Nilsson'
__email__ = 'jacob.nilsson@ltu.se'
40 changes: 40 additions & 0 deletions arrowhead_client/abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from abc import abstractmethod
from typing import Any, Callable
try:
from typing import Protocol
except ImportError:
from typing_extensions import Protocol # type: ignore


class BaseConsumer(Protocol):
@abstractmethod
def consume_service(
self,
service_uri: str,
method: str,
**kwargs) -> Any: # type: ignore
raise NotImplementedError

@abstractmethod
def extract_payload(
self,
service_response: Any,
payload_type: str):
raise NotImplementedError


class BaseProvider(Protocol):
@abstractmethod
def add_provided_service(
self,
service_definition: str,
service_uri: str,
method: str,
func: Callable,
*func_args,
**func_kwargs, ) -> None:
pass

@abstractmethod
def run_forever(self) -> None:
pass
44 changes: 36 additions & 8 deletions arrowhead_client/api.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
"""
Arrowhead Client API module
===========================
This module contains the api of the :code:`arrowhead_client` module.
"""
from arrowhead_client.configuration import config
from arrowhead_client.application import ArrowheadApplication
from arrowhead_client.client import ArrowheadClient
from arrowhead_client.system import ArrowheadSystem
from arrowhead_client.consumer import Consumer
from arrowhead_client.provider import Provider
from arrowhead_client.httpconsumer import HttpConsumer
from arrowhead_client.httpprovider import HttpProvider
from arrowhead_client.service import Service # noqa: F401
from arrowhead_client.logs import get_logger

from gevent import pywsgi # type: ignore


class ArrowheadHttpClient(ArrowheadClient):
"""
Arrowhead client using HTTP.
Args:
system_name: A string to assign the system name
address: A string to assign the system address
port: An int to assign the system port
authentication_info: A string to assign the system authentication info
keyfile: A string to assign the PEM keyfile
certfile: A string to assign the PEM certfile
"""

class ArrowheadHttpApplication(ArrowheadApplication):
def __init__(self,
system_name: str,
address: str,
port: int,
authentication_info: str = '',
keyfile: str = '',
certfile: str = ''):
logger = get_logger(system_name, 'debug')
wsgi_server = pywsgi.WSGIServer(
(address, port),
None,
keyfile=keyfile,
certfile=certfile,
log=logger,
)
super().__init__(
ArrowheadSystem(system_name, address, port, authentication_info),
Consumer(keyfile, certfile),
Provider(),
get_logger(system_name, 'debug'),
HttpConsumer(),
HttpProvider(wsgi_server),
logger,
config,
keyfile=keyfile,
certfile=certfile
)
self._logger.info(f'{self.__class__.__name__} initialized at {self.system.address}:{self.system.port}')
#TODO: This line is a hack and needs to be fixed
# TODO: This line is a hack and needs to be fixed
Loading

0 comments on commit dc61232

Please sign in to comment.