Skip to content
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

Change API requests to use Moodle'sCurl Class #46

Merged
merged 3 commits into from
Feb 28, 2024

Conversation

rohansharmasitoula
Copy link
Contributor

@rohansharmasitoula rohansharmasitoula commented Feb 28, 2024

Description

  • Change API requests to use Moodle_curl_class

Github Issue

Checklist before requesting a review

  • One line description of the changes is added in the PR
  • Issue is linked to the PR via commits (eg: resolves #123)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires only a documentation update

Summary by CodeRabbit

  • Refactor
    • Improved the structure and error handling in the authentication token generation and quiz details sending process.
  • Chores
    • Updated plugin version and release information.

Copy link

coderabbitai bot commented Feb 28, 2024

Important

Auto Review Skipped

Auto reviews are disabled on base/target branches other than the default branch. Please add the base/target branch pattern to the list of additional branches to be reviewed in the settings.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.

Walkthrough

The recent modifications in the project focus on enhancing the way authentication tokens are generated and quiz details are dispatched. By transitioning to a custom curl class for API calls and refining token management and data transmission methods, the changes aim to bolster code structure, error handling, and the overall efficiency of these processes. Additionally, the plugin's version has been updated to reflect these improvements.

Changes

File(s) Summary of Changes
classes/observer.php Refactored API calls to use a custom curl class, improved token handling and quiz data transmission.
version.php Updated plugin version and release information.

Related issues

  • Change the API calls to use Moodle's Curl Class #45: The issue requests using Moodle's Curl Class for API calls, which aligns with the refactoring done in observer.php to utilize a custom curl class for improved API interactions. This suggests the PR addresses the core concern of the issue.

🐇✨
In the realm of code, where logic intertwines,
A rabbit hopped through, refining the lines.
With a flick and a hop, and a gentle curl twist,
Authentication flows, now smoothly persist.
Quiz details take flight, with less fuss and more grace,
In this digital burrow, improvements embrace.
🚀🌟


Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@rohansharmasitoula rohansharmasitoula changed the base branch from master to release/v1.3.1-OpenUniversity February 28, 2024 16:45
@rohansharmasitoula rohansharmasitoula changed the title Change API requests to use Moodle_curl_class Change API requests to use Moodle'sCurl Class Feb 28, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 461bf5e and d8f8e74.
Files selected for processing (2)
  • classes/observer.php (1 hunks)
  • version.php (1 hunks)
Additional comments: 2
version.php (1)
  • 28-30: The updates to the plugin version and release information are correctly implemented, following semantic versioning and including a build identifier. This ensures that the plugin's version accurately reflects the new changes.
classes/observer.php (1)
  • 125-125: The handling of the authentication token ($token) and its subsequent use in sending quiz details is correctly implemented. This change not only improves the code's structure but also its security by ensuring that API requests are made with proper authentication.

Comment on lines 135 to 152
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$curl = new curl();
$headers = array('Content-Type: application/json');
$curl->setHeader($headers);
$request_url = $api_base_url . '/auth';
$json_payload = json_encode($payload);
try {
$response = curl_exec($curl);
$err = curl_error($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($err) {
throw new CustomException($err);
} elseif ($response && $httpcode != 200) {
throw new CustomException($response);
} else {
return $response;
$response = $curl->post($request_url, $json_payload);
if ($curl->get_errno()) {
$error_msg = $curl->error;
throw new moodle_exception('errorapirequest', 'yourplugin', '', $error_msg);
}
$decoded_response = json_decode($response, true);
if (!isset($decoded_response['access_token'])) {
throw new CustomException("Auth Token Not generated");
}
return $decoded_response;

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rohansharmasitoula: Acknowledged. If you've made the suggested change, that's great! Please proceed with any further testing or reviews required for your pull request. If you have any more questions or need further assistance, feel free to ask.

Comment on lines 160 to 177
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$curl = new curl();
$headers = array(
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
);
$curl->setHeader($headers);
$request_url = $api_base_url . '/quiz';
$json_eventdata = json_encode($eventdata);
try {
$response = curl_exec($curl);
$err = curl_error($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($err) {
throw new CustomException($err);
} elseif ($response && $httpcode != 201) {
throw new CustomException($response);
} else {
return $response;
$response = $curl->post($request_url, $json_eventdata);
if ($curl->get_errno()) {
$error_msg = $curl->error;
throw new moodle_exception('errorapirequest', 'yourplugin', '', $error_msg);
}
$decoded_response = json_decode($response, true);
return $decoded_response;

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rohansharmasitoula: Great to hear that you've made the change! If you have any more questions or need further assistance, feel free to ask. Keep up the good work!

@rohansharmasitoula rohansharmasitoula merged commit 4f46a46 into release/v1.3.1-proxy-support Feb 28, 2024
1 check passed
@rohansharmasitoula rohansharmasitoula deleted the Proxy branch February 28, 2024 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change the API calls to use Moodle's Curl Class
2 participants