-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DB and API Refactors #99
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once we've addressed the model breakage good to go.
There are some fuzzy bits of feedback here; don't feel I know Python well enough to be sure of myself.
# a new one will have to be generated, then added to Meta's callback URL of the *testing* app | ||
# (Noting that the *production* app's callback URL will be different anyway, so the 3rd party won't be able to access that app) | ||
# (but we still don't want random calls to be made to our testing app, so that's why we'll still have to change an exposed token :]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the extensive comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I was thinking of other ways to document these comments externally, but until we figure something out, will stick to inline commenting as I do think these should help newcomers .
}, | ||
"http_version": "1.1", | ||
"server": ["127.0.0.1", 8000], // When running locally | ||
"client": ["127.0.0.1", 11563], // The port here changes dynamically |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way of handling this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If "this" refers to the "port changes dynamically" comment, then I don't know...
To be honest, I don't even know why this port changes ... even though I've bounded Ansari frontend with port 3000 ... unless "client" here refers to something else, since origin does indeed have port 3000 ...
src/ansari/ansari_db.py
Outdated
or only after all of them are executed. | ||
|
||
Returns: | ||
Union[Optional[List], List[Optional[List]]]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, will adjust it 👍
Also, I love the attached screenshot; any attempts to explain reasoning/advantages/disadvantages are always appreciated :]
which_fetch = [which_fetch] * len(query) | ||
|
||
caller_function_name = inspect.stack()[1].function | ||
logger.debug(f"Function {caller_function_name}() \nis running queries: \n{query} \nwith params: \n{params}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if this is called from an object method? Just curious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite get your question... if I understand correctly, an "object's method" is like account_exists()
method in Ansari
class (object) ... all other functions that are in this file are also methods which I call in it the `_execute_query() method.
If you're asking what this code snippet does in general... then the answer is this: it fetches the name of the function/method that called this _execute_query()
method (e.g., "account_exists", etc.)
Hopefully I understood your question correctly 🙌
@@ -109,13 +112,95 @@ def _get_token_from_request(self, request: Request) -> str: | |||
detail="Authorization header is malformed", | |||
) | |||
|
|||
def _execute_query( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You realize we are taking baby steps towards building an ORM, right ? :-)
Wonder if we should just fully commit to an ORM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally slipped my mind, I don't know how!
I don't mind later changing this code to support ORM ... my main motivation on why I created this method, was that I found duplications in other methods, so I wanted to refactor, + add logs ... but if we're headed towards ORM, no problems at all 👍
src/ansari/ansari_db.py
Outdated
result = cur.fetchall() | ||
|
||
# Remove possible SQL comments at the start of the q variable | ||
q = re.sub(r"^\s*--.*\n", "", q, flags=re.MULTILINE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit weird to me. Why is this necessary? Why is it two dashes instead of the normal 3? Does pysql not handle comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can adjust the regex to activate on 2 or 3 ISA ... actually, this regex isn't that necessary 😅 , as we don't have any SQL statements starting with dashes in our code, but I will adjust the regex nonetheless 🙌
src/ansari/config.py
Outdated
from functools import lru_cache | ||
from pathlib import Path | ||
from typing import Literal | ||
|
||
from pydantic import DirectoryPath, Field, PostgresDsn, SecretStr, field_validator | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
# Can't use get_logger() here due to circular import | ||
logger = logging.getLogger(__name__) | ||
# # Can't use get_logger() here due to circular import |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary #?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch! Will replace "# #" with "#" 👍
|
||
|
||
# Defined in a separate file to avoid circular imports between main_*.py files | ||
def validate_cors(request: Request, settings: Settings = Depends(get_settings)) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this! Are there othetr helpers that should be moved in here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Whenever I find code that is used in multiple files (e.g., validate_cors
is used in main_api and main_whatsapp), I'll move them here, but now I can't think of any.
src/ansari/util/fastapi_helpers.py
Outdated
# Defined in a separate file to avoid circular imports between main_*.py files | ||
def validate_cors(request: Request, settings: Settings = Depends(get_settings)) -> bool: | ||
try: | ||
logger.info(f"Headers of raw request are: {request.headers}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All headers feel more like a "debug" level log.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, will adjust ISA 👍
Oh, you're right. Sorry. False alarm.
…On Tue, Dec 17, 2024 at 2:09 PM Ashraf Haress ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In
docs/structure_of_api_responses/ansari_website_api_structure_of_a_request_sent_using_zrok.json
<#99 (comment)>
:
> @@ -0,0 +1,76 @@
+{
+ "scope": {
+ "type": "http",
+ "asgi": {
+ "version": "3.0",
+ "spec_version": "2.4"
+ },
+ "http_version": "1.1",
+ "server": ["127.0.0.1", 8000], // When running locally
+ "client": ["127.0.0.1", 11563], // The port here changes dynamically
If "this" refers to the "port changes dynamically" comment, then I don't
know...
To be honest, I don't even know why this port changes ... even though I've
bounded Ansari frontend with port 3000 ... unless "client" here refers to
something else, since origin does indeed have
<https://github.com/ansari-project/ansari-backend/blob/2f9eeb223bb6ad74ce0411128a20c47aecd77368/docs/structure_of_api_responses/ansari_website_api_structure_of_a_request_sent_using_zrok.json#L28>
port 3000 ...
—
Reply to this email directly, view it on GitHub
<#99 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAUXGUERVVAOIL2OBSYI4ID2F6IVXAVCNFSM6AAAAABTHFFRVCVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDKMBXG42TMOJTGI>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Based on this PR: ansari-project#99
feat: loguru, cors_ok & DB Code Refactors
Also: Zrok Notes, Meta API Notes
Regarding ansari_db.py's code refactors:
Regarding cors_ok logic in main_api.py:
Regarding Zrok:
Regarding Loguru: