Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 047e5c0

Browse files
fixed resource issues
1 parent 44ff25f commit 047e5c0

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

‎src/authorize/authorize.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ async def return_kwargs(kwargs):
185185
request: Request = kwargs.get('request')
186186
api_key = request.query_params.get('api_key')
187187
path = kwargs.get('path')
188+
auth_logger.info(f"path : {path}, api_key : {api_key}")
188189
return api_key, path
189190

190191
async def rate_limiter(api_key):
@@ -226,7 +227,7 @@ async def rate_limiter(api_key):
226227
async def wrapper(*args, **kwargs):
227228
"""main wrapper"""
228229
api_key, path = await return_kwargs(kwargs)
229-
230+
auth_logger.info(f"path: {path}, api_key: {api_key}")
230231
path = f"/api/v1/{path}"
231232
api_key_found = api_key in api_keys
232233
if not api_key_found:
@@ -244,9 +245,11 @@ async def wrapper(*args, **kwargs):
244245
# Authorization Section
245246
# Use asyncio.gather to run is_resource_authorized and monthly_credit_available concurrently
246247
is_authorized_task = asyncio.create_task(is_resource_authorized(path_param=path, api_key=api_key))
248+
247249
monthly_credit_task = asyncio.create_task(monthly_credit_available(api_key=api_key))
248-
is_authorized, monthly_credit = await asyncio.gather(is_authorized_task, monthly_credit_task)
249250

251+
is_authorized, monthly_credit = await asyncio.gather(is_authorized_task, monthly_credit_task)
252+
auth_logger.info(f"is authorized: {is_authorized}, monthly credit: {monthly_credit}")
250253
if is_authorized and monthly_credit:
251254
return await func(*args, **kwargs)
252255

‎src/authorize/resources.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@
4949
"news.articles.date": "/api/v1/news/articles-by-date/",
5050
"news.articles.publisher": "/api/v1/news/articles-by-publisher/",
5151
"news.articles.stock_code": "/api/v1/news/articles-by-ticker/",
52+
"news.articles.paged": "/api/v1/news/articles-by-page/",
5253

5354
"social.trend_setters.stock_code": "/api/v1/sentiment/trend-setters/stock/",
5455

5556
"sentiment_analysis.stock_code": "/api/v1/sentiment/trending/stock/",
56-
"sentiment_analysis.tweeter.stock_code": "/api/v1/sentiment/tweet/stock/"}
57+
"sentiment_analysis.tweeter.stock_code": "/api/v1/sentiment/tweet/stock/",
58+
59+
}
5760

5861

5962
def path_to_resource() -> dict[str, str]:
@@ -77,10 +80,11 @@ async def get_resource_name(path: str) -> str:
7780
resource = path_dict[_path]
7881
return resource
7982
except KeyError:
80-
_path = "/".join(_path.split("/")[:-1])
81-
if not _path:
83+
new_path = "/".join(_path.split("/")[:-1])
84+
85+
if new_path == _path:
8286
raise KeyError(f"No Resource found for path: {path}")
83-
_path = f"{_path}/"
87+
_path = new_path+"/"
8488

8589

8690
resource_name_request_size: dict[str, int] = {
@@ -133,6 +137,7 @@ async def get_resource_name(path: str) -> str:
133137
"news.articles.date": 3,
134138
"news.articles.publisher": 5,
135139
"news.articles.stock_code": 5,
140+
"news.articles.paged": 5,
136141

137142
"sentiment_analysis.stock_code": 3, # X result size
138143
"sentiment_analysis.tweeter.stock_code": 3, # x result size

‎src/database/plans/plans.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ def resource_exist(self, resource_name: str) -> bool:
204204
:param resource_name:
205205
:return:
206206
"""
207+
# TODO Upgrade this on database
208+
_new_resources = ["news.articles.paged"]
209+
if resource_name in _new_resources:
210+
return True
207211
return resource_name in self.resource_set
208212

209213
@classmethod

‎src/main/main.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ async def add_security_headers(request: Request, call_next):
174174

175175

176176
if is_development(config_instance=config_instance):
177-
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["gateway.eod-stock-api.site", "localhost", "127.0.0.1"])
177+
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["eod-stock-api.local", "localhost", "127.0.0.1"])
178178
else:
179179
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["master-gateway.eod-stock-api.site", "gateway.eod-stock-api.site"])
180180

@@ -254,7 +254,7 @@ async def validate_request_middleware(request, call_next):
254254
# You can modify the request here, or perform any other
255255
# pre-processing that you need.
256256
# allowedPaths = ["/", "/api/", "/redoc", "/docs", "/_admin/"]
257-
257+
app_logger.info("validate_request")
258258
async def compare_tokens():
259259
"""will check headers to see if the request comes from cloudflare"""
260260
_cf_secret_token = request.headers.get('X-SECRET-TOKEN')
@@ -281,7 +281,7 @@ async def compare_tokens():
281281
"message": "Request Contains Suspicious patterns cannot continue"}
282282
response = JSONResponse(content=mess, status_code=404)
283283
return response
284-
284+
app_logger.info(f"Request is not malicios")
285285
if path.startswith("/_admin") or path.startswith("/redoc") or path.startswith("/docs") or path.startswith(
286286
"/static"):
287287
app_logger.info("starts with admin going in ")
@@ -293,6 +293,7 @@ async def compare_tokens():
293293
response = await call_next(request)
294294

295295
elif is_development(config_instance=config_instance):
296+
app_logger.info(f"Development")
296297
response = await call_next(request)
297298

298299
elif not await compare_tokens():
@@ -443,15 +444,17 @@ async def v1_gateway(request: Request, path: str):
443444

444445
api_key: dict = request.query_params.get('api_key')
445446
_path = f"/api/v1/{path}"
447+
app_logger.info(f"Gateway")
446448
await create_take_credit_args(api_key=api_key, path=_path)
447449

448450
api_urls = [f'{api_server_url}/api/v1/{path}' for api_server_url in remote_servers.healthy_server_urls]
449451

450452
# Will Take at least six second on the cache if it finds nothing will return None
451453
# need an improved get timeout for the articles
452454
tasks = [redis_cache.get(key=api_url, timeout=60 * 5) for api_url in api_urls]
455+
app_logger.info("fetching responses")
453456
cached_responses = await asyncio.gather(*tasks)
454-
457+
app_logger.info("fetched records")
455458
for i, response in enumerate(cached_responses):
456459
if response and response.get('payload'):
457460
app_logger.info(msg=f"Found cached response from {api_urls[i]}")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /