Skip to content

Commit 87f8cf8

Browse files
author
Sergio García Prado
authored
Merge pull request #76 from minos-framework/0.1.0
0.1.0
2 parents e1ae93a + 69d5d90 commit 87f8cf8

File tree

9 files changed

+95
-2
lines changed

9 files changed

+95
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,4 @@ ENV/
112112

113113
# Sphinx Api Documentation
114114
docs/api
115+
.run

HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ History
5050
------------------
5151

5252
* modified PORT from string to INT
53+
54+
0.1.0 (2022-02-3)
55+
------------------
56+
57+
* New view /endpoints. Return all endpoints stored.

minos/api_gateway/discovery/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.0.8"
1+
__version__ = "0.1.0"
22

33
from .cli import (
44
app,

minos/api_gateway/discovery/database/client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ class OrdersMinosApiRouter(MinosApiRouter):
1313
"""
1414

1515
import json
16+
from typing import (
17+
Any,
18+
)
1619

1720
import aioredis
1821

1922
from minos.api_gateway.common import (
2023
MinosConfig,
2124
)
25+
from minos.api_gateway.discovery.domain.microservice import (
26+
MICROSERVICE_KEY_PREFIX,
27+
)
2228

2329

2430
class MinosRedisClient:
@@ -53,6 +59,19 @@ async def get_data(self, key: str) -> str:
5359

5460
return json_data
5561

62+
async def get_all(self) -> list:
63+
"""Get redis value by key"""
64+
data = list()
65+
try:
66+
async for key in self.redis.scan_iter(match=f"{MICROSERVICE_KEY_PREFIX}:*"):
67+
decoded_key = key.decode("utf-8")
68+
redis_data: dict[str, Any] = await self.redis.get(decoded_key)
69+
data.append(json.loads(redis_data))
70+
except Exception:
71+
pass
72+
73+
return data
74+
5675
async def set_data(self, key: str, data: dict):
5776
flag = True
5877
try:

minos/api_gateway/discovery/domain/microservice.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ async def load(cls, microservice_key: bytes, db_client) -> Microservice:
6767
]
6868
return cls(**microservice_dict)
6969

70+
@classmethod
71+
async def get_all(cls, db_client) -> Microservice:
72+
"""Load an instance from the database.
73+
74+
:param microservice_key: The microservice key.
75+
:param db_client: The database client.
76+
:return: A ``Microservice`` instance.
77+
"""
78+
79+
res = await db_client.get_all()
80+
return res
81+
7082
async def save(self, db_client) -> None:
7183
"""Store the instance into the database.
7284

minos/api_gateway/discovery/views/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from .endpoint import (
22
EndpointView,
33
)
4+
from .endpoints import (
5+
EndpointsView,
6+
)
47
from .microservice import (
58
MicroserviceView,
69
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from aiohttp import (
2+
web,
3+
)
4+
5+
from ..domain import (
6+
Microservice,
7+
)
8+
from .router import (
9+
routes,
10+
)
11+
12+
13+
@routes.view("/endpoints")
14+
class EndpointsView(web.View):
15+
async def get(self):
16+
redis_client = self.request.app["db_client"]
17+
endpoints = await Microservice.get_all(redis_client)
18+
19+
return web.json_response(endpoints)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "minos_discovery"
3-
version = "0.0.8"
3+
version = "0.1.0"
44
description = "Minos Discovery service for Microservices subscription."
55
readme = "README.md"
66
repository = "https://github.com/clariteia/discovery"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from aiohttp.test_utils import (
2+
AioHTTPTestCase,
3+
)
4+
5+
from minos.api_gateway.common import (
6+
MinosConfig,
7+
)
8+
from minos.api_gateway.discovery import (
9+
DiscoveryService,
10+
)
11+
from tests.utils import (
12+
BASE_PATH,
13+
)
14+
15+
16+
class TestMicroserviceEndpoints(AioHTTPTestCase):
17+
CONFIG_FILE_PATH = BASE_PATH / "config.yml"
18+
19+
async def get_application(self):
20+
"""
21+
Override the get_app method to return your application.
22+
"""
23+
config = MinosConfig(self.CONFIG_FILE_PATH)
24+
service = DiscoveryService(
25+
address=config.discovery.connection.host, port=config.discovery.connection.port, config=config
26+
)
27+
28+
return await service.create_application()
29+
30+
async def test_get(self):
31+
response = await self.client.get("/endpoints")
32+
33+
self.assertEqual(200, response.status)
34+
self.assertIsInstance(await response.json(), list)

0 commit comments

Comments
 (0)