-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
1 deletion.
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
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,65 @@ | ||
import logging | ||
|
||
import os | ||
from typing import Any, Dict, Optional, Text | ||
import rasa_sdk.utils | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
DEFAULT_ENCODING = "utf-8" | ||
|
||
|
||
def read_endpoint_config( | ||
filename: Text, endpoint_type: Text | ||
) -> Optional["EndpointConfig"]: | ||
"""Read an endpoint configuration file from disk and extract one | ||
config.""" | ||
if not filename: | ||
return None | ||
|
||
try: | ||
content = rasa_sdk.utils.read_file(filename) | ||
content = rasa_sdk.utils.read_yaml(content) | ||
|
||
if content.get(endpoint_type) is None: | ||
return None | ||
|
||
return EndpointConfig.from_dict(content[endpoint_type]) | ||
except FileNotFoundError: | ||
logger.error( | ||
"Failed to read endpoint configuration " | ||
"from {}. No such file.".format(os.path.abspath(filename)) | ||
) | ||
return None | ||
|
||
|
||
class EndpointConfig: | ||
"""Configuration for an external HTTP endpoint.""" | ||
|
||
def __init__( | ||
self, | ||
url: Optional[Text] = None, | ||
params: Optional[Dict[Text, Any]] = None, | ||
headers: Optional[Dict[Text, Any]] = None, | ||
basic_auth: Optional[Dict[Text, Text]] = None, | ||
token: Optional[Text] = None, | ||
token_name: Text = "token", | ||
cafile: Optional[Text] = None, | ||
**kwargs: Any, | ||
) -> None: | ||
"""Creates an `EndpointConfig` instance.""" | ||
self.url = url | ||
self.params = params or {} | ||
self.headers = headers or {} | ||
self.basic_auth = basic_auth or {} | ||
self.token = token | ||
self.token_name = token_name | ||
self.type = kwargs.pop("store_type", kwargs.pop("type", None)) | ||
self.cafile = cafile | ||
self.kwargs = kwargs | ||
|
||
|
||
@classmethod | ||
def from_dict(cls, data: Dict[Text, Any]) -> "EndpointConfig": | ||
return EndpointConfig(**data) |