-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* message adapter * Invalid Input Error * change adapter load mode to simple registry mode * Create registry * adjust lint * Suport adapter query param
- Loading branch information
Showing
7 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ clean: | |
rm -rf docs/_build | ||
|
||
formatter: | ||
isort -rc -q david tests | ||
black david tests | ||
|
||
lint: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
from typing import Dict, List, Text | ||
|
||
from david.typing import Message | ||
|
||
|
||
class Adapter: | ||
@property | ||
def name(self): | ||
"""The name property is a function of the class - its __name__.""" | ||
|
||
return self.__class__.__name__ | ||
|
||
def validade_data(self, payload: Dict) -> bool: | ||
raise NotImplementedError | ||
|
||
def input(self, payload: Dict) -> Message: | ||
raise NotImplementedError | ||
|
||
def output(self, message: Message) -> Dict: | ||
raise NotImplementedError | ||
|
||
|
||
class MessageAdapter(Adapter): | ||
@property | ||
def name(self): | ||
return "message" | ||
|
||
def validade_data(self, payload: Dict) -> bool: | ||
return "input" in payload and "text" in payload["input"] | ||
|
||
def input(self, payload: Dict) -> Message: | ||
return Message.build(payload=payload) | ||
|
||
def output(self, message: Message) -> Dict: | ||
return message.__dict__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from david.adapters.adapter import Adapter, MessageAdapter | ||
|
||
|
||
# [TODO] refactory as generic registry | ||
class Registry: | ||
|
||
_instance = None | ||
|
||
adapters = {} | ||
|
||
def __init__(self, defaultAdapter=None): | ||
|
||
messageAdapter = MessageAdapter() | ||
self.registryAdapter(messageAdapter) | ||
|
||
if defaultAdapter: | ||
self.defaultAdapter = defaultAdapter | ||
else: | ||
self.defaultAdapter = messageAdapter.name | ||
|
||
@classmethod | ||
def get_instance(cls): | ||
if not cls._instance: | ||
cls._instance = Registry() | ||
return cls._instance | ||
|
||
def registryAdapter(self, adapter: Adapter): | ||
self.adapters[adapter.name] = adapter | ||
|
||
def getAdapter(self, adapterName=None): | ||
|
||
if not adapterName: | ||
adapterName = self.defaultAdapter | ||
|
||
if adapterName in self.adapters: | ||
return self.adapters[adapterName] | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters