-
Notifications
You must be signed in to change notification settings - Fork 678
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
Replace requests with NetworkClient #12096
Replace requests with NetworkClient #12096
Conversation
kolibri/core/api.py
Outdated
@@ -30,7 +30,8 @@ def register(self, request): | |||
facility = Facility.objects.get(id=request.data.get("facility_id")) | |||
try: | |||
response = registerfacility(request.data.get("token"), facility) | |||
except requests.exceptions.RequestException as e: # bubble up any response error | |||
# as NetworkLocationResponseFailure is a subclass of requests.exceptions.RequestException | |||
except errors.NetworkLocationResponseFailure as e: # bubble up any response error |
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.
Are we supposed to do this? Because here, requests is only used for getting the Error Class
kolibri/core/api.py
Outdated
return Response({"status": "offline"}, status=HTTP_503_SERVICE_UNAVAILABLE) | ||
except errors.NetworkLocationResponseFailure as e: |
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.
Initially i thought NetworkClient is not working properly as for this endpoint i was getting a 404, but with requests it was going smoothly for the same endpoint. Turns out there were no exception handling for 404(directly using requests).
I think endpoint should return a 400 if the token is invalid(or NONE) instead of a 404???
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.
Looks like you could just set:
response = e.response
here and let the code continue?
Build Artifacts
|
Not so straight forward as I thought:)
These were passing but then what is the use of Are there other ways to do this? |
|
||
# we cannot pass None address to get_normalized_url_variations() | ||
# as it uses parse_address_into_components() which takes no None | ||
if address is None: |
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 found some places in code using requests that the baseurl is none, so to migrate to NetworkClient without raising any error, I did 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.
Are these the cases where the baseurl is the server itself? Maybe for that case we should make a build_for_localhost
method or something to capture this case?
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.
By server you mean https://studio.learningequality.org
?
One place i can recall where i observed the baseurl is None is this, this happens when from the browser only we try to import channel from studio.
So this behaviour(base being NONE) is already handled in get_content_server_url
which our _make_channel_endpoint_request
eventually calls to stack.
So for these cases instead of building network client from build_for_address which will throw an error is address is None. Can we simply create a NetworkClient obj as:
client = NetworkClient(baseurl) # it deals with baseurl being null right?
Not sure if I am completely understanding your question, but I think using a mock for the NetworkClient does seem like a good idea - the alternative would be to use something like https://requests-mock.readthedocs.io/en/latest/ - but maybe that's best used for the NetworkClient tests specifically, and then we mock the NetworkClient here. It does seem that being able to distinguish a 404 response from other responses might be useful? As the NetworkLocationResponseFailure has the requests response attached to it as the |
what i actually wanted to ask is that - if you see this test case you will see in the So how do i leverage the authentication( |
Maybe I am missing something, but these are two separate things. The NetworkClient is only ever being used to make calls to another Kolibri, either within the course of an API request, or inside an asynchronous task. The test client used in the test cases is used to make requests to API endpoints in an efficient way (it actually kind of cheats and bypasses the http layer). So, you can still login to the test client to make requests to API endpoints, then if inside that API endpoint it is using NetworkClient to make a further request to an external Kolibri, that's where the mocking of the NetworkClient comes into play, because now instead of reaching out to the remote Kolibri (or Kolibri Studio) we just provide a mocked response from the NetworkClient. This is good, as this is one source of flakiness for our automated tests, where if Kolibri Studio is slow to respond (either because of Studio or local network conditions), it can cause a false positive test failure. |
06fd5d7
to
056ea2a
Compare
Hi @thesujai, is this ready for re-review or work in progress? |
Hello @MisRob |
Yes, done. Thank you. |
056ea2a
to
66cb5d0
Compare
This is ready for review! |
Thanks @thesujai |
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 the code changes here make sense to me - I am not concerned about the KDP interactions, but wonder if this might mess with telemetry reporting. Will be checking locally.
Have confirmed I see no issues with telemetry pingback. |
@radinamatic @pcenov to confirm no regressions here, I think the main things to regression test here are:
|
Hi @thesujai @rtibbles, Here are 3 videos (which I have shortened) with different scenarios illustrating that.
merge.user.mp4Logs: mergeUserLogs.zip
create.new.user.mp4Logs: createNewUserLogs.zip
Move.account.mp4Logs: moveAccountLogs.zip |
Thanks @pcenov - I will double check the logs you shared and confirm whether this is caused by this PR or whether it is something that has been independently fixed, and this PR is not up to date with! |
Yes - it looks like these errors and the apparently stalling migration are all things that were independently fixed in the release-v0.17.x branch that have not been merged up to here, so I think this is good to go. None of the endpoints that were modified in this PR are causing any issues! |
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.
Code review passed, manual QA has shown no regressions from these changes.
Summary
References
Fixes #11018
Reviewer guidance
I did a manual test thoroughly, and mostly followed TDD.
The only place I am not sure is core.api.KolibriDataPortalViewSet as testing it required a token and no tests are written for this. Maybe we can have a issue to do this?
Testing checklist
PR process
Reviewer checklist
yarn
andpip
)