Skip to content

Commit

Permalink
Add methods expected from neon_mq_connector 0.2.0 (#42) (#43)
Browse files Browse the repository at this point in the history
* Add methods expected from neon_mq_connector 0.2.0 (#42)

Loosen dependencies
Cleanup Dockerfile
Fix typo in config
Update docs

* Increment Version

* Replace to-be-deprecated envvars in Dockerfile for backwards compat. (#44)

* Increment Version

* Catch content encoding errors raised in some cases (#45)

* Increment Version

* Increment to 0.1.8 (#46)

Co-authored-by: Daniel McKnight <[email protected]>
Co-authored-by: NeonDaniel <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2021
1 parent 080ae02 commit 9cea100
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ RUN apt-get update && \
&& pip install wheel \
&& pip install .

WORKDIR /config
ENV NEON_CONFIG_PATH /config

# TODO: Deprecate below after updating to neon_mq_connector 0.2.0+ DM
ENV NEON_MQ_CONFIG_PATH /config/config.json

# TODO: Deprecate below after updating to ngi_auth_vars internally
ENV NEON_API_PROXY_CONFIG_PATH /config/config.json
ENV NEON_MQ_PROXY_CONFIG_PATH /config/config.json

CMD ["neon_api_proxy"]
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ Responses will be returned as dictionaries. Responses should contain the followi
- `encoding` = Usually contains the HTTP content encoding if content is the byte representation of a string, may be `None`
## Docker Configuration
When running this as a docker container, the path to configuration files should be mounted to `/config`.
When running this as a docker container, the path to configuration files should be mounted to `/config`. This container
expects `mq_config.json` to contain service `neon_api_connector` and `ngi_auth_vars.yml` to contain dict `api_services`.
For example, if your configuration resides in `~/.config`:
```commandline
docker run -v /home/$USER/.config:/config neon_api_proxy
```shell
export CONFIG_PATH="/home/${USER}/.config"
docker run -v ${CONFIG_PATH}:/config neon_api_proxy
```
14 changes: 13 additions & 1 deletion neon_api_proxy/api_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import pika.channel

from typing import Optional
from neon_utils import LOG
from neon_utils.socket_utils import b64_to_dict, dict_to_b64
from neon_mq_connector.connector import MQConnector, ConsumerThread
Expand Down Expand Up @@ -66,7 +67,10 @@ def handle_api_input(self,
respond = self.proxy.resolve_query(request)
LOG.info(f"message={message_id} status={respond.get('status_code')}")

respond['content'] = bytes(respond.get('content', '')).decode(encoding='utf-8')
try:
respond['content'] = bytes(respond.get('content', b'')).decode(encoding='utf-8')
except Exception as e:
LOG.error(e)
respond = {**respond, **tokens}
LOG.debug(f"respond={respond}")
data = dict_to_b64(respond)
Expand Down Expand Up @@ -116,3 +120,11 @@ def pre_run(self, **kwargs):
self.vhost,
f'neon_api_input_{self.service_id}',
self.handle_api_input, auto_ack=False)

# TODO: Remove below methods after MQ Connector dep bumped to 0.2.0 DM
def run(self, **kwargs):
self.pre_run(**kwargs)
self.run_consumers()

def stop(self):
pass
22 changes: 13 additions & 9 deletions neon_api_proxy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,31 @@ def get_proxy_config() -> dict:
Locates a valid configuration file for proxy service credentials
:return: dict containing "SERVICES" key with proxy service configurations
"""
config_path = environ.get('NEON_API_PROXY_CONFIG_PATH', 'config.json')
if path.isfile(path.expanduser(config_path)):
valid_config_path = path.expanduser(config_path)
elif path.isfile(path.expanduser("~/.config/neon/credentials.json")):
valid_config_path = path.expanduser("~/.config/neon/credentials.json")
elif path.isfile(path.expanduser("~/.local/share/neon/credentials.json")):
valid_config_path = path.expanduser("~/.local/share/neon/credentials.json")
else:
valid_config_paths = [
environ.get('NEON_API_PROXY_CONFIG_PATH'),
path.expanduser("~/.config/neon/credentials.json"),
path.expanduser("~/.local/share/neon/credentials.json")
]
valid_config_path = None
for p in valid_config_paths:
if p and path.isfile(p):
valid_config_path = p
break
if not valid_config_path:
return dict()
with open(valid_config_path) as input_file:
proxy_service_config = json.load(input_file)
return proxy_service_config


def get_mq_config() -> dict:
# TODO: Deprecate after mq_controller bumped to 0.3 DM
"""
Locates a valid MQ config for MQ Authentication
:return: dict containing "MQ" key with server and users configurations
"""
if path.isfile(environ.get('NEON_MQ_CONFIG_PATH', 'config.json')):
valid_config_path = environ.get('NEON_API_PROXY_CONFIG_PATH', 'config.json')
valid_config_path = environ.get('NEON_MQ_CONFIG_PATH', 'config.json')
elif path.isfile(path.expanduser("~/.config/neon/mq_config.json")):
valid_config_path = path.expanduser("~/.config/neon/mq_config.json")
elif path.isfile(path.expanduser("~/.local/share/neon/mq_config.json")):
Expand Down
4 changes: 2 additions & 2 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests-cache==0.6.4
requests~=2.25
neon_utils==0.6.2
neon_mq_connector==0.1.2
neon_utils~=0.12.0
neon_mq_connector~=0.1.2
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
# US Patents 2008-2021: US7424516, US20140161250, US20140177813, US8638908, US8068604, US8553852, US10530923, US10530924
# China Patent: CN102017585 - Europe Patent: EU2156652 - Patents Pending

__version__ = "0.1.6"
__version__ = "0.1.8"

0 comments on commit 9cea100

Please sign in to comment.